Skip to content

Commit

Permalink
Add integration tests regarding name field functionality in THING_CRE…
Browse files Browse the repository at this point in the history
…ATED

Signed-off-by: Ammar Bikic <[email protected]>
  • Loading branch information
AmmarBikic committed Sep 19, 2019
1 parent 22cf51c commit 158f442
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,20 @@ public void findOrRegisterTargetIfItDoesNotExist() {
assertThat(targetRepository.count()).as("Only 1 target should be registred").isEqualTo(1L);
}

@Test
@Description("Register a controller with name which does not exist")
@ExpectEvents({@Expect(type = TargetCreatedEvent.class, count = 1),
@Expect(type = TargetPollEvent.class, count = 2)})
public void findOrRegisterTargetIfItDoesNotExistWithName() {
final Target target = controllerManagement.findOrRegisterTargetIfItDoesNotExist("AA", LOCALHOST, "TestName");
assertThat(target).as("target should not be null").isNotNull();

final Target sameTarget = controllerManagement.findOrRegisterTargetIfItDoesNotExist("AA", LOCALHOST, "TestName");
assertThat(target.getId()).as("Target should be the equals").isEqualTo(sameTarget.getId());
assertThat(target.getName()).as("Taget names should be equal").isEqualTo(sameTarget.getName());
assertThat(targetRepository.count()).as("Only 1 target should be registred").isEqualTo(1L);
}

@Test
@Description("Tries to register a target with an invalid controller id")
public void findOrRegisterTargetIfItDoesNotExistThrowsExceptionForInvalidControllerIdParam() {
Expand All @@ -536,6 +550,27 @@ public void findOrRegisterTargetIfItDoesNotExistThrowsExceptionForInvalidControl
.as("register target with too long controllerId should fail");
}

@Test
@Description("Tries to register a target with an invalid controller id")
public void findOrRegisterTargetIfItDoesNotExistWithNameThrowsExceptionForInvalidControllerIdParam() {
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> controllerManagement.findOrRegisterTargetIfItDoesNotExist(null, LOCALHOST, "TestName"))
.as("register target with null as controllerId should fail");

assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> controllerManagement.findOrRegisterTargetIfItDoesNotExist("", LOCALHOST, "TestName"))
.as("register target with empty controllerId should fail");

assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> controllerManagement.findOrRegisterTargetIfItDoesNotExist(" ", LOCALHOST, "TestName"))
.as("register target with empty controllerId should fail");

assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> controllerManagement.findOrRegisterTargetIfItDoesNotExist(
RandomStringUtils.randomAlphabetic(Target.CONTROLLER_ID_MAX_SIZE + 1), LOCALHOST, "TestName"))
.as("register target with too long controllerId should fail");
}

@Test
@Description("Register a controller which does not exist, when a ConcurrencyFailureException is raised, the "
+ "exception is rethrown after max retries")
Expand All @@ -555,6 +590,25 @@ public void findOrRegisterTargetIfItDoesNotExistThrowsExceptionAfterMaxRetries()
}
}

@Test
@Description("Register a controller which does not exist, when a ConcurrencyFailureException is raised, the "
+ "exception is rethrown after max retries")
public void findOrRegisterTargetIfItDoesNotExistWithNameThrowsExceptionAfterMaxRetries() {
final TargetRepository mockTargetRepository = Mockito.mock(TargetRepository.class);
when(mockTargetRepository.findOne(any())).thenThrow(ConcurrencyFailureException.class);
((JpaControllerManagement) controllerManagement).setTargetRepository(mockTargetRepository);

try {
controllerManagement.findOrRegisterTargetIfItDoesNotExist("AA", LOCALHOST, "TestName");
fail("Expected an ConcurrencyFailureException to be thrown!");
} catch (final ConcurrencyFailureException e) {
verify(mockTargetRepository, times(TX_RT_MAX)).findOne(any());
} finally {
// revert
((JpaControllerManagement) controllerManagement).setTargetRepository(targetRepository);
}
}

@Test
@Description("Register a controller which does not exist, when a ConcurrencyFailureException is raised, the "
+ "exception is not rethrown when the max retries are not yet reached")
Expand Down Expand Up @@ -582,6 +636,33 @@ public void findOrRegisterTargetIfItDoesNotExistDoesNotThrowExceptionBeforeMaxRe
}
}

@Test
@Description("Register a controller which does not exist, when a ConcurrencyFailureException is raised, the "
+ "exception is not rethrown when the max retries are not yet reached")
@ExpectEvents({@Expect(type = TargetCreatedEvent.class, count = 1),
@Expect(type = TargetPollEvent.class, count = 1)})
public void findOrRegisterTargetIfItDoesNotExistWithNameDoesNotThrowExceptionBeforeMaxRetries() {

final TargetRepository mockTargetRepository = Mockito.mock(TargetRepository.class);
((JpaControllerManagement) controllerManagement).setTargetRepository(mockTargetRepository);
final Target target = testdataFactory.createTargetWithName("TestName");

when(mockTargetRepository.findOne(any())).thenThrow(ConcurrencyFailureException.class)
.thenThrow(ConcurrencyFailureException.class).thenReturn(Optional.of((JpaTarget) target));
when(mockTargetRepository.save(any())).thenReturn(target);

try {
final Target targetFromControllerManagement = controllerManagement
.findOrRegisterTargetIfItDoesNotExist(target.getControllerId(), LOCALHOST, "TestName");
verify(mockTargetRepository, times(3)).findOne(any());
verify(mockTargetRepository, times(1)).save(any());
assertThat(target).isEqualTo(targetFromControllerManagement);
} finally {
// revert
((JpaControllerManagement) controllerManagement).setTargetRepository(targetRepository);
}
}

