Docu for target type endpoints in Target API (#1185)

* add assign/unassign type endpoints to target api docs

Signed-off-by: Natalia Kislicyn <natalia.kislicyn@bosch.io>

* fix target type creation for api rest docs test

Signed-off-by: Natalia Kislicyn <natalia.kislicyn@bosch.io>

* fix review comments: rename tests

Signed-off-by: Natalia Kislicyn <natalia.kislicyn@bosch.io>

* adapt test to avoid failing when translated

Signed-off-by: Natalia Kislicyn <natalia.kislicyn@bosch.io>
This commit is contained in:
Natalia Kislicyn
2021-10-15 16:22:20 +02:00
committed by GitHub
parent e5dbbbbc7b
commit 745a0c6a10
8 changed files with 246 additions and 8 deletions

View File

@@ -632,7 +632,7 @@ public interface TargetManagement {
* if TargetType with given target ID does not exist
*/
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_UPDATE_TARGET)
Target assignType(@NotEmpty String controllerID, @NotNull long targetTypeId);
Target assignType(@NotEmpty String controllerID, @NotNull Long targetTypeId);
/**
* updates the {@link Target}.

View File

@@ -607,7 +607,7 @@ public class JpaTargetManagement implements TargetManagement {
@Transactional
@Retryable(include = {
ConcurrencyFailureException.class }, maxAttempts = Constants.TX_RT_MAX, backoff = @Backoff(delay = Constants.TX_RT_DELAY))
public Target assignType(final String controllerID, final long targetTypeId) {
public Target assignType(final String controllerID, final Long targetTypeId) {
final JpaTarget target = getByControllerIdAndThrowIfNotFound(controllerID);
final JpaTargetType targetType = getTargetTypeByIdAndThrowIfNotFound(targetTypeId);
target.setTargetType(targetType);

View File

@@ -42,6 +42,7 @@ import org.eclipse.hawkbit.repository.event.remote.entity.TargetTagCreatedEvent;
import org.eclipse.hawkbit.repository.event.remote.entity.TargetUpdatedEvent;
import org.eclipse.hawkbit.repository.exception.AssignmentQuotaExceededException;
import org.eclipse.hawkbit.repository.exception.EntityAlreadyExistsException;
import org.eclipse.hawkbit.repository.exception.EntityNotFoundException;
import org.eclipse.hawkbit.repository.exception.InvalidTargetAddressException;
import org.eclipse.hawkbit.repository.exception.TenantNotExistException;
import org.eclipse.hawkbit.repository.jpa.model.JpaTarget;
@@ -1137,6 +1138,58 @@ public class TargetManagementTest extends AbstractJpaIntegrationTest {
return target;
}
@Test
@WithUser(allSpPermissions = true)
@Description("Checks that target type is not assigned to target if invalid.")
public void assignInvalidTargetTypeToTarget() {
// create a target
final Target target = testdataFactory.createTarget("target1", "testtarget");
// initial opt lock revision must be one
Optional<JpaTarget> targetFound = targetRepository.findById(target.getId());
assertThat(targetFound).isPresent();
assertThat(targetFound.get().getOptLockRevision()).isEqualTo(1);
assertThat(targetFound.get().getTargetType()).isNull();
// assign target type to target
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> targetManagement.assignType(targetFound.get().getControllerId(), null))
.as("target type with id=null cannot be assigned");
assertThatExceptionOfType(EntityNotFoundException.class)
.isThrownBy(() -> targetManagement.assignType(targetFound.get().getControllerId(), 114L))
.as("target type with id that does not exists cannot be assigned");
// opt lock revision is not changed
Optional<JpaTarget> targetFound1 = targetRepository.findById(target.getId());
assertThat(targetFound1).isPresent();
assertThat(targetFound1.get().getOptLockRevision()).isEqualTo(1);
}
@Test
@WithUser(allSpPermissions = true)
@Description("Checks that target type can be unassigned from target.")
public void unAssignTargetTypeFromTarget() {
// create a target type
TargetType targetType = testdataFactory.findOrCreateTargetType("targettype");
assertThat(targetType).isNotNull();
// create a target
final Target target = testdataFactory.createTarget("target1", "testtarget", targetType.getId());
// initial opt lock revision must be one
Optional<JpaTarget> targetFound = targetRepository.findById(target.getId());
assertThat(targetFound).isPresent();
assertThat(targetFound.get().getOptLockRevision()).isEqualTo(1);
assertThat(targetFound.get().getTargetType().getName()).isEqualTo(targetType.getName());
// un-assign target type from target
targetManagement.unAssignType(targetFound.get().getControllerId());
// opt lock revision must be changed
Optional<JpaTarget> targetFound1 = targetRepository.findById(target.getId());
assertThat(targetFound1).isPresent();
assertThat(targetFound1.get().getOptLockRevision()).isEqualTo(2);
assertThat(targetFound1.get().getTargetType()).isNull();
}
@Test
@Description("Test that RSQL filter finds targets with metadata and/or controllerId.")
public void findTargetsByRsqlWithMetadata() {