Skip to content

Commit

Permalink
Refactor code and update failing tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
nitinks-ee committed Oct 29, 2021
1 parent 77e22fc commit 2c3d8a9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ by the Lombok annotations will not be visible (and the project will be littered

To build the entire application, including running unit and functional tests:

`./gradlew setupTheKeycloakTestServer && ./gradlew build`
`./gradlew startServer && ./gradlew build`

(Note that functional tests share the same port number for embedded database as for the containerised database, if
tests fail try running `./gradlew composeDown` first. Free-up port for the keycloak test server as well).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
import engineering.everest.starterkit.filestorage.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PostMapping;
import springfox.documentation.annotations.ApiIgnore;

import org.springframework.beans.factory.annotation.Autowired;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ public void on(UserDetailsUpdatedByAdminEvent event) throws Exception {
var user = usersReadService.getById(event.getUserId());

waitForTheProjectionUpdate(() -> user.getEmail()
.equals(event.getEmailChange() ) || user.getDisplayName().equals(event.getDisplayNameChange()),
"user email and displayName projection update");
.equals(event.getEmailChange()) || user.getDisplayName().equals(event.getDisplayNameChange()),
"user email or displayName projection update");

keycloakSynchronizationService.updateUserAttributes(event.getUserId(),
Map.of("attributes", new UserAttribute(user.getOrganizationId(), user.getRoles(), event.getDisplayNameChange()),
"email", event.getEmailChange()) );
"email", event.getEmailChange()));
}