@Test
@Description("Register a controller which does not exist, if a EntityAlreadyExistsException is raised, the "
+ "exception is rethrown and no further retries will be attempted")
Expand All @@ -605,6 +686,29 @@ public void findOrRegisterTargetIfItDoesNotExistDoesntRetryWhenEntityAlreadyExis
}
}

@Test
@Description("Register a controller which does not exist, if a EntityAlreadyExistsException is raised, the "
+ "exception is rethrown and no further retries will be attempted")
public void findOrRegisterTargetIfItDoesNotExistWithNameDoesntRetryWhenEntityAlreadyExistsException() {

final TargetRepository mockTargetRepository = Mockito.mock(TargetRepository.class);
((JpaControllerManagement) controllerManagement).setTargetRepository(mockTargetRepository);

when(mockTargetRepository.findOne(any())).thenReturn(Optional.empty());
when(mockTargetRepository.save(any())).thenThrow(EntityAlreadyExistsException.class);

try {
controllerManagement.findOrRegisterTargetIfItDoesNotExist("1234", LOCALHOST, "TestName");
fail("Expected an EntityAlreadyExistsException to be thrown!");
} catch (final EntityAlreadyExistsException e) {
verify(mockTargetRepository, times(1)).findOne(any());
verify(mockTargetRepository, times(1)).save(any());
} finally {
// revert
((JpaControllerManagement) controllerManagement).setTargetRepository(targetRepository);
}
}

@Test
@Description("Retry is aborted when an unchecked exception is thrown and the exception should also be "
+ "rethrown")
Expand All @@ -626,6 +730,27 @@ public void recoverFindOrRegisterTargetIfItDoesNotExistIsNotInvokedForOtherExcep
}
}

@Test
@Description("Retry is aborted when an unchecked exception is thrown and the exception should also be "
+ "rethrown")
public void recoverFindOrRegisterTargetIfItDoesNotExistWithNameIsNotInvokedForOtherExceptions() {

final TargetRepository mockTargetRepository = Mockito.mock(TargetRepository.class);
((JpaControllerManagement) controllerManagement).setTargetRepository(mockTargetRepository);

when(mockTargetRepository.findOne(any())).thenThrow(RuntimeException.class);

try {
controllerManagement.findOrRegisterTargetIfItDoesNotExist("aControllerId", LOCALHOST, "TestName");
fail("Expected a RuntimeException to be thrown!");
} catch (final RuntimeException e) {
verify(mockTargetRepository, times(1)).findOne(any());
} finally {
// revert
((JpaControllerManagement) controllerManagement).setTargetRepository(targetRepository);
}
}

@Test
@Description("Verify that targetVisible metadata is returned from repository")
@ExpectEvents({@Expect(type = DistributionSetCreatedEvent.class, count = 1),
Expand Down Expand Up @@ -653,6 +778,16 @@ public void targetPollEventNotSendIfDisabled() {
repositoryProperties.setPublishTargetPollEvent(true);
}

@Test
@Description("Verify that controller registration does not result in a TargetPollEvent if feature is disabled")
@ExpectEvents({@Expect(type = TargetCreatedEvent.class, count = 1),
@Expect(type = TargetPollEvent.class, count = 0)})
public void targetPollEventNotSendIfDisabledWithName() {
repositoryProperties.setPublishTargetPollEvent(false);
controllerManagement.findOrRegisterTargetIfItDoesNotExist("AA", LOCALHOST, "TestName");
repositoryProperties.setPublishTargetPollEvent(true);
}

@Test
@Description("Controller tries to finish an update process after it has been finished by an error action status.")
@ExpectEvents({@Expect(type = TargetCreatedEvent.class, count = 1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,22 @@ public Target createTarget(final String controllerId) {
return target;
}

/**
* @param targetName
* name of the target
* @return persisted {@link Target}
*/
public Target createTargetWithName(final String targetName) {
final Target target = targetManagement.create(entityFactory.target().create().controllerId(DEFAULT_CONTROLLER_ID).name(targetName));
assertThat(target.getCreatedBy()).isNotNull();
assertThat(target.getCreatedAt()).isNotNull();
assertThat(target.getLastModifiedBy()).isNotNull();
assertThat(target.getLastModifiedAt()).isNotNull();

assertThat(target.getUpdateStatus()).isEqualTo(TargetUpdateStatus.UNKNOWN);
return target;
}

/**
* Creates {@link DistributionSet}s in repository including three
* {@link SoftwareModule}s of types {@link #SM_TYPE_OS}, {@link #SM_TYPE_RT}
Expand Down

0 comments on commit 158f442

Please sign in to comment.