Skip to content

Commit

Permalink
Add junit test for CERN lifecycle handler
Browse files Browse the repository at this point in the history
  • Loading branch information
rmiccoli committed Oct 5, 2023
1 parent 254b34e commit bbfeddb
Showing 1 changed file with 113 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.UUID;

import org.junit.After;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -71,7 +72,7 @@
@SpringBootTest(classes = {IamLoginService.class, CoreControllerTestSupport.class,
CernAccountLifecycleTests.TestConfig.class})
@TestPropertySource(properties = {
// @formatter:off
// @formatter:off
"lifecycle.account.expiredAccountPolicy.suspensionGracePeriodDays=0",
"lifecycle.account.expiredAccountPolicy.removalGracePeriodDays=30",
"cern.task.pageSize=5"
Expand Down Expand Up @@ -109,7 +110,7 @@ CernHrDBApiService hrDb() {

@Autowired
Clock clock;

@After
public void teardown() {
reset(hrDb);
Expand Down Expand Up @@ -155,7 +156,7 @@ public void testSuspendLifecycleWorks() {
@Test
public void testNoActionLifecycleWorksForValidAccounts() {
VOPersonDTO voPerson = voPerson("988211");

when(hrDb.hasValidExperimentParticipation(anyString())).thenReturn(true);
when(hrDb.getHrDbPersonRecord(anyString())).thenReturn(voPerson);

Expand All @@ -175,7 +176,7 @@ public void testNoActionLifecycleWorksForValidAccounts() {
assertThat(testAccount.getUserInfo().getGivenName(), is(voPerson.getFirstName()));
assertThat(testAccount.getUserInfo().getFamilyName(), is(voPerson.getName()));
assertThat(testAccount.getUserInfo().getEmail(), is(voPerson.getEmail()));

assertThat(testAccount.isActive(), is(true));
Optional<IamLabel> statusLabel =
testAccount.getLabelByPrefixAndName(LABEL_CERN_PREFIX, LABEL_STATUS);
Expand All @@ -193,7 +194,7 @@ public void testNoActionLifecycleWorksForValidAccounts() {
assertThat(timestampLabel.isPresent(), is(true));
assertThat(timestampLabel.get().getValue(), is(valueOf(clock.instant().toEpochMilli())));
}

@Test
public void testNoActionLifecycleWorksForInValidAccounts() {

Expand All @@ -206,7 +207,7 @@ public void testNoActionLifecycleWorksForInValidAccounts() {

testAccount.setActive(false);
service.setLabel(testAccount, cernPersonIdLabel());

handler.run();

testAccount =
Expand All @@ -230,6 +231,87 @@ public void testNoActionLifecycleWorksForInValidAccounts() {
assertThat(timestampLabel.get().getValue(), is(valueOf(clock.instant().toEpochMilli())));
}

@Ignore
@Test
public void testLifecycleStates() {

when(hrDb.hasValidExperimentParticipation(anyString())).thenReturn(false);

IamAccount testAccount =
repo.findByUuid(TEST_USER_UUID).orElseThrow(assertionError(EXPECTED_ACCOUNT_NOT_FOUND));

testAccount.setActive(true);
service.setLabel(testAccount, cernPersonIdLabel());

handler.run();

testAccount =
repo.findByUuid(TEST_USER_UUID).orElseThrow(assertionError(EXPECTED_ACCOUNT_NOT_FOUND));

assertThat(testAccount.isActive(), is(false));
Optional<IamLabel> statusLabel =
testAccount.getLabelByPrefixAndName(LABEL_CERN_PREFIX, LABEL_STATUS);
Optional<IamLabel> actionLabel =
testAccount.getLabelByPrefixAndName(LABEL_CERN_PREFIX, LABEL_ACTION);
Optional<IamLabel> timestampLabel =
testAccount.getLabelByPrefixAndName(LABEL_CERN_PREFIX, LABEL_TIMESTAMP);

assertThat(statusLabel.isPresent(), is(true));
assertThat(statusLabel.get().getValue(), is(CernHrLifecycleHandler.Status.OK.name()));

assertThat(actionLabel.isPresent(), is(true));
assertThat(actionLabel.get().getValue(),
is(CernHrLifecycleHandler.Action.DISABLE_ACCOUNT.name()));

assertThat(timestampLabel.isPresent(), is(true));
assertThat(timestampLabel.get().getValue(), is(valueOf(clock.instant().toEpochMilli())));

handler.run();

statusLabel = testAccount.getLabelByPrefixAndName(LABEL_CERN_PREFIX, LABEL_STATUS);
actionLabel = testAccount.getLabelByPrefixAndName(LABEL_CERN_PREFIX, LABEL_ACTION);

assertThat(statusLabel.isPresent(), is(true));
assertThat(statusLabel.get().getValue(), is(CernHrLifecycleHandler.Status.OK.name()));

assertThat(actionLabel.isPresent(), is(true));
assertThat(actionLabel.get().getValue(),
is(CernHrLifecycleHandler.Action.DISABLE_ACCOUNT.name()));

when(hrDb.hasValidExperimentParticipation(anyString())).thenReturn(true);
when(hrDb.getHrDbPersonRecord(anyString())).thenReturn(voPerson("988211"));

assertThat(testAccount.isActive(), is(false));

handler.run();

statusLabel = testAccount.getLabelByPrefixAndName(LABEL_CERN_PREFIX, LABEL_STATUS);
actionLabel = testAccount.getLabelByPrefixAndName(LABEL_CERN_PREFIX, LABEL_ACTION);

assertThat(testAccount.isActive(), is(true));

assertThat(statusLabel.isPresent(), is(true));
assertThat(statusLabel.get().getValue(), is(CernHrLifecycleHandler.Status.OK.name()));

assertThat(actionLabel.isPresent(), is(true));
assertThat(actionLabel.get().getValue(),
is(CernHrLifecycleHandler.Action.RESTORE_ACCOUNT.name()));

handler.run();

assertThat(testAccount.isActive(), is(true));

statusLabel = testAccount.getLabelByPrefixAndName(LABEL_CERN_PREFIX, LABEL_STATUS);
actionLabel = testAccount.getLabelByPrefixAndName(LABEL_CERN_PREFIX, LABEL_ACTION);

assertThat(statusLabel.isPresent(), is(true));
assertThat(statusLabel.get().getValue(), is(CernHrLifecycleHandler.Status.OK.name()));

assertThat(actionLabel.isPresent(), is(true));
assertThat(actionLabel.get().getValue(), is(CernHrLifecycleHandler.Action.NO_ACTION.name()));

}

@Test
public void testRestoreLifecycleWorks() {

Expand Down Expand Up @@ -268,20 +350,21 @@ public void testRestoreLifecycleWorks() {
assertThat(timestampLabel.isPresent(), is(true));
assertThat(timestampLabel.get().getValue(), is(valueOf(clock.instant().toEpochMilli())));
}

@Test
public void testApiErrorIsHandled() {
when(hrDb.hasValidExperimentParticipation(anyString())).thenThrow(new CernHrDbApiError("API is unreachable"));

when(hrDb.hasValidExperimentParticipation(anyString()))
.thenThrow(new CernHrDbApiError("API is unreachable"));

IamAccount testAccount =
repo.findByUuid(TEST_USER_UUID).orElseThrow(assertionError(EXPECTED_ACCOUNT_NOT_FOUND));
service.setLabel(testAccount, cernPersonIdLabel());

handler.run();

testAccount =
repo.findByUuid(TEST_USER_UUID).orElseThrow(assertionError(EXPECTED_ACCOUNT_NOT_FOUND));

assertThat(testAccount.isActive(), is(true));
Optional<IamLabel> statusLabel =
testAccount.getLabelByPrefixAndName(LABEL_CERN_PREFIX, LABEL_STATUS);
Expand All @@ -293,18 +376,18 @@ public void testApiErrorIsHandled() {
testAccount.getLabelByPrefixAndName(LABEL_CERN_PREFIX, LABEL_MESSAGE);

assertThat(actionLabel.isPresent(), is(false));

assertThat(statusLabel.isPresent(), is(true));
assertThat(statusLabel.get().getValue(), is(CernHrLifecycleHandler.Status.ERROR.name()));

assertThat(timestampLabel.isPresent(), is(true));
assertThat(timestampLabel.get().getValue(), is(valueOf(clock.instant().toEpochMilli())));

assertThat(messageLabel.isPresent(), is(true));
assertThat(messageLabel.get().getValue(), is(HR_DB_API_ERROR));

}

@Test
public void testRestoreLifecycleDoesNotTouchSuspendedAccount() {

Expand Down Expand Up @@ -336,27 +419,26 @@ public void testRestoreLifecycleDoesNotTouchSuspendedAccount() {
assertThat(statusLabel.get().getValue(), is(CernHrLifecycleHandler.Status.OK.name()));

assertThat(actionLabel.isPresent(), is(true));
assertThat(actionLabel.get().getValue(),
is(CernHrLifecycleHandler.Action.NO_ACTION.name()));
assertThat(actionLabel.get().getValue(), is(CernHrLifecycleHandler.Action.NO_ACTION.name()));

assertThat(timestampLabel.isPresent(), is(true));
assertThat(timestampLabel.get().getValue(), is(valueOf(clock.instant().toEpochMilli())));
}

@Test
public void testIgnoreAccount() {

when(hrDb.hasValidExperimentParticipation(anyString())).thenReturn(false);
IamAccount testAccount =
repo.findByUuid(TEST_USER_UUID).orElseThrow(assertionError(EXPECTED_ACCOUNT_NOT_FOUND));

assertThat(testAccount.isActive(), is(true));

service.setLabel(testAccount, cernPersonIdLabel());
service.setLabel(testAccount, cernIgnoreLabel());

handler.run();

testAccount =
repo.findByUuid(TEST_USER_UUID).orElseThrow(assertionError(EXPECTED_ACCOUNT_NOT_FOUND));

Expand All @@ -369,17 +451,16 @@ public void testIgnoreAccount() {
testAccount.getLabelByPrefixAndName(LABEL_CERN_PREFIX, LABEL_TIMESTAMP);
Optional<IamLabel> messageLabel =
testAccount.getLabelByPrefixAndName(LABEL_CERN_PREFIX, LABEL_MESSAGE);

assertThat(statusLabel.isPresent(), is(true));
assertThat(statusLabel.get().getValue(), is(CernHrLifecycleHandler.Status.OK.name()));

assertThat(actionLabel.isPresent(), is(true));
assertThat(actionLabel.get().getValue(),
is(CernHrLifecycleHandler.Action.NO_ACTION.name()));
assertThat(actionLabel.get().getValue(), is(CernHrLifecycleHandler.Action.NO_ACTION.name()));

assertThat(timestampLabel.isPresent(), is(true));
assertThat(timestampLabel.get().getValue(), is(valueOf(clock.instant().toEpochMilli())));

assertThat(messageLabel.isPresent(), is(true));
assertThat(messageLabel.get().getValue(), is(IGNORE_MESSAGE));
}
Expand All @@ -400,7 +481,7 @@ public void testPaginationWorks() {
accountPage = repo.findAll(pageRequest);

for (IamAccount account : accountPage.getContent()) {

assertThat(account.isActive(), is(false));
Optional<IamLabel> statusLabel =
account.getLabelByPrefixAndName(LABEL_CERN_PREFIX, LABEL_STATUS);
Expand All @@ -418,8 +499,8 @@ public void testPaginationWorks() {

assertThat(timestampLabel.isPresent(), is(true));
assertThat(timestampLabel.get().getValue(), is(valueOf(clock.instant().toEpochMilli())));


}
}

Expand Down

0 comments on commit bbfeddb

Please sign in to comment.