@StartSaga
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,14 @@ class UserAggregateTest {
private static final UUID PROFILE_PHOTO_FILE_ID = randomUUID();
private static final String USERNAME = "[email protected]";
private static final String USER_DISPLAY_NAME = "user-display-name";
private static final String USER_ENCODED_PASSWORD = "encoded-password";
private static final String DISPLAY_NAME_CHANGE = "display-name-change";
private static final String ENCODED_PASSWORD_CHANGE = "encoded-password-change";
private static final String EMAIL_CHANGE = "[email protected]";
private static final String NO_CHANGE = null;
private static final String BLANK_FIELD = "";

private static final UserCreatedByAdminEvent USER_CREATED_BY_ADMIN_EVENT =
new UserCreatedByAdminEvent(USER_ID, ORGANIZATION_ID, ADMIN_ID, USER_DISPLAY_NAME, USERNAME, USER_ENCODED_PASSWORD);
new UserCreatedByAdminEvent(USER_ID, ORGANIZATION_ID, ADMIN_ID, USER_DISPLAY_NAME, USERNAME);
public static final UUID CONFIRMATION_CODE = randomUUID();

private FixtureConfiguration<UserAggregate> testFixture;

Expand Down Expand Up @@ -88,20 +87,20 @@ void aggregateHasExplicitlyDefinedRepository() {
@Test
void createUserCommandEmits_WhenAllMandatoryFieldsArePresentInCreationCommand() {
testFixture.givenNoPriorActivity()
.when(new CreateUserCommand(USER_ID, ORGANIZATION_ID, ADMIN_ID, USERNAME, USER_ENCODED_PASSWORD, USER_DISPLAY_NAME))
.expectEvents(new UserCreatedByAdminEvent(USER_ID, ORGANIZATION_ID, ADMIN_ID, USER_DISPLAY_NAME, USERNAME, USER_ENCODED_PASSWORD));
.when(new CreateUserCommand(USER_ID, ORGANIZATION_ID, ADMIN_ID, USERNAME, USER_DISPLAY_NAME))
.expectEvents(new UserCreatedByAdminEvent(USER_ID, ORGANIZATION_ID, ADMIN_ID, USER_DISPLAY_NAME, USERNAME));
}

@Test
void createUserForNewlyRegisteredOrganizationCommandEmits_WhenAllMandatoryFieldsArePresentInCreationCommand() {
testFixture.givenNoPriorActivity()
.when(new CreateUserForNewlyRegisteredOrganizationCommand(ORGANIZATION_ID, USER_ID, USERNAME, USER_ENCODED_PASSWORD, USER_DISPLAY_NAME))
.expectEvents(new UserCreatedForNewlyRegisteredOrganizationEvent(ORGANIZATION_ID, USER_ID, USER_DISPLAY_NAME, USERNAME, USER_ENCODED_PASSWORD));
.when(new CreateUserForNewlyRegisteredOrganizationCommand(ORGANIZATION_ID, USER_ID, USERNAME, USER_DISPLAY_NAME))
.expectEvents(new UserCreatedForNewlyRegisteredOrganizationEvent(ORGANIZATION_ID, USER_ID, USER_DISPLAY_NAME, USERNAME));
}

@Test
void rejectsCreateUserCommand_WhenEmailValidatorFails() {
CreateUserCommand command = new CreateUserCommand(USER_ID, ORGANIZATION_ID, ADMIN_ID, null, USER_ENCODED_PASSWORD, USER_DISPLAY_NAME);
CreateUserCommand command = new CreateUserCommand(USER_ID, ORGANIZATION_ID, ADMIN_ID, null, USER_DISPLAY_NAME);

testFixture.givenNoPriorActivity()
.when(command)
Expand All @@ -112,46 +111,46 @@ void rejectsCreateUserCommand_WhenEmailValidatorFails() {
@Test
void rejectsCreateUserForNewlyRegisteredOrganizationCommand_WhenEmailValidatorFails() {
testFixture.givenNoPriorActivity()
.when(new CreateUserForNewlyRegisteredOrganizationCommand(ORGANIZATION_ID, USER_ID,null, USER_ENCODED_PASSWORD, USER_DISPLAY_NAME))
.when(new CreateUserForNewlyRegisteredOrganizationCommand(ORGANIZATION_ID, USER_ID,null, USER_DISPLAY_NAME))
.expectNoEvents()
.expectException(ConstraintViolationException.class);
}

@Test
void rejectsCreateUserCommand_WhenDisplayNameIsBlank() {
testFixture.givenNoPriorActivity()
.when(new CreateUserCommand(USER_ID, ORGANIZATION_ID, ADMIN_ID, USERNAME, USER_ENCODED_PASSWORD, ""))
.when(new CreateUserCommand(USER_ID, ORGANIZATION_ID, ADMIN_ID, USERNAME, ""))
.expectNoEvents()
.expectException(ConstraintViolationException.class);
}

@Test
void rejectsCreateUserForNewlyRegisteredOrganizationCommand_WhenDisplayNameIsBlank() {
testFixture.givenNoPriorActivity()
.when(new CreateUserForNewlyRegisteredOrganizationCommand(ORGANIZATION_ID, USER_ID, USERNAME, USER_ENCODED_PASSWORD, ""))
.when(new CreateUserForNewlyRegisteredOrganizationCommand(ORGANIZATION_ID, USER_ID, USERNAME, ""))
.expectNoEvents()
.expectException(ConstraintViolationException.class);
}

@Test
void rejectsCreateUserCommand_WhenDisplayNameIsNull() {
testFixture.givenNoPriorActivity()
.when(new CreateUserCommand(USER_ID, ORGANIZATION_ID, ADMIN_ID, USERNAME, USER_ENCODED_PASSWORD, null))
.when(new CreateUserCommand(USER_ID, ORGANIZATION_ID, ADMIN_ID, USERNAME, null))
.expectNoEvents()
.expectException(ConstraintViolationException.class);
}

@Test
void rejectsCreateUserForNewlyRegisteredOrganizationCommand_WhenDisplayNameIsNull() {
testFixture.givenNoPriorActivity()
.when(new CreateUserForNewlyRegisteredOrganizationCommand(ORGANIZATION_ID, USER_ID, USERNAME, USER_ENCODED_PASSWORD, null))
.when(new CreateUserForNewlyRegisteredOrganizationCommand(ORGANIZATION_ID, USER_ID, USERNAME, null))
.expectNoEvents()
.expectException(ConstraintViolationException.class);
}

@Test
void rejectsCreateUserCommand_WhenRequestingUserIsNull() {
CreateUserCommand command = new CreateUserCommand(USER_ID, ORGANIZATION_ID, null, USERNAME, USER_ENCODED_PASSWORD, USER_DISPLAY_NAME);
CreateUserCommand command = new CreateUserCommand(USER_ID, ORGANIZATION_ID, null, USERNAME, USER_DISPLAY_NAME);

testFixture.givenNoPriorActivity()
.when(command)
Expand All @@ -162,14 +161,14 @@ void rejectsCreateUserCommand_WhenRequestingUserIsNull() {
@Test
void rejectsCreateUserForNewlyRegisteredOrganizationCommand_WhenRequestingUserIsNull() {
testFixture.givenNoPriorActivity()
.when(new CreateUserForNewlyRegisteredOrganizationCommand(ORGANIZATION_ID, null, USERNAME, USER_ENCODED_PASSWORD, USER_DISPLAY_NAME))
.when(new CreateUserForNewlyRegisteredOrganizationCommand(ORGANIZATION_ID, null, USERNAME, USER_DISPLAY_NAME))
.expectNoEvents()
.expectException(ConstraintViolationException.class);
}

@Test
void rejectsCreateUserCommand_WhenOrganizationIdIsInvalid() {
var command = new CreateUserCommand(USER_ID, ORGANIZATION_ID, ADMIN_ID, USERNAME, USER_ENCODED_PASSWORD, USER_DISPLAY_NAME);
var command = new CreateUserCommand(USER_ID, ORGANIZATION_ID, ADMIN_ID, USERNAME, USER_DISPLAY_NAME);
doThrow(IllegalStateException.class).when(organizationStatusValidator).validate(command);

testFixture.givenNoPriorActivity()
Expand All @@ -180,7 +179,7 @@ void rejectsCreateUserCommand_WhenOrganizationIdIsInvalid() {

@Test
void rejectsCreateUserCommand_WhenUniqueUserEmailValidatorFails() {
var command = new CreateUserCommand(USER_ID, ORGANIZATION_ID, ADMIN_ID, USERNAME, USER_DISPLAY_NAME, USER_ENCODED_PASSWORD);
var command = new CreateUserCommand(USER_ID, ORGANIZATION_ID, ADMIN_ID, USERNAME, USER_DISPLAY_NAME);
doThrow(IllegalArgumentException.class).when(usersUniqueEmailValidator).validate(command);

testFixture.givenNoPriorActivity()
Expand All @@ -192,24 +191,24 @@ void rejectsCreateUserCommand_WhenUniqueUserEmailValidatorFails() {
@Test
void createUserForNewlyRegisteredOrganizationCommandEmits() {
testFixture.givenNoPriorActivity()
.when(new CreateUserForNewlyRegisteredOrganizationCommand(USER_ID, ORGANIZATION_ID, USERNAME, USER_ENCODED_PASSWORD, USER_DISPLAY_NAME))
.expectEvents(new UserCreatedForNewlyRegisteredOrganizationEvent(USER_ID, ORGANIZATION_ID, USER_DISPLAY_NAME, USERNAME, USER_ENCODED_PASSWORD));
.when(new CreateUserForNewlyRegisteredOrganizationCommand(USER_ID, ORGANIZATION_ID, USERNAME, USER_DISPLAY_NAME))
.expectEvents(new UserCreatedForNewlyRegisteredOrganizationEvent(USER_ID, ORGANIZATION_ID, USER_DISPLAY_NAME, USERNAME));
}

@Test
void createUserCommandEmits() {
testFixture.givenNoPriorActivity()
.when(new CreateUserCommand(USER_ID, ORGANIZATION_ID, ADMIN_ID, USERNAME, USER_ENCODED_PASSWORD, USER_DISPLAY_NAME))
.expectEvents(new UserCreatedByAdminEvent(USER_ID, ORGANIZATION_ID, ADMIN_ID, USER_DISPLAY_NAME, USERNAME, USER_ENCODED_PASSWORD));
.when(new CreateUserCommand(USER_ID, ORGANIZATION_ID, ADMIN_ID, USERNAME, USER_DISPLAY_NAME))
.expectEvents(new UserCreatedByAdminEvent(USER_ID, ORGANIZATION_ID, ADMIN_ID, USER_DISPLAY_NAME, USERNAME));
}

@Test
void updateUserDetailsCommandEmits_WhenCommandAccepted() {
testFixture.given(USER_CREATED_BY_ADMIN_EVENT)
.when(new UpdateUserDetailsCommand(USER_ID, EMAIL_CHANGE,
DISPLAY_NAME_CHANGE, ENCODED_PASSWORD_CHANGE, ADMIN_ID))
DISPLAY_NAME_CHANGE, ADMIN_ID))
.expectEvents(new UserDetailsUpdatedByAdminEvent(USER_ID, ORGANIZATION_ID, DISPLAY_NAME_CHANGE,
EMAIL_CHANGE, ENCODED_PASSWORD_CHANGE, ADMIN_ID));
EMAIL_CHANGE, ADMIN_ID));
}

@Test
Expand Down Expand Up @@ -239,7 +238,7 @@ void rejectsUpdateUserRolesCommand_WhenAdminRoleProvided() {

@Test
void rejectsUpdateUserDetailsCommand_WhenEmailValidatorFails() {
var command = new UpdateUserDetailsCommand(USER_ID, "not-a-valid-email", DISPLAY_NAME_CHANGE, ENCODED_PASSWORD_CHANGE, ADMIN_ID);
var command = new UpdateUserDetailsCommand(USER_ID, "not-a-valid-email", DISPLAY_NAME_CHANGE, ADMIN_ID);
doThrow(IllegalArgumentException.class).when(emailAddressValidator).validate(command);

testFixture.given(USER_CREATED_BY_ADMIN_EVENT)
Expand All @@ -250,7 +249,7 @@ void rejectsUpdateUserDetailsCommand_WhenEmailValidatorFails() {

@Test
void rejectsUpdateUserDetailsCommand_WhenRequestingUserIdIsNull() {
var command = new UpdateUserDetailsCommand(USER_ID, EMAIL_CHANGE, DISPLAY_NAME_CHANGE, ENCODED_PASSWORD_CHANGE, null);
var command = new UpdateUserDetailsCommand(USER_ID, EMAIL_CHANGE, DISPLAY_NAME_CHANGE, null);

testFixture.given(USER_CREATED_BY_ADMIN_EVENT)
.when(command)
Expand All @@ -261,7 +260,7 @@ void rejectsUpdateUserDetailsCommand_WhenRequestingUserIdIsNull() {
@Test
void rejectsUpdateUserDetailsCommand_WhenNoFieldsAreBeingChanged() {
testFixture.given(USER_CREATED_BY_ADMIN_EVENT)
.when(new UpdateUserDetailsCommand(USER_ID, NO_CHANGE, NO_CHANGE, NO_CHANGE, ADMIN_ID))
.when(new UpdateUserDetailsCommand(USER_ID, NO_CHANGE, NO_CHANGE, ADMIN_ID))
.expectNoEvents()
.expectException(TranslatableIllegalArgumentException.class)
.expectExceptionMessage("USER_UPDATE_NO_FIELDS_CHANGED");
Expand All @@ -270,15 +269,15 @@ void rejectsUpdateUserDetailsCommand_WhenNoFieldsAreBeingChanged() {
@Test
void rejectsUpdateUserDetailsCommand_WhenDisplayNameIsBlanked() {
testFixture.given(USER_CREATED_BY_ADMIN_EVENT)
.when(new UpdateUserDetailsCommand(USER_ID, NO_CHANGE, BLANK_FIELD, NO_CHANGE, ADMIN_ID))
.when(new UpdateUserDetailsCommand(USER_ID, NO_CHANGE, BLANK_FIELD, ADMIN_ID))
.expectNoEvents()
.expectException(TranslatableIllegalArgumentException.class)
.expectExceptionMessage("USER_DISPLAY_NAME_MISSING");
}

@Test
void rejectsUpdateUserDetailsCommand_WhenUniqueEmailValidatorFails() {
UpdateUserDetailsCommand command = new UpdateUserDetailsCommand(USER_ID, EMAIL_CHANGE, DISPLAY_NAME_CHANGE, NO_CHANGE, ADMIN_ID);
UpdateUserDetailsCommand command = new UpdateUserDetailsCommand(USER_ID, EMAIL_CHANGE, DISPLAY_NAME_CHANGE, ADMIN_ID);

doThrow(IllegalStateException.class).when(usersUniqueEmailValidator).validate(command);

Expand Down

0 comments on commit 2c3d8a9

Please sign in to comment.