From bdf498890bb4a11dce7a07cf3658ae022db1af5a Mon Sep 17 00:00:00 2001 From: m1erla <116915007+m1erla@users.noreply.github.com> Date: Sat, 4 Jan 2025 20:26:56 +0300 Subject: [PATCH 1/8] AYS-609 | Add default value for hasObstaclePersonExist field --- .../entity/EmergencyEvacuationApplicationEntity.java | 8 ++++---- src/main/resources/db/changelog/changes/1-ays-ddl.xml | 4 +++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/ays/emergency_application/model/entity/EmergencyEvacuationApplicationEntity.java b/src/main/java/org/ays/emergency_application/model/entity/EmergencyEvacuationApplicationEntity.java index 709c7f705..85245991f 100644 --- a/src/main/java/org/ays/emergency_application/model/entity/EmergencyEvacuationApplicationEntity.java +++ b/src/main/java/org/ays/emergency_application/model/entity/EmergencyEvacuationApplicationEntity.java @@ -21,7 +21,8 @@ /** * A JPA entity class that represents an emergency evacuation entity. - * The emergency evacuation applications are defined in the AYS_EMERGENCY_EVACUATION_APPLICATION table in the database. + * The emergency evacuation applications are defined in the + * AYS_EMERGENCY_EVACUATION_APPLICATION table in the database. */ @Entity @Getter @@ -92,13 +93,12 @@ public class EmergencyEvacuationApplicationEntity extends BaseEntity { @Column(name = "IS_IN_PERSON") private Boolean isInPerson; - @Column(name = "HAS_OBSTACLE_PERSON_EXIST") - private Boolean hasObstaclePersonExist; + @Column(name = "HAS_OBSTACLE_PERSON_EXIST", nullable = false) + private Boolean hasObstaclePersonExist = false; @Column(name = "NOTES") private String notes; - @OneToOne @JoinColumn(name = "INSTITUTION_ID", insertable = false, updatable = false) private InstitutionEntity institution; diff --git a/src/main/resources/db/changelog/changes/1-ays-ddl.xml b/src/main/resources/db/changelog/changes/1-ays-ddl.xml index 9f4b0ab8d..e188520c1 100644 --- a/src/main/resources/db/changelog/changes/1-ays-ddl.xml +++ b/src/main/resources/db/changelog/changes/1-ays-ddl.xml @@ -144,7 +144,9 @@ - + + + From 8445f183863105c48a1e5aa3c50189d148164a32 Mon Sep 17 00:00:00 2001 From: m1erla <116915007+m1erla@users.noreply.github.com> Date: Tue, 14 Jan 2025 22:34:27 +0300 Subject: [PATCH 2/8] Refactor email validation limits - AYS-661 : Update email validation min length to 6 characters - AYS-661 : Set email validation max length to 254 characters - Add test cases for email validation limits --- ...egistrationApplicationCompleteRequest.java | 5 +- .../request/AysPasswordForgotRequest.java | 2 +- .../model/request/AysUserCreateRequest.java | 11 +- .../model/request/AysUserUpdateRequest.java | 5 +- .../validation/EmailAddressValidator.java | 34 +- .../java/org/ays/AysRestControllerTest.java | 193 +- ...RegistrationApplicationControllerTest.java | 1603 ++++++------- .../controller/AysAuthControllerTest.java | 948 ++++---- .../controller/AysUserControllerTest.java | 2001 +++++++++-------- 9 files changed, 2474 insertions(+), 2328 deletions(-) diff --git a/src/main/java/org/ays/auth/model/request/AdminRegistrationApplicationCompleteRequest.java b/src/main/java/org/ays/auth/model/request/AdminRegistrationApplicationCompleteRequest.java index c3b48ef6d..aed27e2f7 100644 --- a/src/main/java/org/ays/auth/model/request/AdminRegistrationApplicationCompleteRequest.java +++ b/src/main/java/org/ays/auth/model/request/AdminRegistrationApplicationCompleteRequest.java @@ -15,7 +15,8 @@ * Represents a complete registration request for an admin user. * *

- * Encapsulates the necessary information for completing the admin registration process. + * Encapsulates the necessary information for completing the admin registration + * process. *

*/ @Getter @@ -39,7 +40,7 @@ public class AdminRegistrationApplicationCompleteRequest { @EmailAddress @NotBlank - @Size(min = 2, max = 255) + @Size(min = 6, max = 254) private String emailAddress; @NotBlank diff --git a/src/main/java/org/ays/auth/model/request/AysPasswordForgotRequest.java b/src/main/java/org/ays/auth/model/request/AysPasswordForgotRequest.java index e687b08e0..1f8002d24 100644 --- a/src/main/java/org/ays/auth/model/request/AysPasswordForgotRequest.java +++ b/src/main/java/org/ays/auth/model/request/AysPasswordForgotRequest.java @@ -12,7 +12,7 @@ public class AysPasswordForgotRequest { @EmailAddress @NotBlank - @Size(min = 2, max = 255) + @Size(min = 6, max = 254) private String emailAddress; } diff --git a/src/main/java/org/ays/auth/model/request/AysUserCreateRequest.java b/src/main/java/org/ays/auth/model/request/AysUserCreateRequest.java index 17e016fce..abd378e6c 100644 --- a/src/main/java/org/ays/auth/model/request/AysUserCreateRequest.java +++ b/src/main/java/org/ays/auth/model/request/AysUserCreateRequest.java @@ -16,10 +16,13 @@ /** * Represents a request for creating a new user in the system. - * This class contains all the necessary details required to create a user, including their personal information, - * contact details, and roles. The fields are validated to ensure they meet the required constraints. + * This class contains all the necessary details required to create a user, + * including their personal information, + * contact details, and roles. The fields are validated to ensure they meet the + * required constraints. *

- * This class uses various annotations for validation to ensure the data integrity and consistency. + * This class uses various annotations for validation to ensure the data + * integrity and consistency. *

*/ @Getter @@ -38,7 +41,7 @@ public class AysUserCreateRequest { @EmailAddress @NotBlank - @Size(min = 2, max = 255) + @Size(min = 6, max = 254) private String emailAddress; @Valid diff --git a/src/main/java/org/ays/auth/model/request/AysUserUpdateRequest.java b/src/main/java/org/ays/auth/model/request/AysUserUpdateRequest.java index 0ffcfeff0..f260764b5 100644 --- a/src/main/java/org/ays/auth/model/request/AysUserUpdateRequest.java +++ b/src/main/java/org/ays/auth/model/request/AysUserUpdateRequest.java @@ -17,7 +17,8 @@ /** * Request object for updating a user's details. *

- * This class encapsulates the data required to update an existing user's details. + * This class encapsulates the data required to update an existing user's + * details. * Each field is validated to ensure it meets the necessary requirements. */ @Getter @@ -36,7 +37,7 @@ public class AysUserUpdateRequest { @EmailAddress @NotBlank - @Size(min = 2, max = 255) + @Size(min = 6, max = 254) private String emailAddress; @Valid diff --git a/src/main/java/org/ays/common/util/validation/EmailAddressValidator.java b/src/main/java/org/ays/common/util/validation/EmailAddressValidator.java index da51c3ac5..4ba29f201 100644 --- a/src/main/java/org/ays/common/util/validation/EmailAddressValidator.java +++ b/src/main/java/org/ays/common/util/validation/EmailAddressValidator.java @@ -11,25 +11,28 @@ */ class EmailAddressValidator implements ConstraintValidator { - private static final String EMAIL_REGEX = - "^(?!.*\\.\\.|.*--|.*-@|.*@\\.|.*\\.-|.*-\\.).+[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"; + private static final String EMAIL_REGEX = "^(?!.*\\.\\.|.*--|.*-@|.*@\\.|.*\\.-|.*-\\.).+[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"; /** * Checks whether the given value is a valid email or not. - *

Some valid emails are:

+ *

+ * Some valid emails are: + *

*
    - *
  • user@example.com
  • - *
  • john.doe123@example.co.uk
  • - *
  • admin_123@example.org
  • + *
  • user@example.com
  • + *
  • john.doe123@example.co.uk
  • + *
  • admin_123@example.org
  • *
* - *

Some invalid emails are:

+ *

+ * Some invalid emails are: + *

*
    - *
  • user@invalid
  • - *
  • user@invalid!.com
  • - *
  • u@ser@.com
  • - *
  • user@..com
  • - *
  • user
  • + *
  • user@invalid
  • + *
  • user@invalid!.com
  • + *
  • u@ser@.com
  • + *
  • user@..com
  • + *
  • user
  • *
* * @param email object to validate @@ -42,6 +45,10 @@ public boolean isValid(String email, ConstraintValidatorContext constraintValida return true; } + if (email.length() < 6 || email.length() > 254) { + return this.buildViolation(constraintValidatorContext, "email length must be between 6 and 254 characters"); + } + if (email.startsWith(" ") || email.endsWith(" ")) { return this.buildViolation(constraintValidatorContext, "email must not start or end with whitespace"); } @@ -56,7 +63,8 @@ public boolean isValid(String email, ConstraintValidatorContext constraintValida String[] parts = email.split("@", 2); if (parts[0].isEmpty() || !Character.isLetterOrDigit(parts[0].charAt(0))) { - return this.buildViolation(constraintValidatorContext, "email local part must start with a letter or number"); + return this.buildViolation(constraintValidatorContext, + "email local part must start with a letter or number"); } String domainPart = parts[1]; diff --git a/src/test/java/org/ays/AysRestControllerTest.java b/src/test/java/org/ays/AysRestControllerTest.java index 7c434989b..515d7e790 100644 --- a/src/test/java/org/ays/AysRestControllerTest.java +++ b/src/test/java/org/ays/AysRestControllerTest.java @@ -31,104 +31,105 @@ @SpringBootTest @AutoConfigureMockMvc -@ActiveProfiles({"default", "test"}) +@ActiveProfiles({ "default", "test" }) @ExtendWith(MockitoExtension.class) public abstract class AysRestControllerTest extends AysTestContainerConfiguration { - @Autowired - protected AysMockMvc aysMockMvc; - - @Autowired - protected AysUserReadPort userReadPort; - - - protected AysToken mockSuperAdminToken; - protected AysToken mockAdminToken; - protected AysToken mockUserToken; - - - @Mock - private AysTokenConfigurationParameter tokenConfiguration; - @Mock - private AysParameterService parameterService; - - @BeforeEach - @SuppressWarnings("OptionalGetWithoutIsPresent disabled because of the test data is valid") - public void initializeAuth() { - Set parameters = Set.of( - AysParameter.builder() - .name("AUTH_TOKEN_PRIVATE_KEY") - .definition("dkNHbEVaSW9rdGQ1cVBKek03dHJqT2daMmdIbEpUSEw=LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCk1JSUV2QUlCQURBTkJna3Foa2lHOXcwQkFRRUZBQVNDQktZd2dnU2lBZ0VBQW9JQkFRQ0puK0lSRUhmclFsWjEKY0RkbllKcndUL1pvV1hick13aGt1enBXdUJVRDJHdTVteU1DdCtyT0p1S3N4VXFIL2IyZGFlZW81dUdndWY2cwpKcjI5bzZLM1drVVBMdG9uNk5GZUhKZEwwcWs5NmVlWDM5UVhFUHYzdS8yRGJXZTJRWk83aW4wNnhLSTQyOUgwCmg4cjdCT0NzemE1WFpkbjVCUHdmZ0pveUFLbkdGODNPMGpUT0Qzem8zV0VvbGEreW1YYXVoUHorUFl0dndSWncKSkZvQW91MTVZM2Z1SDBna3B4NDdRd0tnVFRwNUl4d1I4RnNxVkVOK1JaNFMzcWhxSktOaGY2SVkwS1RjaHphQQpmelp3QnRON0E1eWI4TURMRnRJTHlmRkdhYW0wbXMzUHJWb0J3LzhzYmxsSGlEMTdOVCtXcmNNVkJRRjJTRFlwCmR3WENMM1JiQWdNQkFBRUNnZ0VBS3FzK2JPbjlOQnlTTDhFRi9IQXdPWGVoUHdNVjRxQWs1dzVCYlNlUHBHeVMKSWE2ZXNUWVNmNjRBczI1THlGUDhXUFMvMVZjWDl6d1RZSTUyWDNoL2QzZHVWK0cvMDRYVWUraERaRWZCSHlnSgpITVpSdklFUWplTmtHejV0WEUyQ255KzEyZVdqSWh2TlFaSmtkV1V5djREWm45RTlQbjYwS0pRM3VtOElOQmt3CjZLNUFIMGxiYy8yMEtFYWZpMUsyMzBTUVFCeFp4bEN6bkNJSmNQYlZGSmxwVUNOM1JkaDFJSkdYZUlLT0NRbjkKQnB4Z2ZKVC9KUFo1cWVXamF1OUYxVWlCbWYyZjFzNnlDSXB2aDVsYUhKMXRuU1NuQXlKalkzYzFFUk1GMHBDSgpwUUtIMlEyaXFEYUxGNGdRQWIyQlRnYm85NWVja1RZaFdPVUlQQUt3UVFLQmdRRGhUbXhvTlQwRXlQWEptVFRPClJvdUNwSW4ycmlldGcwUXdYVVVVSDhMMHNJRUVqRDFpeXp5NVBvNk55OWN1QVNYSXBRM05RaDExZ2k1MTJXNmYKYXIyYWtXdXVRK01ENmRNM3RCa0xQSlJITVQ1SmVJbDBQQTJXc2tvMktTcmZtbUI1TFI5MzdpeFNjZmhvajY4bQpWSHBGQ2FhRDBtdVVzUHZYVVR0ZmhMV1ZZUUtCZ1FDY1g0ekllNS9HSWk3Z0pMZTZZTm5ITmprUmhIb3VreEVECm1HbjB2WlRlUzh1SXZrL1JQL2dMdjIwN3hIOXcwMEc1cS9BbytnWG1DV3pQWlhybmR1SVVZbUxwZ2NKVDh5bWMKQVovSWhsbkU4azBxTHlleWRtWmV0NHNXcXY3NkFseFlhNTdFZ3daaTNMQ1Z3OFBIVnIwb1pHR09vTGd3a3g3QgpRV2V4cXJ4bk93S0JnRXlGNlZYL2R3a1FCRU1Ea1NiYVdQbjNUcENGR0I3YnJhWkxsM0c5VStidHAvUldlV2I3CnBsVTRoUXh1QmxpdXRSbVB6YjlBVEdjajN3blIzcnV3Y2xOMFBzR0NkekZXRXBJaHpqdTl5SkxoaThsQ2NsVVQKTEg1WmNkRXhiRWxqMG81MW4vR0k2RzdjSE1YT3YydGlWK0RvNVRCeW9HMXhLeWczZzlYdWFnb2hBb0dBUkJ0Wgp0ZmdpSHFuRXdOczlLbkFFYWorem0yMlh5YkZFTjh5cVdXNDQ2SmthalBSV3oweU5QSkNqZ3VTU25SRm1EdmhVCklZVEVETzBOOTBhN3dSU0dZMXAydWoxSjVrYUNXUEJjSjNwY251cnBzUFhZMUdHOU5JTzhrS0xwYXZxY1BlYWgKdi9WUlVyM01LMjZZVnJud3FTY1BWbytwcVg1cVpzR1Y2RXYwd3dFQ2dZQkdSckF2cEptVWluazVoc0ZldzRvaAozclMwRTlqQklqekszWU9WWTJJVWxydmJYd2N2OWJ0UGpaVkd4VzZ1UEt0UEtPNTFaUExzRS94YWRiUGllOGhmCmM1aTB3LzVPK24rYlc3TlpmcWwvUFk4OTB5ajZJWHlzZElYMVRwQ1NST1Uzb1ptUWZBTG9WWWNBNHU3QWxpY0sKYnBKbTYwampiMHNNejRIVU9CaVZIUT09Ci0tLS0tRU5EIFBSSVZBVEUgS0VZLS0tLS0KNjNLcEhJM2FXeUtrOHNsVkFiaGY3UjBvcURVeHh5RUk=").build(), - AysParameter.builder() - .name("AUTH_TOKEN_PUBLIC_KEY") - .definition("cExiWUJBSVFTbHVwOXFXSkxSTDhsOGRDVkh1VkxyQ0E=LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUlJQklqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUFpWi9pRVJCMzYwSldkWEEzWjJDYQo4RS8yYUZsMjZ6TUlaTHM2VnJnVkE5aHJ1WnNqQXJmcXppYmlyTVZLaC8yOW5Xbm5xT2Job0xuK3JDYTl2YU9pCnQxcEZEeTdhSitqUlhoeVhTOUtwUGVubmw5L1VGeEQ3OTd2OWcyMW50a0dUdTRwOU9zU2lPTnZSOUlmSyt3VGcKck0ydVYyWForUVQ4SDRDYU1nQ3B4aGZOenRJMHpnOTg2TjFoS0pXdnNwbDJyb1Q4L2oyTGI4RVdjQ1JhQUtMdAplV04zN2g5SUpLY2VPME1Db0UwNmVTTWNFZkJiS2xSRGZrV2VFdDZvYWlTallYK2lHTkNrM0ljMmdIODJjQWJUCmV3T2NtL0RBeXhiU0M4bnhSbW1wdEpyTno2MWFBY1AvTEc1WlI0ZzllelUvbHEzREZRVUJka2cyS1hjRndpOTAKV3dJREFRQUIKLS0tLS1FTkQgUFVCTElDIEtFWS0tLS0tCg==SlhaNTl3eFBsWTJEWkNSWDdlZFRlaFhPaFhZRzlBN1Q=").build(), - AysParameter.builder() - .name("AUTH_ACCESS_TOKEN_EXPIRE_MINUTE") - .definition("120") - .build(), - AysParameter.builder() - .name("AUTH_REFRESH_TOKEN_EXPIRE_DAY") - .definition("1") - .build(), - AysParameter.builder() - .name("AUTH_LOGIN_MAX_TRY_COUNT") - .definition("3") - .build() - ); - Mockito.when(parameterService.findAll(Mockito.anyString())) - .thenReturn(parameters); - - this.tokenConfiguration = new AysTokenConfigurationParameter(parameterService); - - final Optional superAdmin = userReadPort.findById(AysValidTestData.SuperAdmin.ID); - final Claims claimsOfMockSuperAdminToken = superAdmin.get().getClaims(); - this.mockSuperAdminToken = this.generate(claimsOfMockSuperAdminToken); - - final Optional admin = userReadPort.findById(AysValidTestData.Admin.ID); - final Claims claimsOfMockAdminToken = admin.get().getClaims(); - this.mockAdminToken = this.generate(claimsOfMockAdminToken); - - final Optional user = userReadPort.findById(AysValidTestData.User.ID); - final Claims claimsOfMockUserToken = user.get().getClaims(); - this.mockUserToken = this.generate(claimsOfMockUserToken); - } - - private AysToken generate(Map claims) { - final long currentTimeMillis = System.currentTimeMillis(); - - final Date tokenIssuedAt = new Date(currentTimeMillis); - - final Date accessTokenExpiresAt = DateUtils.addMinutes(new Date(currentTimeMillis), tokenConfiguration.getAccessTokenExpireMinute()); - final String accessToken = Jwts.builder() - .header() - .add(AysTokenClaims.TYPE.getValue(), OAuth2AccessToken.TokenType.BEARER.getValue()) - .and() - .id(AysRandomUtil.generateUUID()) - .issuer(tokenConfiguration.getIssuer()) - .issuedAt(tokenIssuedAt) - .expiration(accessTokenExpiresAt) - .signWith(tokenConfiguration.getPrivateKey()) - .claims(claims) - .compact(); - - final Date refreshTokenExpiresAt = DateUtils.addDays(new Date(currentTimeMillis), tokenConfiguration.getRefreshTokenExpireDay()); - final JwtBuilder refreshTokenBuilder = Jwts.builder(); - final String refreshToken = refreshTokenBuilder - .header() - .add(AysTokenClaims.TYPE.getValue(), OAuth2AccessToken.TokenType.BEARER.getValue()) - .and() - .id(AysRandomUtil.generateUUID()) - .issuer(tokenConfiguration.getIssuer()) - .issuedAt(tokenIssuedAt) - .expiration(refreshTokenExpiresAt) - .signWith(tokenConfiguration.getPrivateKey()) - .claim(AysTokenClaims.USER_ID.getValue(), claims.get(AysTokenClaims.USER_ID.getValue())) - .compact(); - - return AysToken.builder() - .accessToken(accessToken) - .refreshToken(refreshToken) - .build(); - } + @Autowired + protected AysMockMvc aysMockMvc; + + @Autowired + protected AysUserReadPort userReadPort; + + protected AysToken mockSuperAdminToken; + protected AysToken mockAdminToken; + protected AysToken mockUserToken; + + @Mock + private AysTokenConfigurationParameter tokenConfiguration; + @Mock + private AysParameterService parameterService; + + @BeforeEach + @SuppressWarnings("OptionalGetWithoutIsPresent disabled because of the test data is valid") + public void initializeAuth() { + Set parameters = Set.of( + AysParameter.builder() + .name("AUTH_TOKEN_PRIVATE_KEY") + .definition("dkNHbEVaSW9rdGQ1cVBKek03dHJqT2daMmdIbEpUSEw=LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCk1JSUV2QUlCQURBTkJna3Foa2lHOXcwQkFRRUZBQVNDQktZd2dnU2lBZ0VBQW9JQkFRQ0puK0lSRUhmclFsWjEKY0RkbllKcndUL1pvV1hick13aGt1enBXdUJVRDJHdTVteU1DdCtyT0p1S3N4VXFIL2IyZGFlZW81dUdndWY2cwpKcjI5bzZLM1drVVBMdG9uNk5GZUhKZEwwcWs5NmVlWDM5UVhFUHYzdS8yRGJXZTJRWk83aW4wNnhLSTQyOUgwCmg4cjdCT0NzemE1WFpkbjVCUHdmZ0pveUFLbkdGODNPMGpUT0Qzem8zV0VvbGEreW1YYXVoUHorUFl0dndSWncKSkZvQW91MTVZM2Z1SDBna3B4NDdRd0tnVFRwNUl4d1I4RnNxVkVOK1JaNFMzcWhxSktOaGY2SVkwS1RjaHphQQpmelp3QnRON0E1eWI4TURMRnRJTHlmRkdhYW0wbXMzUHJWb0J3LzhzYmxsSGlEMTdOVCtXcmNNVkJRRjJTRFlwCmR3WENMM1JiQWdNQkFBRUNnZ0VBS3FzK2JPbjlOQnlTTDhFRi9IQXdPWGVoUHdNVjRxQWs1dzVCYlNlUHBHeVMKSWE2ZXNUWVNmNjRBczI1THlGUDhXUFMvMVZjWDl6d1RZSTUyWDNoL2QzZHVWK0cvMDRYVWUraERaRWZCSHlnSgpITVpSdklFUWplTmtHejV0WEUyQ255KzEyZVdqSWh2TlFaSmtkV1V5djREWm45RTlQbjYwS0pRM3VtOElOQmt3CjZLNUFIMGxiYy8yMEtFYWZpMUsyMzBTUVFCeFp4bEN6bkNJSmNQYlZGSmxwVUNOM1JkaDFJSkdYZUlLT0NRbjkKQnB4Z2ZKVC9KUFo1cWVXamF1OUYxVWlCbWYyZjFzNnlDSXB2aDVsYUhKMXRuU1NuQXlKalkzYzFFUk1GMHBDSgpwUUtIMlEyaXFEYUxGNGdRQWIyQlRnYm85NWVja1RZaFdPVUlQQUt3UVFLQmdRRGhUbXhvTlQwRXlQWEptVFRPClJvdUNwSW4ycmlldGcwUXdYVVVVSDhMMHNJRUVqRDFpeXp5NVBvNk55OWN1QVNYSXBRM05RaDExZ2k1MTJXNmYKYXIyYWtXdXVRK01ENmRNM3RCa0xQSlJITVQ1SmVJbDBQQTJXc2tvMktTcmZtbUI1TFI5MzdpeFNjZmhvajY4bQpWSHBGQ2FhRDBtdVVzUHZYVVR0ZmhMV1ZZUUtCZ1FDY1g0ekllNS9HSWk3Z0pMZTZZTm5ITmprUmhIb3VreEVECm1HbjB2WlRlUzh1SXZrL1JQL2dMdjIwN3hIOXcwMEc1cS9BbytnWG1DV3pQWlhybmR1SVVZbUxwZ2NKVDh5bWMKQVovSWhsbkU4azBxTHlleWRtWmV0NHNXcXY3NkFseFlhNTdFZ3daaTNMQ1Z3OFBIVnIwb1pHR09vTGd3a3g3QgpRV2V4cXJ4bk93S0JnRXlGNlZYL2R3a1FCRU1Ea1NiYVdQbjNUcENGR0I3YnJhWkxsM0c5VStidHAvUldlV2I3CnBsVTRoUXh1QmxpdXRSbVB6YjlBVEdjajN3blIzcnV3Y2xOMFBzR0NkekZXRXBJaHpqdTl5SkxoaThsQ2NsVVQKTEg1WmNkRXhiRWxqMG81MW4vR0k2RzdjSE1YT3YydGlWK0RvNVRCeW9HMXhLeWczZzlYdWFnb2hBb0dBUkJ0Wgp0ZmdpSHFuRXdOczlLbkFFYWorem0yMlh5YkZFTjh5cVdXNDQ2SmthalBSV3oweU5QSkNqZ3VTU25SRm1EdmhVCklZVEVETzBOOTBhN3dSU0dZMXAydWoxSjVrYUNXUEJjSjNwY251cnBzUFhZMUdHOU5JTzhrS0xwYXZxY1BlYWgKdi9WUlVyM01LMjZZVnJud3FTY1BWbytwcVg1cVpzR1Y2RXYwd3dFQ2dZQkdSckF2cEptVWluazVoc0ZldzRvaAozclMwRTlqQklqekszWU9WWTJJVWxydmJYd2N2OWJ0UGpaVkd4VzZ1UEt0UEtPNTFaUExzRS94YWRiUGllOGhmCmM1aTB3LzVPK24rYlc3TlpmcWwvUFk4OTB5ajZJWHlzZElYMVRwQ1NST1Uzb1ptUWZBTG9WWWNBNHU3QWxpY0sKYnBKbTYwampiMHNNejRIVU9CaVZIUT09Ci0tLS0tRU5EIFBSSVZBVEUgS0VZLS0tLS0KNjNLcEhJM2FXeUtrOHNsVkFiaGY3UjBvcURVeHh5RUk=") + .build(), + AysParameter.builder() + .name("AUTH_TOKEN_PUBLIC_KEY") + .definition("cExiWUJBSVFTbHVwOXFXSkxSTDhsOGRDVkh1VkxyQ0E=LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUlJQklqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUFpWi9pRVJCMzYwSldkWEEzWjJDYQo4RS8yYUZsMjZ6TUlaTHM2VnJnVkE5aHJ1WnNqQXJmcXppYmlyTVZLaC8yOW5Xbm5xT2Job0xuK3JDYTl2YU9pCnQxcEZEeTdhSitqUlhoeVhTOUtwUGVubmw5L1VGeEQ3OTd2OWcyMW50a0dUdTRwOU9zU2lPTnZSOUlmSyt3VGcKck0ydVYyWForUVQ4SDRDYU1nQ3B4aGZOenRJMHpnOTg2TjFoS0pXdnNwbDJyb1Q4L2oyTGI4RVdjQ1JhQUtMdAplV04zN2g5SUpLY2VPME1Db0UwNmVTTWNFZkJiS2xSRGZrV2VFdDZvYWlTallYK2lHTkNrM0ljMmdIODJjQWJUCmV3T2NtL0RBeXhiU0M4bnhSbW1wdEpyTno2MWFBY1AvTEc1WlI0ZzllelUvbHEzREZRVUJka2cyS1hjRndpOTAKV3dJREFRQUIKLS0tLS1FTkQgUFVCTElDIEtFWS0tLS0tCg==SlhaNTl3eFBsWTJEWkNSWDdlZFRlaFhPaFhZRzlBN1Q=") + .build(), + AysParameter.builder() + .name("AUTH_ACCESS_TOKEN_EXPIRE_MINUTE") + .definition("120") + .build(), + AysParameter.builder() + .name("AUTH_REFRESH_TOKEN_EXPIRE_DAY") + .definition("1") + .build(), + AysParameter.builder() + .name("AUTH_LOGIN_MAX_TRY_COUNT") + .definition("3") + .build()); + Mockito.when(parameterService.findAll(Mockito.anyString())) + .thenReturn(parameters); + + this.tokenConfiguration = new AysTokenConfigurationParameter(parameterService); + + final Optional superAdmin = userReadPort.findById(AysValidTestData.SuperAdmin.ID); + final Claims claimsOfMockSuperAdminToken = superAdmin.get().getClaims(); + this.mockSuperAdminToken = this.generate(claimsOfMockSuperAdminToken); + + final Optional admin = userReadPort.findById(AysValidTestData.Admin.ID); + final Claims claimsOfMockAdminToken = admin.get().getClaims(); + this.mockAdminToken = this.generate(claimsOfMockAdminToken); + + final Optional user = userReadPort.findById(AysValidTestData.User.ID); + final Claims claimsOfMockUserToken = user.get().getClaims(); + this.mockUserToken = this.generate(claimsOfMockUserToken); + } + + private AysToken generate(Map claims) { + final long currentTimeMillis = System.currentTimeMillis(); + + final Date tokenIssuedAt = new Date(currentTimeMillis); + + final Date accessTokenExpiresAt = DateUtils.addMinutes(new Date(currentTimeMillis), + tokenConfiguration.getAccessTokenExpireMinute()); + final String accessToken = Jwts.builder() + .header() + .add(AysTokenClaims.TYPE.getValue(), OAuth2AccessToken.TokenType.BEARER.getValue()) + .and() + .id(AysRandomUtil.generateUUID()) + .issuer(tokenConfiguration.getIssuer()) + .issuedAt(tokenIssuedAt) + .expiration(accessTokenExpiresAt) + .signWith(tokenConfiguration.getPrivateKey()) + .claims(claims) + .compact(); + + final Date refreshTokenExpiresAt = DateUtils.addDays(new Date(currentTimeMillis), + tokenConfiguration.getRefreshTokenExpireDay()); + final JwtBuilder refreshTokenBuilder = Jwts.builder(); + final String refreshToken = refreshTokenBuilder + .header() + .add(AysTokenClaims.TYPE.getValue(), OAuth2AccessToken.TokenType.BEARER.getValue()) + .and() + .id(AysRandomUtil.generateUUID()) + .issuer(tokenConfiguration.getIssuer()) + .issuedAt(tokenIssuedAt) + .expiration(refreshTokenExpiresAt) + .signWith(tokenConfiguration.getPrivateKey()) + .claim(AysTokenClaims.USER_ID.getValue(), claims.get(AysTokenClaims.USER_ID.getValue())) + .compact(); + + return AysToken.builder() + .accessToken(accessToken) + .refreshToken(refreshToken) + .build(); + } } diff --git a/src/test/java/org/ays/auth/controller/AdminRegistrationApplicationControllerTest.java b/src/test/java/org/ays/auth/controller/AdminRegistrationApplicationControllerTest.java index 68b38cde1..d48d9c60c 100644 --- a/src/test/java/org/ays/auth/controller/AdminRegistrationApplicationControllerTest.java +++ b/src/test/java/org/ays/auth/controller/AdminRegistrationApplicationControllerTest.java @@ -48,784 +48,829 @@ class AdminRegistrationApplicationControllerTest extends AysRestControllerTest { - @MockBean - private AdminRegistrationApplicationService adminRegistrationApplicationService; - - @MockBean - private AdminRegistrationCompleteService adminRegistrationCompleteService; - - private final AdminRegistrationApplicationToApplicationsResponseMapper adminRegistrationApplicationToApplicationsResponseMapper = AdminRegistrationApplicationToApplicationsResponseMapper.initialize(); - private final AdminRegistrationApplicationToApplicationResponseMapper adminRegistrationApplicationToApplicationResponseMapper = AdminRegistrationApplicationToApplicationResponseMapper.initialize(); - private final AdminRegistrationApplicationToSummaryResponseMapper adminRegistrationApplicationToSummaryResponseMapper = AdminRegistrationApplicationToSummaryResponseMapper.initialize(); - private final AdminRegistrationApplicationToCreateResponseMapper adminRegistrationApplicationToCreateResponseMapper = AdminRegistrationApplicationToCreateResponseMapper.initialize(); - - - private static final String BASE_PATH = "/api/v1"; - - - @Test - void givenValidAdminRegisterApplicationListRequest_whenAdminRegisterApplicationsFound_thenReturnAysPageResponseOfAdminRegisterApplicationsResponse() throws Exception { - - // Given - AdminRegistrationApplicationListRequest mockListRequest = new AdminRegistrationApplicationListRequestBuilder() - .withValidValues() - .build(); - - // When - List mockApplications = List.of( - new AdminRegistrationApplicationBuilder().withValidValues().build() - ); - - AysPage mockApplicationPage = AysPageBuilder - .from(mockApplications, mockListRequest.getPageable()); - - Mockito.when(adminRegistrationApplicationService.findAll(Mockito.any(AdminRegistrationApplicationListRequest.class))) - .thenReturn(mockApplicationPage); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-applications"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockSuperAdminToken.getAccessToken(), mockListRequest); - - List mockApplicationsResponse = adminRegistrationApplicationToApplicationsResponseMapper.map(mockApplications); - AysPageResponse pageOfResponse = AysPageResponse.builder() - .of(mockApplicationPage) - .content(mockApplicationsResponse) - .build(); - AysResponse> mockResponse = AysResponse - .successOf(pageOfResponse); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .isNotEmpty()); - - // Verify - Mockito.verify(adminRegistrationApplicationService, Mockito.times(1)) - .findAll(Mockito.any(AdminRegistrationApplicationListRequest.class)); - } - - @Test - void givenValidAdminRegisterApplicationListRequest_whenUnauthorizedForListing_thenReturnAccessDeniedException() throws Exception { - // Given - AdminRegistrationApplicationListRequest mockListRequest = new AdminRegistrationApplicationListRequestBuilder() - .withValidValues() - .build(); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-applications"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockUserToken.getAccessToken(), mockListRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isForbidden()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .doesNotExist()); - - // Verify - Mockito.verify(adminRegistrationApplicationService, Mockito.never()) - .findAll(Mockito.any(AdminRegistrationApplicationListRequest.class)); - } - - @Test - void givenValidAdminRegisterApplicationId_whenAdminRegisterApplicationFound_thenReturnAdminRegisterApplicationResponse() throws Exception { - - // Given - String mockApplicationId = "a0c2351d-54ce-4019-8ffe-a2f8a2700824"; - - // When - AdminRegistrationApplication mockRegisterApplication = new AdminRegistrationApplicationBuilder() - .withId(mockApplicationId) - .build(); - Mockito.when(adminRegistrationApplicationService.findById(mockApplicationId)) - .thenReturn(mockRegisterApplication); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockApplicationId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .get(endpoint, mockSuperAdminToken.getAccessToken()); - - AdminRegistrationApplicationResponse mockApplicationResponse = adminRegistrationApplicationToApplicationResponseMapper - .map(mockRegisterApplication); - AysResponse mockResponse = AysResponse - .successOf(mockApplicationResponse); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .isNotEmpty()); - - // Verify - Mockito.verify(adminRegistrationApplicationService, Mockito.times(1)) - .findById(mockApplicationId); - - } - - @Test - void givenValidAdminRegisterApplicationId_whenUnauthorizedForGettingAdminRegisterApplicationById_thenReturnAccessDeniedException() throws Exception { - - // Given - String mockApplicationId = "68c867b4-e84a-405c-b1ab-f8dcaa9c41f2"; - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application/".concat(mockApplicationId)); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .get(endpoint, mockUserToken.getAccessToken()); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isForbidden()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .doesNotExist()); - - // Verify - Mockito.verify(adminRegistrationApplicationService, Mockito.never()) - .findById(mockApplicationId); - } - - @ParameterizedTest - @ValueSource(strings = { - "This is a valid text with sufficient length and contains alphabetic characters.", - "This text includes numbers 12345 and still should be accepted because it's within limits.", - "This text, which includes punctuation marks, should be accepted.", - "ÇalıŞkan ve dÜrüst İnsanlar her zaman başarıyı yakalar.(:;?/)" - }) - void givenValidAdminRegisterApplicationCreateRequest_whenCreatingAdminRegisterApplication_thenReturnAdminRegisterApplicationCreateResponse(String reason) throws Exception { - - // Given - AdminRegistrationApplicationCreateRequest mockRequest = new AdminRegistrationApplicationCreateRequestBuilder() - .withValidValues() - .withInstitutionId(AysValidTestData.SuperAdmin.INSTITUTION_ID) - .withReason(reason) - .build(); - - // When - Institution mockInstitution = new InstitutionBuilder() - .withId(mockRequest.getInstitutionId()) - .build(); - AdminRegistrationApplication mockRegisterApplication = new AdminRegistrationApplicationBuilder() - .withValidValues() - .withInstitution(mockInstitution) - .withReason(mockRequest.getReason()) - .withStatus(AdminRegistrationApplicationStatus.WAITING) - .build(); - - Mockito.when(adminRegistrationApplicationService.create(Mockito.any(AdminRegistrationApplicationCreateRequest.class))) - .thenReturn(mockRegisterApplication); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockSuperAdminToken.getAccessToken(), mockRequest); - - AdminRegistrationApplicationCreateResponse mockApplicationCreateResponse = adminRegistrationApplicationToCreateResponseMapper - .map(mockRegisterApplication); - AysResponse mockResponse = AysResponse.successOf(mockApplicationCreateResponse); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .isNotEmpty()); - - // Verify - Mockito.verify(adminRegistrationApplicationService, Mockito.times(1)) - .create(Mockito.any(AdminRegistrationApplicationCreateRequest.class)); - } - - @ParameterizedTest - @ValueSource(strings = { - "Invalid reason with special characters: #$%", - "#$½#$£#$£#$$#½#£$£#$#£½#$½#$½$£#$#£$$#½#$$½", - ".,..,.,.,.,.,,.,.,.,.,.,.,.,.,..,.,.,,.,.,.,", - "Too short", - " a", - " spaceAtTheBeginning", - "spaceAtTheEnd ", - " both ", - " justAString ", - "151201485621548562154851458614125461254125412", - "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean vestibulum commodo turpis, sed venenatis sapien suscipit sit amet. Etiam malesuada, ligula in semper varius, nisi mi pulvinar libero, ut commodo dolor orci quis urna. Vivamus ac euismod ex. Proin vel vulputate orci. Ut id nibh finibus, mattis sem id, maximus ante. Proin fringilla ipsum at arcu venenatis, non bibendum justo luctus. Phasellus vestibulum feugiat est sit amet bibendum. Donec nulla leo, ultricies sed pharetra sed, hendrerit vel nunc." - }) - void givenInvalidAdminRegisterApplicationCreateRequest_whenCreatingAdminRegisterApplication_thenReturnValidationError(String invalidReason) throws Exception { - - // Given - AdminRegistrationApplicationCreateRequest createRequest = new AdminRegistrationApplicationCreateRequestBuilder() - .withValidValues() - .withReason(invalidReason) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockUserToken.getAccessToken(), createRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(adminRegistrationApplicationService, Mockito.never()) - .create(Mockito.any(AdminRegistrationApplicationCreateRequest.class)); - } - - @Test - void givenValidAdminRegisterApplicationCreateRequest_whenUnauthorizedForCreatingAdminRegisterApplication_thenReturnAccessDeniedException() throws Exception { - - // Given - AdminRegistrationApplicationCreateRequest mockRequest = new AdminRegistrationApplicationCreateRequestBuilder() - .withValidValues() - .withInstitutionId(AysValidTestData.SuperAdmin.INSTITUTION_ID) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockUserToken.getAccessToken(), mockRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isForbidden()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .doesNotExist()); - - // Verify - Mockito.verify(adminRegistrationApplicationService, Mockito.never()) - .create(Mockito.any(AdminRegistrationApplicationCreateRequest.class)); - } - - - @Test - void givenValidAdminRegisterApplicationId_whenAdminApplicationFound_thenReturnAdminApplicationSummaryResponse() throws Exception { - - // Given - String mockId = "085fbe72-caa7-439d-8db1-166ed005e120"; - AdminRegistrationApplication mockAdminRegistrationApplication = new AdminRegistrationApplicationBuilder() - .withId(mockId) - .build(); - - // When - Mockito.when(adminRegistrationApplicationService.findSummaryById(mockId)) - .thenReturn(mockAdminRegistrationApplication); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application/".concat(mockId).concat("/summary")); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .get(endpoint); - - AdminRegistrationApplicationSummaryResponse mockSummaryResponse = adminRegistrationApplicationToSummaryResponseMapper - .map(mockAdminRegistrationApplication); - AysResponse mockResponse = AysResponse - .successOf(mockSummaryResponse); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .isNotEmpty()); - } - - @Test - void givenIdAndAdminRegisterApplication_whenAdminApplicationNotFound_thenReturnUnauthorizedError() throws Exception { - - // Given - String mockId = "181e8310-6dfd-444c-aa38-056ce8401345"; - - // When - Mockito.when(adminRegistrationApplicationService.findSummaryById(mockId)) - .thenThrow(new AysAdminRegistrationApplicationNotExistByIdAuthException(mockId)); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application/".concat(mockId).concat("/summary")); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .get(endpoint); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.UNAUTHORIZED; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isUnauthorized()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .doesNotHaveJsonPath()); - } - - - @Test - void givenValidAdminRegisterRequest_whenAdminRegistered_thenReturnSuccessResponse() throws Exception { - - // Given - String mockId = "e8de09dc-a44e-40eb-bcc7-cf0141f8733c"; - AdminRegistrationApplicationCompleteRequest mockRequest = new AdminRegistrationApplicationCompleteRequestBuilder() - .withValidValues().build(); - - // When - Mockito.doNothing().when(adminRegistrationCompleteService).complete(Mockito.anyString(), Mockito.any()); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId).concat("/complete"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockRequest); - - AysResponse mockResponse = AysResponseBuilder.success(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()); - - // Verify - Mockito.verify(adminRegistrationCompleteService, Mockito.times(1)) - .complete(Mockito.anyString(), Mockito.any()); - } - - - @Test - void givenIdAndAdminRegisterRequest_whenAdminApplicationNotFound_thenReturnUnauthorizedError() throws Exception { - - // Given - String mockId = "181e8310-6dfd-444c-aa38-056ce8401345"; - AdminRegistrationApplicationCompleteRequest mockRequest = new AdminRegistrationApplicationCompleteRequestBuilder() - .withValidValues().build(); - - // When - Mockito.doThrow(new AysAdminRegistrationApplicationNotExistByIdAuthException(mockId)) - .when(adminRegistrationCompleteService) - .complete(Mockito.anyString(), Mockito.any()); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId).concat("/complete"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.UNAUTHORIZED; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isUnauthorized()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .doesNotHaveJsonPath()); - - // Verify - Mockito.verify(adminRegistrationCompleteService, Mockito.times(1)) - .complete(Mockito.anyString(), Mockito.any()); - } - - @Test - void givenPhoneNumberWithAlphanumericCharacter_whenPhoneNumberIsNotValid_thenReturnValidationError() throws Exception { - - // Given - String mockId = "f1b789d0-6095-4860-85bb-e1a0b20f1d13"; - AysPhoneNumberRequest mockPhoneNumber = new AysPhoneNumberRequestBuilder() - .withCountryCode("ABC") - .withLineNumber("ABC").build(); - AdminRegistrationApplicationCompleteRequest mockRequest = new AdminRegistrationApplicationCompleteRequestBuilder() - .withValidValues() - .withPhoneNumber(mockPhoneNumber).build(); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId).concat("/complete"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(adminRegistrationCompleteService, Mockito.never()) - .complete(Mockito.anyString(), Mockito.any()); - } - - @Test - void givenPhoneNumberWithInvalidLength_whenPhoneNumberIsNotValid_thenReturnValidationError() throws Exception { - - // Given - String mockId = "25930d3f-4cea-4147-a21a-0f22c9bf72de"; - AysPhoneNumberRequest mockPhoneNumber = new AysPhoneNumberRequestBuilder() - .withCountryCode("456786745645") - .withLineNumber("6546467456435548676845321346656654").build(); - AdminRegistrationApplicationCompleteRequest mockRequest = new AdminRegistrationApplicationCompleteRequestBuilder() - .withValidValues() - .withPhoneNumber(mockPhoneNumber).build(); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId).concat("/complete"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(adminRegistrationCompleteService, Mockito.never()) - .complete(Mockito.anyString(), Mockito.any()); - } - - @Test - void givenPhoneNumberWithInvalidOperator_whenPhoneNumberIsNotValid_thenReturnValidationError() throws Exception { - - // Given - String mockId = "2028b456-e06c-4ea1-9017-b45523529576"; - final String invalidOperator = "123"; - AysPhoneNumberRequest mockPhoneNumber = new AysPhoneNumberRequestBuilder() - .withCountryCode("90") - .withLineNumber(invalidOperator + "6327218").build(); - AdminRegistrationApplicationCompleteRequest mockRequest = new AdminRegistrationApplicationCompleteRequestBuilder() - .withValidValues() - .withPhoneNumber(mockPhoneNumber).build(); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId).concat("/complete"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(adminRegistrationCompleteService, Mockito.never()) - .complete(Mockito.anyString(), Mockito.any()); - } - - @ParameterizedTest - @ValueSource(strings = { - "John 1234", - "John *^%$#", - " John", - "? John", - "J", - "J----", - "City--King", - "John Doe" - }) - void givenInvalidAdminRegisterApplicationCompleteRequestWithParametrizedInvalidNames_whenNamesAreNotValid_thenReturnValidationError(String invalidName) throws Exception { - - // Given - String mockId = "f423facc-36fe-4615-a68d-f7f1fe5cd860"; - AdminRegistrationApplicationCompleteRequest mockRequest = new AdminRegistrationApplicationCompleteRequestBuilder() - .withValidValues() - .withFirstName(invalidName) - .withLastName(invalidName) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId).concat("/complete"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(adminRegistrationCompleteService, Mockito.never()) - .complete(Mockito.anyString(), Mockito.any()); - } - - @ParameterizedTest - @ValueSource(strings = { - "abc.def@mail.c", - "abc.def@mail#archive.com", - "abc.def@mail", - "abcdef@mail..com", - "abc-@mail.com", - "admin@test@ays.com", - "admintest@ays..com", - "username@gmail..co.uk", - "user@ example.com", - "user@-example.com", - "user@example-.com", - "(user)@example.com", - "user@[192.168.1.1", - "user@exam ple.com", - "user@.com", - ".user@example.com", - " user@example.com", - "user@example.com ", - " user@example.com ", - "@missingusername.com" - }) - void givenInvalidAdminRegisterApplicationCompleteRequestWithParametrizedInvalidEmails_whenEmailsAreNotValid_thenReturnValidationError(String invalidEmail) throws Exception { - - // Given - String mockId = "53617d24-e32c-4249-b9e6-b10e63a439bd"; - AdminRegistrationApplicationCompleteRequest mockRequest = new AdminRegistrationApplicationCompleteRequestBuilder() - .withValidValues() - .withEmailAddress(invalidEmail) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId).concat("/complete"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(adminRegistrationCompleteService, Mockito.never()) - .complete(Mockito.anyString(), Mockito.any()); - } - - @ParameterizedTest - @ValueSource(strings = { - "abcdef@mail.com", - "abc+def@archive.com", - "john.doe123@example.co.uk", - "admin_123@example.org", - "admin-test@ays.com", - }) - void givenValidAdminRegisterApplicationCompleteRequestWithParametrizedValidEmails_whenEmailsAreValid_thenReturnSuccessResponse(String validEmail) throws Exception { - // Given - String mockId = "fe3760f1-8b44-4587-99a6-43e426c8c6d1"; - AdminRegistrationApplicationCompleteRequest mockRequest = new AdminRegistrationApplicationCompleteRequestBuilder() - .withValidValues() - .withEmailAddress(validEmail) - .build(); - - // When - Mockito.doNothing().when(adminRegistrationCompleteService).complete(Mockito.anyString(), Mockito.any()); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId).concat("/complete"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockRequest); - - AysResponse mockResponse = AysResponseBuilder.success(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()); - - // Verify - Mockito.verify(adminRegistrationCompleteService, Mockito.times(1)) - .complete(Mockito.anyString(), Mockito.any()); - } - - @ParameterizedTest - @ValueSource(strings = { - "g", - "gfh2j" - }) - void givenInvalidAdminRegisterApplicationCompleteRequestWithInvalidPassword_whenPasswordDoesNotValid_thenReturnValidationError(String mockPassword) throws Exception { - - // Given - String mockId = "de2b9621-0bf6-45df-a173-4697797446b7"; - AdminRegistrationApplicationCompleteRequest mockRequest = new AdminRegistrationApplicationCompleteRequestBuilder() - .withValidValues() - .withPassword(mockPassword) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId).concat("/complete"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(adminRegistrationCompleteService, Mockito.never()) - .complete(Mockito.anyString(), Mockito.any()); - } - - @Test - void givenValidAdminRegisterApplicationId_whenApproveAdminRegisterApplication_thenReturnNothing() throws Exception { - // Given - String mockId = "47d91587-612f-462b-975f-ed227e2ee3a7"; - - // When - Mockito.doNothing() - .when(adminRegistrationApplicationService).approve(mockId); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application/".concat(mockId).concat("/approve")); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockSuperAdminToken.getAccessToken()); - - AysResponse mockResponse = AysResponseBuilder.success(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()); - - // Verify - Mockito.verify(adminRegistrationApplicationService, Mockito.times(1)) - .approve(mockId); - } - - @Test - void givenValidAdminRegisterApplicationId_whenUnauthorizedForApprovingAdminRegisterApplication_thenReturnAccessDeniedException() throws Exception { - - // Given - String mockId = "5cbc8774-e2c3-414b-a1d8-26abcf3c9d17"; - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application/".concat(mockId).concat("/approve")); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockUserToken.getAccessToken()); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isForbidden()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .doesNotExist()); - - // Verify - Mockito.verify(adminRegistrationApplicationService, Mockito.never()) - .approve(mockId); - } - - @Test - void givenValidAdminRegisterApplicationRejectRequest_whenRejectingAdminRegisterApplication_thenReturnSuccess() throws Exception { - - // Given - String mockId = "c5c504a6-1223-483b-a8ba-6fb4ea309e00"; - AdminRegistrationApplicationRejectRequest mockRequest = new AdminRegistrationApplicationRejectRequestBuilder() - .withValidValues() - .build(); - - // When - Mockito.doNothing().when(adminRegistrationApplicationService).reject(mockId, mockRequest); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId).concat("/reject"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockSuperAdminToken.getAccessToken(), mockRequest); - - AysResponse mockResponse = AysResponseBuilder.success(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()); - - // Verify - Mockito.verify(adminRegistrationApplicationService, Mockito.times(1)) - .reject(Mockito.eq(mockId), Mockito.any(AdminRegistrationApplicationRejectRequest.class)); - - } - - @Test - void givenValidAdminRegisterApplicationRejectRequest_whenUnauthorizedForRejectingAdminRegisterApplication_thenReturnAccessDeniedException() throws Exception { - - // Given - String mockId = "c27aa25e-7e2f-4bc1-8056-eb9c2948f507"; - AdminRegistrationApplicationRejectRequest mockRequest = new AdminRegistrationApplicationRejectRequestBuilder() - .withValidValues() - .build(); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId).concat("/reject"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockUserToken.getAccessToken(), mockRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isForbidden()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .doesNotExist()); - - // Verify - Mockito.verify(adminRegistrationApplicationService, Mockito.never()) - .reject(Mockito.eq(mockId), Mockito.any(AdminRegistrationApplicationRejectRequest.class)); - } - - @ParameterizedTest - @ValueSource(strings = { - "", - " ", - "less than 40", - """ - Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. - Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. - Donec qudam felis, ultricies nec, pellentesque eu, pretscsxwium quis, sem. Nulla consequat massa quis - enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, - imperdiet a, venenatdskjvndshjcndsis vitae, justo. Nullam dictum felis eu pedde mollis pretium. Integer tincidunt. - Cras dapibus. Vivdamus ewl - """, - " spaceAtTheBeginning", - "spaceAtTheEnd ", - " both ", - " justAString " - }) - void givenInvalidAdminRegisterApplicationRejectRequest_whenRejectingAdminRegisterApplication_thenReturnValidationError(String rejectReason) throws Exception { - - // Given - String mockId = "4d04bd1e-6318-43ba-ab40-57efb8afc918"; - AdminRegistrationApplicationRejectRequest mockRequest = new AdminRegistrationApplicationRejectRequestBuilder() - .withValidValues() - .withRejectReason(rejectReason) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId).concat("/reject"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockUserToken.getAccessToken(), mockRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(adminRegistrationApplicationService, Mockito.never()) - .reject(Mockito.anyString(), - Mockito.any(AdminRegistrationApplicationRejectRequest.class)); - } + @MockBean + private AdminRegistrationApplicationService adminRegistrationApplicationService; + + @MockBean + private AdminRegistrationCompleteService adminRegistrationCompleteService; + + private final AdminRegistrationApplicationToApplicationsResponseMapper adminRegistrationApplicationToApplicationsResponseMapper = AdminRegistrationApplicationToApplicationsResponseMapper + .initialize(); + private final AdminRegistrationApplicationToApplicationResponseMapper adminRegistrationApplicationToApplicationResponseMapper = AdminRegistrationApplicationToApplicationResponseMapper + .initialize(); + private final AdminRegistrationApplicationToSummaryResponseMapper adminRegistrationApplicationToSummaryResponseMapper = AdminRegistrationApplicationToSummaryResponseMapper + .initialize(); + private final AdminRegistrationApplicationToCreateResponseMapper adminRegistrationApplicationToCreateResponseMapper = AdminRegistrationApplicationToCreateResponseMapper + .initialize(); + + private static final String BASE_PATH = "/api/v1"; + + @Test + void givenValidAdminRegisterApplicationListRequest_whenAdminRegisterApplicationsFound_thenReturnAysPageResponseOfAdminRegisterApplicationsResponse() + throws Exception { + + // Given + AdminRegistrationApplicationListRequest mockListRequest = new AdminRegistrationApplicationListRequestBuilder() + .withValidValues() + .build(); + + // When + List mockApplications = List.of( + new AdminRegistrationApplicationBuilder().withValidValues().build()); + + AysPage mockApplicationPage = AysPageBuilder + .from(mockApplications, mockListRequest.getPageable()); + + Mockito.when(adminRegistrationApplicationService + .findAll(Mockito.any(AdminRegistrationApplicationListRequest.class))) + .thenReturn(mockApplicationPage); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-applications"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockSuperAdminToken.getAccessToken(), mockListRequest); + + List mockApplicationsResponse = adminRegistrationApplicationToSummaryResponseMapper + .map(mockApplications); + AysPageResponse pageOfResponse = AysPageResponse + .builder() + .of(mockApplicationPage) + .content(mockApplicationsResponse) + .build(); + AysResponse> mockResponse = AysResponse + .successOf(pageOfResponse); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .isNotEmpty()); + + // Verify + Mockito.verify(adminRegistrationApplicationService, Mockito.times(1)) + .findAll(Mockito.any(AdminRegistrationApplicationListRequest.class)); + } + + @Test + void givenValidAdminRegisterApplicationListRequest_whenUnauthorizedForListing_thenReturnAccessDeniedException() + throws Exception { + // Given + AdminRegistrationApplicationListRequest mockListRequest = new AdminRegistrationApplicationListRequestBuilder() + .withValidValues() + .build(); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-applications"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockUserToken.getAccessToken(), mockListRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isForbidden()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .doesNotExist()); + + // Verify + Mockito.verify(adminRegistrationApplicationService, Mockito.never()) + .findAll(Mockito.any(AdminRegistrationApplicationListRequest.class)); + } + + @Test + void givenValidAdminRegisterApplicationId_whenAdminRegisterApplicationFound_thenReturnAdminRegisterApplicationResponse() + throws Exception { + + // Given + String mockApplicationId = "a0c2351d-54ce-4019-8ffe-a2f8a2700824"; + + // When + AdminRegistrationApplication mockRegisterApplication = new AdminRegistrationApplicationBuilder() + .withId(mockApplicationId) + .build(); + Mockito.when(adminRegistrationApplicationService.findById(mockApplicationId)) + .thenReturn(mockRegisterApplication); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockApplicationId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .get(endpoint, mockSuperAdminToken.getAccessToken()); + + AdminRegistrationApplicationResponse mockApplicationResponse = adminRegistrationApplicationToApplicationResponseMapper + .map(mockRegisterApplication); + AysResponse mockResponse = AysResponse + .successOf(mockApplicationResponse); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .isNotEmpty()); + + // Verify + Mockito.verify(adminRegistrationApplicationService, Mockito.times(1)) + .findById(mockApplicationId); + + } + + @Test + void givenValidAdminRegisterApplicationId_whenUnauthorizedForGettingAdminRegisterApplicationById_thenReturnAccessDeniedException() + throws Exception { + + // Given + String mockApplicationId = "68c867b4-e84a-405c-b1ab-f8dcaa9c41f2"; + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockApplicationId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .get(endpoint, mockUserToken.getAccessToken()); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isForbidden()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .doesNotExist()); + + // Verify + Mockito.verify(adminRegistrationApplicationService, Mockito.never()) + .findById(mockApplicationId); + } + + @ParameterizedTest + @ValueSource(strings = { + "This is a valid text with sufficient length and contains alphabetic characters.", + "This text includes numbers 12345 and still should be accepted because it's within limits.", + "This text, which includes punctuation marks, should be accepted.", + "ÇalıŞkan ve dÜrüst İnsanlar her zaman başarıyı yakalar.(:;?/)" + }) + void givenValidAdminRegisterApplicationCreateRequest_whenCreatingAdminRegisterApplication_thenReturnAdminRegisterApplicationCreateResponse( + String reason) throws Exception { + + // Given + AdminRegistrationApplicationCreateRequest mockRequest = new AdminRegistrationApplicationCreateRequestBuilder() + .withValidValues() + .withInstitutionId(AysValidTestData.SuperAdmin.INSTITUTION_ID) + .withReason(reason) + .build(); + + // When + Institution mockInstitution = new InstitutionBuilder() + .withId(mockRequest.getInstitutionId()) + .build(); + AdminRegistrationApplication mockRegisterApplication = new AdminRegistrationApplicationBuilder() + .withValidValues() + .withInstitution(mockInstitution) + .withReason(mockRequest.getReason()) + .withStatus(AdminRegistrationApplicationStatus.WAITING) + .build(); + + Mockito.when(adminRegistrationApplicationService + .create(Mockito.any(AdminRegistrationApplicationCreateRequest.class))) + .thenReturn(mockRegisterApplication); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockSuperAdminToken.getAccessToken(), mockRequest); + + AdminRegistrationApplicationCreateResponse mockApplicationCreateResponse = adminRegistrationApplicationToCreateResponseMapper + .map(mockRegisterApplication); + AysResponse mockResponse = AysResponse + .successOf(mockApplicationCreateResponse); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .isNotEmpty()); + + // Verify + Mockito.verify(adminRegistrationApplicationService, Mockito.times(1)) + .create(Mockito.any(AdminRegistrationApplicationCreateRequest.class)); + } + + @ParameterizedTest + @ValueSource(strings = { + "Invalid reason with special characters: #$%", + "#$½#$£#$£#$$#½#£$£#$#£½#$½#$½$£#$#£$$#½#$$½", + ".,..,.,.,.,.,,.,.,.,.,.,.,..,.,.,,.,.,.,", + "Too short", + " a", + " spaceAtTheBeginning", + "spaceAtTheEnd ", + " both ", + " justAString ", + "151201485621548562154851458614125461254125412", + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean vestibulum commodo turpis, sed venenatis sapien suscipit sit amet. Etiam malesuada, ligula in semper varius, nisi mi pulvinar libero, ut commodo dolor orci quis urna. Vivamus ac euismod ex. Proin vel vulputate orci. Ut id nibh finibus, mattis sem id, maximus ante. Proin fringilla ipsum at arcu venenatis, non bibendum justo luctus. Phasellus vestibulum feugiat est sit amet bibendum. Donec nulla leo, ultricies sed pharetra sed, hendrerit vel nunc." + }) + void givenInvalidAdminRegisterApplicationCreateRequest_whenCreatingAdminRegisterApplication_thenReturnValidationError( + String invalidReason) throws Exception { + + // Given + AdminRegistrationApplicationCreateRequest createRequest = new AdminRegistrationApplicationCreateRequestBuilder() + .withValidValues() + .withReason(invalidReason) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockUserToken.getAccessToken(), createRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(adminRegistrationApplicationService, Mockito.never()) + .create(Mockito.any(AdminRegistrationApplicationCreateRequest.class)); + } + + @Test + void givenValidAdminRegisterApplicationCreateRequest_whenUnauthorizedForCreatingAdminRegisterApplication_thenReturnAccessDeniedException() + throws Exception { + + // Given + AdminRegistrationApplicationCreateRequest mockRequest = new AdminRegistrationApplicationCreateRequestBuilder() + .withValidValues() + .withInstitutionId(AysValidTestData.SuperAdmin.INSTITUTION_ID) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockUserToken.getAccessToken(), mockRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isForbidden()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .doesNotExist()); + + // Verify + Mockito.verify(adminRegistrationApplicationService, Mockito.never()) + .create(Mockito.any(AdminRegistrationApplicationCreateRequest.class)); + } + + @Test + void givenValidAdminRegisterApplicationId_whenAdminApplicationFound_thenReturnAdminApplicationSummaryResponse() + throws Exception { + + // Given + String mockId = "085fbe72-caa7-439d-8db1-166ed005e120"; + AdminRegistrationApplication mockAdminRegistrationApplication = new AdminRegistrationApplicationBuilder() + .withId(mockId) + .build(); + + // When + Mockito.when(adminRegistrationApplicationService.findSummaryById(mockId)) + .thenReturn(mockAdminRegistrationApplication); + + // Then + String endpoint = BASE_PATH + .concat("/admin-registration-application/".concat(mockId).concat("/summary")); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .get(endpoint); + + AdminRegistrationApplicationSummaryResponse mockSummaryResponse = adminRegistrationApplicationToSummaryResponseMapper + .map(mockAdminRegistrationApplication); + AysResponse mockResponse = AysResponse + .successOf(mockSummaryResponse); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .isNotEmpty()); + } + + @Test + void givenIdAndAdminRegisterApplication_whenAdminApplicationNotFound_thenReturnUnauthorizedError() + throws Exception { + + // Given + String mockId = "181e8310-6dfd-444c-aa38-056ce8401345"; + + // When + Mockito.when(adminRegistrationApplicationService.findSummaryById(mockId)) + .thenThrow(new AysAdminRegistrationApplicationNotExistByIdAuthException(mockId)); + + // Then + String endpoint = BASE_PATH + .concat("/admin-registration-application/".concat(mockId).concat("/summary")); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .get(endpoint); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.UNAUTHORIZED; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isUnauthorized()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .doesNotHaveJsonPath()); + } + + @Test + void givenValidAdminRegisterRequest_whenAdminRegistered_thenReturnSuccessResponse() throws Exception { + + // Given + String mockId = "e8de09dc-a44e-40eb-bcc7-cf0141f8733c"; + AdminRegistrationApplicationCompleteRequest mockRequest = new AdminRegistrationApplicationCompleteRequestBuilder() + .withValidValues().build(); + + // When + Mockito.doNothing().when(adminRegistrationCompleteService).complete(Mockito.anyString(), Mockito.any()); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId) + .concat("/complete"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockRequest); + + AysResponse mockResponse = AysResponseBuilder.success(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Mockito.verify(adminRegistrationCompleteService, Mockito.times(1)) + .complete(Mockito.anyString(), Mockito.any()); + } + + @Test + void givenIdAndAdminRegisterRequest_whenAdminApplicationNotFound_thenReturnUnauthorizedError() + throws Exception { + + // Given + String mockId = "181e8310-6dfd-444c-aa38-056ce8401345"; + AdminRegistrationApplicationCompleteRequest mockRequest = new AdminRegistrationApplicationCompleteRequestBuilder() + .withValidValues().build(); + + // When + Mockito.doThrow(new AysAdminRegistrationApplicationNotExistByIdAuthException(mockId)) + .when(adminRegistrationCompleteService) + .complete(Mockito.anyString(), Mockito.any()); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId) + .concat("/complete"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.UNAUTHORIZED; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isUnauthorized()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .doesNotHaveJsonPath()); + + // Verify + Mockito.verify(adminRegistrationCompleteService, Mockito.times(1)) + .complete(Mockito.anyString(), Mockito.any()); + } + + @Test + void givenPhoneNumberWithAlphanumericCharacter_whenPhoneNumberIsNotValid_thenReturnValidationError() + throws Exception { + + // Given + String mockId = "f1b789d0-6095-4860-85bb-e1a0b20f1d13"; + AysPhoneNumberRequest mockPhoneNumber = new AysPhoneNumberRequestBuilder() + .withCountryCode("ABC") + .withLineNumber("ABC").build(); + AdminRegistrationApplicationCompleteRequest mockRequest = new AdminRegistrationApplicationCompleteRequestBuilder() + .withValidValues() + .withPhoneNumber(mockPhoneNumber).build(); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId) + .concat("/complete"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(adminRegistrationCompleteService, Mockito.never()) + .complete(Mockito.anyString(), Mockito.any()); + } + + @Test + void givenPhoneNumberWithInvalidLength_whenPhoneNumberIsNotValid_thenReturnValidationError() throws Exception { + + // Given + String mockId = "25930d3f-4cea-4147-a21a-0f22c9bf72de"; + AysPhoneNumberRequest mockPhoneNumber = new AysPhoneNumberRequestBuilder() + .withCountryCode("456786745645") + .withLineNumber("6546467456435548676845321346656654").build(); + AdminRegistrationApplicationCompleteRequest mockRequest = new AdminRegistrationApplicationCompleteRequestBuilder() + .withValidValues() + .withPhoneNumber(mockPhoneNumber).build(); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId) + .concat("/complete"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(adminRegistrationCompleteService, Mockito.never()) + .complete(Mockito.anyString(), Mockito.any()); + } + + @Test + void givenPhoneNumberWithInvalidOperator_whenPhoneNumberIsNotValid_thenReturnValidationError() + throws Exception { + + // Given + String mockId = "2028b456-e06c-4ea1-9017-b45523529576"; + final String invalidOperator = "123"; + AysPhoneNumberRequest mockPhoneNumber = new AysPhoneNumberRequestBuilder() + .withCountryCode("90") + .withLineNumber(invalidOperator + "6327218").build(); + AdminRegistrationApplicationCompleteRequest mockRequest = new AdminRegistrationApplicationCompleteRequestBuilder() + .withValidValues() + .withPhoneNumber(mockPhoneNumber).build(); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId) + .concat("/complete"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(adminRegistrationCompleteService, Mockito.never()) + .complete(Mockito.anyString(), Mockito.any()); + } + + @ParameterizedTest + @ValueSource(strings = { + "John 1234", + "John *^%$#", + " John", + "? John", + "J", + "J----", + "City--King", + "John Doe" + }) + void givenInvalidAdminRegisterApplicationCompleteRequestWithParametrizedInvalidNames_whenNamesAreNotValid_thenReturnValidationError( + String invalidName) throws Exception { + + // Given + String mockId = "f423facc-36fe-4615-a68d-f7f1fe5cd860"; + AdminRegistrationApplicationCompleteRequest mockRequest = new AdminRegistrationApplicationCompleteRequestBuilder() + .withValidValues() + .withFirstName(invalidName) + .withLastName(invalidName) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId) + .concat("/complete"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(adminRegistrationCompleteService, Mockito.never()) + .complete(Mockito.anyString(), Mockito.any()); + } + + @ParameterizedTest + @ValueSource(strings = { + "abc.def@mail.c", + "abc.def@mail#archive.com", + "abc.def@mail", + "abcdef@mail..com", + "abc-@mail.com", + "admin@test@ays.com", + "admintest@ays..com", + "username@gmail..co.uk", + "user@ example.com", + "user@-example.com", + "user@example-.com", + "(user)@example.com", + "user@[192.168.1.1", + "user@exam ple.com", + "user@.com", + ".user@example.com", + " user@example.com", + "user@example.com ", + " user@example.com ", + "@missingusername.com", + "a@b.c", // 5 karakter - minimum limit altında + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@domain.com" // 255 + // karakter + // - + // maksimum + // limit + // üstünde + }) + void givenInvalidAdminRegisterApplicationCompleteRequestWithParametrizedInvalidEmails_whenEmailsAreNotValid_thenReturnValidationError( + String invalidEmail) throws Exception { + + // Given + String mockId = "53617d24-e32c-4249-b9e6-b10e63a439bd"; + AdminRegistrationApplicationCompleteRequest mockRequest = new AdminRegistrationApplicationCompleteRequestBuilder() + .withValidValues() + .withEmailAddress(invalidEmail) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId) + .concat("/complete"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(adminRegistrationCompleteService, Mockito.never()) + .complete(Mockito.anyString(), Mockito.any()); + } + + @ParameterizedTest + @ValueSource(strings = { + "user@example.com", // 14 karakter - geçerli + "abcdef@mail.com", // 14 karakter - geçerli + "a.b.c.d@test.com", // 15 karakter - geçerli + "test.123@afetyonetimsistemi.org" // 29 karakter - geçerli + }) + void givenValidAdminRegisterApplicationCompleteRequestWithParametrizedValidEmails_whenEmailsAreValid_thenReturnSuccessResponse( + String validEmail) throws Exception { + // Given + String mockId = "fe3760f1-8b44-4587-99a6-43e426c8c6d1"; + AdminRegistrationApplicationCompleteRequest mockRequest = new AdminRegistrationApplicationCompleteRequestBuilder() + .withValidValues() + .withEmailAddress(validEmail) + .build(); + + // When + Mockito.doNothing().when(adminRegistrationCompleteService).complete(Mockito.anyString(), Mockito.any()); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId) + .concat("/complete"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockRequest); + + AysResponse mockResponse = AysResponseBuilder.success(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Mockito.verify(adminRegistrationCompleteService, Mockito.times(1)) + .complete(Mockito.anyString(), Mockito.any()); + } + + @ParameterizedTest + @ValueSource(strings = { + "g", + "gfh2j" + }) + void givenInvalidAdminRegisterApplicationCompleteRequestWithInvalidPassword_whenPasswordDoesNotValid_thenReturnValidationError( + String mockPassword) throws Exception { + + // Given + String mockId = "de2b9621-0bf6-45df-a173-4697797446b7"; + AdminRegistrationApplicationCompleteRequest mockRequest = new AdminRegistrationApplicationCompleteRequestBuilder() + .withValidValues() + .withPassword(mockPassword) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId) + .concat("/complete"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(adminRegistrationCompleteService, Mockito.never()) + .complete(Mockito.anyString(), Mockito.any()); + } + + @Test + void givenValidAdminRegisterApplicationId_whenApproveAdminRegisterApplication_thenReturnNothing() + throws Exception { + // Given + String mockId = "47d91587-612f-462b-975f-ed227e2ee3a7"; + + // When + Mockito.doNothing() + .when(adminRegistrationApplicationService).approve(mockId); + + // Then + String endpoint = BASE_PATH + .concat("/admin-registration-application/".concat(mockId).concat("/approve")); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockSuperAdminToken.getAccessToken()); + + AysResponse mockResponse = AysResponseBuilder.success(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Mockito.verify(adminRegistrationApplicationService, Mockito.times(1)) + .approve(mockId); + } + + @Test + void givenValidAdminRegisterApplicationId_whenUnauthorizedForApprovingAdminRegisterApplication_thenReturnAccessDeniedException() + throws Exception { + + // Given + String mockId = "5cbc8774-e2c3-414b-a1d8-26abcf3c9d17"; + + // Then + String endpoint = BASE_PATH + .concat("/admin-registration-application/".concat(mockId).concat("/approve")); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockUserToken.getAccessToken()); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isForbidden()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .doesNotExist()); + + // Verify + Mockito.verify(adminRegistrationApplicationService, Mockito.never()) + .approve(mockId); + } + + @Test + void givenValidAdminRegisterApplicationRejectRequest_whenRejectingAdminRegisterApplication_thenReturnSuccess() + throws Exception { + + // Given + String mockId = "c5c504a6-1223-483b-a8ba-6fb4ea309e00"; + AdminRegistrationApplicationRejectRequest mockRequest = new AdminRegistrationApplicationRejectRequestBuilder() + .withValidValues() + .build(); + + // When + Mockito.doNothing().when(adminRegistrationApplicationService).reject(mockId, mockRequest); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId).concat("/reject"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockSuperAdminToken.getAccessToken(), mockRequest); + + AysResponse mockResponse = AysResponseBuilder.success(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Mockito.verify(adminRegistrationApplicationService, Mockito.times(1)) + .reject(Mockito.eq(mockId), + Mockito.any(AdminRegistrationApplicationRejectRequest.class)); + + } + + @Test + void givenValidAdminRegisterApplicationRejectRequest_whenUnauthorizedForRejectingAdminRegisterApplication_thenReturnAccessDeniedException() + throws Exception { + + // Given + String mockId = "c27aa25e-7e2f-4bc1-8056-eb9c2948f507"; + AdminRegistrationApplicationRejectRequest mockRequest = new AdminRegistrationApplicationRejectRequestBuilder() + .withValidValues() + .build(); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId).concat("/reject"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockUserToken.getAccessToken(), mockRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isForbidden()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .doesNotExist()); + + // Verify + Mockito.verify(adminRegistrationApplicationService, Mockito.never()) + .reject(Mockito.eq(mockId), + Mockito.any(AdminRegistrationApplicationRejectRequest.class)); + } + + @ParameterizedTest + @ValueSource(strings = { + "", + " ", + "less than 40", + """ + Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. + Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. + Donec qudam felis, ultricies nec, pellentesque eu, pretscsxwium quis, sem. Nulla consequat massa quis + enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, + imperdiet a, venenatdskjvndshjcndsis vitae, justo. Nullam dictum felis eu pedde mollis pretium. Integer tincidunt. + Cras dapibus. Vivdamus ewl + """, + " spaceAtTheBeginning", + "spaceAtTheEnd ", + " both ", + " justAString " + }) + void givenInvalidAdminRegisterApplicationRejectRequest_whenRejectingAdminRegisterApplication_thenReturnValidationError( + String rejectReason) throws Exception { + + // Given + String mockId = "4d04bd1e-6318-43ba-ab40-57efb8afc918"; + AdminRegistrationApplicationRejectRequest mockRequest = new AdminRegistrationApplicationRejectRequestBuilder() + .withValidValues() + .withRejectReason(rejectReason) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId).concat("/reject"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockUserToken.getAccessToken(), mockRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(adminRegistrationApplicationService, Mockito.never()) + .reject(Mockito.anyString(), + Mockito.any(AdminRegistrationApplicationRejectRequest.class)); + } } diff --git a/src/test/java/org/ays/auth/controller/AysAuthControllerTest.java b/src/test/java/org/ays/auth/controller/AysAuthControllerTest.java index 3e27f6808..71e9f7653 100644 --- a/src/test/java/org/ays/auth/controller/AysAuthControllerTest.java +++ b/src/test/java/org/ays/auth/controller/AysAuthControllerTest.java @@ -33,457 +33,501 @@ class AysAuthControllerTest extends AysRestControllerTest { - - @MockBean - private AysAuthService authService; - - @MockBean - private AysUserPasswordService userPasswordService; - - - private final AysTokenToResponseMapper tokenToResponseMapper = AysTokenToResponseMapper.initialize(); - - - private static final String BASE_PATH = "/api/v1/authentication"; - - - @ParameterizedTest - @ValueSource(strings = { - "abcdef@mail.com", - "abc+def@archive.com", - "john.doe123@example.co.uk", - "admin_123@example.org", - "admin-test@ays.com", - }) - void givenValidLoginRequestWithValidEmailAddress_whenTokensGeneratedSuccessfully_thenReturnTokenResponse(String mockEmailAddress) throws Exception { - // Given - AysLoginRequest mockLoginRequest = new AysLoginRequestBuilder() - .withEmailAddress(mockEmailAddress) - .withPassword(AysValidTestData.PASSWORD) - .withSourcePage(AysSourcePage.INSTITUTION) - .build(); - - // When - Mockito.when(authService.authenticate(Mockito.any())) - .thenReturn(mockUserToken); - - // Then - String endpoint = BASE_PATH.concat("/token"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockLoginRequest); - - AysTokenResponse mockTokenResponse = tokenToResponseMapper.map(mockUserToken); - AysResponse mockResponse = AysResponseBuilder.successOf(mockTokenResponse); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .isNotEmpty()) - .andExpect(MockMvcResultMatchers.jsonPath("$.response.accessToken") - .value(mockResponse.getResponse().getAccessToken())) - .andExpect(MockMvcResultMatchers.jsonPath("$.response.accessTokenExpiresAt") - .doesNotHaveJsonPath()) - .andExpect(MockMvcResultMatchers.jsonPath("$.response.refreshToken") - .value(mockResponse.getResponse().getRefreshToken())); - - // Verify - Mockito.verify(authService, Mockito.times(1)) - .authenticate(Mockito.any()); - } - - @ParameterizedTest - @ValueSource(strings = { - "g", - "gfh2j" - }) - void givenInvalidLoginRequestWithInvalidPassword_whenPasswordDoesNotValid_thenReturnValidationError(String mockPassword) throws Exception { - // Given - AysLoginRequest mockLoginRequest = new AysLoginRequestBuilder() - .withEmailAddress(AysValidTestData.User.EMAIL_ADDRESS) - .withPassword(mockPassword) - .withSourcePage(AysSourcePage.INSTITUTION) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/token"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockLoginRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(authService, Mockito.never()) - .authenticate(Mockito.any()); - } - - @ParameterizedTest - @ValueSource(strings = { - "abc.def@mail.c", - "abc.def@mail#archive.com", - "abc.def@mail", - "abcdef@mail..com", - "abc-@mail.com", - "admin@test@ays.com", - "admintest@ays..com", - "username@gmail..co.uk", - "user@ example.com", - "user@-example.com", - "user@example-.com", - "(user)@example.com", - "user@[192.168.1.1", - "user@exam ple.com", - "user@.com", - ".user@example.com", - " user@example.com", - "user@example.com ", - " user@example.com ", - "@missingusername.com" - }) - void givenInvalidLoginRequestWithInvalidEmailAddress_whenEmailsAreNotValid_thenReturnValidationError(String mockEmailAddress) throws Exception { - // Given - AysLoginRequest mockLoginRequest = new AysLoginRequestBuilder() - .withEmailAddress(mockEmailAddress) - .withPassword(AysValidTestData.PASSWORD) - .withSourcePage(AysSourcePage.INSTITUTION) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/token"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockLoginRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(authService, Mockito.never()) - .authenticate(Mockito.any()); - } - - @Test - void givenValidTokenRefreshRequest_whenAccessTokenGeneratedSuccessfully_thenReturnTokenResponse() throws Exception { - // Given - AysTokenRefreshRequest mockTokenRefreshRequest = AysTokenRefreshRequestBuilder.VALID_FOR_USER; - - // When - Mockito.when(authService.refreshAccessToken(Mockito.anyString())) - .thenReturn(mockUserToken); - - // Then - String endpoint = BASE_PATH.concat("/token/refresh"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockTokenRefreshRequest); - - AysTokenResponse mockTokenResponse = tokenToResponseMapper.map(mockUserToken); - AysResponse mockResponse = AysResponseBuilder.successOf(mockTokenResponse); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .isNotEmpty()) - .andExpect(MockMvcResultMatchers.jsonPath("$.response.accessToken") - .value(mockResponse.getResponse().getAccessToken())) - .andExpect(MockMvcResultMatchers.jsonPath("$.response.accessTokenExpiresAt") - .doesNotHaveJsonPath()) - .andExpect(MockMvcResultMatchers.jsonPath("$.response.refreshToken") - .value(mockResponse.getResponse().getRefreshToken())); - - // Verify - Mockito.verify(authService, Mockito.times(1)) - .refreshAccessToken(Mockito.anyString()); - } - - @Test - void givenValidAysTokenInvalidateRequest_whenTokensInvalidated_thenReturnSuccessResponse() throws Exception { - // Given - AysTokenInvalidateRequest mockTokenInvalidateRequest = AysTokenInvalidateRequestBuilder.VALID_FOR_USER; - - // When - Mockito.doNothing() - .when(authService) - .invalidateTokens(Mockito.any()); - - // Then - String endpoint = BASE_PATH.concat("/token/invalidate"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockUserToken.getAccessToken(), mockTokenInvalidateRequest); - - AysResponse mockResponse = AysResponseBuilder.success(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()); - - // Verify - Mockito.verify(authService, Mockito.times(1)) - .invalidateTokens(Mockito.any()); - } - - - @Test - void givenValidForgotPasswordRequest_whenSendPasswordCreateMail_thenReturnSuccessResponse() throws Exception { - // Given - AysPasswordForgotRequest mockForgotPasswordRequest = new AysForgotPasswordRequestBuilder() - .withEmailAddress(AysValidTestData.Admin.EMAIL_ADDRESS) - .build(); - - // When - Mockito.doNothing() - .when(userPasswordService) - .forgotPassword(Mockito.any(AysPasswordForgotRequest.class)); - - // Then - String endpoint = BASE_PATH.concat("/password/forgot"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockForgotPasswordRequest); - - AysResponse mockResponse = AysResponseBuilder.success(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()); - - // Verify - Mockito.verify(userPasswordService, Mockito.times(1)) - .forgotPassword(Mockito.any(AysPasswordForgotRequest.class)); - } - - @ParameterizedTest - @ValueSource(strings = { - "abc.def@mail.c", - "abc.def@mail#archive.com", - "abc.def@mail", - "abcdef@mail..com", - "abc-@mail.com", - "admin@test@ays.com", - "admintest@ays..com", - "username@gmail..co.uk", - "user@ example.com", - "user@-example.com", - "user@example-.com", - "(user)@example.com", - "user@[192.168.1.1", - "user@exam ple.com", - "user@.com", - ".user@example.com", - " user@example.com", - "user@example.com ", - " user@example.com ", - "@missingusername.com" - }) - void givenForgotPasswordRequestWithInvalidEmailAddress_whenEmailDoesNotValid_thenReturnValidationError(String mockEmailAddress) throws Exception { - - // Given - AysPasswordForgotRequest mockForgotPasswordRequest = new AysForgotPasswordRequestBuilder() - .withEmailAddress(mockEmailAddress) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/password/forgot"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockForgotPasswordRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userPasswordService, Mockito.never()) - .forgotPassword(Mockito.any(AysPasswordForgotRequest.class)); - } - - @Test - void givenForgotPasswordRequestWithoutEmailAddress_whenEmailIsNull_thenReturnValidationError() throws Exception { - - // Given - AysPasswordForgotRequest mockForgotPasswordRequest = new AysForgotPasswordRequestBuilder() - .withEmailAddress(null) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/password/forgot"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockForgotPasswordRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userPasswordService, Mockito.never()) - .forgotPassword(Mockito.any(AysPasswordForgotRequest.class)); - } - - - @Test - void givenValidId_whenCheckPasswordIdSuccessfully_thenReturnSuccessResponse() throws Exception { - // Given - String mockId = "40fb7a46-40bd-46cb-b44f-1f47162133b1"; - - // When - Mockito.doNothing() - .when(userPasswordService) - .checkPasswordChangingValidity(Mockito.anyString()); - - // Then - String endpoint = BASE_PATH.concat("/password/").concat(mockId).concat("/validity"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .get(endpoint); - - AysResponse mockResponse = AysResponseBuilder.success(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()); - - // Verify - Mockito.verify(userPasswordService, Mockito.times(1)) - .checkPasswordChangingValidity(Mockito.anyString()); - } - - @ParameterizedTest - @ValueSource(strings = { - "A", - "493268349068342" - }) - void givenId_whenIdDoesNotValid_thenReturnValidationError(String invalidId) throws Exception { - - // Then - String endpoint = BASE_PATH.concat("/password/").concat(invalidId).concat("/validity"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .get(endpoint); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userPasswordService, Mockito.never()) - .checkPasswordChangingValidity(Mockito.anyString()); - } - - - @Test - void givenValidPasswordCreateRequest_whenPasswordCreated_thenReturnSuccessResponse() throws Exception { - // Given - String mockId = "1fa43c75-7a7a-4041-8cef-03be8429dd30"; - AysPasswordCreateRequest mockPasswordCreateRequest = new AysPasswordCreateRequestBuilder() - .withValidValues() - .build(); - - // When - Mockito.doNothing() - .when(userPasswordService) - .createPassword(Mockito.anyString(), Mockito.any(AysPasswordCreateRequest.class)); - - // Then - String endpoint = BASE_PATH.concat("/password/").concat(mockId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockPasswordCreateRequest); - - AysResponse mockResponse = AysResponseBuilder.success(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()); - - // Verify - Mockito.verify(userPasswordService, Mockito.times(1)) - .createPassword(Mockito.anyString(), Mockito.any(AysPasswordCreateRequest.class)); - } - - @Test - void givenPasswordCreateRequest_whenPasswordsNotEqual_thenReturnValidationError() throws Exception { - - // Given - String mockId = "1fa43c75-7a7a-4041-8cef-03be8429dd30"; - AysPasswordCreateRequest mockPasswordCreateRequest = new AysPasswordCreateRequestBuilder() - .withPassword("password") - .withPasswordRepeat("password1") - .build(); - - // Then - String endpoint = BASE_PATH.concat("/password/").concat(mockId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockPasswordCreateRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userPasswordService, Mockito.never()) - .createPassword(Mockito.anyString(), Mockito.any(AysPasswordCreateRequest.class)); - } - - - @ParameterizedTest - @ValueSource(strings = { - "g", - "gfh2j" - }) - void givenPasswordCreateRequest_whenPasswordDoesNotValid_thenReturnValidationError(String mockPassword) throws Exception { - - // Given - String mockId = "1fa43c75-7a7a-4041-8cef-03be8429dd30"; - AysPasswordCreateRequest mockPasswordCreateRequest = new AysPasswordCreateRequestBuilder() - .withPassword(mockPassword) - .withPasswordRepeat(mockPassword) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/password/").concat(mockId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockPasswordCreateRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userPasswordService, Mockito.never()) - .createPassword(Mockito.anyString(), Mockito.any(AysPasswordCreateRequest.class)); - } + @MockBean + private AysAuthService authService; + + @MockBean + private AysUserPasswordService userPasswordService; + + private final AysTokenToResponseMapper tokenToResponseMapper = AysTokenToResponseMapper.initialize(); + + private static final String BASE_PATH = "/api/v1/authentication"; + + @ParameterizedTest + @ValueSource(strings = { + "abcdef@mail.com", + "abc+def@archive.com", + "john.doe123@example.co.uk", + "admin_123@example.org", + "admin-test@ays.com", + }) + void givenValidLoginRequestWithValidEmailAddress_whenTokensGeneratedSuccessfully_thenReturnTokenResponse( + String mockEmailAddress) throws Exception { + // Given + AysLoginRequest mockLoginRequest = new AysLoginRequestBuilder() + .withEmailAddress(mockEmailAddress) + .withPassword(AysValidTestData.PASSWORD) + .withSourcePage(AysSourcePage.INSTITUTION) + .build(); + + // When + Mockito.when(authService.authenticate(Mockito.any())) + .thenReturn(mockUserToken); + + // Then + String endpoint = BASE_PATH.concat("/token"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockLoginRequest); + + AysTokenResponse mockTokenResponse = tokenToResponseMapper.map(mockUserToken); + AysResponse mockResponse = AysResponseBuilder.successOf(mockTokenResponse); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .isNotEmpty()) + .andExpect(MockMvcResultMatchers.jsonPath("$.response.accessToken") + .value(mockResponse.getResponse().getAccessToken())) + .andExpect(MockMvcResultMatchers.jsonPath("$.response.accessTokenExpiresAt") + .doesNotHaveJsonPath()) + .andExpect(MockMvcResultMatchers.jsonPath("$.response.refreshToken") + .value(mockResponse.getResponse().getRefreshToken())); + + // Verify + Mockito.verify(authService, Mockito.times(1)) + .authenticate(Mockito.any()); + } + + @ParameterizedTest + @ValueSource(strings = { + "g", + "gfh2j" + }) + void givenInvalidLoginRequestWithInvalidPassword_whenPasswordDoesNotValid_thenReturnValidationError( + String mockPassword) throws Exception { + // Given + AysLoginRequest mockLoginRequest = new AysLoginRequestBuilder() + .withEmailAddress(AysValidTestData.User.EMAIL_ADDRESS) + .withPassword(mockPassword) + .withSourcePage(AysSourcePage.INSTITUTION) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/token"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockLoginRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(authService, Mockito.never()) + .authenticate(Mockito.any()); + } + + @ParameterizedTest + @ValueSource(strings = { + "abc.def@mail.c", + "abc.def@mail#archive.com", + "abc.def@mail", + "abcdef@mail..com", + "abc-@mail.com", + "admin@test@ays.com", + "admintest@ays..com", + "username@gmail..co.uk", + "user@ example.com", + "user@-example.com", + "user@example-.com", + "(user)@example.com", + "user@[192.168.1.1", + "user@exam ple.com", + "user@.com", + ".user@example.com", + " user@example.com", + "user@example.com ", + " user@example.com ", + "@missingusername.com", + "a@b.c", // 5 karakter - minimum limit altında + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@domain.com" // 255 + // karakter + // - + // maksimum + // limit + // üstünde + }) + void givenInvalidLoginRequestWithInvalidEmailAddress_whenEmailsAreNotValid_thenReturnValidationError( + String mockEmailAddress) throws Exception { + // Given + AysLoginRequest mockLoginRequest = new AysLoginRequestBuilder() + .withEmailAddress(mockEmailAddress) + .withPassword(AysValidTestData.PASSWORD) + .withSourcePage(AysSourcePage.INSTITUTION) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/token"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockLoginRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(authService, Mockito.never()) + .authenticate(Mockito.any()); + } + + @ParameterizedTest + @ValueSource(strings = { + "user@example.com", // 14 karakter - geçerli + "abcdef@mail.com", // 14 karakter - geçerli + "a.b.c.d@test.com", // 15 karakter - geçerli + "test.123@afetyonetimsistemi.org" // 29 karakter - geçerli + }) + void givenValidLoginRequestWithValidEmailAddress_whenEmailsAreValid_thenReturnSuccessResponse( + String mockEmailAddress) throws Exception { + // Given + AysLoginRequest mockLoginRequest = new AysLoginRequestBuilder() + .withEmailAddress(mockEmailAddress) + .withPassword(AysValidTestData.PASSWORD) + .withSourcePage(AysSourcePage.INSTITUTION) + .build(); + + // When + Mockito.when(authService.authenticate(Mockito.any())) + .thenReturn(mockUserToken); + + // Then + String endpoint = BASE_PATH.concat("/token"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockLoginRequest); + + AysResponse mockResponse = AysResponseBuilder.success(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .isNotEmpty()); + + // Verify + Mockito.verify(authService, Mockito.times(1)) + .authenticate(Mockito.any()); + } + + @Test + void givenValidTokenRefreshRequest_whenAccessTokenGeneratedSuccessfully_thenReturnTokenResponse() + throws Exception { + // Given + AysTokenRefreshRequest mockTokenRefreshRequest = AysTokenRefreshRequestBuilder.VALID_FOR_USER; + + // When + Mockito.when(authService.refreshAccessToken(Mockito.anyString())) + .thenReturn(mockUserToken); + + // Then + String endpoint = BASE_PATH.concat("/token/refresh"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockTokenRefreshRequest); + + AysTokenResponse mockTokenResponse = tokenToResponseMapper.map(mockUserToken); + AysResponse mockResponse = AysResponseBuilder.successOf(mockTokenResponse); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .isNotEmpty()) + .andExpect(MockMvcResultMatchers.jsonPath("$.response.accessToken") + .value(mockResponse.getResponse().getAccessToken())) + .andExpect(MockMvcResultMatchers.jsonPath("$.response.accessTokenExpiresAt") + .doesNotHaveJsonPath()) + .andExpect(MockMvcResultMatchers.jsonPath("$.response.refreshToken") + .value(mockResponse.getResponse().getRefreshToken())); + + // Verify + Mockito.verify(authService, Mockito.times(1)) + .refreshAccessToken(Mockito.anyString()); + } + + @Test + void givenValidAysTokenInvalidateRequest_whenTokensInvalidated_thenReturnSuccessResponse() throws Exception { + // Given + AysTokenInvalidateRequest mockTokenInvalidateRequest = AysTokenInvalidateRequestBuilder.VALID_FOR_USER; + + // When + Mockito.doNothing() + .when(authService) + .invalidateTokens(Mockito.any()); + + // Then + String endpoint = BASE_PATH.concat("/token/invalidate"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockUserToken.getAccessToken(), mockTokenInvalidateRequest); + + AysResponse mockResponse = AysResponseBuilder.success(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Mockito.verify(authService, Mockito.times(1)) + .invalidateTokens(Mockito.any()); + } + + @Test + void givenValidForgotPasswordRequest_whenSendPasswordCreateMail_thenReturnSuccessResponse() throws Exception { + // Given + AysPasswordForgotRequest mockForgotPasswordRequest = new AysForgotPasswordRequestBuilder() + .withEmailAddress(AysValidTestData.Admin.EMAIL_ADDRESS) + .build(); + + // When + Mockito.doNothing() + .when(userPasswordService) + .forgotPassword(Mockito.any(AysPasswordForgotRequest.class)); + + // Then + String endpoint = BASE_PATH.concat("/password/forgot"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockForgotPasswordRequest); + + AysResponse mockResponse = AysResponseBuilder.success(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Mockito.verify(userPasswordService, Mockito.times(1)) + .forgotPassword(Mockito.any(AysPasswordForgotRequest.class)); + } + + @ParameterizedTest + @ValueSource(strings = { + "abc.def@mail.c", + "abc.def@mail#archive.com", + "abc.def@mail", + "abcdef@mail..com", + "abc-@mail.com", + "admin@test@ays.com", + "admintest@ays..com", + "username@gmail..co.uk", + "user@ example.com", + "user@-example.com", + "user@example-.com", + "(user)@example.com", + "user@[192.168.1.1", + "user@exam ple.com", + "user@.com", + ".user@example.com", + " user@example.com", + "user@example.com ", + " user@example.com ", + "@missingusername.com" + }) + void givenForgotPasswordRequestWithInvalidEmailAddress_whenEmailDoesNotValid_thenReturnValidationError( + String mockEmailAddress) throws Exception { + + // Given + AysPasswordForgotRequest mockForgotPasswordRequest = new AysForgotPasswordRequestBuilder() + .withEmailAddress(mockEmailAddress) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/password/forgot"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockForgotPasswordRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userPasswordService, Mockito.never()) + .forgotPassword(Mockito.any(AysPasswordForgotRequest.class)); + } + + @Test + void givenForgotPasswordRequestWithoutEmailAddress_whenEmailIsNull_thenReturnValidationError() + throws Exception { + + // Given + AysPasswordForgotRequest mockForgotPasswordRequest = new AysForgotPasswordRequestBuilder() + .withEmailAddress(null) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/password/forgot"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockForgotPasswordRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userPasswordService, Mockito.never()) + .forgotPassword(Mockito.any(AysPasswordForgotRequest.class)); + } + + @Test + void givenValidId_whenCheckPasswordIdSuccessfully_thenReturnSuccessResponse() throws Exception { + // Given + String mockId = "40fb7a46-40bd-46cb-b44f-1f47162133b1"; + + // When + Mockito.doNothing() + .when(userPasswordService) + .checkPasswordChangingValidity(Mockito.anyString()); + + // Then + String endpoint = BASE_PATH.concat("/password/").concat(mockId).concat("/validity"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .get(endpoint); + + AysResponse mockResponse = AysResponseBuilder.success(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Mockito.verify(userPasswordService, Mockito.times(1)) + .checkPasswordChangingValidity(Mockito.anyString()); + } + + @ParameterizedTest + @ValueSource(strings = { + "A", + "493268349068342" + }) + void givenId_whenIdDoesNotValid_thenReturnValidationError(String invalidId) throws Exception { + + // Then + String endpoint = BASE_PATH.concat("/password/").concat(invalidId).concat("/validity"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .get(endpoint); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userPasswordService, Mockito.never()) + .checkPasswordChangingValidity(Mockito.anyString()); + } + + @Test + void givenValidPasswordCreateRequest_whenPasswordCreated_thenReturnSuccessResponse() throws Exception { + // Given + String mockId = "1fa43c75-7a7a-4041-8cef-03be8429dd30"; + AysPasswordCreateRequest mockPasswordCreateRequest = new AysPasswordCreateRequestBuilder() + .withValidValues() + .build(); + + // When + Mockito.doNothing() + .when(userPasswordService) + .createPassword(Mockito.anyString(), Mockito.any(AysPasswordCreateRequest.class)); + + // Then + String endpoint = BASE_PATH.concat("/password/").concat(mockId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockPasswordCreateRequest); + + AysResponse mockResponse = AysResponseBuilder.success(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Mockito.verify(userPasswordService, Mockito.times(1)) + .createPassword(Mockito.anyString(), Mockito.any(AysPasswordCreateRequest.class)); + } + + @Test + void givenPasswordCreateRequest_whenPasswordsNotEqual_thenReturnValidationError() throws Exception { + + // Given + String mockId = "1fa43c75-7a7a-4041-8cef-03be8429dd30"; + AysPasswordCreateRequest mockPasswordCreateRequest = new AysPasswordCreateRequestBuilder() + .withPassword("password") + .withPasswordRepeat("password1") + .build(); + + // Then + String endpoint = BASE_PATH.concat("/password/").concat(mockId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockPasswordCreateRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userPasswordService, Mockito.never()) + .createPassword(Mockito.anyString(), Mockito.any(AysPasswordCreateRequest.class)); + } + + @ParameterizedTest + @ValueSource(strings = { + "g", + "gfh2j" + }) + void givenPasswordCreateRequest_whenPasswordDoesNotValid_thenReturnValidationError(String mockPassword) + throws Exception { + + // Given + String mockId = "1fa43c75-7a7a-4041-8cef-03be8429dd30"; + AysPasswordCreateRequest mockPasswordCreateRequest = new AysPasswordCreateRequestBuilder() + .withPassword(mockPassword) + .withPasswordRepeat(mockPassword) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/password/").concat(mockId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockPasswordCreateRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userPasswordService, Mockito.never()) + .createPassword(Mockito.anyString(), Mockito.any(AysPasswordCreateRequest.class)); + } } diff --git a/src/test/java/org/ays/auth/controller/AysUserControllerTest.java b/src/test/java/org/ays/auth/controller/AysUserControllerTest.java index c178b2e25..d3902d14f 100644 --- a/src/test/java/org/ays/auth/controller/AysUserControllerTest.java +++ b/src/test/java/org/ays/auth/controller/AysUserControllerTest.java @@ -1,6 +1,5 @@ package org.ays.auth.controller; - import org.ays.AysRestControllerTest; import org.ays.auth.model.AysUser; import org.ays.auth.model.AysUserBuilder; @@ -43,982 +42,1026 @@ class AysUserControllerTest extends AysRestControllerTest { - @MockBean - private AysUserReadService userReadService; - - @MockBean - private AysUserCreateService userCreateService; - - @MockBean - private AysUserUpdateService userUpdateService; - - - private final AysUserToUsersResponseMapper userToUsersResponseMapper = AysUserToUsersResponseMapper.initialize(); - private final AysUserToResponseMapper userToResponseMapper = AysUserToResponseMapper.initialize(); - - - private static final String BASE_PATH = "/api/v1"; - - - @Test - void givenValidUserListRequest_whenUsersFound_thenReturnAysPageResponseOfUsersResponse() throws Exception { - // Given - AysUserListRequest mockListRequest = new AysUserListRequestBuilder() - .withValidValues() - .build(); - - //When - List mockUsers = List.of( - new AysUserBuilder().withValidValues().build() - ); - - AysPage mockUserPage = AysPageBuilder - .from(mockUsers, mockListRequest.getPageable()); - - Mockito.when(userReadService.findAll(Mockito.any(AysUserListRequest.class))) - .thenReturn(mockUserPage); - - // Then - String endpoint = BASE_PATH.concat("/users"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockSuperAdminToken.getAccessToken(), mockListRequest); - - List mockUsersResponse = userToUsersResponseMapper.map(mockUsers); - AysPageResponse pageOfResponse = AysPageResponse.builder() - .of(mockUserPage) - .content(mockUsersResponse) - .build(); - AysResponse> mockResponse = AysResponse - .successOf(pageOfResponse); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .isNotEmpty()); - - // Verify - Mockito.verify(userReadService, Mockito.times(1)) - .findAll(Mockito.any(AysUserListRequest.class)); - } - - @ParameterizedTest - @ValueSource(strings = { - "Lorem ipsum dolor sit amet consectetur adipiscing elit Integer nec odio Praesent libero Sed cursus ante dapibus diam Sed nisi Nulla quis sem at nibh", - "Test user 1234", - "User *^%$#", - " Test", - "? User", - "J", - "J----", - "Martin-Luther--King", - "John Doe" - }) - void givenUserListRequest_whenFirstNameDoesNotValid_thenReturnValidationError(String invalidName) throws Exception { - - // Given - AysUserListRequest mockListRequest = new AysUserListRequestBuilder() - .withValidValues() - .withFirstName(invalidName) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/users"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockSuperAdminToken.getAccessToken(), mockListRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userReadService, Mockito.never()) - .findAll(Mockito.any(AysUserListRequest.class)); - } - - @ParameterizedTest - @ValueSource(strings = { - "Lorem ipsum dolor sit amet consectetur adipiscing elit Integer nec odio Praesent libero Sed cursus ante dapibus diam Sed nisi Nulla quis sem at nibh", - "Test user 1234", - "User *^%$#", - " Test", - "? User", - "J", - "J----", - "Martin-Luther--King", - "John Doe" - }) - void givenUserListRequest_whenLastNameDoesNotValid_thenReturnValidationError(String invalidName) throws Exception { - - // Given - AysUserListRequest mockListRequest = new AysUserListRequestBuilder() - .withValidValues() - .withLastName(invalidName) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/users"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockSuperAdminToken.getAccessToken(), mockListRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userReadService, Mockito.never()) - .findAll(Mockito.any(AysUserListRequest.class)); - } - - @ParameterizedTest - @ValueSource(strings = { - "City user 1234", - "City *^%$#", - " Test", - "? User", - "J", - "J----", - "City--King", - "John Doe" - }) - void givenUserListRequest_whenCityDoesNotValid_thenReturnValidationError(String invalidName) throws Exception { - - // Given - AysUserListRequest mockListRequest = new AysUserListRequestBuilder() - .withValidValues() - .withCity(invalidName) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/users"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockSuperAdminToken.getAccessToken(), mockListRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userReadService, Mockito.never()) - .findAll(Mockito.any(AysUserListRequest.class)); - } - - @ParameterizedTest - @ValueSource(strings = { - "1", - "109", - "99999", - "15" - }) - void givenInvalidUserListRequest_whenPageSizeNotTen_thenReturnValidationError(int invalidPageSize) throws Exception { - - // Given - AysUserListRequest mockListRequest = new AysUserListRequestBuilder() - .withPageable(new AysPageableBuilder() - .withPage(1) - .withPageSize(invalidPageSize) - .build() - ) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/users"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockSuperAdminToken.getAccessToken(), mockListRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userReadService, Mockito.never()) - .findAll(Mockito.any(AysUserListRequest.class)); - } - - @Test - void givenValidUserListRequest_whenUserUnauthorized_thenReturnAccessDeniedException() throws Exception { - // Given - AysUserListRequest mockListRequest = new AysUserListRequestBuilder() - .withValidValues() - .build(); - - // Then - String endpoint = BASE_PATH.concat("/users"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockUserToken.getAccessToken(), mockListRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isForbidden()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .doesNotExist()); - - // Verify - Mockito.verify(userReadService, Mockito.never()) - .findAll(Mockito.any(AysUserListRequest.class)); - } - - - @Test - void givenValidUserId_whenUserFound_thenReturnAysUserResponse() throws Exception { - - // Given - String mockUserId = AysRandomUtil.generateUUID(); - - // When - AysUser mockUser = new AysUserBuilder() - .withValidValues() - .withId(mockUserId) - .build(); - - Mockito.when(userReadService.findById(mockUserId)) - .thenReturn(mockUser); - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(mockUserId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .get(endpoint, mockAdminToken.getAccessToken()); - - AysUserResponse mockUserResponse = userToResponseMapper - .map(mockUser); - AysResponse mockResponse = AysResponse - .successOf(mockUserResponse); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .isNotEmpty()); - - // Verify - Mockito.verify(userReadService, Mockito.times(1)) - .findById(mockUserId); - } - - @Test - void givenUserId_whenUnauthorizedForGettingUserById_thenReturnAccessDeniedException() throws Exception { - - // Given - String mockUserId = AysRandomUtil.generateUUID(); - - // Then - String endpoint = BASE_PATH.concat("/user/".concat(mockUserId)); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .get(endpoint, mockUserToken.getAccessToken()); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isForbidden()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .doesNotExist()); - - // Verify - Mockito.verify(userReadService, Mockito.never()) - .findById(mockUserId); - } - - @ParameterizedTest - @ValueSource(strings = { - "A", - "493268349068342" - }) - void givenInvalidId_whenIdNotValid_thenReturnValidationError(String invalidId) throws Exception { - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(invalidId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .get(endpoint, mockAdminToken.getAccessToken()); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userReadService, Mockito.never()) - .findById(Mockito.anyString()); - } - - - @Test - void givenUserCreateRequest_whenUserCreated_thenReturnSuccess() throws Exception { - - // Given - AysUserCreateRequest mockCreateRequest = new AysUserCreateRequestBuilder() - .withValidValues() - .build(); - - // When - Mockito.doNothing() - .when(userCreateService) - .create(Mockito.any(AysUserCreateRequest.class)); - - // Then - String endpoint = BASE_PATH.concat("/user"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockAdminToken.getAccessToken(), mockCreateRequest); - - AysResponse mockResponse = AysResponseBuilder.success(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()); - - // Verify - Mockito.verify(userCreateService, Mockito.times(1)) - .create(Mockito.any(AysUserCreateRequest.class)); - } - - @Test - void givenValidUserCreateRequest_whenUserUnauthorized_thenReturnAccessDeniedException() throws Exception { - - // Given - AysUserCreateRequest mockCreateRequest = new AysUserCreateRequestBuilder() - .withValidValues() - .build(); - - // Then - String endpoint = BASE_PATH.concat("/user"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockUserToken.getAccessToken(), mockCreateRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isForbidden()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .doesNotExist()); - - // Verify - Mockito.verify(userCreateService, Mockito.never()) - .create(Mockito.any(AysUserCreateRequest.class)); - } - - @Test - void givenUserCreateRequest_whenRoleIdsAreNull_thenReturnValidationError() throws Exception { - - // Given - AysUserCreateRequest mockCreateRequest = new AysUserCreateRequestBuilder() - .withValidValues() - .withRoleIds(null) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/user"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockAdminToken.getAccessToken(), mockCreateRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userCreateService, Mockito.never()) - .create(Mockito.any(AysUserCreateRequest.class)); - } - - @Test - void givenUserCreateRequest_whenRoleIdsAreEmpty_thenReturnValidationError() throws Exception { - - // Given - AysUserCreateRequest mockCreateRequest = new AysUserCreateRequestBuilder() - .withValidValues() - .withRoleIds(Set.of()) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/user"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockAdminToken.getAccessToken(), mockCreateRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userCreateService, Mockito.never()) - .create(Mockito.any(AysUserCreateRequest.class)); - } - - @ParameterizedTest - @NullSource - @ValueSource(strings = { - "", - "55aed4c4facb4b66bdb5-309eaaef4453" - }) - void givenUserCreateRequest_whenRoleIdIsNotValid_thenReturnValidationError(String invalidRoleId) throws Exception { - - // Given - AysUserCreateRequest mockCreateRequest = new AysUserCreateRequestBuilder() - .withValidValues() - .withRoleIds(Collections.singleton(invalidRoleId)) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/user"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockAdminToken.getAccessToken(), mockCreateRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userCreateService, Mockito.never()) - .create(Mockito.any(AysUserCreateRequest.class)); - } - - @ParameterizedTest - @ValueSource(strings = { - "abc.def@mail.c", - "abc.def@mail#archive.com", - "abc.def@mail", - "abcdef@mail..com", - "abc-@mail.com", - "admin@test@ays.com", - "admintest@ays..com", - "username@gmail..co.uk", - "user@ example.com", - "user@-example.com", - "user@example-.com", - "(user)@example.com", - "user@[192.168.1.1", - "user@exam ple.com", - "user@.com", - ".user@example.com", - " user@example.com", - "user@example.com ", - " user@example.com ", - "@missingusername.com" - }) - void givenUserCreateRequest_whenEmailNotValid_thenReturnValidationError(String mockEmailAddress) throws Exception { - - // Given - AysUserCreateRequest mockCreateRequest = new AysUserCreateRequestBuilder() - .withValidValues() - .withEmailAddress(mockEmailAddress) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/user"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockAdminToken.getAccessToken(), mockCreateRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userCreateService, Mockito.never()) - .create(Mockito.any(AysUserCreateRequest.class)); - } - - - @Test - void givenValidIdAndUserUpdateRequest_whenUserUpdated_thenReturnSuccess() throws Exception { - - // Given - String mockId = "2cb9f39b-490f-4035-97ac-9afbb87506df"; - AysUserUpdateRequest mockUpdateRequest = new AysUserUpdateRequestBuilder() - .withValidValues() - .build(); - - // When - Mockito.doNothing() - .when(userUpdateService) - .update(Mockito.any(), Mockito.any(AysUserUpdateRequest.class)); - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(mockId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .put(endpoint, mockAdminToken.getAccessToken(), mockUpdateRequest); - - AysResponse mockResponse = AysResponseBuilder.success(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()); - - // Verify - Mockito.verify(userUpdateService, Mockito.times(1)) - .update(Mockito.anyString(), Mockito.any(AysUserUpdateRequest.class)); - } - - @Test - void givenValidIdAndUserUpdateRequest_whenUserUnauthorized_thenReturnAccessDeniedException() throws Exception { - - // Given - String mockId = "0deb5f16-479f-4625-9354-7db29c861e85"; - AysUserUpdateRequest mockUpdateRequest = new AysUserUpdateRequestBuilder() - .withValidValues() - .build(); - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(mockId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .put(endpoint, mockUserToken.getAccessToken(), mockUpdateRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isForbidden()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .doesNotExist()); - - // Verify - Mockito.verify(userUpdateService, Mockito.never()) - .update(Mockito.anyString(), Mockito.any(AysUserUpdateRequest.class)); - } - - @ParameterizedTest - @ValueSource(strings = { - "A", - "493268349068342" - }) - void givenInvalidIdAndValidUserUpdateRequest_whenIdNotValid_thenReturnValidationError(String invalidId) throws Exception { - - // Given - AysUserUpdateRequest mockUpdateRequest = new AysUserUpdateRequestBuilder() - .withValidValues() - .build(); - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(invalidId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .put(endpoint, mockAdminToken.getAccessToken(), mockUpdateRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userUpdateService, Mockito.never()) - .update(Mockito.anyString(), Mockito.any(AysUserUpdateRequest.class)); - } - - @Test - void givenValidIdAndInvalidUserUpdateRequest_whenRoleIdsAreNull_thenReturnValidationError() throws Exception { - - // Given - String mockId = "3b5a65ba-c90e-41ad-97ec-56c7dd4fe708"; - AysUserUpdateRequest mockUpdateRequest = new AysUserUpdateRequestBuilder() - .withValidValues() - .withRoleIds(null) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(mockId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .put(endpoint, mockAdminToken.getAccessToken(), mockUpdateRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userUpdateService, Mockito.never()) - .update(Mockito.anyString(), Mockito.any(AysUserUpdateRequest.class)); - } - - @Test - void givenValidIdAndInvalidUserUpdateRequest_whenRoleIdsAreEmpty_thenReturnValidationError() throws Exception { - - // Given - String mockId = "daaa28f9-f7b3-4f43-a10f-a5fe2b0eb2fa"; - AysUserUpdateRequest mockUpdateRequest = new AysUserUpdateRequestBuilder() - .withValidValues() - .withRoleIds(Set.of()) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(mockId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .put(endpoint, mockAdminToken.getAccessToken(), mockUpdateRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userUpdateService, Mockito.never()) - .update(Mockito.anyString(), Mockito.any(AysUserUpdateRequest.class)); - } - - @ParameterizedTest - @NullSource - @ValueSource(strings = { - "", - "55aed4c4facb4b66bdb5-309eaaef4453" - }) - void givenValidIdAndInvalidUserUpdateRequest_whenRoleIdIsNotValid_thenReturnValidationError(String invalidRoleId) throws Exception { - - // Given - String mockId = "b8758dce-ad8e-438d-b31c-b440b352068a"; - - Set mockRoleIds = new HashSet<>(); - mockRoleIds.add(invalidRoleId); - AysUserUpdateRequest mockUpdateRequest = new AysUserUpdateRequestBuilder() - .withValidValues() - .withRoleIds(mockRoleIds) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(mockId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .put(endpoint, mockAdminToken.getAccessToken(), mockUpdateRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userUpdateService, Mockito.never()) - .update(Mockito.anyString(), Mockito.any(AysUserUpdateRequest.class)); - } - - @ParameterizedTest - @ValueSource(strings = { - "abc.def@mail.c", - "abc.def@mail#archive.com", - "abc.def@mail", - "abcdef@mail..com", - "abc-@mail.com", - "admin@test@ays.com", - "admintest@ays..com", - "username@gmail..co.uk", - "user@ example.com", - "user@-example.com", - "user@example-.com", - "(user)@example.com", - "user@[192.168.1.1", - "user@exam ple.com", - "user@.com", - ".user@example.com", - " user@example.com", - "user@example.com ", - " user@example.com ", - "@missingusername.com" - }) - void givenValidIdAndInvalidUserUpdateRequest_whenEmailNotValid_thenReturnValidationError(String mockEmailAddress) throws Exception { - // Given - String mockId = "b66afcb0-3029-4968-a87b-e1d94fc75642"; - - AysUserUpdateRequest mockUpdateRequest = new AysUserUpdateRequestBuilder() - .withValidValues() - .withEmailAddress(mockEmailAddress) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(mockId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .put(endpoint, mockAdminToken.getAccessToken(), mockUpdateRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userUpdateService, Mockito.never()) - .update(Mockito.anyString(), Mockito.any(AysUserUpdateRequest.class)); - } - - - @Test - void givenValidId_whenActivateUser_thenReturnSuccess() throws Exception { - - // Given - String mockId = "793fcc5d-31cc-4704-9f0a-627ac7da517d"; - - // When - Mockito.doNothing() - .when(userUpdateService) - .activate(Mockito.anyString()); - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(mockId).concat("/activate"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .patch(endpoint, mockAdminToken.getAccessToken()); - - AysResponse mockResponse = AysResponseBuilder.success(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()); - - // Verify - Mockito.verify(userUpdateService, Mockito.times(1)) - .activate(Mockito.anyString()); - } - - @Test - void givenValidId_whenUserUnauthorized_thenReturnAccessDeniedException() throws Exception { - - // Given - String mockId = "201aec72-ecd8-49fc-86f5-2b5458871edb"; - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(mockId).concat("/activate"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .patch(endpoint, mockUserToken.getAccessToken()); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isForbidden()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()); - - // Verify - Mockito.verify(userUpdateService, Mockito.never()) - .activate(Mockito.anyString()); - } - - @ParameterizedTest - @ValueSource(strings = { - "A", - "493268349068342" - }) - void givenInvalidUserId_whenIdNotValid_thenReturnValidationError(String invalidId) throws Exception { - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(invalidId).concat("/activate"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .patch(endpoint, mockAdminToken.getAccessToken()); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userUpdateService, Mockito.never()) - .activate(Mockito.anyString()); - } - - - @Test - void givenValidId_whenUserDeleted_thenReturnSuccess() throws Exception { - - // Given - String mockId = "2e574ecf-929c-4923-8aea-d061d29934da"; - - // When - Mockito.doNothing() - .when(userUpdateService) - .delete(Mockito.any()); - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(mockId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .delete(endpoint, mockAdminToken.getAccessToken()); - - AysResponse mockResponse = AysResponseBuilder.success(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()); - - // Verify - Mockito.verify(userUpdateService, Mockito.times(1)) - .delete(mockId); - } - - @ParameterizedTest - @ValueSource(strings = { - "A", - "493268349068342" - }) - void givenId_whenIdDoesNotValid_thenReturnValidationError(String invalidId) throws Exception { - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(invalidId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .delete(endpoint, mockAdminToken.getAccessToken()); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userUpdateService, Mockito.never()) - .delete(Mockito.anyString()); - } - - @Test - void givenUserDelete_whenUserUnauthorized_thenReturnAccessDeniedException() throws Exception { - - // Given - String mockId = "45082f52-011b-41d1-b4bd-6eba4e1f1ea8"; - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(mockId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .delete(endpoint, mockUserToken.getAccessToken()); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isForbidden()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()); - - // Verify - Mockito.verify(userUpdateService, Mockito.never()) - .delete(Mockito.anyString()); - } - - - @Test - void givenValidId_whenPassivateUser_thenReturnSuccess() throws Exception { - - // Given - String mockId = "894dcc5d-31cc-4704-9f0a-627ac7da517d"; - - // When - Mockito.doNothing() - .when(userUpdateService) - .passivate(Mockito.anyString()); - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(mockId).concat("/passivate"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .patch(endpoint, mockAdminToken.getAccessToken()); - - AysResponse mockResponse = AysResponseBuilder.success(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()); - - // Verify - Mockito.verify(userUpdateService, Mockito.times(1)) - .passivate(Mockito.anyString()); - } - - @ParameterizedTest - @ValueSource(strings = { - "A", - "493268349068342" - }) - void givenInvalidUserId_whenIdNotValid_thenReturnValidationErrorForPassivate(String invalidId) throws Exception { - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(invalidId).concat("/passivate"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .patch(endpoint, mockAdminToken.getAccessToken()); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userUpdateService, Mockito.never()) - .passivate(Mockito.anyString()); - } - - @Test - void givenValidUserId_whenUserUnauthorized_thenReturnAccessDeniedException() throws Exception { - - // Given - String mockId = "333aec72-ecd8-49fc-86f5-2b5458871edb"; - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(mockId).concat("/passivate"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .patch(endpoint, mockUserToken.getAccessToken()); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isForbidden()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()); - - // Verify - Mockito.verify(userUpdateService, Mockito.never()) - .passivate(Mockito.anyString()); - } + @MockBean + private AysUserReadService userReadService; + + @MockBean + private AysUserCreateService userCreateService; + + @MockBean + private AysUserUpdateService userUpdateService; + + private final AysUserToUsersResponseMapper userToUsersResponseMapper = AysUserToUsersResponseMapper + .initialize(); + private final AysUserToResponseMapper userToResponseMapper = AysUserToResponseMapper.initialize(); + + private static final String BASE_PATH = "/api/v1"; + + @Test + void givenValidUserListRequest_whenUsersFound_thenReturnAysPageResponseOfUsersResponse() throws Exception { + // Given + AysUserListRequest mockListRequest = new AysUserListRequestBuilder() + .withValidValues() + .build(); + + // When + List mockUsers = List.of( + new AysUserBuilder().withValidValues().build()); + + AysPage mockUserPage = AysPageBuilder + .from(mockUsers, mockListRequest.getPageable()); + + Mockito.when(userReadService.findAll(Mockito.any(AysUserListRequest.class))) + .thenReturn(mockUserPage); + + // Then + String endpoint = BASE_PATH.concat("/users"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockSuperAdminToken.getAccessToken(), mockListRequest); + + List mockUsersResponse = userToUsersResponseMapper.map(mockUsers); + AysPageResponse pageOfResponse = AysPageResponse.builder() + .of(mockUserPage) + .content(mockUsersResponse) + .build(); + AysResponse> mockResponse = AysResponse + .successOf(pageOfResponse); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .isNotEmpty()); + + // Verify + Mockito.verify(userReadService, Mockito.times(1)) + .findAll(Mockito.any(AysUserListRequest.class)); + } + + @ParameterizedTest + @ValueSource(strings = { + "Lorem ipsum dolor sit amet consectetur adipiscing elit Integer nec odio Praesent libero Sed cursus ante dapibus diam Sed nisi Nulla quis sem at nibh", + "Test user 1234", + "User *^%$#", + " Test", + "? User", + "J", + "J----", + "Martin-Luther--King", + "John Doe" + }) + void givenUserListRequest_whenFirstNameDoesNotValid_thenReturnValidationError(String invalidName) + throws Exception { + + // Given + AysUserListRequest mockListRequest = new AysUserListRequestBuilder() + .withValidValues() + .withFirstName(invalidName) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/users"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockSuperAdminToken.getAccessToken(), mockListRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userReadService, Mockito.never()) + .findAll(Mockito.any(AysUserListRequest.class)); + } + + @ParameterizedTest + @ValueSource(strings = { + "Lorem ipsum dolor sit amet consectetur adipiscing elit Integer nec odio Praesent libero Sed cursus ante dapibus diam Sed nisi Nulla quis sem at nibh", + "Test user 1234", + "User *^%$#", + " Test", + "? User", + "J", + "J----", + "Martin-Luther--King", + "John Doe" + }) + void givenUserListRequest_whenLastNameDoesNotValid_thenReturnValidationError(String invalidName) + throws Exception { + + // Given + AysUserListRequest mockListRequest = new AysUserListRequestBuilder() + .withValidValues() + .withLastName(invalidName) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/users"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockSuperAdminToken.getAccessToken(), mockListRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userReadService, Mockito.never()) + .findAll(Mockito.any(AysUserListRequest.class)); + } + + @ParameterizedTest + @ValueSource(strings = { + "City user 1234", + "City *^%$#", + " Test", + "? User", + "J", + "J----", + "City--King", + "John Doe" + }) + void givenUserListRequest_whenCityDoesNotValid_thenReturnValidationError(String invalidName) throws Exception { + + // Given + AysUserListRequest mockListRequest = new AysUserListRequestBuilder() + .withValidValues() + .withCity(invalidName) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/users"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockSuperAdminToken.getAccessToken(), mockListRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userReadService, Mockito.never()) + .findAll(Mockito.any(AysUserListRequest.class)); + } + + @ParameterizedTest + @ValueSource(strings = { + "1", + "109", + "99999", + "15" + }) + void givenInvalidUserListRequest_whenPageSizeNotTen_thenReturnValidationError(int invalidPageSize) + throws Exception { + + // Given + AysUserListRequest mockListRequest = new AysUserListRequestBuilder() + .withPageable(new AysPageableBuilder() + .withPage(1) + .withPageSize(invalidPageSize) + .build()) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/users"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockSuperAdminToken.getAccessToken(), mockListRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userReadService, Mockito.never()) + .findAll(Mockito.any(AysUserListRequest.class)); + } + + @Test + void givenValidUserListRequest_whenUserUnauthorized_thenReturnAccessDeniedException() throws Exception { + // Given + AysUserListRequest mockListRequest = new AysUserListRequestBuilder() + .withValidValues() + .build(); + + // Then + String endpoint = BASE_PATH.concat("/users"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockUserToken.getAccessToken(), mockListRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isForbidden()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .doesNotExist()); + + // Verify + Mockito.verify(userReadService, Mockito.never()) + .findAll(Mockito.any(AysUserListRequest.class)); + } + + @Test + void givenValidUserId_whenUserFound_thenReturnAysUserResponse() throws Exception { + + // Given + String mockUserId = AysRandomUtil.generateUUID(); + + // When + AysUser mockUser = new AysUserBuilder() + .withValidValues() + .withId(mockUserId) + .build(); + + Mockito.when(userReadService.findById(mockUserId)) + .thenReturn(mockUser); + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(mockUserId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .get(endpoint, mockAdminToken.getAccessToken()); + + AysUserResponse mockUserResponse = userToResponseMapper + .map(mockUser); + AysResponse mockResponse = AysResponse + .successOf(mockUserResponse); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .isNotEmpty()); + + // Verify + Mockito.verify(userReadService, Mockito.times(1)) + .findById(mockUserId); + } + + @Test + void givenUserId_whenUnauthorizedForGettingUserById_thenReturnAccessDeniedException() throws Exception { + + // Given + String mockUserId = AysRandomUtil.generateUUID(); + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(mockUserId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .get(endpoint, mockUserToken.getAccessToken()); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isForbidden()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .doesNotExist()); + + // Verify + Mockito.verify(userReadService, Mockito.never()) + .findById(mockUserId); + } + + @ParameterizedTest + @ValueSource(strings = { + "A", + "493268349068342" + }) + void givenInvalidId_whenIdNotValid_thenReturnValidationError(String invalidId) throws Exception { + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(invalidId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .get(endpoint, mockAdminToken.getAccessToken()); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userReadService, Mockito.never()) + .findById(Mockito.anyString()); + } + + @Test + void givenUserCreateRequest_whenUserCreated_thenReturnSuccess() throws Exception { + + // Given + AysUserCreateRequest mockCreateRequest = new AysUserCreateRequestBuilder() + .withValidValues() + .build(); + + // When + Mockito.doNothing() + .when(userCreateService) + .create(Mockito.any(AysUserCreateRequest.class)); + + // Then + String endpoint = BASE_PATH.concat("/user"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockAdminToken.getAccessToken(), mockCreateRequest); + + AysResponse mockResponse = AysResponseBuilder.success(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Mockito.verify(userCreateService, Mockito.times(1)) + .create(Mockito.any(AysUserCreateRequest.class)); + } + + @Test + void givenValidUserCreateRequest_whenUserUnauthorized_thenReturnAccessDeniedException() throws Exception { + + // Given + AysUserCreateRequest mockCreateRequest = new AysUserCreateRequestBuilder() + .withValidValues() + .build(); + + // Then + String endpoint = BASE_PATH.concat("/user"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockUserToken.getAccessToken(), mockCreateRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isForbidden()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .doesNotExist()); + + // Verify + Mockito.verify(userCreateService, Mockito.never()) + .create(Mockito.any(AysUserCreateRequest.class)); + } + + @Test + void givenUserCreateRequest_whenRoleIdsAreNull_thenReturnValidationError() throws Exception { + + // Given + AysUserCreateRequest mockCreateRequest = new AysUserCreateRequestBuilder() + .withValidValues() + .withRoleIds(null) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/user"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockAdminToken.getAccessToken(), mockCreateRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userCreateService, Mockito.never()) + .create(Mockito.any(AysUserCreateRequest.class)); + } + + @Test + void givenUserCreateRequest_whenRoleIdsAreEmpty_thenReturnValidationError() throws Exception { + + // Given + AysUserCreateRequest mockCreateRequest = new AysUserCreateRequestBuilder() + .withValidValues() + .withRoleIds(Set.of()) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/user"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockAdminToken.getAccessToken(), mockCreateRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userCreateService, Mockito.never()) + .create(Mockito.any(AysUserCreateRequest.class)); + } + + @ParameterizedTest + @NullSource + @ValueSource(strings = { + "", + "55aed4c4facb4b66bdb5-309eaaef4453" + }) + void givenUserCreateRequest_whenRoleIdIsNotValid_thenReturnValidationError(String invalidRoleId) + throws Exception { + + // Given + AysUserCreateRequest mockCreateRequest = new AysUserCreateRequestBuilder() + .withValidValues() + .withRoleIds(Collections.singleton(invalidRoleId)) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/user"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockAdminToken.getAccessToken(), mockCreateRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userCreateService, Mockito.never()) + .create(Mockito.any(AysUserCreateRequest.class)); + } + + @ParameterizedTest + @ValueSource(strings = { + "abc.def@mail.c", + "abc.def@mail#archive.com", + "abc.def@mail", + "abcdef@mail..com", + "abc-@mail.com", + "admin@test@ays.com", + "admintest@ays..com", + "username@gmail..co.uk", + "user@ example.com", + "user@-example.com", + "user@example-.com", + "(user)@example.com", + "user@[192.168.1.1", + "user@exam ple.com", + "user@.com", + ".user@example.com", + " user@example.com", + "user@example.com ", + " user@example.com ", + "@missingusername.com", + "a@b.c", // 5 karakter - minimum limit altında + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@domain.com" // 255 + // karakter + // - + // maksimum + // limit + // üstünde + }) + void givenUserCreateRequest_whenEmailNotValid_thenReturnValidationError(String mockEmailAddress) + throws Exception { + // Given + AysUserCreateRequest mockCreateRequest = new AysUserCreateRequestBuilder() + .withValidValues() + .withEmailAddress(mockEmailAddress) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/user"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockAdminToken.getAccessToken(), mockCreateRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userCreateService, Mockito.never()) + .create(Mockito.any(AysUserCreateRequest.class)); + } + + @ParameterizedTest + @ValueSource(strings = { + "user@example.com", // 14 karakter - geçerli + "abcdef@mail.com", // 14 karakter - geçerli + "a.b.c.d@test.com", // 15 karakter - geçerli + "test.123@afetyonetimsistemi.org" // 29 karakter - geçerli + }) + void givenUserCreateRequest_whenEmailValid_thenReturnSuccessResponse(String mockEmailAddress) throws Exception { + // Given + AysUserCreateRequest mockCreateRequest = new AysUserCreateRequestBuilder() + .withValidValues() + .withEmailAddress(mockEmailAddress) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/user"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockAdminToken.getAccessToken(), mockCreateRequest); + + AysResponse mockResponse = AysResponseBuilder.success(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Mockito.verify(userCreateService, Mockito.times(1)) + .create(Mockito.any(AysUserCreateRequest.class)); + } + + @Test + void givenValidIdAndUserUpdateRequest_whenUserUpdated_thenReturnSuccess() throws Exception { + + // Given + String mockId = "2cb9f39b-490f-4035-97ac-9afbb87506df"; + AysUserUpdateRequest mockUpdateRequest = new AysUserUpdateRequestBuilder() + .withValidValues() + .build(); + + // When + Mockito.doNothing() + .when(userUpdateService) + .update(Mockito.any(), Mockito.any(AysUserUpdateRequest.class)); + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(mockId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .put(endpoint, mockAdminToken.getAccessToken(), mockUpdateRequest); + + AysResponse mockResponse = AysResponseBuilder.success(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Mockito.verify(userUpdateService, Mockito.times(1)) + .update(Mockito.anyString(), Mockito.any(AysUserUpdateRequest.class)); + } + + @Test + void givenValidIdAndUserUpdateRequest_whenUserUnauthorized_thenReturnAccessDeniedException() throws Exception { + + // Given + String mockId = "0deb5f16-479f-4625-9354-7db29c861e85"; + AysUserUpdateRequest mockUpdateRequest = new AysUserUpdateRequestBuilder() + .withValidValues() + .build(); + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(mockId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .put(endpoint, mockUserToken.getAccessToken(), mockUpdateRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isForbidden()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .doesNotExist()); + + // Verify + Mockito.verify(userUpdateService, Mockito.never()) + .update(Mockito.anyString(), Mockito.any(AysUserUpdateRequest.class)); + } + + @ParameterizedTest + @ValueSource(strings = { + "A", + "493268349068342" + }) + void givenInvalidIdAndValidUserUpdateRequest_whenIdNotValid_thenReturnValidationError(String invalidId) + throws Exception { + + // Given + AysUserUpdateRequest mockUpdateRequest = new AysUserUpdateRequestBuilder() + .withValidValues() + .build(); + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(invalidId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .put(endpoint, mockAdminToken.getAccessToken(), mockUpdateRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userUpdateService, Mockito.never()) + .update(Mockito.anyString(), Mockito.any(AysUserUpdateRequest.class)); + } + + @Test + void givenValidIdAndInvalidUserUpdateRequest_whenRoleIdsAreNull_thenReturnValidationError() throws Exception { + + // Given + String mockId = "3b5a65ba-c90e-41ad-97ec-56c7dd4fe708"; + AysUserUpdateRequest mockUpdateRequest = new AysUserUpdateRequestBuilder() + .withValidValues() + .withRoleIds(null) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(mockId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .put(endpoint, mockAdminToken.getAccessToken(), mockUpdateRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userUpdateService, Mockito.never()) + .update(Mockito.anyString(), Mockito.any(AysUserUpdateRequest.class)); + } + + @Test + void givenValidIdAndInvalidUserUpdateRequest_whenRoleIdsAreEmpty_thenReturnValidationError() throws Exception { + + // Given + String mockId = "daaa28f9-f7b3-4f43-a10f-a5fe2b0eb2fa"; + AysUserUpdateRequest mockUpdateRequest = new AysUserUpdateRequestBuilder() + .withValidValues() + .withRoleIds(Set.of()) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(mockId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .put(endpoint, mockAdminToken.getAccessToken(), mockUpdateRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userUpdateService, Mockito.never()) + .update(Mockito.anyString(), Mockito.any(AysUserUpdateRequest.class)); + } + + @ParameterizedTest + @NullSource + @ValueSource(strings = { + "", + "55aed4c4facb4b66bdb5-309eaaef4453" + }) + void givenValidIdAndInvalidUserUpdateRequest_whenRoleIdIsNotValid_thenReturnValidationError( + String invalidRoleId) throws Exception { + + // Given + String mockId = "b8758dce-ad8e-438d-b31c-b440b352068a"; + + Set mockRoleIds = new HashSet<>(); + mockRoleIds.add(invalidRoleId); + AysUserUpdateRequest mockUpdateRequest = new AysUserUpdateRequestBuilder() + .withValidValues() + .withRoleIds(mockRoleIds) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(mockId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .put(endpoint, mockAdminToken.getAccessToken(), mockUpdateRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userUpdateService, Mockito.never()) + .update(Mockito.anyString(), Mockito.any(AysUserUpdateRequest.class)); + } + + @ParameterizedTest + @ValueSource(strings = { + "abc.def@mail.c", + "abc.def@mail#archive.com", + "abc.def@mail", + "abcdef@mail..com", + "abc-@mail.com", + "admin@test@ays.com", + "admintest@ays..com", + "username@gmail..co.uk", + "user@ example.com", + "user@-example.com", + "user@example-.com", + "(user)@example.com", + "user@[192.168.1.1", + "user@exam ple.com", + "user@.com", + ".user@example.com", + " user@example.com", + "user@example.com ", + " user@example.com ", + "@missingusername.com", + "a@b.c", // 5 karakter - minimum limit altında + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@domain.com" // 255 + // karakter + // - + // maksimum + // limit + // üstünde + }) + void givenValidIdAndInvalidUserUpdateRequest_whenEmailNotValid_thenReturnValidationError( + String mockEmailAddress) throws Exception { + // Given + String mockId = "b66afcb0-3029-4968-a87b-e1d94fc75642"; + + AysUserUpdateRequest mockUpdateRequest = new AysUserUpdateRequestBuilder() + .withValidValues() + .withEmailAddress(mockEmailAddress) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(mockId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .put(endpoint, mockAdminToken.getAccessToken(), mockUpdateRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userUpdateService, Mockito.never()) + .update(Mockito.anyString(), Mockito.any(AysUserUpdateRequest.class)); + } + + @Test + void givenValidId_whenActivateUser_thenReturnSuccess() throws Exception { + + // Given + String mockId = "793fcc5d-31cc-4704-9f0a-627ac7da517d"; + + // When + Mockito.doNothing() + .when(userUpdateService) + .activate(Mockito.anyString()); + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(mockId).concat("/activate"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .patch(endpoint, mockAdminToken.getAccessToken()); + + AysResponse mockResponse = AysResponseBuilder.success(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Mockito.verify(userUpdateService, Mockito.times(1)) + .activate(Mockito.anyString()); + } + + @Test + void givenValidId_whenUserUnauthorized_thenReturnAccessDeniedException() throws Exception { + + // Given + String mockId = "201aec72-ecd8-49fc-86f5-2b5458871edb"; + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(mockId).concat("/activate"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .patch(endpoint, mockUserToken.getAccessToken()); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isForbidden()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Mockito.verify(userUpdateService, Mockito.never()) + .activate(Mockito.anyString()); + } + + @ParameterizedTest + @ValueSource(strings = { + "A", + "493268349068342" + }) + void givenInvalidUserId_whenIdNotValid_thenReturnValidationError(String invalidId) throws Exception { + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(invalidId).concat("/activate"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .patch(endpoint, mockAdminToken.getAccessToken()); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userUpdateService, Mockito.never()) + .activate(Mockito.anyString()); + } + + @Test + void givenValidId_whenUserDeleted_thenReturnSuccess() throws Exception { + + // Given + String mockId = "2e574ecf-929c-4923-8aea-d061d29934da"; + + // When + Mockito.doNothing() + .when(userUpdateService) + .delete(Mockito.any()); + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(mockId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .delete(endpoint, mockAdminToken.getAccessToken()); + + AysResponse mockResponse = AysResponseBuilder.success(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Mockito.verify(userUpdateService, Mockito.times(1)) + .delete(mockId); + } + + @ParameterizedTest + @ValueSource(strings = { + "A", + "493268349068342" + }) + void givenId_whenIdDoesNotValid_thenReturnValidationError(String invalidId) throws Exception { + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(invalidId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .delete(endpoint, mockAdminToken.getAccessToken()); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userUpdateService, Mockito.never()) + .delete(Mockito.anyString()); + } + + @Test + void givenUserDelete_whenUserUnauthorized_thenReturnAccessDeniedException() throws Exception { + + // Given + String mockId = "45082f52-011b-41d1-b4bd-6eba4e1f1ea8"; + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(mockId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .delete(endpoint, mockUserToken.getAccessToken()); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isForbidden()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Mockito.verify(userUpdateService, Mockito.never()) + .delete(Mockito.anyString()); + } + + @Test + void givenValidId_whenPassivateUser_thenReturnSuccess() throws Exception { + + // Given + String mockId = "894dcc5d-31cc-4704-9f0a-627ac7da517d"; + + // When + Mockito.doNothing() + .when(userUpdateService) + .passivate(Mockito.anyString()); + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(mockId).concat("/passivate"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .patch(endpoint, mockAdminToken.getAccessToken()); + + AysResponse mockResponse = AysResponseBuilder.success(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Mockito.verify(userUpdateService, Mockito.times(1)) + .passivate(Mockito.anyString()); + } + + @ParameterizedTest + @ValueSource(strings = { + "A", + "493268349068342" + }) + void givenInvalidUserId_whenIdNotValid_thenReturnValidationErrorForPassivate(String invalidId) + throws Exception { + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(invalidId).concat("/passivate"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .patch(endpoint, mockAdminToken.getAccessToken()); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userUpdateService, Mockito.never()) + .passivate(Mockito.anyString()); + } + + @Test + void givenValidUserId_whenUserUnauthorized_thenReturnAccessDeniedException() throws Exception { + + // Given + String mockId = "333aec72-ecd8-49fc-86f5-2b5458871edb"; + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(mockId).concat("/passivate"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .patch(endpoint, mockUserToken.getAccessToken()); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isForbidden()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Mockito.verify(userUpdateService, Mockito.never()) + .passivate(Mockito.anyString()); + } } From 06a5c25a0630bf18675623a5fb04ab5dc3e912ff Mon Sep 17 00:00:00 2001 From: m1erla <116915007+m1erla@users.noreply.github.com> Date: Wed, 15 Jan 2025 22:08:20 +0300 Subject: [PATCH 3/8] AYS-661 | Fix emergency evacuation application tests --- ...EmergencyEvacuationApplicationRequest.java | 27 +- ...encyEvacuationApplicationEndToEndTest.java | 886 +++++++++--------- ...EmergencyEvacuationApplicationBuilder.java | 2 +- .../EmergencyEvacuationRequestBuilder.java | 16 +- 4 files changed, 482 insertions(+), 449 deletions(-) diff --git a/src/main/java/org/ays/emergency_application/model/request/EmergencyEvacuationApplicationRequest.java b/src/main/java/org/ays/emergency_application/model/request/EmergencyEvacuationApplicationRequest.java index 7545a7a0a..c0f55acd3 100644 --- a/src/main/java/org/ays/emergency_application/model/request/EmergencyEvacuationApplicationRequest.java +++ b/src/main/java/org/ays/emergency_application/model/request/EmergencyEvacuationApplicationRequest.java @@ -13,10 +13,11 @@ import org.ays.common.util.validation.Name; import org.hibernate.validator.constraints.Range; - /** - * Represents a request to complete emergency evacuation request. The request includes fields for the required user - * information, such as the user's phone number, as well as their first and last name. + * Represents a request to complete emergency evacuation request. The request + * includes fields for the required user + * information, such as the user's phone number, as well as their first and last + * name. */ @Getter @Setter @@ -64,7 +65,6 @@ public class EmergencyEvacuationApplicationRequest { @Size(min = 2, max = 100) private String targetDistrict; - @Name @Size(min = 2, max = 100) private String applicantFirstName; @@ -76,22 +76,25 @@ public class EmergencyEvacuationApplicationRequest { @Valid private AysPhoneNumberRequest applicantPhoneNumber; + @NotNull + private Boolean hasObstaclePersonExist; @JsonIgnore @AssertTrue(message = "all applicant fields must be filled") @SuppressWarnings("This method is unused by the application directly but Spring is using it in the background.") private boolean isAllApplicantFieldsFilled() { - if (StringUtils.isEmpty(this.applicantFirstName) && StringUtils.isEmpty(this.applicantLastName) && this.applicantPhoneNumber == null) { + if (StringUtils.isEmpty(this.applicantFirstName) && StringUtils.isEmpty(this.applicantLastName) + && this.applicantPhoneNumber == null) { return true; } - return !StringUtils.isBlank(this.applicantFirstName) && !StringUtils.isBlank(this.applicantLastName) && this.applicantPhoneNumber != null && - !(StringUtils.isBlank(this.applicantPhoneNumber.getCountryCode()) && StringUtils.isBlank(this.applicantPhoneNumber.getLineNumber())); + !(StringUtils.isBlank(this.applicantPhoneNumber.getCountryCode()) + && StringUtils.isBlank(this.applicantPhoneNumber.getLineNumber())); } @JsonIgnore @@ -103,7 +106,8 @@ private boolean isPhoneNumberMustNotBeSameOne() { return true; } - if (StringUtils.isEmpty(this.applicantPhoneNumber.getLineNumber()) || StringUtils.isEmpty(this.phoneNumber.getLineNumber())) { + if (StringUtils.isEmpty(this.applicantPhoneNumber.getLineNumber()) + || StringUtils.isEmpty(this.phoneNumber.getLineNumber())) { return true; } @@ -115,11 +119,12 @@ private boolean isPhoneNumberMustNotBeSameOne() { @SuppressWarnings("This method is unused by the application directly but Spring is using it in the background.") private boolean isSourceCityAndDistrictDifferentFromTargetCityAndDistrict() { - if (this.sourceCity == null || this.sourceDistrict == null || this.targetCity == null || this.targetDistrict == null) { + if (this.sourceCity == null || this.sourceDistrict == null || this.targetCity == null + || this.targetDistrict == null) { return true; - } + } - if (!this.sourceCity.equalsIgnoreCase(this.targetCity)){ + if (!this.sourceCity.equalsIgnoreCase(this.targetCity)) { return true; } diff --git a/src/test/java/org/ays/emergency_application/controller/EmergencyEvacuationApplicationEndToEndTest.java b/src/test/java/org/ays/emergency_application/controller/EmergencyEvacuationApplicationEndToEndTest.java index 4d76feb0b..5cd4f89f0 100644 --- a/src/test/java/org/ays/emergency_application/controller/EmergencyEvacuationApplicationEndToEndTest.java +++ b/src/test/java/org/ays/emergency_application/controller/EmergencyEvacuationApplicationEndToEndTest.java @@ -37,445 +37,461 @@ class EmergencyEvacuationApplicationEndToEndTest extends AysEndToEndTest { - @Autowired - private EmergencyEvacuationApplicationSavePort emergencyEvacuationApplicationSavePort; - - @Autowired - private EmergencyEvacuationApplicationReadPort emergencyEvacuationApplicationReadPort; - - @Autowired - private EmergencyEvacuationApplicationRepository emergencyEvacuationApplicationRepository; - - - private final EmergencyEvacuationApplicationToApplicationResponseMapper emergencyEvacuationApplicationToApplicationResponseMapper = EmergencyEvacuationApplicationToApplicationResponseMapper.initialize(); - - - private static final String BASE_PATH = "/api/v1"; - - - @Test - void givenValidEmergencyEvacuationApplicationListRequest_whenApplicationsFound_thenReturnAysPageResponseOfEmergencyEvacuationApplicationsResponse() throws Exception { - - // Initialize - emergencyEvacuationApplicationRepository.deleteAll(); - - List applications = List.of( - emergencyEvacuationApplicationSavePort.save( - new EmergencyEvacuationApplicationBuilder() + @Autowired + private EmergencyEvacuationApplicationSavePort emergencyEvacuationApplicationSavePort; + + @Autowired + private EmergencyEvacuationApplicationReadPort emergencyEvacuationApplicationReadPort; + + @Autowired + private EmergencyEvacuationApplicationRepository emergencyEvacuationApplicationRepository; + + private final EmergencyEvacuationApplicationToApplicationResponseMapper emergencyEvacuationApplicationToApplicationResponseMapper = EmergencyEvacuationApplicationToApplicationResponseMapper + .initialize(); + + private static final String BASE_PATH = "/api/v1"; + + @Test + void givenValidEmergencyEvacuationApplicationListRequest_whenApplicationsFound_thenReturnAysPageResponseOfEmergencyEvacuationApplicationsResponse() + throws Exception { + + // Initialize + emergencyEvacuationApplicationRepository.deleteAll(); + + List applications = List.of( + emergencyEvacuationApplicationSavePort.save( + new EmergencyEvacuationApplicationBuilder() + .withValidValues() + .withoutId() + .withoutInstitution() + .withStatus(EmergencyEvacuationApplicationStatus.PENDING) + .withoutApplicant() + .build()), + emergencyEvacuationApplicationSavePort.save( + new EmergencyEvacuationApplicationBuilder() + .withValidValues() + .withoutId() + .withInstitution( + new InstitutionBuilder() + .withValidValues() + .withId(AysValidTestData.Admin.INSTITUTION_ID) + .build()) + .withStatus(EmergencyEvacuationApplicationStatus.PENDING) + .withoutApplicant() + .build())); + applications.forEach(application -> emergencyEvacuationApplicationSavePort.save(application)); + + // Given + AysPageable mockAysPageable = new AysPageableBuilder() + .withPage(1) + .withPageSize(10) + .build(); + EmergencyEvacuationApplicationListRequest mockListRequest = new EmergencyEvacuationApplicationListRequestBuilder() .withValidValues() - .withoutId() - .withoutInstitution() - .withStatus(EmergencyEvacuationApplicationStatus.PENDING) - .withoutApplicant() - .build() - ), + .withPageable(mockAysPageable) + .withFilter(null) + .withoutOrders() + .build(); + + // Then + String endpoint = BASE_PATH.concat("/emergency-evacuation-applications"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, adminToken.getAccessToken(), mockListRequest); + + AysResponse> mockResponse = AysResponseBuilder + .successPage(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.contentSize() + .value(applications.size())) + .andExpect(AysMockResultMatchersBuilders.contents("id") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.contents("referenceNumber") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.contents("firstName") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.contents("lastName") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.contents("phoneNumber.countryCode") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.contents("phoneNumber.lineNumber") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.contents("applicantFirstName") + .exists()) + .andExpect(AysMockResultMatchersBuilders.contents("applicantLastName") + .exists()) + .andExpect(AysMockResultMatchersBuilders.contents("applicantPhoneNumber.countryCode") + .exists()) + .andExpect(AysMockResultMatchersBuilders.contents("applicantPhoneNumber.lineNumber") + .exists()) + .andExpect(AysMockResultMatchersBuilders.contents("seatingCount") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.contents("status") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.contents("isInPerson") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.contents("createdAt") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.response("pageNumber") + .value(1)) + .andExpect(AysMockResultMatchersBuilders.response("pageSize") + .value(applications.size())) + .andExpect(AysMockResultMatchersBuilders.response("totalPageCount") + .value(1)) + .andExpect(AysMockResultMatchersBuilders.response("totalElementCount") + .value(applications.size())) + .andExpect(AysMockResultMatchersBuilders.response("orderedBy") + .isEmpty()) + .andExpect(AysMockResultMatchersBuilders.response("filteredBy") + .isNotEmpty()); + } + + @Test + void givenValidEmergencyEvacuationApplicationListRequest_whenEmergencyEvacuationApplicationsNotFound_thenReturnAysPageResponseOfEmergencyEvacuationApplicationsResponse() + throws Exception { + + // Initialize + emergencyEvacuationApplicationRepository.deleteAll(); emergencyEvacuationApplicationSavePort.save( - new EmergencyEvacuationApplicationBuilder() + new EmergencyEvacuationApplicationBuilder() + .withValidValues() + .withoutId() + .withoutInstitution() + .withStatus(EmergencyEvacuationApplicationStatus.PENDING) + .withoutApplicant() + .build()); + + // Given + EmergencyEvacuationApplicationListRequest mockListRequest = new EmergencyEvacuationApplicationListRequestBuilder() .withValidValues() - .withoutId() - .withInstitution( - new InstitutionBuilder() + .withStatuses(Set.of(EmergencyEvacuationApplicationStatus.COMPLETED)) + .withoutOrders() + .build(); + + // Then + String endpoint = BASE_PATH.concat("/emergency-evacuation-applications"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, adminToken.getAccessToken(), mockListRequest); + + AysResponse> mockResponse = AysResponseBuilder + .successPage(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.contentSize() + .value(0)) + .andExpect(AysMockResultMatchersBuilders.contents("id") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("referenceNumber") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("firstName") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("lastName") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("phoneNumber.countryCode") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("phoneNumber.lineNumber") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("applicantFirstName") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("applicantLastName") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("applicantPhoneNumber.countryCode") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("applicantPhoneNumber.lineNumber") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("seatingCount") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("status") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("isInPerson") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("createdAt") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.response("pageNumber") + .value(1)) + .andExpect(AysMockResultMatchersBuilders.response("pageSize") + .value(0)) + .andExpect(AysMockResultMatchersBuilders.response("totalPageCount") + .value(0)) + .andExpect(AysMockResultMatchersBuilders.response("totalElementCount") + .value(0)) + .andExpect(AysMockResultMatchersBuilders.response("orderedBy") + .isEmpty()) + .andExpect(AysMockResultMatchersBuilders.response("filteredBy") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.response("filteredBy.statuses") + .isNotEmpty()); + } + + @Test + void givenValidEmergencyEvacuationApplicationId_whenEmergencyEvacuationApplicationExists_thenReturnEmergencyEvacuationApplicationResponse() + throws Exception { + + // Initialize + emergencyEvacuationApplicationRepository.deleteAll(); + EmergencyEvacuationApplication application = emergencyEvacuationApplicationSavePort.save( + new EmergencyEvacuationApplicationBuilder() .withValidValues() - .withId(AysValidTestData.Admin.INSTITUTION_ID) - .build() - ) - .withStatus(EmergencyEvacuationApplicationStatus.PENDING) + .withoutId() + .withoutInstitution() + .withoutApplicant() + .build()); + + // Given + String applicationId = application.getId(); + + // Then + String endpoint = BASE_PATH.concat("/emergency-evacuation-application/").concat(applicationId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .get(endpoint, adminToken.getAccessToken()); + + EmergencyEvacuationApplicationResponse mockApplicationResponse = emergencyEvacuationApplicationToApplicationResponseMapper + .map(application); + + AysResponse mockResponse = AysResponse + .successOf(mockApplicationResponse); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.response("id") + .value(application.getId())) + .andExpect(AysMockResultMatchersBuilders.response("referenceNumber") + .value(application.getReferenceNumber())) + .andExpect(AysMockResultMatchersBuilders.response("firstName") + .value(application.getFirstName())) + .andExpect(AysMockResultMatchersBuilders.response("lastName") + .value(application.getLastName())) + .andExpect(AysMockResultMatchersBuilders.response("phoneNumber.countryCode") + .value(application.getPhoneNumber().getCountryCode())) + .andExpect(AysMockResultMatchersBuilders.response("phoneNumber.lineNumber") + .value(application.getPhoneNumber().getLineNumber())) + .andExpect(AysMockResultMatchersBuilders.response("sourceCity") + .value(application.getSourceCity())) + .andExpect(AysMockResultMatchersBuilders.response("sourceDistrict") + .value(application.getSourceDistrict())) + .andExpect(AysMockResultMatchersBuilders.response("address") + .value(application.getAddress())) + .andExpect(AysMockResultMatchersBuilders.response("seatingCount") + .value(application.getSeatingCount())) + .andExpect(AysMockResultMatchersBuilders.response("targetCity") + .value(application.getTargetCity())) + .andExpect(AysMockResultMatchersBuilders.response("targetDistrict") + .value(application.getTargetDistrict())) + .andExpect(AysMockResultMatchersBuilders.response("status") + .value(application.getStatus().toString())) + .andExpect(AysMockResultMatchersBuilders.response("isInPerson") + .value(application.getIsInPerson())) + .andExpect(AysMockResultMatchersBuilders.response("hasObstaclePersonExist") + .value(application.getHasObstaclePersonExist())) + .andExpect(AysMockResultMatchersBuilders.response("notes") + .value(application.getNotes())) + .andExpect(AysMockResultMatchersBuilders.response("createdUser") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.response("createdAt") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.response("updatedUser") + .isEmpty()) + .andExpect(AysMockResultMatchersBuilders.response("updatedAt") + .isEmpty()); + } + + @Test + void givenValidEmergencyEvacuationApplicationRequest_whenApplicationSaved_thenReturnSuccessResponse() + throws Exception { + // Given + String firstName = "Test Application with Applicant"; + EmergencyEvacuationApplicationRequest applicationRequest = new EmergencyEvacuationRequestBuilder() + .withValidValues() + .withFirstName(firstName) + .withHasObstaclePersonExist(false) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/emergency-evacuation-application"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, applicationRequest); + + AysResponse mockResponse = AysResponseBuilder.success(); + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.isSuccess() + .value(true)); + + // Verify + AysPageable pageable = AysPageable.builder() + .page(1) + .pageSize(1000) + .build(); + Optional application = emergencyEvacuationApplicationReadPort + .findAll(pageable, null) + .getContent().stream() + .filter(evacuationApplication -> evacuationApplication.getFirstName() + .equals(applicationRequest.getFirstName())) + .findFirst(); + + Assertions.assertTrue(application.isPresent()); + Assertions.assertNull(application.get().getInstitution()); + Assertions.assertNotNull(application.get().getReferenceNumber()); + Assertions.assertEquals(10, application.get().getReferenceNumber().length()); + Assertions.assertEquals(application.get().getFirstName(), firstName); + Assertions.assertEquals(application.get().getLastName(), applicationRequest.getLastName()); + Assertions.assertEquals(application.get().getPhoneNumber().getCountryCode(), + applicationRequest.getPhoneNumber().getCountryCode()); + Assertions.assertEquals(application.get().getPhoneNumber().getLineNumber(), + applicationRequest.getPhoneNumber().getLineNumber()); + Assertions.assertEquals(application.get().getSourceCity(), applicationRequest.getSourceCity()); + Assertions.assertEquals(application.get().getSourceDistrict(), applicationRequest.getSourceDistrict()); + Assertions.assertEquals(application.get().getAddress(), applicationRequest.getAddress()); + Assertions.assertEquals(application.get().getSeatingCount(), applicationRequest.getSeatingCount()); + Assertions.assertEquals(application.get().getTargetCity(), applicationRequest.getTargetCity()); + Assertions.assertEquals(application.get().getTargetDistrict(), applicationRequest.getTargetDistrict()); + Assertions.assertEquals(EmergencyEvacuationApplicationStatus.PENDING, application.get().getStatus()); + Assertions.assertEquals(application.get().getApplicantFirstName(), + applicationRequest.getApplicantFirstName()); + Assertions.assertEquals(application.get().getApplicantLastName(), + applicationRequest.getApplicantLastName()); + Assertions.assertEquals(application.get().getApplicantPhoneNumber().getCountryCode(), + applicationRequest.getApplicantPhoneNumber().getCountryCode()); + Assertions.assertEquals(application.get().getApplicantPhoneNumber().getLineNumber(), + applicationRequest.getApplicantPhoneNumber().getLineNumber()); + Assertions.assertFalse(application.get().getIsInPerson()); + Assertions.assertFalse(application.get().getHasObstaclePersonExist()); + Assertions.assertNull(application.get().getNotes()); + Assertions.assertEquals("AYS", application.get().getCreatedUser()); + } + + @Test + void givenValidEmergencyEvacuationApplicationRequestWithoutApplicant_whenApplicationSaved_thenReturnSuccessResponse() + throws Exception { + // Given + String firstName = "Test Application without Applicant"; + EmergencyEvacuationApplicationRequest applicationRequest = new EmergencyEvacuationRequestBuilder() + .withValidValues() + .withFirstName(firstName) .withoutApplicant() - .build() - ) - ); - applications.forEach(application -> emergencyEvacuationApplicationSavePort.save(application)); - - // Given - AysPageable mockAysPageable = new AysPageableBuilder() - .withPage(1) - .withPageSize(10) - .build(); - EmergencyEvacuationApplicationListRequest mockListRequest = new EmergencyEvacuationApplicationListRequestBuilder() - .withValidValues() - .withPageable(mockAysPageable) - .withFilter(null) - .withoutOrders() - .build(); - - // Then - String endpoint = BASE_PATH.concat("/emergency-evacuation-applications"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, adminToken.getAccessToken(), mockListRequest); - - AysResponse> mockResponse = AysResponseBuilder.successPage(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.contentSize() - .value(applications.size())) - .andExpect(AysMockResultMatchersBuilders.contents("id") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.contents("referenceNumber") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.contents("firstName") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.contents("lastName") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.contents("phoneNumber.countryCode") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.contents("phoneNumber.lineNumber") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.contents("applicantFirstName") - .exists()) - .andExpect(AysMockResultMatchersBuilders.contents("applicantLastName") - .exists()) - .andExpect(AysMockResultMatchersBuilders.contents("applicantPhoneNumber.countryCode") - .exists()) - .andExpect(AysMockResultMatchersBuilders.contents("applicantPhoneNumber.lineNumber") - .exists()) - .andExpect(AysMockResultMatchersBuilders.contents("seatingCount") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.contents("status") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.contents("isInPerson") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.contents("createdAt") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.response("pageNumber") - .value(1)) - .andExpect(AysMockResultMatchersBuilders.response("pageSize") - .value(applications.size())) - .andExpect(AysMockResultMatchersBuilders.response("totalPageCount") - .value(1)) - .andExpect(AysMockResultMatchersBuilders.response("totalElementCount") - .value(applications.size())) - .andExpect(AysMockResultMatchersBuilders.response("orderedBy") - .isEmpty()) - .andExpect(AysMockResultMatchersBuilders.response("filteredBy") - .isNotEmpty()); - } - - @Test - void givenValidEmergencyEvacuationApplicationListRequest_whenEmergencyEvacuationApplicationsNotFound_thenReturnAysPageResponseOfEmergencyEvacuationApplicationsResponse() throws Exception { - - // Initialize - emergencyEvacuationApplicationRepository.deleteAll(); - emergencyEvacuationApplicationSavePort.save( - new EmergencyEvacuationApplicationBuilder() - .withValidValues() - .withoutId() - .withoutInstitution() - .withStatus(EmergencyEvacuationApplicationStatus.PENDING) - .withoutApplicant() - .build() - ); - - // Given - EmergencyEvacuationApplicationListRequest mockListRequest = new EmergencyEvacuationApplicationListRequestBuilder() - .withValidValues() - .withStatuses(Set.of(EmergencyEvacuationApplicationStatus.COMPLETED)) - .withoutOrders() - .build(); - - // Then - String endpoint = BASE_PATH.concat("/emergency-evacuation-applications"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, adminToken.getAccessToken(), mockListRequest); - - AysResponse> mockResponse = AysResponseBuilder.successPage(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.contentSize() - .value(0)) - .andExpect(AysMockResultMatchersBuilders.contents("id") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("referenceNumber") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("firstName") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("lastName") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("phoneNumber.countryCode") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("phoneNumber.lineNumber") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("applicantFirstName") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("applicantLastName") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("applicantPhoneNumber.countryCode") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("applicantPhoneNumber.lineNumber") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("seatingCount") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("status") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("isInPerson") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("createdAt") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.response("pageNumber") - .value(1)) - .andExpect(AysMockResultMatchersBuilders.response("pageSize") - .value(0)) - .andExpect(AysMockResultMatchersBuilders.response("totalPageCount") - .value(0)) - .andExpect(AysMockResultMatchersBuilders.response("totalElementCount") - .value(0)) - .andExpect(AysMockResultMatchersBuilders.response("orderedBy") - .isEmpty()) - .andExpect(AysMockResultMatchersBuilders.response("filteredBy") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.response("filteredBy.statuses") - .isNotEmpty()); - } - - @Test - void givenValidEmergencyEvacuationApplicationId_whenEmergencyEvacuationApplicationExists_thenReturnEmergencyEvacuationApplicationResponse() throws Exception { - - // Initialize - emergencyEvacuationApplicationRepository.deleteAll(); - EmergencyEvacuationApplication application = emergencyEvacuationApplicationSavePort.save( - new EmergencyEvacuationApplicationBuilder() - .withValidValues() - .withoutId() - .withoutInstitution() - .withoutApplicant() - .build() - ); - - // Given - String applicationId = application.getId(); - - // Then - String endpoint = BASE_PATH.concat("/emergency-evacuation-application/").concat(applicationId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .get(endpoint, adminToken.getAccessToken()); - - EmergencyEvacuationApplicationResponse mockApplicationResponse = emergencyEvacuationApplicationToApplicationResponseMapper - .map(application); - - AysResponse mockResponse = AysResponse - .successOf(mockApplicationResponse); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.response("id") - .value(application.getId())) - .andExpect(AysMockResultMatchersBuilders.response("referenceNumber") - .value(application.getReferenceNumber())) - .andExpect(AysMockResultMatchersBuilders.response("firstName") - .value(application.getFirstName())) - .andExpect(AysMockResultMatchersBuilders.response("lastName") - .value(application.getLastName())) - .andExpect(AysMockResultMatchersBuilders.response("phoneNumber.countryCode") - .value(application.getPhoneNumber().getCountryCode())) - .andExpect(AysMockResultMatchersBuilders.response("phoneNumber.lineNumber") - .value(application.getPhoneNumber().getLineNumber())) - .andExpect(AysMockResultMatchersBuilders.response("sourceCity") - .value(application.getSourceCity())) - .andExpect(AysMockResultMatchersBuilders.response("sourceDistrict") - .value(application.getSourceDistrict())) - .andExpect(AysMockResultMatchersBuilders.response("address") - .value(application.getAddress())) - .andExpect(AysMockResultMatchersBuilders.response("seatingCount") - .value(application.getSeatingCount())) - .andExpect(AysMockResultMatchersBuilders.response("targetCity") - .value(application.getTargetCity())) - .andExpect(AysMockResultMatchersBuilders.response("targetDistrict") - .value(application.getTargetDistrict())) - .andExpect(AysMockResultMatchersBuilders.response("status") - .value(application.getStatus().toString())) - .andExpect(AysMockResultMatchersBuilders.response("isInPerson") - .value(application.getIsInPerson())) - .andExpect(AysMockResultMatchersBuilders.response("hasObstaclePersonExist") - .value(application.getHasObstaclePersonExist())) - .andExpect(AysMockResultMatchersBuilders.response("notes") - .value(application.getNotes())) - .andExpect(AysMockResultMatchersBuilders.response("createdUser") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.response("createdAt") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.response("updatedUser") - .isEmpty()) - .andExpect(AysMockResultMatchersBuilders.response("updatedAt") - .isEmpty()); - } - - - @Test - void givenValidEmergencyEvacuationApplicationRequest_whenApplicationSaved_thenReturnSuccessResponse() throws Exception { - // Given - String firstName = "Test Application with Applicant"; - EmergencyEvacuationApplicationRequest applicationRequest = new EmergencyEvacuationRequestBuilder() - .withValidValues() - .withFirstName(firstName) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/emergency-evacuation-application"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, applicationRequest); - - AysResponse mockResponse = AysResponseBuilder.success(); - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .doesNotExist()); - - // Verify - AysPageable pageable = AysPageable.builder() - .page(1) - .pageSize(1000) - .build(); - Optional application = emergencyEvacuationApplicationReadPort - .findAll(pageable, null) - .getContent().stream() - .filter(evacuationApplication -> evacuationApplication.getFirstName().equals(applicationRequest.getFirstName())) - .findFirst(); - - Assertions.assertTrue(application.isPresent()); - Assertions.assertNull(application.get().getInstitution()); - Assertions.assertNotNull(application.get().getReferenceNumber()); - Assertions.assertEquals(10, application.get().getReferenceNumber().length()); - Assertions.assertEquals(application.get().getFirstName(), firstName); - Assertions.assertEquals(application.get().getLastName(), applicationRequest.getLastName()); - Assertions.assertEquals(application.get().getPhoneNumber().getCountryCode(), applicationRequest.getPhoneNumber().getCountryCode()); - Assertions.assertEquals(application.get().getPhoneNumber().getLineNumber(), applicationRequest.getPhoneNumber().getLineNumber()); - Assertions.assertEquals(application.get().getSourceCity(), applicationRequest.getSourceCity()); - Assertions.assertEquals(application.get().getSourceDistrict(), applicationRequest.getSourceDistrict()); - Assertions.assertEquals(application.get().getAddress(), applicationRequest.getAddress()); - Assertions.assertEquals(application.get().getSeatingCount(), applicationRequest.getSeatingCount()); - Assertions.assertEquals(application.get().getTargetCity(), applicationRequest.getTargetCity()); - Assertions.assertEquals(application.get().getTargetDistrict(), applicationRequest.getTargetDistrict()); - Assertions.assertEquals(EmergencyEvacuationApplicationStatus.PENDING, application.get().getStatus()); - Assertions.assertEquals(application.get().getApplicantFirstName(), applicationRequest.getApplicantFirstName()); - Assertions.assertEquals(application.get().getApplicantLastName(), applicationRequest.getApplicantLastName()); - Assertions.assertEquals(application.get().getApplicantPhoneNumber().getCountryCode(), applicationRequest.getApplicantPhoneNumber().getCountryCode()); - Assertions.assertEquals(application.get().getApplicantPhoneNumber().getLineNumber(), applicationRequest.getApplicantPhoneNumber().getLineNumber()); - Assertions.assertFalse(application.get().getIsInPerson()); - Assertions.assertNull(application.get().getHasObstaclePersonExist()); - Assertions.assertNull(application.get().getNotes()); - Assertions.assertEquals("AYS", application.get().getCreatedUser()); - } - - @Test - void givenValidEmergencyEvacuationApplicationRequestWithoutApplicant_whenApplicationSaved_thenReturnSuccessResponse() throws Exception { - // Given - String firstName = "Test Application without Applicant"; - EmergencyEvacuationApplicationRequest applicationRequest = new EmergencyEvacuationRequestBuilder() - .withValidValues() - .withFirstName(firstName) - .withoutApplicant() - .build(); - - // Then - String endpoint = BASE_PATH.concat("/emergency-evacuation-application"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, applicationRequest); - - AysResponse mockResponse = AysResponseBuilder.success(); - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .doesNotExist()); - - // Verify - AysPageable pageable = AysPageable.builder() - .page(1) - .pageSize(1000) - .build(); - Optional application = emergencyEvacuationApplicationReadPort - .findAll(pageable, null) - .getContent().stream() - .filter(evacuationApplication -> evacuationApplication.getFirstName().equals(applicationRequest.getFirstName())) - .findFirst(); - - Assertions.assertTrue(application.isPresent()); - Assertions.assertNull(application.get().getInstitution()); - Assertions.assertNotNull(application.get().getReferenceNumber()); - Assertions.assertEquals(10, application.get().getReferenceNumber().length()); - Assertions.assertEquals(application.get().getFirstName(), firstName); - Assertions.assertEquals(application.get().getLastName(), applicationRequest.getLastName()); - Assertions.assertEquals(application.get().getPhoneNumber().getCountryCode(), applicationRequest.getPhoneNumber().getCountryCode()); - Assertions.assertEquals(application.get().getPhoneNumber().getLineNumber(), applicationRequest.getPhoneNumber().getLineNumber()); - Assertions.assertEquals(application.get().getSourceCity(), applicationRequest.getSourceCity()); - Assertions.assertEquals(application.get().getSourceDistrict(), applicationRequest.getSourceDistrict()); - Assertions.assertEquals(application.get().getAddress(), applicationRequest.getAddress()); - Assertions.assertEquals(application.get().getSeatingCount(), applicationRequest.getSeatingCount()); - Assertions.assertEquals(application.get().getTargetCity(), applicationRequest.getTargetCity()); - Assertions.assertEquals(application.get().getTargetDistrict(), applicationRequest.getTargetDistrict()); - Assertions.assertEquals(EmergencyEvacuationApplicationStatus.PENDING, application.get().getStatus()); - Assertions.assertEquals(application.get().getApplicantFirstName(), applicationRequest.getApplicantFirstName()); - Assertions.assertEquals(application.get().getApplicantLastName(), applicationRequest.getApplicantLastName()); - Assertions.assertNull(application.get().getApplicantPhoneNumber().getCountryCode()); - Assertions.assertNull(application.get().getApplicantPhoneNumber().getLineNumber()); - Assertions.assertTrue(application.get().getIsInPerson()); - Assertions.assertNull(application.get().getHasObstaclePersonExist()); - Assertions.assertNull(application.get().getNotes()); - Assertions.assertEquals("AYS", application.get().getCreatedUser()); - } - - - @Test - void givenValidIdAndValidUpdateRequest_whenApplicationUpdated_thenReturnSuccessResponse() throws Exception { - - // Initialize - emergencyEvacuationApplicationRepository.deleteAll(); - EmergencyEvacuationApplication application = emergencyEvacuationApplicationSavePort.save( - new EmergencyEvacuationApplicationBuilder() - .withValidValues() - .withoutId() - .withoutInstitution() - .withoutApplicant() - .withSeatingCount(5) - .withStatus(EmergencyEvacuationApplicationStatus.PENDING) - .withoutHasObstaclePersonExist() - .withoutNotes() - .build() - ); - - // Given - String id = application.getId(); - EmergencyEvacuationApplicationUpdateRequest updateRequest = new EmergencyEvacuationApplicationUpdateRequestBuilder() - .withValidValues() - .build(); - - // Then - String endpoint = BASE_PATH.concat("/emergency-evacuation-application/").concat(id); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .put(endpoint, adminToken.getAccessToken(), updateRequest); - - AysResponse mockResponse = AysResponseBuilder.success(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(MockMvcResultMatchers.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()); - - // Verify - Optional applicationFromDatabase = emergencyEvacuationApplicationReadPort - .findById(id); - - Assertions.assertTrue(applicationFromDatabase.isPresent()); - Assertions.assertNotNull(applicationFromDatabase.get().getInstitution()); - Assertions.assertEquals(applicationFromDatabase.get().getSeatingCount(), updateRequest.getSeatingCount()); - Assertions.assertEquals(applicationFromDatabase.get().getHasObstaclePersonExist(), updateRequest.getHasObstaclePersonExist()); - Assertions.assertEquals(applicationFromDatabase.get().getStatus(), updateRequest.getStatus()); - Assertions.assertEquals(applicationFromDatabase.get().getNotes(), updateRequest.getNotes()); - Assertions.assertNotNull(applicationFromDatabase.get().getUpdatedUser()); - Assertions.assertNotNull(applicationFromDatabase.get().getUpdatedAt()); - Assertions.assertTrue(UUIDTestUtil.isValid(applicationFromDatabase.get().getUpdatedUser())); - } + .withHasObstaclePersonExist(false) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/emergency-evacuation-application"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, applicationRequest); + + AysResponse mockResponse = AysResponseBuilder.success(); + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.isSuccess() + .value(true)); + + // Verify + AysPageable pageable = AysPageable.builder() + .page(1) + .pageSize(1000) + .build(); + Optional application = emergencyEvacuationApplicationReadPort + .findAll(pageable, null) + .getContent().stream() + .filter(evacuationApplication -> evacuationApplication.getFirstName() + .equals(applicationRequest.getFirstName())) + .findFirst(); + + Assertions.assertTrue(application.isPresent()); + Assertions.assertNull(application.get().getInstitution()); + Assertions.assertNotNull(application.get().getReferenceNumber()); + Assertions.assertEquals(10, application.get().getReferenceNumber().length()); + Assertions.assertEquals(application.get().getFirstName(), firstName); + Assertions.assertEquals(application.get().getLastName(), applicationRequest.getLastName()); + Assertions.assertEquals(application.get().getPhoneNumber().getCountryCode(), + applicationRequest.getPhoneNumber().getCountryCode()); + Assertions.assertEquals(application.get().getPhoneNumber().getLineNumber(), + applicationRequest.getPhoneNumber().getLineNumber()); + Assertions.assertEquals(application.get().getSourceCity(), applicationRequest.getSourceCity()); + Assertions.assertEquals(application.get().getSourceDistrict(), applicationRequest.getSourceDistrict()); + Assertions.assertEquals(application.get().getAddress(), applicationRequest.getAddress()); + Assertions.assertEquals(application.get().getSeatingCount(), applicationRequest.getSeatingCount()); + Assertions.assertEquals(application.get().getTargetCity(), applicationRequest.getTargetCity()); + Assertions.assertEquals(application.get().getTargetDistrict(), applicationRequest.getTargetDistrict()); + Assertions.assertEquals(EmergencyEvacuationApplicationStatus.PENDING, application.get().getStatus()); + Assertions.assertEquals(application.get().getApplicantFirstName(), + applicationRequest.getApplicantFirstName()); + Assertions.assertEquals(application.get().getApplicantLastName(), + applicationRequest.getApplicantLastName()); + Assertions.assertNull(application.get().getApplicantPhoneNumber().getCountryCode()); + Assertions.assertNull(application.get().getApplicantPhoneNumber().getLineNumber()); + Assertions.assertTrue(application.get().getIsInPerson()); + Assertions.assertFalse(application.get().getHasObstaclePersonExist()); + Assertions.assertNull(application.get().getNotes()); + Assertions.assertEquals("AYS", application.get().getCreatedUser()); + } + + @Test + void givenValidIdAndValidUpdateRequest_whenApplicationUpdated_thenReturnSuccessResponse() throws Exception { + + // Initialize + emergencyEvacuationApplicationRepository.deleteAll(); + EmergencyEvacuationApplication application = emergencyEvacuationApplicationSavePort.save( + new EmergencyEvacuationApplicationBuilder() + .withValidValues() + .withoutId() + .withoutInstitution() + .withoutApplicant() + .withSeatingCount(5) + .withStatus(EmergencyEvacuationApplicationStatus.PENDING) + .withHasObstaclePersonExist(false) + .withoutNotes() + .build()); + + // Given + String id = application.getId(); + EmergencyEvacuationApplicationUpdateRequest updateRequest = new EmergencyEvacuationApplicationUpdateRequestBuilder() + .withValidValues() + .build(); + + // Then + String endpoint = BASE_PATH.concat("/emergency-evacuation-application/").concat(id); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .put(endpoint, adminToken.getAccessToken(), updateRequest); + + AysResponse mockResponse = AysResponseBuilder.success(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(MockMvcResultMatchers.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Optional applicationFromDatabase = emergencyEvacuationApplicationReadPort + .findById(id); + + Assertions.assertTrue(applicationFromDatabase.isPresent()); + Assertions.assertNotNull(applicationFromDatabase.get().getInstitution()); + Assertions.assertEquals(applicationFromDatabase.get().getSeatingCount(), + updateRequest.getSeatingCount()); + Assertions.assertEquals(applicationFromDatabase.get().getHasObstaclePersonExist(), + updateRequest.getHasObstaclePersonExist()); + Assertions.assertEquals(applicationFromDatabase.get().getStatus(), updateRequest.getStatus()); + Assertions.assertEquals(applicationFromDatabase.get().getNotes(), updateRequest.getNotes()); + Assertions.assertNotNull(applicationFromDatabase.get().getUpdatedUser()); + Assertions.assertNotNull(applicationFromDatabase.get().getUpdatedAt()); + Assertions.assertTrue(UUIDTestUtil.isValid(applicationFromDatabase.get().getUpdatedUser())); + } } diff --git a/src/test/java/org/ays/emergency_application/model/EmergencyEvacuationApplicationBuilder.java b/src/test/java/org/ays/emergency_application/model/EmergencyEvacuationApplicationBuilder.java index df3187f12..5bd31e2d4 100644 --- a/src/test/java/org/ays/emergency_application/model/EmergencyEvacuationApplicationBuilder.java +++ b/src/test/java/org/ays/emergency_application/model/EmergencyEvacuationApplicationBuilder.java @@ -58,7 +58,7 @@ public EmergencyEvacuationApplicationBuilder withHasObstaclePersonExist(Boolean } public EmergencyEvacuationApplicationBuilder withoutHasObstaclePersonExist() { - data.setHasObstaclePersonExist(null); + data.setHasObstaclePersonExist(false); return this; } diff --git a/src/test/java/org/ays/emergency_application/model/request/EmergencyEvacuationRequestBuilder.java b/src/test/java/org/ays/emergency_application/model/request/EmergencyEvacuationRequestBuilder.java index 4a280fe1b..d657b7b99 100644 --- a/src/test/java/org/ays/emergency_application/model/request/EmergencyEvacuationRequestBuilder.java +++ b/src/test/java/org/ays/emergency_application/model/request/EmergencyEvacuationRequestBuilder.java @@ -12,10 +12,17 @@ public EmergencyEvacuationRequestBuilder() { public EmergencyEvacuationRequestBuilder withValidValues() { return this - .withSeatingCount(1) + .withFirstName("Test") + .withLastName("User") .withPhoneNumber(new AysPhoneNumberRequestBuilder().withValidValues().build()) + .withSourceCity("Source City") + .withSourceDistrict("Source District") + .withAddress("Lorem Ipsum is simply dummy text of the printing and typesetting industry.") + .withSeatingCount(1) + .withTargetCity("Target City") + .withTargetDistrict("Target District") .withApplicantPhoneNumber(new AysPhoneNumberRequestBuilder().withValidValues().build()) - .withAddress("Lorem Ipsum is simply dummy text of the printing and typesetting industry."); + .withHasObstaclePersonExist(false); } public EmergencyEvacuationRequestBuilder withFirstName(String firstName) { @@ -85,4 +92,9 @@ public EmergencyEvacuationRequestBuilder withoutApplicant() { return this; } + public EmergencyEvacuationRequestBuilder withHasObstaclePersonExist(Boolean hasObstaclePersonExist) { + data.setHasObstaclePersonExist(hasObstaclePersonExist); + return this; + } + } From 6e5e703b6bf1dffbc5ab6a325a4a7e8d51a91e43 Mon Sep 17 00:00:00 2001 From: m1erla <116915007+m1erla@users.noreply.github.com> Date: Thu, 16 Jan 2025 22:16:29 +0300 Subject: [PATCH 4/8] AYS-661 | Refactor: revert formatting changes in test files and revert @Size annotations on emailAddress. Added some invalid email value. --- ...egistrationApplicationCompleteRequest.java | 4 +- .../request/AysPasswordForgotRequest.java | 1 - .../model/request/AysUserCreateRequest.java | 10 +- .../model/request/AysUserUpdateRequest.java | 4 +- .../validation/EmailAddressValidator.java | 35 +- .../model/EmergencyEvacuationApplication.java | 9 +- .../EmergencyEvacuationApplicationEntity.java | 4 +- ...EmergencyEvacuationApplicationRequest.java | 1 - .../db/changelog/changes/1-ays-ddl.xml | 4 +- .../java/org/ays/AysRestControllerTest.java | 193 +- ...RegistrationApplicationControllerTest.java | 1603 +++++++------ .../controller/AysAuthControllerTest.java | 948 ++++---- .../controller/AysUserControllerTest.java | 2001 ++++++++--------- ...encyEvacuationApplicationEndToEndTest.java | 886 ++++---- ...EmergencyEvacuationApplicationBuilder.java | 2 +- .../EmergencyEvacuationRequestBuilder.java | 15 +- 16 files changed, 2770 insertions(+), 2950 deletions(-) diff --git a/src/main/java/org/ays/auth/model/request/AdminRegistrationApplicationCompleteRequest.java b/src/main/java/org/ays/auth/model/request/AdminRegistrationApplicationCompleteRequest.java index aed27e2f7..ad4ca27f4 100644 --- a/src/main/java/org/ays/auth/model/request/AdminRegistrationApplicationCompleteRequest.java +++ b/src/main/java/org/ays/auth/model/request/AdminRegistrationApplicationCompleteRequest.java @@ -15,8 +15,7 @@ * Represents a complete registration request for an admin user. * *

- * Encapsulates the necessary information for completing the admin registration - * process. + * Encapsulates the necessary information for completing the admin registration process. *

*/ @Getter @@ -40,7 +39,6 @@ public class AdminRegistrationApplicationCompleteRequest { @EmailAddress @NotBlank - @Size(min = 6, max = 254) private String emailAddress; @NotBlank diff --git a/src/main/java/org/ays/auth/model/request/AysPasswordForgotRequest.java b/src/main/java/org/ays/auth/model/request/AysPasswordForgotRequest.java index 1f8002d24..b17ca3549 100644 --- a/src/main/java/org/ays/auth/model/request/AysPasswordForgotRequest.java +++ b/src/main/java/org/ays/auth/model/request/AysPasswordForgotRequest.java @@ -12,7 +12,6 @@ public class AysPasswordForgotRequest { @EmailAddress @NotBlank - @Size(min = 6, max = 254) private String emailAddress; } diff --git a/src/main/java/org/ays/auth/model/request/AysUserCreateRequest.java b/src/main/java/org/ays/auth/model/request/AysUserCreateRequest.java index abd378e6c..572d0bcfd 100644 --- a/src/main/java/org/ays/auth/model/request/AysUserCreateRequest.java +++ b/src/main/java/org/ays/auth/model/request/AysUserCreateRequest.java @@ -16,13 +16,10 @@ /** * Represents a request for creating a new user in the system. - * This class contains all the necessary details required to create a user, - * including their personal information, - * contact details, and roles. The fields are validated to ensure they meet the - * required constraints. + * This class contains all the necessary details required to create a user, including their personal information, + * contact details, and roles. The fields are validated to ensure they meet the required constraints. *

- * This class uses various annotations for validation to ensure the data - * integrity and consistency. + * This class uses various annotations for validation to ensure the data integrity and consistency. *

*/ @Getter @@ -41,7 +38,6 @@ public class AysUserCreateRequest { @EmailAddress @NotBlank - @Size(min = 6, max = 254) private String emailAddress; @Valid diff --git a/src/main/java/org/ays/auth/model/request/AysUserUpdateRequest.java b/src/main/java/org/ays/auth/model/request/AysUserUpdateRequest.java index f260764b5..997e87eeb 100644 --- a/src/main/java/org/ays/auth/model/request/AysUserUpdateRequest.java +++ b/src/main/java/org/ays/auth/model/request/AysUserUpdateRequest.java @@ -17,8 +17,7 @@ /** * Request object for updating a user's details. *

- * This class encapsulates the data required to update an existing user's - * details. + * This class encapsulates the data required to update an existing user's details. * Each field is validated to ensure it meets the necessary requirements. */ @Getter @@ -37,7 +36,6 @@ public class AysUserUpdateRequest { @EmailAddress @NotBlank - @Size(min = 6, max = 254) private String emailAddress; @Valid diff --git a/src/main/java/org/ays/common/util/validation/EmailAddressValidator.java b/src/main/java/org/ays/common/util/validation/EmailAddressValidator.java index 4ba29f201..234d1f9e9 100644 --- a/src/main/java/org/ays/common/util/validation/EmailAddressValidator.java +++ b/src/main/java/org/ays/common/util/validation/EmailAddressValidator.java @@ -11,28 +11,26 @@ */ class EmailAddressValidator implements ConstraintValidator { - private static final String EMAIL_REGEX = "^(?!.*\\.\\.|.*--|.*-@|.*@\\.|.*\\.-|.*-\\.).+[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"; + private static final String EMAIL_REGEX = + "^(?!.*\\.\\.|.*--|.*-@|.*@\\.|.*\\.-|.*-\\.).+[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"; /** * Checks whether the given value is a valid email or not. - *

- * Some valid emails are: - *

+ *

Some valid emails are:

*
    - *
  • user@example.com
  • - *
  • john.doe123@example.co.uk
  • - *
  • admin_123@example.org
  • + *
  • user@example.com
  • + *
  • john.doe123@example.co.uk
  • + *
  • admin_123@example.org
  • *
* - *

- * Some invalid emails are: - *

+ *

Some invalid emails are:

*
    - *
  • user@invalid
  • - *
  • user@invalid!.com
  • - *
  • u@ser@.com
  • - *
  • user@..com
  • - *
  • user
  • + *
  • user@invalid
  • + *
  • user@invalid!.com
  • + *
  • u@ser@.com
  • + *
  • user@..com
  • + *
  • user
  • + *
  • a@b.c
  • *
* * @param email object to validate @@ -45,10 +43,6 @@ public boolean isValid(String email, ConstraintValidatorContext constraintValida return true; } - if (email.length() < 6 || email.length() > 254) { - return this.buildViolation(constraintValidatorContext, "email length must be between 6 and 254 characters"); - } - if (email.startsWith(" ") || email.endsWith(" ")) { return this.buildViolation(constraintValidatorContext, "email must not start or end with whitespace"); } @@ -63,8 +57,7 @@ public boolean isValid(String email, ConstraintValidatorContext constraintValida String[] parts = email.split("@", 2); if (parts[0].isEmpty() || !Character.isLetterOrDigit(parts[0].charAt(0))) { - return this.buildViolation(constraintValidatorContext, - "email local part must start with a letter or number"); + return this.buildViolation(constraintValidatorContext, "email local part must start with a letter or number"); } String domainPart = parts[1]; diff --git a/src/main/java/org/ays/emergency_application/model/EmergencyEvacuationApplication.java b/src/main/java/org/ays/emergency_application/model/EmergencyEvacuationApplication.java index d36304852..b6f883bbb 100644 --- a/src/main/java/org/ays/emergency_application/model/EmergencyEvacuationApplication.java +++ b/src/main/java/org/ays/emergency_application/model/EmergencyEvacuationApplication.java @@ -12,7 +12,8 @@ /** * Represents an emergency evacuation application. - * Extends {@link BaseDomainModel} and includes details such as personal and contact information, + * Extends {@link BaseDomainModel} and includes details such as personal and + * contact information, * location details, and application status. */ @Getter @@ -42,7 +43,6 @@ public class EmergencyEvacuationApplication extends BaseDomainModel { private Institution institution; - /** * Checks if the application does not have an associated institution. * @@ -62,7 +62,6 @@ public boolean isInstitutionOwner(final String institutionId) { return this.institution.getId().equals(institutionId); } - /** * Marks the emergency evacuation application as pending. * Generates a reference number and updates the status to pending. @@ -72,12 +71,14 @@ public void pending() { this.referenceNumber = AysRandomUtil.generateNumber(10).toString(); this.status = EmergencyEvacuationApplicationStatus.PENDING; this.isInPerson = this.applicantPhoneNumber == null; + this.hasObstaclePersonExist = null; } /** * Sets the institution ID for the application. * - * @param institutionId the ID of the institution to associate with the application. + * @param institutionId the ID of the institution to associate with the + * application. */ public void setInstitutionId(final String institutionId) { this.institution = Institution.builder() diff --git a/src/main/java/org/ays/emergency_application/model/entity/EmergencyEvacuationApplicationEntity.java b/src/main/java/org/ays/emergency_application/model/entity/EmergencyEvacuationApplicationEntity.java index 85245991f..cb3defe63 100644 --- a/src/main/java/org/ays/emergency_application/model/entity/EmergencyEvacuationApplicationEntity.java +++ b/src/main/java/org/ays/emergency_application/model/entity/EmergencyEvacuationApplicationEntity.java @@ -93,8 +93,8 @@ public class EmergencyEvacuationApplicationEntity extends BaseEntity { @Column(name = "IS_IN_PERSON") private Boolean isInPerson; - @Column(name = "HAS_OBSTACLE_PERSON_EXIST", nullable = false) - private Boolean hasObstaclePersonExist = false; + @Column(name = "HAS_OBSTACLE_PERSON_EXIST") + private Boolean hasObstaclePersonExist; @Column(name = "NOTES") private String notes; diff --git a/src/main/java/org/ays/emergency_application/model/request/EmergencyEvacuationApplicationRequest.java b/src/main/java/org/ays/emergency_application/model/request/EmergencyEvacuationApplicationRequest.java index c0f55acd3..00cbf2d58 100644 --- a/src/main/java/org/ays/emergency_application/model/request/EmergencyEvacuationApplicationRequest.java +++ b/src/main/java/org/ays/emergency_application/model/request/EmergencyEvacuationApplicationRequest.java @@ -76,7 +76,6 @@ public class EmergencyEvacuationApplicationRequest { @Valid private AysPhoneNumberRequest applicantPhoneNumber; - @NotNull private Boolean hasObstaclePersonExist; @JsonIgnore diff --git a/src/main/resources/db/changelog/changes/1-ays-ddl.xml b/src/main/resources/db/changelog/changes/1-ays-ddl.xml index e188520c1..9f4b0ab8d 100644 --- a/src/main/resources/db/changelog/changes/1-ays-ddl.xml +++ b/src/main/resources/db/changelog/changes/1-ays-ddl.xml @@ -144,9 +144,7 @@ - - - + diff --git a/src/test/java/org/ays/AysRestControllerTest.java b/src/test/java/org/ays/AysRestControllerTest.java index 515d7e790..7c434989b 100644 --- a/src/test/java/org/ays/AysRestControllerTest.java +++ b/src/test/java/org/ays/AysRestControllerTest.java @@ -31,105 +31,104 @@ @SpringBootTest @AutoConfigureMockMvc -@ActiveProfiles({ "default", "test" }) +@ActiveProfiles({"default", "test"}) @ExtendWith(MockitoExtension.class) public abstract class AysRestControllerTest extends AysTestContainerConfiguration { - @Autowired - protected AysMockMvc aysMockMvc; - - @Autowired - protected AysUserReadPort userReadPort; - - protected AysToken mockSuperAdminToken; - protected AysToken mockAdminToken; - protected AysToken mockUserToken; - - @Mock - private AysTokenConfigurationParameter tokenConfiguration; - @Mock - private AysParameterService parameterService; - - @BeforeEach - @SuppressWarnings("OptionalGetWithoutIsPresent disabled because of the test data is valid") - public void initializeAuth() { - Set parameters = Set.of( - AysParameter.builder() - .name("AUTH_TOKEN_PRIVATE_KEY") - .definition("dkNHbEVaSW9rdGQ1cVBKek03dHJqT2daMmdIbEpUSEw=LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCk1JSUV2QUlCQURBTkJna3Foa2lHOXcwQkFRRUZBQVNDQktZd2dnU2lBZ0VBQW9JQkFRQ0puK0lSRUhmclFsWjEKY0RkbllKcndUL1pvV1hick13aGt1enBXdUJVRDJHdTVteU1DdCtyT0p1S3N4VXFIL2IyZGFlZW81dUdndWY2cwpKcjI5bzZLM1drVVBMdG9uNk5GZUhKZEwwcWs5NmVlWDM5UVhFUHYzdS8yRGJXZTJRWk83aW4wNnhLSTQyOUgwCmg4cjdCT0NzemE1WFpkbjVCUHdmZ0pveUFLbkdGODNPMGpUT0Qzem8zV0VvbGEreW1YYXVoUHorUFl0dndSWncKSkZvQW91MTVZM2Z1SDBna3B4NDdRd0tnVFRwNUl4d1I4RnNxVkVOK1JaNFMzcWhxSktOaGY2SVkwS1RjaHphQQpmelp3QnRON0E1eWI4TURMRnRJTHlmRkdhYW0wbXMzUHJWb0J3LzhzYmxsSGlEMTdOVCtXcmNNVkJRRjJTRFlwCmR3WENMM1JiQWdNQkFBRUNnZ0VBS3FzK2JPbjlOQnlTTDhFRi9IQXdPWGVoUHdNVjRxQWs1dzVCYlNlUHBHeVMKSWE2ZXNUWVNmNjRBczI1THlGUDhXUFMvMVZjWDl6d1RZSTUyWDNoL2QzZHVWK0cvMDRYVWUraERaRWZCSHlnSgpITVpSdklFUWplTmtHejV0WEUyQ255KzEyZVdqSWh2TlFaSmtkV1V5djREWm45RTlQbjYwS0pRM3VtOElOQmt3CjZLNUFIMGxiYy8yMEtFYWZpMUsyMzBTUVFCeFp4bEN6bkNJSmNQYlZGSmxwVUNOM1JkaDFJSkdYZUlLT0NRbjkKQnB4Z2ZKVC9KUFo1cWVXamF1OUYxVWlCbWYyZjFzNnlDSXB2aDVsYUhKMXRuU1NuQXlKalkzYzFFUk1GMHBDSgpwUUtIMlEyaXFEYUxGNGdRQWIyQlRnYm85NWVja1RZaFdPVUlQQUt3UVFLQmdRRGhUbXhvTlQwRXlQWEptVFRPClJvdUNwSW4ycmlldGcwUXdYVVVVSDhMMHNJRUVqRDFpeXp5NVBvNk55OWN1QVNYSXBRM05RaDExZ2k1MTJXNmYKYXIyYWtXdXVRK01ENmRNM3RCa0xQSlJITVQ1SmVJbDBQQTJXc2tvMktTcmZtbUI1TFI5MzdpeFNjZmhvajY4bQpWSHBGQ2FhRDBtdVVzUHZYVVR0ZmhMV1ZZUUtCZ1FDY1g0ekllNS9HSWk3Z0pMZTZZTm5ITmprUmhIb3VreEVECm1HbjB2WlRlUzh1SXZrL1JQL2dMdjIwN3hIOXcwMEc1cS9BbytnWG1DV3pQWlhybmR1SVVZbUxwZ2NKVDh5bWMKQVovSWhsbkU4azBxTHlleWRtWmV0NHNXcXY3NkFseFlhNTdFZ3daaTNMQ1Z3OFBIVnIwb1pHR09vTGd3a3g3QgpRV2V4cXJ4bk93S0JnRXlGNlZYL2R3a1FCRU1Ea1NiYVdQbjNUcENGR0I3YnJhWkxsM0c5VStidHAvUldlV2I3CnBsVTRoUXh1QmxpdXRSbVB6YjlBVEdjajN3blIzcnV3Y2xOMFBzR0NkekZXRXBJaHpqdTl5SkxoaThsQ2NsVVQKTEg1WmNkRXhiRWxqMG81MW4vR0k2RzdjSE1YT3YydGlWK0RvNVRCeW9HMXhLeWczZzlYdWFnb2hBb0dBUkJ0Wgp0ZmdpSHFuRXdOczlLbkFFYWorem0yMlh5YkZFTjh5cVdXNDQ2SmthalBSV3oweU5QSkNqZ3VTU25SRm1EdmhVCklZVEVETzBOOTBhN3dSU0dZMXAydWoxSjVrYUNXUEJjSjNwY251cnBzUFhZMUdHOU5JTzhrS0xwYXZxY1BlYWgKdi9WUlVyM01LMjZZVnJud3FTY1BWbytwcVg1cVpzR1Y2RXYwd3dFQ2dZQkdSckF2cEptVWluazVoc0ZldzRvaAozclMwRTlqQklqekszWU9WWTJJVWxydmJYd2N2OWJ0UGpaVkd4VzZ1UEt0UEtPNTFaUExzRS94YWRiUGllOGhmCmM1aTB3LzVPK24rYlc3TlpmcWwvUFk4OTB5ajZJWHlzZElYMVRwQ1NST1Uzb1ptUWZBTG9WWWNBNHU3QWxpY0sKYnBKbTYwampiMHNNejRIVU9CaVZIUT09Ci0tLS0tRU5EIFBSSVZBVEUgS0VZLS0tLS0KNjNLcEhJM2FXeUtrOHNsVkFiaGY3UjBvcURVeHh5RUk=") - .build(), - AysParameter.builder() - .name("AUTH_TOKEN_PUBLIC_KEY") - .definition("cExiWUJBSVFTbHVwOXFXSkxSTDhsOGRDVkh1VkxyQ0E=LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUlJQklqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUFpWi9pRVJCMzYwSldkWEEzWjJDYQo4RS8yYUZsMjZ6TUlaTHM2VnJnVkE5aHJ1WnNqQXJmcXppYmlyTVZLaC8yOW5Xbm5xT2Job0xuK3JDYTl2YU9pCnQxcEZEeTdhSitqUlhoeVhTOUtwUGVubmw5L1VGeEQ3OTd2OWcyMW50a0dUdTRwOU9zU2lPTnZSOUlmSyt3VGcKck0ydVYyWForUVQ4SDRDYU1nQ3B4aGZOenRJMHpnOTg2TjFoS0pXdnNwbDJyb1Q4L2oyTGI4RVdjQ1JhQUtMdAplV04zN2g5SUpLY2VPME1Db0UwNmVTTWNFZkJiS2xSRGZrV2VFdDZvYWlTallYK2lHTkNrM0ljMmdIODJjQWJUCmV3T2NtL0RBeXhiU0M4bnhSbW1wdEpyTno2MWFBY1AvTEc1WlI0ZzllelUvbHEzREZRVUJka2cyS1hjRndpOTAKV3dJREFRQUIKLS0tLS1FTkQgUFVCTElDIEtFWS0tLS0tCg==SlhaNTl3eFBsWTJEWkNSWDdlZFRlaFhPaFhZRzlBN1Q=") - .build(), - AysParameter.builder() - .name("AUTH_ACCESS_TOKEN_EXPIRE_MINUTE") - .definition("120") - .build(), - AysParameter.builder() - .name("AUTH_REFRESH_TOKEN_EXPIRE_DAY") - .definition("1") - .build(), - AysParameter.builder() - .name("AUTH_LOGIN_MAX_TRY_COUNT") - .definition("3") - .build()); - Mockito.when(parameterService.findAll(Mockito.anyString())) - .thenReturn(parameters); - - this.tokenConfiguration = new AysTokenConfigurationParameter(parameterService); - - final Optional superAdmin = userReadPort.findById(AysValidTestData.SuperAdmin.ID); - final Claims claimsOfMockSuperAdminToken = superAdmin.get().getClaims(); - this.mockSuperAdminToken = this.generate(claimsOfMockSuperAdminToken); - - final Optional admin = userReadPort.findById(AysValidTestData.Admin.ID); - final Claims claimsOfMockAdminToken = admin.get().getClaims(); - this.mockAdminToken = this.generate(claimsOfMockAdminToken); - - final Optional user = userReadPort.findById(AysValidTestData.User.ID); - final Claims claimsOfMockUserToken = user.get().getClaims(); - this.mockUserToken = this.generate(claimsOfMockUserToken); - } - - private AysToken generate(Map claims) { - final long currentTimeMillis = System.currentTimeMillis(); - - final Date tokenIssuedAt = new Date(currentTimeMillis); - - final Date accessTokenExpiresAt = DateUtils.addMinutes(new Date(currentTimeMillis), - tokenConfiguration.getAccessTokenExpireMinute()); - final String accessToken = Jwts.builder() - .header() - .add(AysTokenClaims.TYPE.getValue(), OAuth2AccessToken.TokenType.BEARER.getValue()) - .and() - .id(AysRandomUtil.generateUUID()) - .issuer(tokenConfiguration.getIssuer()) - .issuedAt(tokenIssuedAt) - .expiration(accessTokenExpiresAt) - .signWith(tokenConfiguration.getPrivateKey()) - .claims(claims) - .compact(); - - final Date refreshTokenExpiresAt = DateUtils.addDays(new Date(currentTimeMillis), - tokenConfiguration.getRefreshTokenExpireDay()); - final JwtBuilder refreshTokenBuilder = Jwts.builder(); - final String refreshToken = refreshTokenBuilder - .header() - .add(AysTokenClaims.TYPE.getValue(), OAuth2AccessToken.TokenType.BEARER.getValue()) - .and() - .id(AysRandomUtil.generateUUID()) - .issuer(tokenConfiguration.getIssuer()) - .issuedAt(tokenIssuedAt) - .expiration(refreshTokenExpiresAt) - .signWith(tokenConfiguration.getPrivateKey()) - .claim(AysTokenClaims.USER_ID.getValue(), claims.get(AysTokenClaims.USER_ID.getValue())) - .compact(); - - return AysToken.builder() - .accessToken(accessToken) - .refreshToken(refreshToken) - .build(); - } + @Autowired + protected AysMockMvc aysMockMvc; + + @Autowired + protected AysUserReadPort userReadPort; + + + protected AysToken mockSuperAdminToken; + protected AysToken mockAdminToken; + protected AysToken mockUserToken; + + + @Mock + private AysTokenConfigurationParameter tokenConfiguration; + @Mock + private AysParameterService parameterService; + + @BeforeEach + @SuppressWarnings("OptionalGetWithoutIsPresent disabled because of the test data is valid") + public void initializeAuth() { + Set parameters = Set.of( + AysParameter.builder() + .name("AUTH_TOKEN_PRIVATE_KEY") + .definition("dkNHbEVaSW9rdGQ1cVBKek03dHJqT2daMmdIbEpUSEw=LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCk1JSUV2QUlCQURBTkJna3Foa2lHOXcwQkFRRUZBQVNDQktZd2dnU2lBZ0VBQW9JQkFRQ0puK0lSRUhmclFsWjEKY0RkbllKcndUL1pvV1hick13aGt1enBXdUJVRDJHdTVteU1DdCtyT0p1S3N4VXFIL2IyZGFlZW81dUdndWY2cwpKcjI5bzZLM1drVVBMdG9uNk5GZUhKZEwwcWs5NmVlWDM5UVhFUHYzdS8yRGJXZTJRWk83aW4wNnhLSTQyOUgwCmg4cjdCT0NzemE1WFpkbjVCUHdmZ0pveUFLbkdGODNPMGpUT0Qzem8zV0VvbGEreW1YYXVoUHorUFl0dndSWncKSkZvQW91MTVZM2Z1SDBna3B4NDdRd0tnVFRwNUl4d1I4RnNxVkVOK1JaNFMzcWhxSktOaGY2SVkwS1RjaHphQQpmelp3QnRON0E1eWI4TURMRnRJTHlmRkdhYW0wbXMzUHJWb0J3LzhzYmxsSGlEMTdOVCtXcmNNVkJRRjJTRFlwCmR3WENMM1JiQWdNQkFBRUNnZ0VBS3FzK2JPbjlOQnlTTDhFRi9IQXdPWGVoUHdNVjRxQWs1dzVCYlNlUHBHeVMKSWE2ZXNUWVNmNjRBczI1THlGUDhXUFMvMVZjWDl6d1RZSTUyWDNoL2QzZHVWK0cvMDRYVWUraERaRWZCSHlnSgpITVpSdklFUWplTmtHejV0WEUyQ255KzEyZVdqSWh2TlFaSmtkV1V5djREWm45RTlQbjYwS0pRM3VtOElOQmt3CjZLNUFIMGxiYy8yMEtFYWZpMUsyMzBTUVFCeFp4bEN6bkNJSmNQYlZGSmxwVUNOM1JkaDFJSkdYZUlLT0NRbjkKQnB4Z2ZKVC9KUFo1cWVXamF1OUYxVWlCbWYyZjFzNnlDSXB2aDVsYUhKMXRuU1NuQXlKalkzYzFFUk1GMHBDSgpwUUtIMlEyaXFEYUxGNGdRQWIyQlRnYm85NWVja1RZaFdPVUlQQUt3UVFLQmdRRGhUbXhvTlQwRXlQWEptVFRPClJvdUNwSW4ycmlldGcwUXdYVVVVSDhMMHNJRUVqRDFpeXp5NVBvNk55OWN1QVNYSXBRM05RaDExZ2k1MTJXNmYKYXIyYWtXdXVRK01ENmRNM3RCa0xQSlJITVQ1SmVJbDBQQTJXc2tvMktTcmZtbUI1TFI5MzdpeFNjZmhvajY4bQpWSHBGQ2FhRDBtdVVzUHZYVVR0ZmhMV1ZZUUtCZ1FDY1g0ekllNS9HSWk3Z0pMZTZZTm5ITmprUmhIb3VreEVECm1HbjB2WlRlUzh1SXZrL1JQL2dMdjIwN3hIOXcwMEc1cS9BbytnWG1DV3pQWlhybmR1SVVZbUxwZ2NKVDh5bWMKQVovSWhsbkU4azBxTHlleWRtWmV0NHNXcXY3NkFseFlhNTdFZ3daaTNMQ1Z3OFBIVnIwb1pHR09vTGd3a3g3QgpRV2V4cXJ4bk93S0JnRXlGNlZYL2R3a1FCRU1Ea1NiYVdQbjNUcENGR0I3YnJhWkxsM0c5VStidHAvUldlV2I3CnBsVTRoUXh1QmxpdXRSbVB6YjlBVEdjajN3blIzcnV3Y2xOMFBzR0NkekZXRXBJaHpqdTl5SkxoaThsQ2NsVVQKTEg1WmNkRXhiRWxqMG81MW4vR0k2RzdjSE1YT3YydGlWK0RvNVRCeW9HMXhLeWczZzlYdWFnb2hBb0dBUkJ0Wgp0ZmdpSHFuRXdOczlLbkFFYWorem0yMlh5YkZFTjh5cVdXNDQ2SmthalBSV3oweU5QSkNqZ3VTU25SRm1EdmhVCklZVEVETzBOOTBhN3dSU0dZMXAydWoxSjVrYUNXUEJjSjNwY251cnBzUFhZMUdHOU5JTzhrS0xwYXZxY1BlYWgKdi9WUlVyM01LMjZZVnJud3FTY1BWbytwcVg1cVpzR1Y2RXYwd3dFQ2dZQkdSckF2cEptVWluazVoc0ZldzRvaAozclMwRTlqQklqekszWU9WWTJJVWxydmJYd2N2OWJ0UGpaVkd4VzZ1UEt0UEtPNTFaUExzRS94YWRiUGllOGhmCmM1aTB3LzVPK24rYlc3TlpmcWwvUFk4OTB5ajZJWHlzZElYMVRwQ1NST1Uzb1ptUWZBTG9WWWNBNHU3QWxpY0sKYnBKbTYwampiMHNNejRIVU9CaVZIUT09Ci0tLS0tRU5EIFBSSVZBVEUgS0VZLS0tLS0KNjNLcEhJM2FXeUtrOHNsVkFiaGY3UjBvcURVeHh5RUk=").build(), + AysParameter.builder() + .name("AUTH_TOKEN_PUBLIC_KEY") + .definition("cExiWUJBSVFTbHVwOXFXSkxSTDhsOGRDVkh1VkxyQ0E=LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUlJQklqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUFpWi9pRVJCMzYwSldkWEEzWjJDYQo4RS8yYUZsMjZ6TUlaTHM2VnJnVkE5aHJ1WnNqQXJmcXppYmlyTVZLaC8yOW5Xbm5xT2Job0xuK3JDYTl2YU9pCnQxcEZEeTdhSitqUlhoeVhTOUtwUGVubmw5L1VGeEQ3OTd2OWcyMW50a0dUdTRwOU9zU2lPTnZSOUlmSyt3VGcKck0ydVYyWForUVQ4SDRDYU1nQ3B4aGZOenRJMHpnOTg2TjFoS0pXdnNwbDJyb1Q4L2oyTGI4RVdjQ1JhQUtMdAplV04zN2g5SUpLY2VPME1Db0UwNmVTTWNFZkJiS2xSRGZrV2VFdDZvYWlTallYK2lHTkNrM0ljMmdIODJjQWJUCmV3T2NtL0RBeXhiU0M4bnhSbW1wdEpyTno2MWFBY1AvTEc1WlI0ZzllelUvbHEzREZRVUJka2cyS1hjRndpOTAKV3dJREFRQUIKLS0tLS1FTkQgUFVCTElDIEtFWS0tLS0tCg==SlhaNTl3eFBsWTJEWkNSWDdlZFRlaFhPaFhZRzlBN1Q=").build(), + AysParameter.builder() + .name("AUTH_ACCESS_TOKEN_EXPIRE_MINUTE") + .definition("120") + .build(), + AysParameter.builder() + .name("AUTH_REFRESH_TOKEN_EXPIRE_DAY") + .definition("1") + .build(), + AysParameter.builder() + .name("AUTH_LOGIN_MAX_TRY_COUNT") + .definition("3") + .build() + ); + Mockito.when(parameterService.findAll(Mockito.anyString())) + .thenReturn(parameters); + + this.tokenConfiguration = new AysTokenConfigurationParameter(parameterService); + + final Optional superAdmin = userReadPort.findById(AysValidTestData.SuperAdmin.ID); + final Claims claimsOfMockSuperAdminToken = superAdmin.get().getClaims(); + this.mockSuperAdminToken = this.generate(claimsOfMockSuperAdminToken); + + final Optional admin = userReadPort.findById(AysValidTestData.Admin.ID); + final Claims claimsOfMockAdminToken = admin.get().getClaims(); + this.mockAdminToken = this.generate(claimsOfMockAdminToken); + + final Optional user = userReadPort.findById(AysValidTestData.User.ID); + final Claims claimsOfMockUserToken = user.get().getClaims(); + this.mockUserToken = this.generate(claimsOfMockUserToken); + } + + private AysToken generate(Map claims) { + final long currentTimeMillis = System.currentTimeMillis(); + + final Date tokenIssuedAt = new Date(currentTimeMillis); + + final Date accessTokenExpiresAt = DateUtils.addMinutes(new Date(currentTimeMillis), tokenConfiguration.getAccessTokenExpireMinute()); + final String accessToken = Jwts.builder() + .header() + .add(AysTokenClaims.TYPE.getValue(), OAuth2AccessToken.TokenType.BEARER.getValue()) + .and() + .id(AysRandomUtil.generateUUID()) + .issuer(tokenConfiguration.getIssuer()) + .issuedAt(tokenIssuedAt) + .expiration(accessTokenExpiresAt) + .signWith(tokenConfiguration.getPrivateKey()) + .claims(claims) + .compact(); + + final Date refreshTokenExpiresAt = DateUtils.addDays(new Date(currentTimeMillis), tokenConfiguration.getRefreshTokenExpireDay()); + final JwtBuilder refreshTokenBuilder = Jwts.builder(); + final String refreshToken = refreshTokenBuilder + .header() + .add(AysTokenClaims.TYPE.getValue(), OAuth2AccessToken.TokenType.BEARER.getValue()) + .and() + .id(AysRandomUtil.generateUUID()) + .issuer(tokenConfiguration.getIssuer()) + .issuedAt(tokenIssuedAt) + .expiration(refreshTokenExpiresAt) + .signWith(tokenConfiguration.getPrivateKey()) + .claim(AysTokenClaims.USER_ID.getValue(), claims.get(AysTokenClaims.USER_ID.getValue())) + .compact(); + + return AysToken.builder() + .accessToken(accessToken) + .refreshToken(refreshToken) + .build(); + } } diff --git a/src/test/java/org/ays/auth/controller/AdminRegistrationApplicationControllerTest.java b/src/test/java/org/ays/auth/controller/AdminRegistrationApplicationControllerTest.java index d48d9c60c..68b38cde1 100644 --- a/src/test/java/org/ays/auth/controller/AdminRegistrationApplicationControllerTest.java +++ b/src/test/java/org/ays/auth/controller/AdminRegistrationApplicationControllerTest.java @@ -48,829 +48,784 @@ class AdminRegistrationApplicationControllerTest extends AysRestControllerTest { - @MockBean - private AdminRegistrationApplicationService adminRegistrationApplicationService; - - @MockBean - private AdminRegistrationCompleteService adminRegistrationCompleteService; - - private final AdminRegistrationApplicationToApplicationsResponseMapper adminRegistrationApplicationToApplicationsResponseMapper = AdminRegistrationApplicationToApplicationsResponseMapper - .initialize(); - private final AdminRegistrationApplicationToApplicationResponseMapper adminRegistrationApplicationToApplicationResponseMapper = AdminRegistrationApplicationToApplicationResponseMapper - .initialize(); - private final AdminRegistrationApplicationToSummaryResponseMapper adminRegistrationApplicationToSummaryResponseMapper = AdminRegistrationApplicationToSummaryResponseMapper - .initialize(); - private final AdminRegistrationApplicationToCreateResponseMapper adminRegistrationApplicationToCreateResponseMapper = AdminRegistrationApplicationToCreateResponseMapper - .initialize(); - - private static final String BASE_PATH = "/api/v1"; - - @Test - void givenValidAdminRegisterApplicationListRequest_whenAdminRegisterApplicationsFound_thenReturnAysPageResponseOfAdminRegisterApplicationsResponse() - throws Exception { - - // Given - AdminRegistrationApplicationListRequest mockListRequest = new AdminRegistrationApplicationListRequestBuilder() - .withValidValues() - .build(); - - // When - List mockApplications = List.of( - new AdminRegistrationApplicationBuilder().withValidValues().build()); - - AysPage mockApplicationPage = AysPageBuilder - .from(mockApplications, mockListRequest.getPageable()); - - Mockito.when(adminRegistrationApplicationService - .findAll(Mockito.any(AdminRegistrationApplicationListRequest.class))) - .thenReturn(mockApplicationPage); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-applications"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockSuperAdminToken.getAccessToken(), mockListRequest); - - List mockApplicationsResponse = adminRegistrationApplicationToSummaryResponseMapper - .map(mockApplications); - AysPageResponse pageOfResponse = AysPageResponse - .builder() - .of(mockApplicationPage) - .content(mockApplicationsResponse) - .build(); - AysResponse> mockResponse = AysResponse - .successOf(pageOfResponse); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .isNotEmpty()); - - // Verify - Mockito.verify(adminRegistrationApplicationService, Mockito.times(1)) - .findAll(Mockito.any(AdminRegistrationApplicationListRequest.class)); - } - - @Test - void givenValidAdminRegisterApplicationListRequest_whenUnauthorizedForListing_thenReturnAccessDeniedException() - throws Exception { - // Given - AdminRegistrationApplicationListRequest mockListRequest = new AdminRegistrationApplicationListRequestBuilder() - .withValidValues() - .build(); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-applications"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockUserToken.getAccessToken(), mockListRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isForbidden()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .doesNotExist()); - - // Verify - Mockito.verify(adminRegistrationApplicationService, Mockito.never()) - .findAll(Mockito.any(AdminRegistrationApplicationListRequest.class)); - } - - @Test - void givenValidAdminRegisterApplicationId_whenAdminRegisterApplicationFound_thenReturnAdminRegisterApplicationResponse() - throws Exception { - - // Given - String mockApplicationId = "a0c2351d-54ce-4019-8ffe-a2f8a2700824"; - - // When - AdminRegistrationApplication mockRegisterApplication = new AdminRegistrationApplicationBuilder() - .withId(mockApplicationId) - .build(); - Mockito.when(adminRegistrationApplicationService.findById(mockApplicationId)) - .thenReturn(mockRegisterApplication); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockApplicationId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .get(endpoint, mockSuperAdminToken.getAccessToken()); - - AdminRegistrationApplicationResponse mockApplicationResponse = adminRegistrationApplicationToApplicationResponseMapper - .map(mockRegisterApplication); - AysResponse mockResponse = AysResponse - .successOf(mockApplicationResponse); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .isNotEmpty()); - - // Verify - Mockito.verify(adminRegistrationApplicationService, Mockito.times(1)) - .findById(mockApplicationId); - - } - - @Test - void givenValidAdminRegisterApplicationId_whenUnauthorizedForGettingAdminRegisterApplicationById_thenReturnAccessDeniedException() - throws Exception { - - // Given - String mockApplicationId = "68c867b4-e84a-405c-b1ab-f8dcaa9c41f2"; - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockApplicationId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .get(endpoint, mockUserToken.getAccessToken()); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isForbidden()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .doesNotExist()); - - // Verify - Mockito.verify(adminRegistrationApplicationService, Mockito.never()) - .findById(mockApplicationId); - } - - @ParameterizedTest - @ValueSource(strings = { - "This is a valid text with sufficient length and contains alphabetic characters.", - "This text includes numbers 12345 and still should be accepted because it's within limits.", - "This text, which includes punctuation marks, should be accepted.", - "ÇalıŞkan ve dÜrüst İnsanlar her zaman başarıyı yakalar.(:;?/)" - }) - void givenValidAdminRegisterApplicationCreateRequest_whenCreatingAdminRegisterApplication_thenReturnAdminRegisterApplicationCreateResponse( - String reason) throws Exception { - - // Given - AdminRegistrationApplicationCreateRequest mockRequest = new AdminRegistrationApplicationCreateRequestBuilder() - .withValidValues() - .withInstitutionId(AysValidTestData.SuperAdmin.INSTITUTION_ID) - .withReason(reason) - .build(); - - // When - Institution mockInstitution = new InstitutionBuilder() - .withId(mockRequest.getInstitutionId()) - .build(); - AdminRegistrationApplication mockRegisterApplication = new AdminRegistrationApplicationBuilder() - .withValidValues() - .withInstitution(mockInstitution) - .withReason(mockRequest.getReason()) - .withStatus(AdminRegistrationApplicationStatus.WAITING) - .build(); - - Mockito.when(adminRegistrationApplicationService - .create(Mockito.any(AdminRegistrationApplicationCreateRequest.class))) - .thenReturn(mockRegisterApplication); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockSuperAdminToken.getAccessToken(), mockRequest); - - AdminRegistrationApplicationCreateResponse mockApplicationCreateResponse = adminRegistrationApplicationToCreateResponseMapper - .map(mockRegisterApplication); - AysResponse mockResponse = AysResponse - .successOf(mockApplicationCreateResponse); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .isNotEmpty()); - - // Verify - Mockito.verify(adminRegistrationApplicationService, Mockito.times(1)) - .create(Mockito.any(AdminRegistrationApplicationCreateRequest.class)); - } - - @ParameterizedTest - @ValueSource(strings = { - "Invalid reason with special characters: #$%", - "#$½#$£#$£#$$#½#£$£#$#£½#$½#$½$£#$#£$$#½#$$½", - ".,..,.,.,.,.,,.,.,.,.,.,.,..,.,.,,.,.,.,", - "Too short", - " a", - " spaceAtTheBeginning", - "spaceAtTheEnd ", - " both ", - " justAString ", - "151201485621548562154851458614125461254125412", - "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean vestibulum commodo turpis, sed venenatis sapien suscipit sit amet. Etiam malesuada, ligula in semper varius, nisi mi pulvinar libero, ut commodo dolor orci quis urna. Vivamus ac euismod ex. Proin vel vulputate orci. Ut id nibh finibus, mattis sem id, maximus ante. Proin fringilla ipsum at arcu venenatis, non bibendum justo luctus. Phasellus vestibulum feugiat est sit amet bibendum. Donec nulla leo, ultricies sed pharetra sed, hendrerit vel nunc." - }) - void givenInvalidAdminRegisterApplicationCreateRequest_whenCreatingAdminRegisterApplication_thenReturnValidationError( - String invalidReason) throws Exception { - - // Given - AdminRegistrationApplicationCreateRequest createRequest = new AdminRegistrationApplicationCreateRequestBuilder() - .withValidValues() - .withReason(invalidReason) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockUserToken.getAccessToken(), createRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(adminRegistrationApplicationService, Mockito.never()) - .create(Mockito.any(AdminRegistrationApplicationCreateRequest.class)); - } - - @Test - void givenValidAdminRegisterApplicationCreateRequest_whenUnauthorizedForCreatingAdminRegisterApplication_thenReturnAccessDeniedException() - throws Exception { - - // Given - AdminRegistrationApplicationCreateRequest mockRequest = new AdminRegistrationApplicationCreateRequestBuilder() - .withValidValues() - .withInstitutionId(AysValidTestData.SuperAdmin.INSTITUTION_ID) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockUserToken.getAccessToken(), mockRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isForbidden()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .doesNotExist()); - - // Verify - Mockito.verify(adminRegistrationApplicationService, Mockito.never()) - .create(Mockito.any(AdminRegistrationApplicationCreateRequest.class)); - } - - @Test - void givenValidAdminRegisterApplicationId_whenAdminApplicationFound_thenReturnAdminApplicationSummaryResponse() - throws Exception { - - // Given - String mockId = "085fbe72-caa7-439d-8db1-166ed005e120"; - AdminRegistrationApplication mockAdminRegistrationApplication = new AdminRegistrationApplicationBuilder() - .withId(mockId) - .build(); - - // When - Mockito.when(adminRegistrationApplicationService.findSummaryById(mockId)) - .thenReturn(mockAdminRegistrationApplication); - - // Then - String endpoint = BASE_PATH - .concat("/admin-registration-application/".concat(mockId).concat("/summary")); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .get(endpoint); - - AdminRegistrationApplicationSummaryResponse mockSummaryResponse = adminRegistrationApplicationToSummaryResponseMapper - .map(mockAdminRegistrationApplication); - AysResponse mockResponse = AysResponse - .successOf(mockSummaryResponse); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .isNotEmpty()); - } - - @Test - void givenIdAndAdminRegisterApplication_whenAdminApplicationNotFound_thenReturnUnauthorizedError() - throws Exception { - - // Given - String mockId = "181e8310-6dfd-444c-aa38-056ce8401345"; - - // When - Mockito.when(adminRegistrationApplicationService.findSummaryById(mockId)) - .thenThrow(new AysAdminRegistrationApplicationNotExistByIdAuthException(mockId)); - - // Then - String endpoint = BASE_PATH - .concat("/admin-registration-application/".concat(mockId).concat("/summary")); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .get(endpoint); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.UNAUTHORIZED; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isUnauthorized()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .doesNotHaveJsonPath()); - } - - @Test - void givenValidAdminRegisterRequest_whenAdminRegistered_thenReturnSuccessResponse() throws Exception { - - // Given - String mockId = "e8de09dc-a44e-40eb-bcc7-cf0141f8733c"; - AdminRegistrationApplicationCompleteRequest mockRequest = new AdminRegistrationApplicationCompleteRequestBuilder() - .withValidValues().build(); - - // When - Mockito.doNothing().when(adminRegistrationCompleteService).complete(Mockito.anyString(), Mockito.any()); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId) - .concat("/complete"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockRequest); - - AysResponse mockResponse = AysResponseBuilder.success(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()); - - // Verify - Mockito.verify(adminRegistrationCompleteService, Mockito.times(1)) - .complete(Mockito.anyString(), Mockito.any()); - } - - @Test - void givenIdAndAdminRegisterRequest_whenAdminApplicationNotFound_thenReturnUnauthorizedError() - throws Exception { - - // Given - String mockId = "181e8310-6dfd-444c-aa38-056ce8401345"; - AdminRegistrationApplicationCompleteRequest mockRequest = new AdminRegistrationApplicationCompleteRequestBuilder() - .withValidValues().build(); - - // When - Mockito.doThrow(new AysAdminRegistrationApplicationNotExistByIdAuthException(mockId)) - .when(adminRegistrationCompleteService) - .complete(Mockito.anyString(), Mockito.any()); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId) - .concat("/complete"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.UNAUTHORIZED; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isUnauthorized()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .doesNotHaveJsonPath()); - - // Verify - Mockito.verify(adminRegistrationCompleteService, Mockito.times(1)) - .complete(Mockito.anyString(), Mockito.any()); - } - - @Test - void givenPhoneNumberWithAlphanumericCharacter_whenPhoneNumberIsNotValid_thenReturnValidationError() - throws Exception { - - // Given - String mockId = "f1b789d0-6095-4860-85bb-e1a0b20f1d13"; - AysPhoneNumberRequest mockPhoneNumber = new AysPhoneNumberRequestBuilder() - .withCountryCode("ABC") - .withLineNumber("ABC").build(); - AdminRegistrationApplicationCompleteRequest mockRequest = new AdminRegistrationApplicationCompleteRequestBuilder() - .withValidValues() - .withPhoneNumber(mockPhoneNumber).build(); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId) - .concat("/complete"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(adminRegistrationCompleteService, Mockito.never()) - .complete(Mockito.anyString(), Mockito.any()); - } - - @Test - void givenPhoneNumberWithInvalidLength_whenPhoneNumberIsNotValid_thenReturnValidationError() throws Exception { - - // Given - String mockId = "25930d3f-4cea-4147-a21a-0f22c9bf72de"; - AysPhoneNumberRequest mockPhoneNumber = new AysPhoneNumberRequestBuilder() - .withCountryCode("456786745645") - .withLineNumber("6546467456435548676845321346656654").build(); - AdminRegistrationApplicationCompleteRequest mockRequest = new AdminRegistrationApplicationCompleteRequestBuilder() - .withValidValues() - .withPhoneNumber(mockPhoneNumber).build(); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId) - .concat("/complete"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(adminRegistrationCompleteService, Mockito.never()) - .complete(Mockito.anyString(), Mockito.any()); - } - - @Test - void givenPhoneNumberWithInvalidOperator_whenPhoneNumberIsNotValid_thenReturnValidationError() - throws Exception { - - // Given - String mockId = "2028b456-e06c-4ea1-9017-b45523529576"; - final String invalidOperator = "123"; - AysPhoneNumberRequest mockPhoneNumber = new AysPhoneNumberRequestBuilder() - .withCountryCode("90") - .withLineNumber(invalidOperator + "6327218").build(); - AdminRegistrationApplicationCompleteRequest mockRequest = new AdminRegistrationApplicationCompleteRequestBuilder() - .withValidValues() - .withPhoneNumber(mockPhoneNumber).build(); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId) - .concat("/complete"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(adminRegistrationCompleteService, Mockito.never()) - .complete(Mockito.anyString(), Mockito.any()); - } - - @ParameterizedTest - @ValueSource(strings = { - "John 1234", - "John *^%$#", - " John", - "? John", - "J", - "J----", - "City--King", - "John Doe" - }) - void givenInvalidAdminRegisterApplicationCompleteRequestWithParametrizedInvalidNames_whenNamesAreNotValid_thenReturnValidationError( - String invalidName) throws Exception { - - // Given - String mockId = "f423facc-36fe-4615-a68d-f7f1fe5cd860"; - AdminRegistrationApplicationCompleteRequest mockRequest = new AdminRegistrationApplicationCompleteRequestBuilder() - .withValidValues() - .withFirstName(invalidName) - .withLastName(invalidName) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId) - .concat("/complete"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(adminRegistrationCompleteService, Mockito.never()) - .complete(Mockito.anyString(), Mockito.any()); - } - - @ParameterizedTest - @ValueSource(strings = { - "abc.def@mail.c", - "abc.def@mail#archive.com", - "abc.def@mail", - "abcdef@mail..com", - "abc-@mail.com", - "admin@test@ays.com", - "admintest@ays..com", - "username@gmail..co.uk", - "user@ example.com", - "user@-example.com", - "user@example-.com", - "(user)@example.com", - "user@[192.168.1.1", - "user@exam ple.com", - "user@.com", - ".user@example.com", - " user@example.com", - "user@example.com ", - " user@example.com ", - "@missingusername.com", - "a@b.c", // 5 karakter - minimum limit altında - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@domain.com" // 255 - // karakter - // - - // maksimum - // limit - // üstünde - }) - void givenInvalidAdminRegisterApplicationCompleteRequestWithParametrizedInvalidEmails_whenEmailsAreNotValid_thenReturnValidationError( - String invalidEmail) throws Exception { - - // Given - String mockId = "53617d24-e32c-4249-b9e6-b10e63a439bd"; - AdminRegistrationApplicationCompleteRequest mockRequest = new AdminRegistrationApplicationCompleteRequestBuilder() - .withValidValues() - .withEmailAddress(invalidEmail) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId) - .concat("/complete"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(adminRegistrationCompleteService, Mockito.never()) - .complete(Mockito.anyString(), Mockito.any()); - } - - @ParameterizedTest - @ValueSource(strings = { - "user@example.com", // 14 karakter - geçerli - "abcdef@mail.com", // 14 karakter - geçerli - "a.b.c.d@test.com", // 15 karakter - geçerli - "test.123@afetyonetimsistemi.org" // 29 karakter - geçerli - }) - void givenValidAdminRegisterApplicationCompleteRequestWithParametrizedValidEmails_whenEmailsAreValid_thenReturnSuccessResponse( - String validEmail) throws Exception { - // Given - String mockId = "fe3760f1-8b44-4587-99a6-43e426c8c6d1"; - AdminRegistrationApplicationCompleteRequest mockRequest = new AdminRegistrationApplicationCompleteRequestBuilder() - .withValidValues() - .withEmailAddress(validEmail) - .build(); - - // When - Mockito.doNothing().when(adminRegistrationCompleteService).complete(Mockito.anyString(), Mockito.any()); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId) - .concat("/complete"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockRequest); - - AysResponse mockResponse = AysResponseBuilder.success(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()); - - // Verify - Mockito.verify(adminRegistrationCompleteService, Mockito.times(1)) - .complete(Mockito.anyString(), Mockito.any()); - } - - @ParameterizedTest - @ValueSource(strings = { - "g", - "gfh2j" - }) - void givenInvalidAdminRegisterApplicationCompleteRequestWithInvalidPassword_whenPasswordDoesNotValid_thenReturnValidationError( - String mockPassword) throws Exception { - - // Given - String mockId = "de2b9621-0bf6-45df-a173-4697797446b7"; - AdminRegistrationApplicationCompleteRequest mockRequest = new AdminRegistrationApplicationCompleteRequestBuilder() - .withValidValues() - .withPassword(mockPassword) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId) - .concat("/complete"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(adminRegistrationCompleteService, Mockito.never()) - .complete(Mockito.anyString(), Mockito.any()); - } - - @Test - void givenValidAdminRegisterApplicationId_whenApproveAdminRegisterApplication_thenReturnNothing() - throws Exception { - // Given - String mockId = "47d91587-612f-462b-975f-ed227e2ee3a7"; - - // When - Mockito.doNothing() - .when(adminRegistrationApplicationService).approve(mockId); - - // Then - String endpoint = BASE_PATH - .concat("/admin-registration-application/".concat(mockId).concat("/approve")); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockSuperAdminToken.getAccessToken()); - - AysResponse mockResponse = AysResponseBuilder.success(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()); - - // Verify - Mockito.verify(adminRegistrationApplicationService, Mockito.times(1)) - .approve(mockId); - } - - @Test - void givenValidAdminRegisterApplicationId_whenUnauthorizedForApprovingAdminRegisterApplication_thenReturnAccessDeniedException() - throws Exception { - - // Given - String mockId = "5cbc8774-e2c3-414b-a1d8-26abcf3c9d17"; - - // Then - String endpoint = BASE_PATH - .concat("/admin-registration-application/".concat(mockId).concat("/approve")); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockUserToken.getAccessToken()); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isForbidden()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .doesNotExist()); - - // Verify - Mockito.verify(adminRegistrationApplicationService, Mockito.never()) - .approve(mockId); - } - - @Test - void givenValidAdminRegisterApplicationRejectRequest_whenRejectingAdminRegisterApplication_thenReturnSuccess() - throws Exception { - - // Given - String mockId = "c5c504a6-1223-483b-a8ba-6fb4ea309e00"; - AdminRegistrationApplicationRejectRequest mockRequest = new AdminRegistrationApplicationRejectRequestBuilder() - .withValidValues() - .build(); - - // When - Mockito.doNothing().when(adminRegistrationApplicationService).reject(mockId, mockRequest); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId).concat("/reject"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockSuperAdminToken.getAccessToken(), mockRequest); - - AysResponse mockResponse = AysResponseBuilder.success(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()); - - // Verify - Mockito.verify(adminRegistrationApplicationService, Mockito.times(1)) - .reject(Mockito.eq(mockId), - Mockito.any(AdminRegistrationApplicationRejectRequest.class)); - - } - - @Test - void givenValidAdminRegisterApplicationRejectRequest_whenUnauthorizedForRejectingAdminRegisterApplication_thenReturnAccessDeniedException() - throws Exception { - - // Given - String mockId = "c27aa25e-7e2f-4bc1-8056-eb9c2948f507"; - AdminRegistrationApplicationRejectRequest mockRequest = new AdminRegistrationApplicationRejectRequestBuilder() - .withValidValues() - .build(); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId).concat("/reject"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockUserToken.getAccessToken(), mockRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isForbidden()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .doesNotExist()); - - // Verify - Mockito.verify(adminRegistrationApplicationService, Mockito.never()) - .reject(Mockito.eq(mockId), - Mockito.any(AdminRegistrationApplicationRejectRequest.class)); - } - - @ParameterizedTest - @ValueSource(strings = { - "", - " ", - "less than 40", - """ - Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. - Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. - Donec qudam felis, ultricies nec, pellentesque eu, pretscsxwium quis, sem. Nulla consequat massa quis - enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, - imperdiet a, venenatdskjvndshjcndsis vitae, justo. Nullam dictum felis eu pedde mollis pretium. Integer tincidunt. - Cras dapibus. Vivdamus ewl - """, - " spaceAtTheBeginning", - "spaceAtTheEnd ", - " both ", - " justAString " - }) - void givenInvalidAdminRegisterApplicationRejectRequest_whenRejectingAdminRegisterApplication_thenReturnValidationError( - String rejectReason) throws Exception { - - // Given - String mockId = "4d04bd1e-6318-43ba-ab40-57efb8afc918"; - AdminRegistrationApplicationRejectRequest mockRequest = new AdminRegistrationApplicationRejectRequestBuilder() - .withValidValues() - .withRejectReason(rejectReason) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId).concat("/reject"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockUserToken.getAccessToken(), mockRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(adminRegistrationApplicationService, Mockito.never()) - .reject(Mockito.anyString(), - Mockito.any(AdminRegistrationApplicationRejectRequest.class)); - } + @MockBean + private AdminRegistrationApplicationService adminRegistrationApplicationService; + + @MockBean + private AdminRegistrationCompleteService adminRegistrationCompleteService; + + private final AdminRegistrationApplicationToApplicationsResponseMapper adminRegistrationApplicationToApplicationsResponseMapper = AdminRegistrationApplicationToApplicationsResponseMapper.initialize(); + private final AdminRegistrationApplicationToApplicationResponseMapper adminRegistrationApplicationToApplicationResponseMapper = AdminRegistrationApplicationToApplicationResponseMapper.initialize(); + private final AdminRegistrationApplicationToSummaryResponseMapper adminRegistrationApplicationToSummaryResponseMapper = AdminRegistrationApplicationToSummaryResponseMapper.initialize(); + private final AdminRegistrationApplicationToCreateResponseMapper adminRegistrationApplicationToCreateResponseMapper = AdminRegistrationApplicationToCreateResponseMapper.initialize(); + + + private static final String BASE_PATH = "/api/v1"; + + + @Test + void givenValidAdminRegisterApplicationListRequest_whenAdminRegisterApplicationsFound_thenReturnAysPageResponseOfAdminRegisterApplicationsResponse() throws Exception { + + // Given + AdminRegistrationApplicationListRequest mockListRequest = new AdminRegistrationApplicationListRequestBuilder() + .withValidValues() + .build(); + + // When + List mockApplications = List.of( + new AdminRegistrationApplicationBuilder().withValidValues().build() + ); + + AysPage mockApplicationPage = AysPageBuilder + .from(mockApplications, mockListRequest.getPageable()); + + Mockito.when(adminRegistrationApplicationService.findAll(Mockito.any(AdminRegistrationApplicationListRequest.class))) + .thenReturn(mockApplicationPage); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-applications"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockSuperAdminToken.getAccessToken(), mockListRequest); + + List mockApplicationsResponse = adminRegistrationApplicationToApplicationsResponseMapper.map(mockApplications); + AysPageResponse pageOfResponse = AysPageResponse.builder() + .of(mockApplicationPage) + .content(mockApplicationsResponse) + .build(); + AysResponse> mockResponse = AysResponse + .successOf(pageOfResponse); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .isNotEmpty()); + + // Verify + Mockito.verify(adminRegistrationApplicationService, Mockito.times(1)) + .findAll(Mockito.any(AdminRegistrationApplicationListRequest.class)); + } + + @Test + void givenValidAdminRegisterApplicationListRequest_whenUnauthorizedForListing_thenReturnAccessDeniedException() throws Exception { + // Given + AdminRegistrationApplicationListRequest mockListRequest = new AdminRegistrationApplicationListRequestBuilder() + .withValidValues() + .build(); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-applications"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockUserToken.getAccessToken(), mockListRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isForbidden()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .doesNotExist()); + + // Verify + Mockito.verify(adminRegistrationApplicationService, Mockito.never()) + .findAll(Mockito.any(AdminRegistrationApplicationListRequest.class)); + } + + @Test + void givenValidAdminRegisterApplicationId_whenAdminRegisterApplicationFound_thenReturnAdminRegisterApplicationResponse() throws Exception { + + // Given + String mockApplicationId = "a0c2351d-54ce-4019-8ffe-a2f8a2700824"; + + // When + AdminRegistrationApplication mockRegisterApplication = new AdminRegistrationApplicationBuilder() + .withId(mockApplicationId) + .build(); + Mockito.when(adminRegistrationApplicationService.findById(mockApplicationId)) + .thenReturn(mockRegisterApplication); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockApplicationId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .get(endpoint, mockSuperAdminToken.getAccessToken()); + + AdminRegistrationApplicationResponse mockApplicationResponse = adminRegistrationApplicationToApplicationResponseMapper + .map(mockRegisterApplication); + AysResponse mockResponse = AysResponse + .successOf(mockApplicationResponse); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .isNotEmpty()); + + // Verify + Mockito.verify(adminRegistrationApplicationService, Mockito.times(1)) + .findById(mockApplicationId); + + } + + @Test + void givenValidAdminRegisterApplicationId_whenUnauthorizedForGettingAdminRegisterApplicationById_thenReturnAccessDeniedException() throws Exception { + + // Given + String mockApplicationId = "68c867b4-e84a-405c-b1ab-f8dcaa9c41f2"; + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application/".concat(mockApplicationId)); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .get(endpoint, mockUserToken.getAccessToken()); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isForbidden()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .doesNotExist()); + + // Verify + Mockito.verify(adminRegistrationApplicationService, Mockito.never()) + .findById(mockApplicationId); + } + + @ParameterizedTest + @ValueSource(strings = { + "This is a valid text with sufficient length and contains alphabetic characters.", + "This text includes numbers 12345 and still should be accepted because it's within limits.", + "This text, which includes punctuation marks, should be accepted.", + "ÇalıŞkan ve dÜrüst İnsanlar her zaman başarıyı yakalar.(:;?/)" + }) + void givenValidAdminRegisterApplicationCreateRequest_whenCreatingAdminRegisterApplication_thenReturnAdminRegisterApplicationCreateResponse(String reason) throws Exception { + + // Given + AdminRegistrationApplicationCreateRequest mockRequest = new AdminRegistrationApplicationCreateRequestBuilder() + .withValidValues() + .withInstitutionId(AysValidTestData.SuperAdmin.INSTITUTION_ID) + .withReason(reason) + .build(); + + // When + Institution mockInstitution = new InstitutionBuilder() + .withId(mockRequest.getInstitutionId()) + .build(); + AdminRegistrationApplication mockRegisterApplication = new AdminRegistrationApplicationBuilder() + .withValidValues() + .withInstitution(mockInstitution) + .withReason(mockRequest.getReason()) + .withStatus(AdminRegistrationApplicationStatus.WAITING) + .build(); + + Mockito.when(adminRegistrationApplicationService.create(Mockito.any(AdminRegistrationApplicationCreateRequest.class))) + .thenReturn(mockRegisterApplication); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockSuperAdminToken.getAccessToken(), mockRequest); + + AdminRegistrationApplicationCreateResponse mockApplicationCreateResponse = adminRegistrationApplicationToCreateResponseMapper + .map(mockRegisterApplication); + AysResponse mockResponse = AysResponse.successOf(mockApplicationCreateResponse); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .isNotEmpty()); + + // Verify + Mockito.verify(adminRegistrationApplicationService, Mockito.times(1)) + .create(Mockito.any(AdminRegistrationApplicationCreateRequest.class)); + } + + @ParameterizedTest + @ValueSource(strings = { + "Invalid reason with special characters: #$%", + "#$½#$£#$£#$$#½#£$£#$#£½#$½#$½$£#$#£$$#½#$$½", + ".,..,.,.,.,.,,.,.,.,.,.,.,.,.,..,.,.,,.,.,.,", + "Too short", + " a", + " spaceAtTheBeginning", + "spaceAtTheEnd ", + " both ", + " justAString ", + "151201485621548562154851458614125461254125412", + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean vestibulum commodo turpis, sed venenatis sapien suscipit sit amet. Etiam malesuada, ligula in semper varius, nisi mi pulvinar libero, ut commodo dolor orci quis urna. Vivamus ac euismod ex. Proin vel vulputate orci. Ut id nibh finibus, mattis sem id, maximus ante. Proin fringilla ipsum at arcu venenatis, non bibendum justo luctus. Phasellus vestibulum feugiat est sit amet bibendum. Donec nulla leo, ultricies sed pharetra sed, hendrerit vel nunc." + }) + void givenInvalidAdminRegisterApplicationCreateRequest_whenCreatingAdminRegisterApplication_thenReturnValidationError(String invalidReason) throws Exception { + + // Given + AdminRegistrationApplicationCreateRequest createRequest = new AdminRegistrationApplicationCreateRequestBuilder() + .withValidValues() + .withReason(invalidReason) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockUserToken.getAccessToken(), createRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(adminRegistrationApplicationService, Mockito.never()) + .create(Mockito.any(AdminRegistrationApplicationCreateRequest.class)); + } + + @Test + void givenValidAdminRegisterApplicationCreateRequest_whenUnauthorizedForCreatingAdminRegisterApplication_thenReturnAccessDeniedException() throws Exception { + + // Given + AdminRegistrationApplicationCreateRequest mockRequest = new AdminRegistrationApplicationCreateRequestBuilder() + .withValidValues() + .withInstitutionId(AysValidTestData.SuperAdmin.INSTITUTION_ID) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockUserToken.getAccessToken(), mockRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isForbidden()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .doesNotExist()); + + // Verify + Mockito.verify(adminRegistrationApplicationService, Mockito.never()) + .create(Mockito.any(AdminRegistrationApplicationCreateRequest.class)); + } + + + @Test + void givenValidAdminRegisterApplicationId_whenAdminApplicationFound_thenReturnAdminApplicationSummaryResponse() throws Exception { + + // Given + String mockId = "085fbe72-caa7-439d-8db1-166ed005e120"; + AdminRegistrationApplication mockAdminRegistrationApplication = new AdminRegistrationApplicationBuilder() + .withId(mockId) + .build(); + + // When + Mockito.when(adminRegistrationApplicationService.findSummaryById(mockId)) + .thenReturn(mockAdminRegistrationApplication); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application/".concat(mockId).concat("/summary")); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .get(endpoint); + + AdminRegistrationApplicationSummaryResponse mockSummaryResponse = adminRegistrationApplicationToSummaryResponseMapper + .map(mockAdminRegistrationApplication); + AysResponse mockResponse = AysResponse + .successOf(mockSummaryResponse); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .isNotEmpty()); + } + + @Test + void givenIdAndAdminRegisterApplication_whenAdminApplicationNotFound_thenReturnUnauthorizedError() throws Exception { + + // Given + String mockId = "181e8310-6dfd-444c-aa38-056ce8401345"; + + // When + Mockito.when(adminRegistrationApplicationService.findSummaryById(mockId)) + .thenThrow(new AysAdminRegistrationApplicationNotExistByIdAuthException(mockId)); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application/".concat(mockId).concat("/summary")); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .get(endpoint); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.UNAUTHORIZED; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isUnauthorized()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .doesNotHaveJsonPath()); + } + + + @Test + void givenValidAdminRegisterRequest_whenAdminRegistered_thenReturnSuccessResponse() throws Exception { + + // Given + String mockId = "e8de09dc-a44e-40eb-bcc7-cf0141f8733c"; + AdminRegistrationApplicationCompleteRequest mockRequest = new AdminRegistrationApplicationCompleteRequestBuilder() + .withValidValues().build(); + + // When + Mockito.doNothing().when(adminRegistrationCompleteService).complete(Mockito.anyString(), Mockito.any()); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId).concat("/complete"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockRequest); + + AysResponse mockResponse = AysResponseBuilder.success(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Mockito.verify(adminRegistrationCompleteService, Mockito.times(1)) + .complete(Mockito.anyString(), Mockito.any()); + } + + + @Test + void givenIdAndAdminRegisterRequest_whenAdminApplicationNotFound_thenReturnUnauthorizedError() throws Exception { + + // Given + String mockId = "181e8310-6dfd-444c-aa38-056ce8401345"; + AdminRegistrationApplicationCompleteRequest mockRequest = new AdminRegistrationApplicationCompleteRequestBuilder() + .withValidValues().build(); + + // When + Mockito.doThrow(new AysAdminRegistrationApplicationNotExistByIdAuthException(mockId)) + .when(adminRegistrationCompleteService) + .complete(Mockito.anyString(), Mockito.any()); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId).concat("/complete"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.UNAUTHORIZED; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isUnauthorized()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .doesNotHaveJsonPath()); + + // Verify + Mockito.verify(adminRegistrationCompleteService, Mockito.times(1)) + .complete(Mockito.anyString(), Mockito.any()); + } + + @Test + void givenPhoneNumberWithAlphanumericCharacter_whenPhoneNumberIsNotValid_thenReturnValidationError() throws Exception { + + // Given + String mockId = "f1b789d0-6095-4860-85bb-e1a0b20f1d13"; + AysPhoneNumberRequest mockPhoneNumber = new AysPhoneNumberRequestBuilder() + .withCountryCode("ABC") + .withLineNumber("ABC").build(); + AdminRegistrationApplicationCompleteRequest mockRequest = new AdminRegistrationApplicationCompleteRequestBuilder() + .withValidValues() + .withPhoneNumber(mockPhoneNumber).build(); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId).concat("/complete"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(adminRegistrationCompleteService, Mockito.never()) + .complete(Mockito.anyString(), Mockito.any()); + } + + @Test + void givenPhoneNumberWithInvalidLength_whenPhoneNumberIsNotValid_thenReturnValidationError() throws Exception { + + // Given + String mockId = "25930d3f-4cea-4147-a21a-0f22c9bf72de"; + AysPhoneNumberRequest mockPhoneNumber = new AysPhoneNumberRequestBuilder() + .withCountryCode("456786745645") + .withLineNumber("6546467456435548676845321346656654").build(); + AdminRegistrationApplicationCompleteRequest mockRequest = new AdminRegistrationApplicationCompleteRequestBuilder() + .withValidValues() + .withPhoneNumber(mockPhoneNumber).build(); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId).concat("/complete"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(adminRegistrationCompleteService, Mockito.never()) + .complete(Mockito.anyString(), Mockito.any()); + } + + @Test + void givenPhoneNumberWithInvalidOperator_whenPhoneNumberIsNotValid_thenReturnValidationError() throws Exception { + + // Given + String mockId = "2028b456-e06c-4ea1-9017-b45523529576"; + final String invalidOperator = "123"; + AysPhoneNumberRequest mockPhoneNumber = new AysPhoneNumberRequestBuilder() + .withCountryCode("90") + .withLineNumber(invalidOperator + "6327218").build(); + AdminRegistrationApplicationCompleteRequest mockRequest = new AdminRegistrationApplicationCompleteRequestBuilder() + .withValidValues() + .withPhoneNumber(mockPhoneNumber).build(); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId).concat("/complete"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(adminRegistrationCompleteService, Mockito.never()) + .complete(Mockito.anyString(), Mockito.any()); + } + + @ParameterizedTest + @ValueSource(strings = { + "John 1234", + "John *^%$#", + " John", + "? John", + "J", + "J----", + "City--King", + "John Doe" + }) + void givenInvalidAdminRegisterApplicationCompleteRequestWithParametrizedInvalidNames_whenNamesAreNotValid_thenReturnValidationError(String invalidName) throws Exception { + + // Given + String mockId = "f423facc-36fe-4615-a68d-f7f1fe5cd860"; + AdminRegistrationApplicationCompleteRequest mockRequest = new AdminRegistrationApplicationCompleteRequestBuilder() + .withValidValues() + .withFirstName(invalidName) + .withLastName(invalidName) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId).concat("/complete"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(adminRegistrationCompleteService, Mockito.never()) + .complete(Mockito.anyString(), Mockito.any()); + } + + @ParameterizedTest + @ValueSource(strings = { + "abc.def@mail.c", + "abc.def@mail#archive.com", + "abc.def@mail", + "abcdef@mail..com", + "abc-@mail.com", + "admin@test@ays.com", + "admintest@ays..com", + "username@gmail..co.uk", + "user@ example.com", + "user@-example.com", + "user@example-.com", + "(user)@example.com", + "user@[192.168.1.1", + "user@exam ple.com", + "user@.com", + ".user@example.com", + " user@example.com", + "user@example.com ", + " user@example.com ", + "@missingusername.com" + }) + void givenInvalidAdminRegisterApplicationCompleteRequestWithParametrizedInvalidEmails_whenEmailsAreNotValid_thenReturnValidationError(String invalidEmail) throws Exception { + + // Given + String mockId = "53617d24-e32c-4249-b9e6-b10e63a439bd"; + AdminRegistrationApplicationCompleteRequest mockRequest = new AdminRegistrationApplicationCompleteRequestBuilder() + .withValidValues() + .withEmailAddress(invalidEmail) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId).concat("/complete"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(adminRegistrationCompleteService, Mockito.never()) + .complete(Mockito.anyString(), Mockito.any()); + } + + @ParameterizedTest + @ValueSource(strings = { + "abcdef@mail.com", + "abc+def@archive.com", + "john.doe123@example.co.uk", + "admin_123@example.org", + "admin-test@ays.com", + }) + void givenValidAdminRegisterApplicationCompleteRequestWithParametrizedValidEmails_whenEmailsAreValid_thenReturnSuccessResponse(String validEmail) throws Exception { + // Given + String mockId = "fe3760f1-8b44-4587-99a6-43e426c8c6d1"; + AdminRegistrationApplicationCompleteRequest mockRequest = new AdminRegistrationApplicationCompleteRequestBuilder() + .withValidValues() + .withEmailAddress(validEmail) + .build(); + + // When + Mockito.doNothing().when(adminRegistrationCompleteService).complete(Mockito.anyString(), Mockito.any()); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId).concat("/complete"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockRequest); + + AysResponse mockResponse = AysResponseBuilder.success(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Mockito.verify(adminRegistrationCompleteService, Mockito.times(1)) + .complete(Mockito.anyString(), Mockito.any()); + } + + @ParameterizedTest + @ValueSource(strings = { + "g", + "gfh2j" + }) + void givenInvalidAdminRegisterApplicationCompleteRequestWithInvalidPassword_whenPasswordDoesNotValid_thenReturnValidationError(String mockPassword) throws Exception { + + // Given + String mockId = "de2b9621-0bf6-45df-a173-4697797446b7"; + AdminRegistrationApplicationCompleteRequest mockRequest = new AdminRegistrationApplicationCompleteRequestBuilder() + .withValidValues() + .withPassword(mockPassword) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId).concat("/complete"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(adminRegistrationCompleteService, Mockito.never()) + .complete(Mockito.anyString(), Mockito.any()); + } + + @Test + void givenValidAdminRegisterApplicationId_whenApproveAdminRegisterApplication_thenReturnNothing() throws Exception { + // Given + String mockId = "47d91587-612f-462b-975f-ed227e2ee3a7"; + + // When + Mockito.doNothing() + .when(adminRegistrationApplicationService).approve(mockId); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application/".concat(mockId).concat("/approve")); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockSuperAdminToken.getAccessToken()); + + AysResponse mockResponse = AysResponseBuilder.success(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Mockito.verify(adminRegistrationApplicationService, Mockito.times(1)) + .approve(mockId); + } + + @Test + void givenValidAdminRegisterApplicationId_whenUnauthorizedForApprovingAdminRegisterApplication_thenReturnAccessDeniedException() throws Exception { + + // Given + String mockId = "5cbc8774-e2c3-414b-a1d8-26abcf3c9d17"; + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application/".concat(mockId).concat("/approve")); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockUserToken.getAccessToken()); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isForbidden()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .doesNotExist()); + + // Verify + Mockito.verify(adminRegistrationApplicationService, Mockito.never()) + .approve(mockId); + } + + @Test + void givenValidAdminRegisterApplicationRejectRequest_whenRejectingAdminRegisterApplication_thenReturnSuccess() throws Exception { + + // Given + String mockId = "c5c504a6-1223-483b-a8ba-6fb4ea309e00"; + AdminRegistrationApplicationRejectRequest mockRequest = new AdminRegistrationApplicationRejectRequestBuilder() + .withValidValues() + .build(); + + // When + Mockito.doNothing().when(adminRegistrationApplicationService).reject(mockId, mockRequest); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId).concat("/reject"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockSuperAdminToken.getAccessToken(), mockRequest); + + AysResponse mockResponse = AysResponseBuilder.success(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Mockito.verify(adminRegistrationApplicationService, Mockito.times(1)) + .reject(Mockito.eq(mockId), Mockito.any(AdminRegistrationApplicationRejectRequest.class)); + + } + + @Test + void givenValidAdminRegisterApplicationRejectRequest_whenUnauthorizedForRejectingAdminRegisterApplication_thenReturnAccessDeniedException() throws Exception { + + // Given + String mockId = "c27aa25e-7e2f-4bc1-8056-eb9c2948f507"; + AdminRegistrationApplicationRejectRequest mockRequest = new AdminRegistrationApplicationRejectRequestBuilder() + .withValidValues() + .build(); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId).concat("/reject"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockUserToken.getAccessToken(), mockRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isForbidden()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .doesNotExist()); + + // Verify + Mockito.verify(adminRegistrationApplicationService, Mockito.never()) + .reject(Mockito.eq(mockId), Mockito.any(AdminRegistrationApplicationRejectRequest.class)); + } + + @ParameterizedTest + @ValueSource(strings = { + "", + " ", + "less than 40", + """ + Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. + Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. + Donec qudam felis, ultricies nec, pellentesque eu, pretscsxwium quis, sem. Nulla consequat massa quis + enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, + imperdiet a, venenatdskjvndshjcndsis vitae, justo. Nullam dictum felis eu pedde mollis pretium. Integer tincidunt. + Cras dapibus. Vivdamus ewl + """, + " spaceAtTheBeginning", + "spaceAtTheEnd ", + " both ", + " justAString " + }) + void givenInvalidAdminRegisterApplicationRejectRequest_whenRejectingAdminRegisterApplication_thenReturnValidationError(String rejectReason) throws Exception { + + // Given + String mockId = "4d04bd1e-6318-43ba-ab40-57efb8afc918"; + AdminRegistrationApplicationRejectRequest mockRequest = new AdminRegistrationApplicationRejectRequestBuilder() + .withValidValues() + .withRejectReason(rejectReason) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/admin-registration-application/").concat(mockId).concat("/reject"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockUserToken.getAccessToken(), mockRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(adminRegistrationApplicationService, Mockito.never()) + .reject(Mockito.anyString(), + Mockito.any(AdminRegistrationApplicationRejectRequest.class)); + } } diff --git a/src/test/java/org/ays/auth/controller/AysAuthControllerTest.java b/src/test/java/org/ays/auth/controller/AysAuthControllerTest.java index 71e9f7653..3e27f6808 100644 --- a/src/test/java/org/ays/auth/controller/AysAuthControllerTest.java +++ b/src/test/java/org/ays/auth/controller/AysAuthControllerTest.java @@ -33,501 +33,457 @@ class AysAuthControllerTest extends AysRestControllerTest { - @MockBean - private AysAuthService authService; - - @MockBean - private AysUserPasswordService userPasswordService; - - private final AysTokenToResponseMapper tokenToResponseMapper = AysTokenToResponseMapper.initialize(); - - private static final String BASE_PATH = "/api/v1/authentication"; - - @ParameterizedTest - @ValueSource(strings = { - "abcdef@mail.com", - "abc+def@archive.com", - "john.doe123@example.co.uk", - "admin_123@example.org", - "admin-test@ays.com", - }) - void givenValidLoginRequestWithValidEmailAddress_whenTokensGeneratedSuccessfully_thenReturnTokenResponse( - String mockEmailAddress) throws Exception { - // Given - AysLoginRequest mockLoginRequest = new AysLoginRequestBuilder() - .withEmailAddress(mockEmailAddress) - .withPassword(AysValidTestData.PASSWORD) - .withSourcePage(AysSourcePage.INSTITUTION) - .build(); - - // When - Mockito.when(authService.authenticate(Mockito.any())) - .thenReturn(mockUserToken); - - // Then - String endpoint = BASE_PATH.concat("/token"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockLoginRequest); - - AysTokenResponse mockTokenResponse = tokenToResponseMapper.map(mockUserToken); - AysResponse mockResponse = AysResponseBuilder.successOf(mockTokenResponse); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .isNotEmpty()) - .andExpect(MockMvcResultMatchers.jsonPath("$.response.accessToken") - .value(mockResponse.getResponse().getAccessToken())) - .andExpect(MockMvcResultMatchers.jsonPath("$.response.accessTokenExpiresAt") - .doesNotHaveJsonPath()) - .andExpect(MockMvcResultMatchers.jsonPath("$.response.refreshToken") - .value(mockResponse.getResponse().getRefreshToken())); - - // Verify - Mockito.verify(authService, Mockito.times(1)) - .authenticate(Mockito.any()); - } - - @ParameterizedTest - @ValueSource(strings = { - "g", - "gfh2j" - }) - void givenInvalidLoginRequestWithInvalidPassword_whenPasswordDoesNotValid_thenReturnValidationError( - String mockPassword) throws Exception { - // Given - AysLoginRequest mockLoginRequest = new AysLoginRequestBuilder() - .withEmailAddress(AysValidTestData.User.EMAIL_ADDRESS) - .withPassword(mockPassword) - .withSourcePage(AysSourcePage.INSTITUTION) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/token"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockLoginRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(authService, Mockito.never()) - .authenticate(Mockito.any()); - } - - @ParameterizedTest - @ValueSource(strings = { - "abc.def@mail.c", - "abc.def@mail#archive.com", - "abc.def@mail", - "abcdef@mail..com", - "abc-@mail.com", - "admin@test@ays.com", - "admintest@ays..com", - "username@gmail..co.uk", - "user@ example.com", - "user@-example.com", - "user@example-.com", - "(user)@example.com", - "user@[192.168.1.1", - "user@exam ple.com", - "user@.com", - ".user@example.com", - " user@example.com", - "user@example.com ", - " user@example.com ", - "@missingusername.com", - "a@b.c", // 5 karakter - minimum limit altında - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@domain.com" // 255 - // karakter - // - - // maksimum - // limit - // üstünde - }) - void givenInvalidLoginRequestWithInvalidEmailAddress_whenEmailsAreNotValid_thenReturnValidationError( - String mockEmailAddress) throws Exception { - // Given - AysLoginRequest mockLoginRequest = new AysLoginRequestBuilder() - .withEmailAddress(mockEmailAddress) - .withPassword(AysValidTestData.PASSWORD) - .withSourcePage(AysSourcePage.INSTITUTION) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/token"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockLoginRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(authService, Mockito.never()) - .authenticate(Mockito.any()); - } - - @ParameterizedTest - @ValueSource(strings = { - "user@example.com", // 14 karakter - geçerli - "abcdef@mail.com", // 14 karakter - geçerli - "a.b.c.d@test.com", // 15 karakter - geçerli - "test.123@afetyonetimsistemi.org" // 29 karakter - geçerli - }) - void givenValidLoginRequestWithValidEmailAddress_whenEmailsAreValid_thenReturnSuccessResponse( - String mockEmailAddress) throws Exception { - // Given - AysLoginRequest mockLoginRequest = new AysLoginRequestBuilder() - .withEmailAddress(mockEmailAddress) - .withPassword(AysValidTestData.PASSWORD) - .withSourcePage(AysSourcePage.INSTITUTION) - .build(); - - // When - Mockito.when(authService.authenticate(Mockito.any())) - .thenReturn(mockUserToken); - - // Then - String endpoint = BASE_PATH.concat("/token"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockLoginRequest); - - AysResponse mockResponse = AysResponseBuilder.success(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .isNotEmpty()); - - // Verify - Mockito.verify(authService, Mockito.times(1)) - .authenticate(Mockito.any()); - } - - @Test - void givenValidTokenRefreshRequest_whenAccessTokenGeneratedSuccessfully_thenReturnTokenResponse() - throws Exception { - // Given - AysTokenRefreshRequest mockTokenRefreshRequest = AysTokenRefreshRequestBuilder.VALID_FOR_USER; - - // When - Mockito.when(authService.refreshAccessToken(Mockito.anyString())) - .thenReturn(mockUserToken); - - // Then - String endpoint = BASE_PATH.concat("/token/refresh"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockTokenRefreshRequest); - - AysTokenResponse mockTokenResponse = tokenToResponseMapper.map(mockUserToken); - AysResponse mockResponse = AysResponseBuilder.successOf(mockTokenResponse); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .isNotEmpty()) - .andExpect(MockMvcResultMatchers.jsonPath("$.response.accessToken") - .value(mockResponse.getResponse().getAccessToken())) - .andExpect(MockMvcResultMatchers.jsonPath("$.response.accessTokenExpiresAt") - .doesNotHaveJsonPath()) - .andExpect(MockMvcResultMatchers.jsonPath("$.response.refreshToken") - .value(mockResponse.getResponse().getRefreshToken())); - - // Verify - Mockito.verify(authService, Mockito.times(1)) - .refreshAccessToken(Mockito.anyString()); - } - - @Test - void givenValidAysTokenInvalidateRequest_whenTokensInvalidated_thenReturnSuccessResponse() throws Exception { - // Given - AysTokenInvalidateRequest mockTokenInvalidateRequest = AysTokenInvalidateRequestBuilder.VALID_FOR_USER; - - // When - Mockito.doNothing() - .when(authService) - .invalidateTokens(Mockito.any()); - - // Then - String endpoint = BASE_PATH.concat("/token/invalidate"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockUserToken.getAccessToken(), mockTokenInvalidateRequest); - - AysResponse mockResponse = AysResponseBuilder.success(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()); - - // Verify - Mockito.verify(authService, Mockito.times(1)) - .invalidateTokens(Mockito.any()); - } - - @Test - void givenValidForgotPasswordRequest_whenSendPasswordCreateMail_thenReturnSuccessResponse() throws Exception { - // Given - AysPasswordForgotRequest mockForgotPasswordRequest = new AysForgotPasswordRequestBuilder() - .withEmailAddress(AysValidTestData.Admin.EMAIL_ADDRESS) - .build(); - - // When - Mockito.doNothing() - .when(userPasswordService) - .forgotPassword(Mockito.any(AysPasswordForgotRequest.class)); - - // Then - String endpoint = BASE_PATH.concat("/password/forgot"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockForgotPasswordRequest); - - AysResponse mockResponse = AysResponseBuilder.success(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()); - - // Verify - Mockito.verify(userPasswordService, Mockito.times(1)) - .forgotPassword(Mockito.any(AysPasswordForgotRequest.class)); - } - - @ParameterizedTest - @ValueSource(strings = { - "abc.def@mail.c", - "abc.def@mail#archive.com", - "abc.def@mail", - "abcdef@mail..com", - "abc-@mail.com", - "admin@test@ays.com", - "admintest@ays..com", - "username@gmail..co.uk", - "user@ example.com", - "user@-example.com", - "user@example-.com", - "(user)@example.com", - "user@[192.168.1.1", - "user@exam ple.com", - "user@.com", - ".user@example.com", - " user@example.com", - "user@example.com ", - " user@example.com ", - "@missingusername.com" - }) - void givenForgotPasswordRequestWithInvalidEmailAddress_whenEmailDoesNotValid_thenReturnValidationError( - String mockEmailAddress) throws Exception { - - // Given - AysPasswordForgotRequest mockForgotPasswordRequest = new AysForgotPasswordRequestBuilder() - .withEmailAddress(mockEmailAddress) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/password/forgot"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockForgotPasswordRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userPasswordService, Mockito.never()) - .forgotPassword(Mockito.any(AysPasswordForgotRequest.class)); - } - - @Test - void givenForgotPasswordRequestWithoutEmailAddress_whenEmailIsNull_thenReturnValidationError() - throws Exception { - - // Given - AysPasswordForgotRequest mockForgotPasswordRequest = new AysForgotPasswordRequestBuilder() - .withEmailAddress(null) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/password/forgot"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockForgotPasswordRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userPasswordService, Mockito.never()) - .forgotPassword(Mockito.any(AysPasswordForgotRequest.class)); - } - - @Test - void givenValidId_whenCheckPasswordIdSuccessfully_thenReturnSuccessResponse() throws Exception { - // Given - String mockId = "40fb7a46-40bd-46cb-b44f-1f47162133b1"; - - // When - Mockito.doNothing() - .when(userPasswordService) - .checkPasswordChangingValidity(Mockito.anyString()); - - // Then - String endpoint = BASE_PATH.concat("/password/").concat(mockId).concat("/validity"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .get(endpoint); - - AysResponse mockResponse = AysResponseBuilder.success(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()); - - // Verify - Mockito.verify(userPasswordService, Mockito.times(1)) - .checkPasswordChangingValidity(Mockito.anyString()); - } - - @ParameterizedTest - @ValueSource(strings = { - "A", - "493268349068342" - }) - void givenId_whenIdDoesNotValid_thenReturnValidationError(String invalidId) throws Exception { - - // Then - String endpoint = BASE_PATH.concat("/password/").concat(invalidId).concat("/validity"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .get(endpoint); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userPasswordService, Mockito.never()) - .checkPasswordChangingValidity(Mockito.anyString()); - } - - @Test - void givenValidPasswordCreateRequest_whenPasswordCreated_thenReturnSuccessResponse() throws Exception { - // Given - String mockId = "1fa43c75-7a7a-4041-8cef-03be8429dd30"; - AysPasswordCreateRequest mockPasswordCreateRequest = new AysPasswordCreateRequestBuilder() - .withValidValues() - .build(); - - // When - Mockito.doNothing() - .when(userPasswordService) - .createPassword(Mockito.anyString(), Mockito.any(AysPasswordCreateRequest.class)); - - // Then - String endpoint = BASE_PATH.concat("/password/").concat(mockId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockPasswordCreateRequest); - - AysResponse mockResponse = AysResponseBuilder.success(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()); - - // Verify - Mockito.verify(userPasswordService, Mockito.times(1)) - .createPassword(Mockito.anyString(), Mockito.any(AysPasswordCreateRequest.class)); - } - - @Test - void givenPasswordCreateRequest_whenPasswordsNotEqual_thenReturnValidationError() throws Exception { - - // Given - String mockId = "1fa43c75-7a7a-4041-8cef-03be8429dd30"; - AysPasswordCreateRequest mockPasswordCreateRequest = new AysPasswordCreateRequestBuilder() - .withPassword("password") - .withPasswordRepeat("password1") - .build(); - - // Then - String endpoint = BASE_PATH.concat("/password/").concat(mockId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockPasswordCreateRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userPasswordService, Mockito.never()) - .createPassword(Mockito.anyString(), Mockito.any(AysPasswordCreateRequest.class)); - } - - @ParameterizedTest - @ValueSource(strings = { - "g", - "gfh2j" - }) - void givenPasswordCreateRequest_whenPasswordDoesNotValid_thenReturnValidationError(String mockPassword) - throws Exception { - - // Given - String mockId = "1fa43c75-7a7a-4041-8cef-03be8429dd30"; - AysPasswordCreateRequest mockPasswordCreateRequest = new AysPasswordCreateRequestBuilder() - .withPassword(mockPassword) - .withPasswordRepeat(mockPassword) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/password/").concat(mockId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockPasswordCreateRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userPasswordService, Mockito.never()) - .createPassword(Mockito.anyString(), Mockito.any(AysPasswordCreateRequest.class)); - } + + @MockBean + private AysAuthService authService; + + @MockBean + private AysUserPasswordService userPasswordService; + + + private final AysTokenToResponseMapper tokenToResponseMapper = AysTokenToResponseMapper.initialize(); + + + private static final String BASE_PATH = "/api/v1/authentication"; + + + @ParameterizedTest + @ValueSource(strings = { + "abcdef@mail.com", + "abc+def@archive.com", + "john.doe123@example.co.uk", + "admin_123@example.org", + "admin-test@ays.com", + }) + void givenValidLoginRequestWithValidEmailAddress_whenTokensGeneratedSuccessfully_thenReturnTokenResponse(String mockEmailAddress) throws Exception { + // Given + AysLoginRequest mockLoginRequest = new AysLoginRequestBuilder() + .withEmailAddress(mockEmailAddress) + .withPassword(AysValidTestData.PASSWORD) + .withSourcePage(AysSourcePage.INSTITUTION) + .build(); + + // When + Mockito.when(authService.authenticate(Mockito.any())) + .thenReturn(mockUserToken); + + // Then + String endpoint = BASE_PATH.concat("/token"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockLoginRequest); + + AysTokenResponse mockTokenResponse = tokenToResponseMapper.map(mockUserToken); + AysResponse mockResponse = AysResponseBuilder.successOf(mockTokenResponse); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .isNotEmpty()) + .andExpect(MockMvcResultMatchers.jsonPath("$.response.accessToken") + .value(mockResponse.getResponse().getAccessToken())) + .andExpect(MockMvcResultMatchers.jsonPath("$.response.accessTokenExpiresAt") + .doesNotHaveJsonPath()) + .andExpect(MockMvcResultMatchers.jsonPath("$.response.refreshToken") + .value(mockResponse.getResponse().getRefreshToken())); + + // Verify + Mockito.verify(authService, Mockito.times(1)) + .authenticate(Mockito.any()); + } + + @ParameterizedTest + @ValueSource(strings = { + "g", + "gfh2j" + }) + void givenInvalidLoginRequestWithInvalidPassword_whenPasswordDoesNotValid_thenReturnValidationError(String mockPassword) throws Exception { + // Given + AysLoginRequest mockLoginRequest = new AysLoginRequestBuilder() + .withEmailAddress(AysValidTestData.User.EMAIL_ADDRESS) + .withPassword(mockPassword) + .withSourcePage(AysSourcePage.INSTITUTION) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/token"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockLoginRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(authService, Mockito.never()) + .authenticate(Mockito.any()); + } + + @ParameterizedTest + @ValueSource(strings = { + "abc.def@mail.c", + "abc.def@mail#archive.com", + "abc.def@mail", + "abcdef@mail..com", + "abc-@mail.com", + "admin@test@ays.com", + "admintest@ays..com", + "username@gmail..co.uk", + "user@ example.com", + "user@-example.com", + "user@example-.com", + "(user)@example.com", + "user@[192.168.1.1", + "user@exam ple.com", + "user@.com", + ".user@example.com", + " user@example.com", + "user@example.com ", + " user@example.com ", + "@missingusername.com" + }) + void givenInvalidLoginRequestWithInvalidEmailAddress_whenEmailsAreNotValid_thenReturnValidationError(String mockEmailAddress) throws Exception { + // Given + AysLoginRequest mockLoginRequest = new AysLoginRequestBuilder() + .withEmailAddress(mockEmailAddress) + .withPassword(AysValidTestData.PASSWORD) + .withSourcePage(AysSourcePage.INSTITUTION) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/token"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockLoginRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(authService, Mockito.never()) + .authenticate(Mockito.any()); + } + + @Test + void givenValidTokenRefreshRequest_whenAccessTokenGeneratedSuccessfully_thenReturnTokenResponse() throws Exception { + // Given + AysTokenRefreshRequest mockTokenRefreshRequest = AysTokenRefreshRequestBuilder.VALID_FOR_USER; + + // When + Mockito.when(authService.refreshAccessToken(Mockito.anyString())) + .thenReturn(mockUserToken); + + // Then + String endpoint = BASE_PATH.concat("/token/refresh"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockTokenRefreshRequest); + + AysTokenResponse mockTokenResponse = tokenToResponseMapper.map(mockUserToken); + AysResponse mockResponse = AysResponseBuilder.successOf(mockTokenResponse); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .isNotEmpty()) + .andExpect(MockMvcResultMatchers.jsonPath("$.response.accessToken") + .value(mockResponse.getResponse().getAccessToken())) + .andExpect(MockMvcResultMatchers.jsonPath("$.response.accessTokenExpiresAt") + .doesNotHaveJsonPath()) + .andExpect(MockMvcResultMatchers.jsonPath("$.response.refreshToken") + .value(mockResponse.getResponse().getRefreshToken())); + + // Verify + Mockito.verify(authService, Mockito.times(1)) + .refreshAccessToken(Mockito.anyString()); + } + + @Test + void givenValidAysTokenInvalidateRequest_whenTokensInvalidated_thenReturnSuccessResponse() throws Exception { + // Given + AysTokenInvalidateRequest mockTokenInvalidateRequest = AysTokenInvalidateRequestBuilder.VALID_FOR_USER; + + // When + Mockito.doNothing() + .when(authService) + .invalidateTokens(Mockito.any()); + + // Then + String endpoint = BASE_PATH.concat("/token/invalidate"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockUserToken.getAccessToken(), mockTokenInvalidateRequest); + + AysResponse mockResponse = AysResponseBuilder.success(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Mockito.verify(authService, Mockito.times(1)) + .invalidateTokens(Mockito.any()); + } + + + @Test + void givenValidForgotPasswordRequest_whenSendPasswordCreateMail_thenReturnSuccessResponse() throws Exception { + // Given + AysPasswordForgotRequest mockForgotPasswordRequest = new AysForgotPasswordRequestBuilder() + .withEmailAddress(AysValidTestData.Admin.EMAIL_ADDRESS) + .build(); + + // When + Mockito.doNothing() + .when(userPasswordService) + .forgotPassword(Mockito.any(AysPasswordForgotRequest.class)); + + // Then + String endpoint = BASE_PATH.concat("/password/forgot"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockForgotPasswordRequest); + + AysResponse mockResponse = AysResponseBuilder.success(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Mockito.verify(userPasswordService, Mockito.times(1)) + .forgotPassword(Mockito.any(AysPasswordForgotRequest.class)); + } + + @ParameterizedTest + @ValueSource(strings = { + "abc.def@mail.c", + "abc.def@mail#archive.com", + "abc.def@mail", + "abcdef@mail..com", + "abc-@mail.com", + "admin@test@ays.com", + "admintest@ays..com", + "username@gmail..co.uk", + "user@ example.com", + "user@-example.com", + "user@example-.com", + "(user)@example.com", + "user@[192.168.1.1", + "user@exam ple.com", + "user@.com", + ".user@example.com", + " user@example.com", + "user@example.com ", + " user@example.com ", + "@missingusername.com" + }) + void givenForgotPasswordRequestWithInvalidEmailAddress_whenEmailDoesNotValid_thenReturnValidationError(String mockEmailAddress) throws Exception { + + // Given + AysPasswordForgotRequest mockForgotPasswordRequest = new AysForgotPasswordRequestBuilder() + .withEmailAddress(mockEmailAddress) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/password/forgot"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockForgotPasswordRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userPasswordService, Mockito.never()) + .forgotPassword(Mockito.any(AysPasswordForgotRequest.class)); + } + + @Test + void givenForgotPasswordRequestWithoutEmailAddress_whenEmailIsNull_thenReturnValidationError() throws Exception { + + // Given + AysPasswordForgotRequest mockForgotPasswordRequest = new AysForgotPasswordRequestBuilder() + .withEmailAddress(null) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/password/forgot"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockForgotPasswordRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userPasswordService, Mockito.never()) + .forgotPassword(Mockito.any(AysPasswordForgotRequest.class)); + } + + + @Test + void givenValidId_whenCheckPasswordIdSuccessfully_thenReturnSuccessResponse() throws Exception { + // Given + String mockId = "40fb7a46-40bd-46cb-b44f-1f47162133b1"; + + // When + Mockito.doNothing() + .when(userPasswordService) + .checkPasswordChangingValidity(Mockito.anyString()); + + // Then + String endpoint = BASE_PATH.concat("/password/").concat(mockId).concat("/validity"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .get(endpoint); + + AysResponse mockResponse = AysResponseBuilder.success(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Mockito.verify(userPasswordService, Mockito.times(1)) + .checkPasswordChangingValidity(Mockito.anyString()); + } + + @ParameterizedTest + @ValueSource(strings = { + "A", + "493268349068342" + }) + void givenId_whenIdDoesNotValid_thenReturnValidationError(String invalidId) throws Exception { + + // Then + String endpoint = BASE_PATH.concat("/password/").concat(invalidId).concat("/validity"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .get(endpoint); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userPasswordService, Mockito.never()) + .checkPasswordChangingValidity(Mockito.anyString()); + } + + + @Test + void givenValidPasswordCreateRequest_whenPasswordCreated_thenReturnSuccessResponse() throws Exception { + // Given + String mockId = "1fa43c75-7a7a-4041-8cef-03be8429dd30"; + AysPasswordCreateRequest mockPasswordCreateRequest = new AysPasswordCreateRequestBuilder() + .withValidValues() + .build(); + + // When + Mockito.doNothing() + .when(userPasswordService) + .createPassword(Mockito.anyString(), Mockito.any(AysPasswordCreateRequest.class)); + + // Then + String endpoint = BASE_PATH.concat("/password/").concat(mockId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockPasswordCreateRequest); + + AysResponse mockResponse = AysResponseBuilder.success(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Mockito.verify(userPasswordService, Mockito.times(1)) + .createPassword(Mockito.anyString(), Mockito.any(AysPasswordCreateRequest.class)); + } + + @Test + void givenPasswordCreateRequest_whenPasswordsNotEqual_thenReturnValidationError() throws Exception { + + // Given + String mockId = "1fa43c75-7a7a-4041-8cef-03be8429dd30"; + AysPasswordCreateRequest mockPasswordCreateRequest = new AysPasswordCreateRequestBuilder() + .withPassword("password") + .withPasswordRepeat("password1") + .build(); + + // Then + String endpoint = BASE_PATH.concat("/password/").concat(mockId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockPasswordCreateRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userPasswordService, Mockito.never()) + .createPassword(Mockito.anyString(), Mockito.any(AysPasswordCreateRequest.class)); + } + + + @ParameterizedTest + @ValueSource(strings = { + "g", + "gfh2j" + }) + void givenPasswordCreateRequest_whenPasswordDoesNotValid_thenReturnValidationError(String mockPassword) throws Exception { + + // Given + String mockId = "1fa43c75-7a7a-4041-8cef-03be8429dd30"; + AysPasswordCreateRequest mockPasswordCreateRequest = new AysPasswordCreateRequestBuilder() + .withPassword(mockPassword) + .withPasswordRepeat(mockPassword) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/password/").concat(mockId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockPasswordCreateRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userPasswordService, Mockito.never()) + .createPassword(Mockito.anyString(), Mockito.any(AysPasswordCreateRequest.class)); + } } diff --git a/src/test/java/org/ays/auth/controller/AysUserControllerTest.java b/src/test/java/org/ays/auth/controller/AysUserControllerTest.java index d3902d14f..c178b2e25 100644 --- a/src/test/java/org/ays/auth/controller/AysUserControllerTest.java +++ b/src/test/java/org/ays/auth/controller/AysUserControllerTest.java @@ -1,5 +1,6 @@ package org.ays.auth.controller; + import org.ays.AysRestControllerTest; import org.ays.auth.model.AysUser; import org.ays.auth.model.AysUserBuilder; @@ -42,1026 +43,982 @@ class AysUserControllerTest extends AysRestControllerTest { - @MockBean - private AysUserReadService userReadService; - - @MockBean - private AysUserCreateService userCreateService; - - @MockBean - private AysUserUpdateService userUpdateService; - - private final AysUserToUsersResponseMapper userToUsersResponseMapper = AysUserToUsersResponseMapper - .initialize(); - private final AysUserToResponseMapper userToResponseMapper = AysUserToResponseMapper.initialize(); - - private static final String BASE_PATH = "/api/v1"; - - @Test - void givenValidUserListRequest_whenUsersFound_thenReturnAysPageResponseOfUsersResponse() throws Exception { - // Given - AysUserListRequest mockListRequest = new AysUserListRequestBuilder() - .withValidValues() - .build(); - - // When - List mockUsers = List.of( - new AysUserBuilder().withValidValues().build()); - - AysPage mockUserPage = AysPageBuilder - .from(mockUsers, mockListRequest.getPageable()); - - Mockito.when(userReadService.findAll(Mockito.any(AysUserListRequest.class))) - .thenReturn(mockUserPage); - - // Then - String endpoint = BASE_PATH.concat("/users"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockSuperAdminToken.getAccessToken(), mockListRequest); - - List mockUsersResponse = userToUsersResponseMapper.map(mockUsers); - AysPageResponse pageOfResponse = AysPageResponse.builder() - .of(mockUserPage) - .content(mockUsersResponse) - .build(); - AysResponse> mockResponse = AysResponse - .successOf(pageOfResponse); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .isNotEmpty()); - - // Verify - Mockito.verify(userReadService, Mockito.times(1)) - .findAll(Mockito.any(AysUserListRequest.class)); - } - - @ParameterizedTest - @ValueSource(strings = { - "Lorem ipsum dolor sit amet consectetur adipiscing elit Integer nec odio Praesent libero Sed cursus ante dapibus diam Sed nisi Nulla quis sem at nibh", - "Test user 1234", - "User *^%$#", - " Test", - "? User", - "J", - "J----", - "Martin-Luther--King", - "John Doe" - }) - void givenUserListRequest_whenFirstNameDoesNotValid_thenReturnValidationError(String invalidName) - throws Exception { - - // Given - AysUserListRequest mockListRequest = new AysUserListRequestBuilder() - .withValidValues() - .withFirstName(invalidName) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/users"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockSuperAdminToken.getAccessToken(), mockListRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userReadService, Mockito.never()) - .findAll(Mockito.any(AysUserListRequest.class)); - } - - @ParameterizedTest - @ValueSource(strings = { - "Lorem ipsum dolor sit amet consectetur adipiscing elit Integer nec odio Praesent libero Sed cursus ante dapibus diam Sed nisi Nulla quis sem at nibh", - "Test user 1234", - "User *^%$#", - " Test", - "? User", - "J", - "J----", - "Martin-Luther--King", - "John Doe" - }) - void givenUserListRequest_whenLastNameDoesNotValid_thenReturnValidationError(String invalidName) - throws Exception { - - // Given - AysUserListRequest mockListRequest = new AysUserListRequestBuilder() - .withValidValues() - .withLastName(invalidName) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/users"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockSuperAdminToken.getAccessToken(), mockListRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userReadService, Mockito.never()) - .findAll(Mockito.any(AysUserListRequest.class)); - } - - @ParameterizedTest - @ValueSource(strings = { - "City user 1234", - "City *^%$#", - " Test", - "? User", - "J", - "J----", - "City--King", - "John Doe" - }) - void givenUserListRequest_whenCityDoesNotValid_thenReturnValidationError(String invalidName) throws Exception { - - // Given - AysUserListRequest mockListRequest = new AysUserListRequestBuilder() - .withValidValues() - .withCity(invalidName) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/users"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockSuperAdminToken.getAccessToken(), mockListRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userReadService, Mockito.never()) - .findAll(Mockito.any(AysUserListRequest.class)); - } - - @ParameterizedTest - @ValueSource(strings = { - "1", - "109", - "99999", - "15" - }) - void givenInvalidUserListRequest_whenPageSizeNotTen_thenReturnValidationError(int invalidPageSize) - throws Exception { - - // Given - AysUserListRequest mockListRequest = new AysUserListRequestBuilder() - .withPageable(new AysPageableBuilder() - .withPage(1) - .withPageSize(invalidPageSize) - .build()) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/users"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockSuperAdminToken.getAccessToken(), mockListRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userReadService, Mockito.never()) - .findAll(Mockito.any(AysUserListRequest.class)); - } - - @Test - void givenValidUserListRequest_whenUserUnauthorized_thenReturnAccessDeniedException() throws Exception { - // Given - AysUserListRequest mockListRequest = new AysUserListRequestBuilder() - .withValidValues() - .build(); - - // Then - String endpoint = BASE_PATH.concat("/users"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockUserToken.getAccessToken(), mockListRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isForbidden()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .doesNotExist()); - - // Verify - Mockito.verify(userReadService, Mockito.never()) - .findAll(Mockito.any(AysUserListRequest.class)); - } - - @Test - void givenValidUserId_whenUserFound_thenReturnAysUserResponse() throws Exception { - - // Given - String mockUserId = AysRandomUtil.generateUUID(); - - // When - AysUser mockUser = new AysUserBuilder() - .withValidValues() - .withId(mockUserId) - .build(); - - Mockito.when(userReadService.findById(mockUserId)) - .thenReturn(mockUser); - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(mockUserId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .get(endpoint, mockAdminToken.getAccessToken()); - - AysUserResponse mockUserResponse = userToResponseMapper - .map(mockUser); - AysResponse mockResponse = AysResponse - .successOf(mockUserResponse); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .isNotEmpty()); - - // Verify - Mockito.verify(userReadService, Mockito.times(1)) - .findById(mockUserId); - } - - @Test - void givenUserId_whenUnauthorizedForGettingUserById_thenReturnAccessDeniedException() throws Exception { - - // Given - String mockUserId = AysRandomUtil.generateUUID(); - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(mockUserId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .get(endpoint, mockUserToken.getAccessToken()); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isForbidden()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .doesNotExist()); - - // Verify - Mockito.verify(userReadService, Mockito.never()) - .findById(mockUserId); - } - - @ParameterizedTest - @ValueSource(strings = { - "A", - "493268349068342" - }) - void givenInvalidId_whenIdNotValid_thenReturnValidationError(String invalidId) throws Exception { - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(invalidId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .get(endpoint, mockAdminToken.getAccessToken()); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userReadService, Mockito.never()) - .findById(Mockito.anyString()); - } - - @Test - void givenUserCreateRequest_whenUserCreated_thenReturnSuccess() throws Exception { - - // Given - AysUserCreateRequest mockCreateRequest = new AysUserCreateRequestBuilder() - .withValidValues() - .build(); - - // When - Mockito.doNothing() - .when(userCreateService) - .create(Mockito.any(AysUserCreateRequest.class)); - - // Then - String endpoint = BASE_PATH.concat("/user"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockAdminToken.getAccessToken(), mockCreateRequest); - - AysResponse mockResponse = AysResponseBuilder.success(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()); - - // Verify - Mockito.verify(userCreateService, Mockito.times(1)) - .create(Mockito.any(AysUserCreateRequest.class)); - } - - @Test - void givenValidUserCreateRequest_whenUserUnauthorized_thenReturnAccessDeniedException() throws Exception { - - // Given - AysUserCreateRequest mockCreateRequest = new AysUserCreateRequestBuilder() - .withValidValues() - .build(); - - // Then - String endpoint = BASE_PATH.concat("/user"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockUserToken.getAccessToken(), mockCreateRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isForbidden()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .doesNotExist()); - - // Verify - Mockito.verify(userCreateService, Mockito.never()) - .create(Mockito.any(AysUserCreateRequest.class)); - } - - @Test - void givenUserCreateRequest_whenRoleIdsAreNull_thenReturnValidationError() throws Exception { - - // Given - AysUserCreateRequest mockCreateRequest = new AysUserCreateRequestBuilder() - .withValidValues() - .withRoleIds(null) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/user"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockAdminToken.getAccessToken(), mockCreateRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userCreateService, Mockito.never()) - .create(Mockito.any(AysUserCreateRequest.class)); - } - - @Test - void givenUserCreateRequest_whenRoleIdsAreEmpty_thenReturnValidationError() throws Exception { - - // Given - AysUserCreateRequest mockCreateRequest = new AysUserCreateRequestBuilder() - .withValidValues() - .withRoleIds(Set.of()) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/user"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockAdminToken.getAccessToken(), mockCreateRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userCreateService, Mockito.never()) - .create(Mockito.any(AysUserCreateRequest.class)); - } - - @ParameterizedTest - @NullSource - @ValueSource(strings = { - "", - "55aed4c4facb4b66bdb5-309eaaef4453" - }) - void givenUserCreateRequest_whenRoleIdIsNotValid_thenReturnValidationError(String invalidRoleId) - throws Exception { - - // Given - AysUserCreateRequest mockCreateRequest = new AysUserCreateRequestBuilder() - .withValidValues() - .withRoleIds(Collections.singleton(invalidRoleId)) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/user"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockAdminToken.getAccessToken(), mockCreateRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userCreateService, Mockito.never()) - .create(Mockito.any(AysUserCreateRequest.class)); - } - - @ParameterizedTest - @ValueSource(strings = { - "abc.def@mail.c", - "abc.def@mail#archive.com", - "abc.def@mail", - "abcdef@mail..com", - "abc-@mail.com", - "admin@test@ays.com", - "admintest@ays..com", - "username@gmail..co.uk", - "user@ example.com", - "user@-example.com", - "user@example-.com", - "(user)@example.com", - "user@[192.168.1.1", - "user@exam ple.com", - "user@.com", - ".user@example.com", - " user@example.com", - "user@example.com ", - " user@example.com ", - "@missingusername.com", - "a@b.c", // 5 karakter - minimum limit altında - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@domain.com" // 255 - // karakter - // - - // maksimum - // limit - // üstünde - }) - void givenUserCreateRequest_whenEmailNotValid_thenReturnValidationError(String mockEmailAddress) - throws Exception { - // Given - AysUserCreateRequest mockCreateRequest = new AysUserCreateRequestBuilder() - .withValidValues() - .withEmailAddress(mockEmailAddress) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/user"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockAdminToken.getAccessToken(), mockCreateRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userCreateService, Mockito.never()) - .create(Mockito.any(AysUserCreateRequest.class)); - } - - @ParameterizedTest - @ValueSource(strings = { - "user@example.com", // 14 karakter - geçerli - "abcdef@mail.com", // 14 karakter - geçerli - "a.b.c.d@test.com", // 15 karakter - geçerli - "test.123@afetyonetimsistemi.org" // 29 karakter - geçerli - }) - void givenUserCreateRequest_whenEmailValid_thenReturnSuccessResponse(String mockEmailAddress) throws Exception { - // Given - AysUserCreateRequest mockCreateRequest = new AysUserCreateRequestBuilder() - .withValidValues() - .withEmailAddress(mockEmailAddress) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/user"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, mockAdminToken.getAccessToken(), mockCreateRequest); - - AysResponse mockResponse = AysResponseBuilder.success(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()); - - // Verify - Mockito.verify(userCreateService, Mockito.times(1)) - .create(Mockito.any(AysUserCreateRequest.class)); - } - - @Test - void givenValidIdAndUserUpdateRequest_whenUserUpdated_thenReturnSuccess() throws Exception { - - // Given - String mockId = "2cb9f39b-490f-4035-97ac-9afbb87506df"; - AysUserUpdateRequest mockUpdateRequest = new AysUserUpdateRequestBuilder() - .withValidValues() - .build(); - - // When - Mockito.doNothing() - .when(userUpdateService) - .update(Mockito.any(), Mockito.any(AysUserUpdateRequest.class)); - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(mockId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .put(endpoint, mockAdminToken.getAccessToken(), mockUpdateRequest); - - AysResponse mockResponse = AysResponseBuilder.success(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()); - - // Verify - Mockito.verify(userUpdateService, Mockito.times(1)) - .update(Mockito.anyString(), Mockito.any(AysUserUpdateRequest.class)); - } - - @Test - void givenValidIdAndUserUpdateRequest_whenUserUnauthorized_thenReturnAccessDeniedException() throws Exception { - - // Given - String mockId = "0deb5f16-479f-4625-9354-7db29c861e85"; - AysUserUpdateRequest mockUpdateRequest = new AysUserUpdateRequestBuilder() - .withValidValues() - .build(); - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(mockId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .put(endpoint, mockUserToken.getAccessToken(), mockUpdateRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isForbidden()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .doesNotExist()); - - // Verify - Mockito.verify(userUpdateService, Mockito.never()) - .update(Mockito.anyString(), Mockito.any(AysUserUpdateRequest.class)); - } - - @ParameterizedTest - @ValueSource(strings = { - "A", - "493268349068342" - }) - void givenInvalidIdAndValidUserUpdateRequest_whenIdNotValid_thenReturnValidationError(String invalidId) - throws Exception { - - // Given - AysUserUpdateRequest mockUpdateRequest = new AysUserUpdateRequestBuilder() - .withValidValues() - .build(); - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(invalidId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .put(endpoint, mockAdminToken.getAccessToken(), mockUpdateRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userUpdateService, Mockito.never()) - .update(Mockito.anyString(), Mockito.any(AysUserUpdateRequest.class)); - } - - @Test - void givenValidIdAndInvalidUserUpdateRequest_whenRoleIdsAreNull_thenReturnValidationError() throws Exception { - - // Given - String mockId = "3b5a65ba-c90e-41ad-97ec-56c7dd4fe708"; - AysUserUpdateRequest mockUpdateRequest = new AysUserUpdateRequestBuilder() - .withValidValues() - .withRoleIds(null) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(mockId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .put(endpoint, mockAdminToken.getAccessToken(), mockUpdateRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userUpdateService, Mockito.never()) - .update(Mockito.anyString(), Mockito.any(AysUserUpdateRequest.class)); - } - - @Test - void givenValidIdAndInvalidUserUpdateRequest_whenRoleIdsAreEmpty_thenReturnValidationError() throws Exception { - - // Given - String mockId = "daaa28f9-f7b3-4f43-a10f-a5fe2b0eb2fa"; - AysUserUpdateRequest mockUpdateRequest = new AysUserUpdateRequestBuilder() - .withValidValues() - .withRoleIds(Set.of()) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(mockId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .put(endpoint, mockAdminToken.getAccessToken(), mockUpdateRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userUpdateService, Mockito.never()) - .update(Mockito.anyString(), Mockito.any(AysUserUpdateRequest.class)); - } - - @ParameterizedTest - @NullSource - @ValueSource(strings = { - "", - "55aed4c4facb4b66bdb5-309eaaef4453" - }) - void givenValidIdAndInvalidUserUpdateRequest_whenRoleIdIsNotValid_thenReturnValidationError( - String invalidRoleId) throws Exception { - - // Given - String mockId = "b8758dce-ad8e-438d-b31c-b440b352068a"; - - Set mockRoleIds = new HashSet<>(); - mockRoleIds.add(invalidRoleId); - AysUserUpdateRequest mockUpdateRequest = new AysUserUpdateRequestBuilder() - .withValidValues() - .withRoleIds(mockRoleIds) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(mockId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .put(endpoint, mockAdminToken.getAccessToken(), mockUpdateRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userUpdateService, Mockito.never()) - .update(Mockito.anyString(), Mockito.any(AysUserUpdateRequest.class)); - } - - @ParameterizedTest - @ValueSource(strings = { - "abc.def@mail.c", - "abc.def@mail#archive.com", - "abc.def@mail", - "abcdef@mail..com", - "abc-@mail.com", - "admin@test@ays.com", - "admintest@ays..com", - "username@gmail..co.uk", - "user@ example.com", - "user@-example.com", - "user@example-.com", - "(user)@example.com", - "user@[192.168.1.1", - "user@exam ple.com", - "user@.com", - ".user@example.com", - " user@example.com", - "user@example.com ", - " user@example.com ", - "@missingusername.com", - "a@b.c", // 5 karakter - minimum limit altında - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@domain.com" // 255 - // karakter - // - - // maksimum - // limit - // üstünde - }) - void givenValidIdAndInvalidUserUpdateRequest_whenEmailNotValid_thenReturnValidationError( - String mockEmailAddress) throws Exception { - // Given - String mockId = "b66afcb0-3029-4968-a87b-e1d94fc75642"; - - AysUserUpdateRequest mockUpdateRequest = new AysUserUpdateRequestBuilder() - .withValidValues() - .withEmailAddress(mockEmailAddress) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(mockId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .put(endpoint, mockAdminToken.getAccessToken(), mockUpdateRequest); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userUpdateService, Mockito.never()) - .update(Mockito.anyString(), Mockito.any(AysUserUpdateRequest.class)); - } - - @Test - void givenValidId_whenActivateUser_thenReturnSuccess() throws Exception { - - // Given - String mockId = "793fcc5d-31cc-4704-9f0a-627ac7da517d"; - - // When - Mockito.doNothing() - .when(userUpdateService) - .activate(Mockito.anyString()); - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(mockId).concat("/activate"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .patch(endpoint, mockAdminToken.getAccessToken()); - - AysResponse mockResponse = AysResponseBuilder.success(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()); - - // Verify - Mockito.verify(userUpdateService, Mockito.times(1)) - .activate(Mockito.anyString()); - } - - @Test - void givenValidId_whenUserUnauthorized_thenReturnAccessDeniedException() throws Exception { - - // Given - String mockId = "201aec72-ecd8-49fc-86f5-2b5458871edb"; - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(mockId).concat("/activate"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .patch(endpoint, mockUserToken.getAccessToken()); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isForbidden()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()); - - // Verify - Mockito.verify(userUpdateService, Mockito.never()) - .activate(Mockito.anyString()); - } - - @ParameterizedTest - @ValueSource(strings = { - "A", - "493268349068342" - }) - void givenInvalidUserId_whenIdNotValid_thenReturnValidationError(String invalidId) throws Exception { - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(invalidId).concat("/activate"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .patch(endpoint, mockAdminToken.getAccessToken()); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userUpdateService, Mockito.never()) - .activate(Mockito.anyString()); - } - - @Test - void givenValidId_whenUserDeleted_thenReturnSuccess() throws Exception { - - // Given - String mockId = "2e574ecf-929c-4923-8aea-d061d29934da"; - - // When - Mockito.doNothing() - .when(userUpdateService) - .delete(Mockito.any()); - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(mockId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .delete(endpoint, mockAdminToken.getAccessToken()); - - AysResponse mockResponse = AysResponseBuilder.success(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()); - - // Verify - Mockito.verify(userUpdateService, Mockito.times(1)) - .delete(mockId); - } - - @ParameterizedTest - @ValueSource(strings = { - "A", - "493268349068342" - }) - void givenId_whenIdDoesNotValid_thenReturnValidationError(String invalidId) throws Exception { - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(invalidId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .delete(endpoint, mockAdminToken.getAccessToken()); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userUpdateService, Mockito.never()) - .delete(Mockito.anyString()); - } - - @Test - void givenUserDelete_whenUserUnauthorized_thenReturnAccessDeniedException() throws Exception { - - // Given - String mockId = "45082f52-011b-41d1-b4bd-6eba4e1f1ea8"; - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(mockId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .delete(endpoint, mockUserToken.getAccessToken()); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isForbidden()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()); - - // Verify - Mockito.verify(userUpdateService, Mockito.never()) - .delete(Mockito.anyString()); - } - - @Test - void givenValidId_whenPassivateUser_thenReturnSuccess() throws Exception { - - // Given - String mockId = "894dcc5d-31cc-4704-9f0a-627ac7da517d"; - - // When - Mockito.doNothing() - .when(userUpdateService) - .passivate(Mockito.anyString()); - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(mockId).concat("/passivate"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .patch(endpoint, mockAdminToken.getAccessToken()); - - AysResponse mockResponse = AysResponseBuilder.success(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()); - - // Verify - Mockito.verify(userUpdateService, Mockito.times(1)) - .passivate(Mockito.anyString()); - } - - @ParameterizedTest - @ValueSource(strings = { - "A", - "493268349068342" - }) - void givenInvalidUserId_whenIdNotValid_thenReturnValidationErrorForPassivate(String invalidId) - throws Exception { - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(invalidId).concat("/passivate"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .patch(endpoint, mockAdminToken.getAccessToken()); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isBadRequest()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .isNotEmpty()); - - // Verify - Mockito.verify(userUpdateService, Mockito.never()) - .passivate(Mockito.anyString()); - } - - @Test - void givenValidUserId_whenUserUnauthorized_thenReturnAccessDeniedException() throws Exception { - - // Given - String mockId = "333aec72-ecd8-49fc-86f5-2b5458871edb"; - - // Then - String endpoint = BASE_PATH.concat("/user/").concat(mockId).concat("/passivate"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .patch(endpoint, mockUserToken.getAccessToken()); - - AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isForbidden()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()); - - // Verify - Mockito.verify(userUpdateService, Mockito.never()) - .passivate(Mockito.anyString()); - } + @MockBean + private AysUserReadService userReadService; + + @MockBean + private AysUserCreateService userCreateService; + + @MockBean + private AysUserUpdateService userUpdateService; + + + private final AysUserToUsersResponseMapper userToUsersResponseMapper = AysUserToUsersResponseMapper.initialize(); + private final AysUserToResponseMapper userToResponseMapper = AysUserToResponseMapper.initialize(); + + + private static final String BASE_PATH = "/api/v1"; + + + @Test + void givenValidUserListRequest_whenUsersFound_thenReturnAysPageResponseOfUsersResponse() throws Exception { + // Given + AysUserListRequest mockListRequest = new AysUserListRequestBuilder() + .withValidValues() + .build(); + + //When + List mockUsers = List.of( + new AysUserBuilder().withValidValues().build() + ); + + AysPage mockUserPage = AysPageBuilder + .from(mockUsers, mockListRequest.getPageable()); + + Mockito.when(userReadService.findAll(Mockito.any(AysUserListRequest.class))) + .thenReturn(mockUserPage); + + // Then + String endpoint = BASE_PATH.concat("/users"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockSuperAdminToken.getAccessToken(), mockListRequest); + + List mockUsersResponse = userToUsersResponseMapper.map(mockUsers); + AysPageResponse pageOfResponse = AysPageResponse.builder() + .of(mockUserPage) + .content(mockUsersResponse) + .build(); + AysResponse> mockResponse = AysResponse + .successOf(pageOfResponse); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .isNotEmpty()); + + // Verify + Mockito.verify(userReadService, Mockito.times(1)) + .findAll(Mockito.any(AysUserListRequest.class)); + } + + @ParameterizedTest + @ValueSource(strings = { + "Lorem ipsum dolor sit amet consectetur adipiscing elit Integer nec odio Praesent libero Sed cursus ante dapibus diam Sed nisi Nulla quis sem at nibh", + "Test user 1234", + "User *^%$#", + " Test", + "? User", + "J", + "J----", + "Martin-Luther--King", + "John Doe" + }) + void givenUserListRequest_whenFirstNameDoesNotValid_thenReturnValidationError(String invalidName) throws Exception { + + // Given + AysUserListRequest mockListRequest = new AysUserListRequestBuilder() + .withValidValues() + .withFirstName(invalidName) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/users"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockSuperAdminToken.getAccessToken(), mockListRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userReadService, Mockito.never()) + .findAll(Mockito.any(AysUserListRequest.class)); + } + + @ParameterizedTest + @ValueSource(strings = { + "Lorem ipsum dolor sit amet consectetur adipiscing elit Integer nec odio Praesent libero Sed cursus ante dapibus diam Sed nisi Nulla quis sem at nibh", + "Test user 1234", + "User *^%$#", + " Test", + "? User", + "J", + "J----", + "Martin-Luther--King", + "John Doe" + }) + void givenUserListRequest_whenLastNameDoesNotValid_thenReturnValidationError(String invalidName) throws Exception { + + // Given + AysUserListRequest mockListRequest = new AysUserListRequestBuilder() + .withValidValues() + .withLastName(invalidName) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/users"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockSuperAdminToken.getAccessToken(), mockListRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userReadService, Mockito.never()) + .findAll(Mockito.any(AysUserListRequest.class)); + } + + @ParameterizedTest + @ValueSource(strings = { + "City user 1234", + "City *^%$#", + " Test", + "? User", + "J", + "J----", + "City--King", + "John Doe" + }) + void givenUserListRequest_whenCityDoesNotValid_thenReturnValidationError(String invalidName) throws Exception { + + // Given + AysUserListRequest mockListRequest = new AysUserListRequestBuilder() + .withValidValues() + .withCity(invalidName) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/users"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockSuperAdminToken.getAccessToken(), mockListRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userReadService, Mockito.never()) + .findAll(Mockito.any(AysUserListRequest.class)); + } + + @ParameterizedTest + @ValueSource(strings = { + "1", + "109", + "99999", + "15" + }) + void givenInvalidUserListRequest_whenPageSizeNotTen_thenReturnValidationError(int invalidPageSize) throws Exception { + + // Given + AysUserListRequest mockListRequest = new AysUserListRequestBuilder() + .withPageable(new AysPageableBuilder() + .withPage(1) + .withPageSize(invalidPageSize) + .build() + ) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/users"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockSuperAdminToken.getAccessToken(), mockListRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userReadService, Mockito.never()) + .findAll(Mockito.any(AysUserListRequest.class)); + } + + @Test + void givenValidUserListRequest_whenUserUnauthorized_thenReturnAccessDeniedException() throws Exception { + // Given + AysUserListRequest mockListRequest = new AysUserListRequestBuilder() + .withValidValues() + .build(); + + // Then + String endpoint = BASE_PATH.concat("/users"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockUserToken.getAccessToken(), mockListRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isForbidden()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .doesNotExist()); + + // Verify + Mockito.verify(userReadService, Mockito.never()) + .findAll(Mockito.any(AysUserListRequest.class)); + } + + + @Test + void givenValidUserId_whenUserFound_thenReturnAysUserResponse() throws Exception { + + // Given + String mockUserId = AysRandomUtil.generateUUID(); + + // When + AysUser mockUser = new AysUserBuilder() + .withValidValues() + .withId(mockUserId) + .build(); + + Mockito.when(userReadService.findById(mockUserId)) + .thenReturn(mockUser); + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(mockUserId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .get(endpoint, mockAdminToken.getAccessToken()); + + AysUserResponse mockUserResponse = userToResponseMapper + .map(mockUser); + AysResponse mockResponse = AysResponse + .successOf(mockUserResponse); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .isNotEmpty()); + + // Verify + Mockito.verify(userReadService, Mockito.times(1)) + .findById(mockUserId); + } + + @Test + void givenUserId_whenUnauthorizedForGettingUserById_thenReturnAccessDeniedException() throws Exception { + + // Given + String mockUserId = AysRandomUtil.generateUUID(); + + // Then + String endpoint = BASE_PATH.concat("/user/".concat(mockUserId)); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .get(endpoint, mockUserToken.getAccessToken()); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isForbidden()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .doesNotExist()); + + // Verify + Mockito.verify(userReadService, Mockito.never()) + .findById(mockUserId); + } + + @ParameterizedTest + @ValueSource(strings = { + "A", + "493268349068342" + }) + void givenInvalidId_whenIdNotValid_thenReturnValidationError(String invalidId) throws Exception { + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(invalidId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .get(endpoint, mockAdminToken.getAccessToken()); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userReadService, Mockito.never()) + .findById(Mockito.anyString()); + } + + + @Test + void givenUserCreateRequest_whenUserCreated_thenReturnSuccess() throws Exception { + + // Given + AysUserCreateRequest mockCreateRequest = new AysUserCreateRequestBuilder() + .withValidValues() + .build(); + + // When + Mockito.doNothing() + .when(userCreateService) + .create(Mockito.any(AysUserCreateRequest.class)); + + // Then + String endpoint = BASE_PATH.concat("/user"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockAdminToken.getAccessToken(), mockCreateRequest); + + AysResponse mockResponse = AysResponseBuilder.success(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Mockito.verify(userCreateService, Mockito.times(1)) + .create(Mockito.any(AysUserCreateRequest.class)); + } + + @Test + void givenValidUserCreateRequest_whenUserUnauthorized_thenReturnAccessDeniedException() throws Exception { + + // Given + AysUserCreateRequest mockCreateRequest = new AysUserCreateRequestBuilder() + .withValidValues() + .build(); + + // Then + String endpoint = BASE_PATH.concat("/user"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockUserToken.getAccessToken(), mockCreateRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isForbidden()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .doesNotExist()); + + // Verify + Mockito.verify(userCreateService, Mockito.never()) + .create(Mockito.any(AysUserCreateRequest.class)); + } + + @Test + void givenUserCreateRequest_whenRoleIdsAreNull_thenReturnValidationError() throws Exception { + + // Given + AysUserCreateRequest mockCreateRequest = new AysUserCreateRequestBuilder() + .withValidValues() + .withRoleIds(null) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/user"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockAdminToken.getAccessToken(), mockCreateRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userCreateService, Mockito.never()) + .create(Mockito.any(AysUserCreateRequest.class)); + } + + @Test + void givenUserCreateRequest_whenRoleIdsAreEmpty_thenReturnValidationError() throws Exception { + + // Given + AysUserCreateRequest mockCreateRequest = new AysUserCreateRequestBuilder() + .withValidValues() + .withRoleIds(Set.of()) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/user"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockAdminToken.getAccessToken(), mockCreateRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userCreateService, Mockito.never()) + .create(Mockito.any(AysUserCreateRequest.class)); + } + + @ParameterizedTest + @NullSource + @ValueSource(strings = { + "", + "55aed4c4facb4b66bdb5-309eaaef4453" + }) + void givenUserCreateRequest_whenRoleIdIsNotValid_thenReturnValidationError(String invalidRoleId) throws Exception { + + // Given + AysUserCreateRequest mockCreateRequest = new AysUserCreateRequestBuilder() + .withValidValues() + .withRoleIds(Collections.singleton(invalidRoleId)) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/user"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockAdminToken.getAccessToken(), mockCreateRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userCreateService, Mockito.never()) + .create(Mockito.any(AysUserCreateRequest.class)); + } + + @ParameterizedTest + @ValueSource(strings = { + "abc.def@mail.c", + "abc.def@mail#archive.com", + "abc.def@mail", + "abcdef@mail..com", + "abc-@mail.com", + "admin@test@ays.com", + "admintest@ays..com", + "username@gmail..co.uk", + "user@ example.com", + "user@-example.com", + "user@example-.com", + "(user)@example.com", + "user@[192.168.1.1", + "user@exam ple.com", + "user@.com", + ".user@example.com", + " user@example.com", + "user@example.com ", + " user@example.com ", + "@missingusername.com" + }) + void givenUserCreateRequest_whenEmailNotValid_thenReturnValidationError(String mockEmailAddress) throws Exception { + + // Given + AysUserCreateRequest mockCreateRequest = new AysUserCreateRequestBuilder() + .withValidValues() + .withEmailAddress(mockEmailAddress) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/user"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, mockAdminToken.getAccessToken(), mockCreateRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userCreateService, Mockito.never()) + .create(Mockito.any(AysUserCreateRequest.class)); + } + + + @Test + void givenValidIdAndUserUpdateRequest_whenUserUpdated_thenReturnSuccess() throws Exception { + + // Given + String mockId = "2cb9f39b-490f-4035-97ac-9afbb87506df"; + AysUserUpdateRequest mockUpdateRequest = new AysUserUpdateRequestBuilder() + .withValidValues() + .build(); + + // When + Mockito.doNothing() + .when(userUpdateService) + .update(Mockito.any(), Mockito.any(AysUserUpdateRequest.class)); + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(mockId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .put(endpoint, mockAdminToken.getAccessToken(), mockUpdateRequest); + + AysResponse mockResponse = AysResponseBuilder.success(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Mockito.verify(userUpdateService, Mockito.times(1)) + .update(Mockito.anyString(), Mockito.any(AysUserUpdateRequest.class)); + } + + @Test + void givenValidIdAndUserUpdateRequest_whenUserUnauthorized_thenReturnAccessDeniedException() throws Exception { + + // Given + String mockId = "0deb5f16-479f-4625-9354-7db29c861e85"; + AysUserUpdateRequest mockUpdateRequest = new AysUserUpdateRequestBuilder() + .withValidValues() + .build(); + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(mockId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .put(endpoint, mockUserToken.getAccessToken(), mockUpdateRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isForbidden()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .doesNotExist()); + + // Verify + Mockito.verify(userUpdateService, Mockito.never()) + .update(Mockito.anyString(), Mockito.any(AysUserUpdateRequest.class)); + } + + @ParameterizedTest + @ValueSource(strings = { + "A", + "493268349068342" + }) + void givenInvalidIdAndValidUserUpdateRequest_whenIdNotValid_thenReturnValidationError(String invalidId) throws Exception { + + // Given + AysUserUpdateRequest mockUpdateRequest = new AysUserUpdateRequestBuilder() + .withValidValues() + .build(); + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(invalidId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .put(endpoint, mockAdminToken.getAccessToken(), mockUpdateRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userUpdateService, Mockito.never()) + .update(Mockito.anyString(), Mockito.any(AysUserUpdateRequest.class)); + } + + @Test + void givenValidIdAndInvalidUserUpdateRequest_whenRoleIdsAreNull_thenReturnValidationError() throws Exception { + + // Given + String mockId = "3b5a65ba-c90e-41ad-97ec-56c7dd4fe708"; + AysUserUpdateRequest mockUpdateRequest = new AysUserUpdateRequestBuilder() + .withValidValues() + .withRoleIds(null) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(mockId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .put(endpoint, mockAdminToken.getAccessToken(), mockUpdateRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userUpdateService, Mockito.never()) + .update(Mockito.anyString(), Mockito.any(AysUserUpdateRequest.class)); + } + + @Test + void givenValidIdAndInvalidUserUpdateRequest_whenRoleIdsAreEmpty_thenReturnValidationError() throws Exception { + + // Given + String mockId = "daaa28f9-f7b3-4f43-a10f-a5fe2b0eb2fa"; + AysUserUpdateRequest mockUpdateRequest = new AysUserUpdateRequestBuilder() + .withValidValues() + .withRoleIds(Set.of()) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(mockId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .put(endpoint, mockAdminToken.getAccessToken(), mockUpdateRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userUpdateService, Mockito.never()) + .update(Mockito.anyString(), Mockito.any(AysUserUpdateRequest.class)); + } + + @ParameterizedTest + @NullSource + @ValueSource(strings = { + "", + "55aed4c4facb4b66bdb5-309eaaef4453" + }) + void givenValidIdAndInvalidUserUpdateRequest_whenRoleIdIsNotValid_thenReturnValidationError(String invalidRoleId) throws Exception { + + // Given + String mockId = "b8758dce-ad8e-438d-b31c-b440b352068a"; + + Set mockRoleIds = new HashSet<>(); + mockRoleIds.add(invalidRoleId); + AysUserUpdateRequest mockUpdateRequest = new AysUserUpdateRequestBuilder() + .withValidValues() + .withRoleIds(mockRoleIds) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(mockId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .put(endpoint, mockAdminToken.getAccessToken(), mockUpdateRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userUpdateService, Mockito.never()) + .update(Mockito.anyString(), Mockito.any(AysUserUpdateRequest.class)); + } + + @ParameterizedTest + @ValueSource(strings = { + "abc.def@mail.c", + "abc.def@mail#archive.com", + "abc.def@mail", + "abcdef@mail..com", + "abc-@mail.com", + "admin@test@ays.com", + "admintest@ays..com", + "username@gmail..co.uk", + "user@ example.com", + "user@-example.com", + "user@example-.com", + "(user)@example.com", + "user@[192.168.1.1", + "user@exam ple.com", + "user@.com", + ".user@example.com", + " user@example.com", + "user@example.com ", + " user@example.com ", + "@missingusername.com" + }) + void givenValidIdAndInvalidUserUpdateRequest_whenEmailNotValid_thenReturnValidationError(String mockEmailAddress) throws Exception { + // Given + String mockId = "b66afcb0-3029-4968-a87b-e1d94fc75642"; + + AysUserUpdateRequest mockUpdateRequest = new AysUserUpdateRequestBuilder() + .withValidValues() + .withEmailAddress(mockEmailAddress) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(mockId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .put(endpoint, mockAdminToken.getAccessToken(), mockUpdateRequest); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userUpdateService, Mockito.never()) + .update(Mockito.anyString(), Mockito.any(AysUserUpdateRequest.class)); + } + + + @Test + void givenValidId_whenActivateUser_thenReturnSuccess() throws Exception { + + // Given + String mockId = "793fcc5d-31cc-4704-9f0a-627ac7da517d"; + + // When + Mockito.doNothing() + .when(userUpdateService) + .activate(Mockito.anyString()); + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(mockId).concat("/activate"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .patch(endpoint, mockAdminToken.getAccessToken()); + + AysResponse mockResponse = AysResponseBuilder.success(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Mockito.verify(userUpdateService, Mockito.times(1)) + .activate(Mockito.anyString()); + } + + @Test + void givenValidId_whenUserUnauthorized_thenReturnAccessDeniedException() throws Exception { + + // Given + String mockId = "201aec72-ecd8-49fc-86f5-2b5458871edb"; + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(mockId).concat("/activate"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .patch(endpoint, mockUserToken.getAccessToken()); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isForbidden()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Mockito.verify(userUpdateService, Mockito.never()) + .activate(Mockito.anyString()); + } + + @ParameterizedTest + @ValueSource(strings = { + "A", + "493268349068342" + }) + void givenInvalidUserId_whenIdNotValid_thenReturnValidationError(String invalidId) throws Exception { + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(invalidId).concat("/activate"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .patch(endpoint, mockAdminToken.getAccessToken()); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userUpdateService, Mockito.never()) + .activate(Mockito.anyString()); + } + + + @Test + void givenValidId_whenUserDeleted_thenReturnSuccess() throws Exception { + + // Given + String mockId = "2e574ecf-929c-4923-8aea-d061d29934da"; + + // When + Mockito.doNothing() + .when(userUpdateService) + .delete(Mockito.any()); + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(mockId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .delete(endpoint, mockAdminToken.getAccessToken()); + + AysResponse mockResponse = AysResponseBuilder.success(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Mockito.verify(userUpdateService, Mockito.times(1)) + .delete(mockId); + } + + @ParameterizedTest + @ValueSource(strings = { + "A", + "493268349068342" + }) + void givenId_whenIdDoesNotValid_thenReturnValidationError(String invalidId) throws Exception { + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(invalidId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .delete(endpoint, mockAdminToken.getAccessToken()); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userUpdateService, Mockito.never()) + .delete(Mockito.anyString()); + } + + @Test + void givenUserDelete_whenUserUnauthorized_thenReturnAccessDeniedException() throws Exception { + + // Given + String mockId = "45082f52-011b-41d1-b4bd-6eba4e1f1ea8"; + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(mockId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .delete(endpoint, mockUserToken.getAccessToken()); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isForbidden()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Mockito.verify(userUpdateService, Mockito.never()) + .delete(Mockito.anyString()); + } + + + @Test + void givenValidId_whenPassivateUser_thenReturnSuccess() throws Exception { + + // Given + String mockId = "894dcc5d-31cc-4704-9f0a-627ac7da517d"; + + // When + Mockito.doNothing() + .when(userUpdateService) + .passivate(Mockito.anyString()); + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(mockId).concat("/passivate"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .patch(endpoint, mockAdminToken.getAccessToken()); + + AysResponse mockResponse = AysResponseBuilder.success(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Mockito.verify(userUpdateService, Mockito.times(1)) + .passivate(Mockito.anyString()); + } + + @ParameterizedTest + @ValueSource(strings = { + "A", + "493268349068342" + }) + void givenInvalidUserId_whenIdNotValid_thenReturnValidationErrorForPassivate(String invalidId) throws Exception { + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(invalidId).concat("/passivate"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .patch(endpoint, mockAdminToken.getAccessToken()); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.VALIDATION_ERROR; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isBadRequest()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .isNotEmpty()); + + // Verify + Mockito.verify(userUpdateService, Mockito.never()) + .passivate(Mockito.anyString()); + } + + @Test + void givenValidUserId_whenUserUnauthorized_thenReturnAccessDeniedException() throws Exception { + + // Given + String mockId = "333aec72-ecd8-49fc-86f5-2b5458871edb"; + + // Then + String endpoint = BASE_PATH.concat("/user/").concat(mockId).concat("/passivate"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .patch(endpoint, mockUserToken.getAccessToken()); + + AysErrorResponse mockErrorResponse = AysErrorResponseBuilder.FORBIDDEN; + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isForbidden()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Mockito.verify(userUpdateService, Mockito.never()) + .passivate(Mockito.anyString()); + } } diff --git a/src/test/java/org/ays/emergency_application/controller/EmergencyEvacuationApplicationEndToEndTest.java b/src/test/java/org/ays/emergency_application/controller/EmergencyEvacuationApplicationEndToEndTest.java index 5cd4f89f0..4d76feb0b 100644 --- a/src/test/java/org/ays/emergency_application/controller/EmergencyEvacuationApplicationEndToEndTest.java +++ b/src/test/java/org/ays/emergency_application/controller/EmergencyEvacuationApplicationEndToEndTest.java @@ -37,461 +37,445 @@ class EmergencyEvacuationApplicationEndToEndTest extends AysEndToEndTest { - @Autowired - private EmergencyEvacuationApplicationSavePort emergencyEvacuationApplicationSavePort; - - @Autowired - private EmergencyEvacuationApplicationReadPort emergencyEvacuationApplicationReadPort; - - @Autowired - private EmergencyEvacuationApplicationRepository emergencyEvacuationApplicationRepository; - - private final EmergencyEvacuationApplicationToApplicationResponseMapper emergencyEvacuationApplicationToApplicationResponseMapper = EmergencyEvacuationApplicationToApplicationResponseMapper - .initialize(); - - private static final String BASE_PATH = "/api/v1"; - - @Test - void givenValidEmergencyEvacuationApplicationListRequest_whenApplicationsFound_thenReturnAysPageResponseOfEmergencyEvacuationApplicationsResponse() - throws Exception { - - // Initialize - emergencyEvacuationApplicationRepository.deleteAll(); - - List applications = List.of( - emergencyEvacuationApplicationSavePort.save( - new EmergencyEvacuationApplicationBuilder() - .withValidValues() - .withoutId() - .withoutInstitution() - .withStatus(EmergencyEvacuationApplicationStatus.PENDING) - .withoutApplicant() - .build()), - emergencyEvacuationApplicationSavePort.save( - new EmergencyEvacuationApplicationBuilder() - .withValidValues() - .withoutId() - .withInstitution( - new InstitutionBuilder() - .withValidValues() - .withId(AysValidTestData.Admin.INSTITUTION_ID) - .build()) - .withStatus(EmergencyEvacuationApplicationStatus.PENDING) - .withoutApplicant() - .build())); - applications.forEach(application -> emergencyEvacuationApplicationSavePort.save(application)); - - // Given - AysPageable mockAysPageable = new AysPageableBuilder() - .withPage(1) - .withPageSize(10) - .build(); - EmergencyEvacuationApplicationListRequest mockListRequest = new EmergencyEvacuationApplicationListRequestBuilder() + @Autowired + private EmergencyEvacuationApplicationSavePort emergencyEvacuationApplicationSavePort; + + @Autowired + private EmergencyEvacuationApplicationReadPort emergencyEvacuationApplicationReadPort; + + @Autowired + private EmergencyEvacuationApplicationRepository emergencyEvacuationApplicationRepository; + + + private final EmergencyEvacuationApplicationToApplicationResponseMapper emergencyEvacuationApplicationToApplicationResponseMapper = EmergencyEvacuationApplicationToApplicationResponseMapper.initialize(); + + + private static final String BASE_PATH = "/api/v1"; + + + @Test + void givenValidEmergencyEvacuationApplicationListRequest_whenApplicationsFound_thenReturnAysPageResponseOfEmergencyEvacuationApplicationsResponse() throws Exception { + + // Initialize + emergencyEvacuationApplicationRepository.deleteAll(); + + List applications = List.of( + emergencyEvacuationApplicationSavePort.save( + new EmergencyEvacuationApplicationBuilder() .withValidValues() - .withPageable(mockAysPageable) - .withFilter(null) - .withoutOrders() - .build(); - - // Then - String endpoint = BASE_PATH.concat("/emergency-evacuation-applications"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, adminToken.getAccessToken(), mockListRequest); - - AysResponse> mockResponse = AysResponseBuilder - .successPage(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.contentSize() - .value(applications.size())) - .andExpect(AysMockResultMatchersBuilders.contents("id") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.contents("referenceNumber") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.contents("firstName") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.contents("lastName") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.contents("phoneNumber.countryCode") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.contents("phoneNumber.lineNumber") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.contents("applicantFirstName") - .exists()) - .andExpect(AysMockResultMatchersBuilders.contents("applicantLastName") - .exists()) - .andExpect(AysMockResultMatchersBuilders.contents("applicantPhoneNumber.countryCode") - .exists()) - .andExpect(AysMockResultMatchersBuilders.contents("applicantPhoneNumber.lineNumber") - .exists()) - .andExpect(AysMockResultMatchersBuilders.contents("seatingCount") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.contents("status") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.contents("isInPerson") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.contents("createdAt") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.response("pageNumber") - .value(1)) - .andExpect(AysMockResultMatchersBuilders.response("pageSize") - .value(applications.size())) - .andExpect(AysMockResultMatchersBuilders.response("totalPageCount") - .value(1)) - .andExpect(AysMockResultMatchersBuilders.response("totalElementCount") - .value(applications.size())) - .andExpect(AysMockResultMatchersBuilders.response("orderedBy") - .isEmpty()) - .andExpect(AysMockResultMatchersBuilders.response("filteredBy") - .isNotEmpty()); - } - - @Test - void givenValidEmergencyEvacuationApplicationListRequest_whenEmergencyEvacuationApplicationsNotFound_thenReturnAysPageResponseOfEmergencyEvacuationApplicationsResponse() - throws Exception { - - // Initialize - emergencyEvacuationApplicationRepository.deleteAll(); + .withoutId() + .withoutInstitution() + .withStatus(EmergencyEvacuationApplicationStatus.PENDING) + .withoutApplicant() + .build() + ), emergencyEvacuationApplicationSavePort.save( - new EmergencyEvacuationApplicationBuilder() - .withValidValues() - .withoutId() - .withoutInstitution() - .withStatus(EmergencyEvacuationApplicationStatus.PENDING) - .withoutApplicant() - .build()); - - // Given - EmergencyEvacuationApplicationListRequest mockListRequest = new EmergencyEvacuationApplicationListRequestBuilder() + new EmergencyEvacuationApplicationBuilder() .withValidValues() - .withStatuses(Set.of(EmergencyEvacuationApplicationStatus.COMPLETED)) - .withoutOrders() - .build(); - - // Then - String endpoint = BASE_PATH.concat("/emergency-evacuation-applications"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, adminToken.getAccessToken(), mockListRequest); - - AysResponse> mockResponse = AysResponseBuilder - .successPage(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.contentSize() - .value(0)) - .andExpect(AysMockResultMatchersBuilders.contents("id") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("referenceNumber") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("firstName") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("lastName") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("phoneNumber.countryCode") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("phoneNumber.lineNumber") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("applicantFirstName") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("applicantLastName") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("applicantPhoneNumber.countryCode") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("applicantPhoneNumber.lineNumber") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("seatingCount") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("status") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("isInPerson") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("createdAt") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.response("pageNumber") - .value(1)) - .andExpect(AysMockResultMatchersBuilders.response("pageSize") - .value(0)) - .andExpect(AysMockResultMatchersBuilders.response("totalPageCount") - .value(0)) - .andExpect(AysMockResultMatchersBuilders.response("totalElementCount") - .value(0)) - .andExpect(AysMockResultMatchersBuilders.response("orderedBy") - .isEmpty()) - .andExpect(AysMockResultMatchersBuilders.response("filteredBy") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.response("filteredBy.statuses") - .isNotEmpty()); - } - - @Test - void givenValidEmergencyEvacuationApplicationId_whenEmergencyEvacuationApplicationExists_thenReturnEmergencyEvacuationApplicationResponse() - throws Exception { - - // Initialize - emergencyEvacuationApplicationRepository.deleteAll(); - EmergencyEvacuationApplication application = emergencyEvacuationApplicationSavePort.save( - new EmergencyEvacuationApplicationBuilder() + .withoutId() + .withInstitution( + new InstitutionBuilder() .withValidValues() - .withoutId() - .withoutInstitution() - .withoutApplicant() - .build()); - - // Given - String applicationId = application.getId(); - - // Then - String endpoint = BASE_PATH.concat("/emergency-evacuation-application/").concat(applicationId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .get(endpoint, adminToken.getAccessToken()); - - EmergencyEvacuationApplicationResponse mockApplicationResponse = emergencyEvacuationApplicationToApplicationResponseMapper - .map(application); - - AysResponse mockResponse = AysResponse - .successOf(mockApplicationResponse); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.response("id") - .value(application.getId())) - .andExpect(AysMockResultMatchersBuilders.response("referenceNumber") - .value(application.getReferenceNumber())) - .andExpect(AysMockResultMatchersBuilders.response("firstName") - .value(application.getFirstName())) - .andExpect(AysMockResultMatchersBuilders.response("lastName") - .value(application.getLastName())) - .andExpect(AysMockResultMatchersBuilders.response("phoneNumber.countryCode") - .value(application.getPhoneNumber().getCountryCode())) - .andExpect(AysMockResultMatchersBuilders.response("phoneNumber.lineNumber") - .value(application.getPhoneNumber().getLineNumber())) - .andExpect(AysMockResultMatchersBuilders.response("sourceCity") - .value(application.getSourceCity())) - .andExpect(AysMockResultMatchersBuilders.response("sourceDistrict") - .value(application.getSourceDistrict())) - .andExpect(AysMockResultMatchersBuilders.response("address") - .value(application.getAddress())) - .andExpect(AysMockResultMatchersBuilders.response("seatingCount") - .value(application.getSeatingCount())) - .andExpect(AysMockResultMatchersBuilders.response("targetCity") - .value(application.getTargetCity())) - .andExpect(AysMockResultMatchersBuilders.response("targetDistrict") - .value(application.getTargetDistrict())) - .andExpect(AysMockResultMatchersBuilders.response("status") - .value(application.getStatus().toString())) - .andExpect(AysMockResultMatchersBuilders.response("isInPerson") - .value(application.getIsInPerson())) - .andExpect(AysMockResultMatchersBuilders.response("hasObstaclePersonExist") - .value(application.getHasObstaclePersonExist())) - .andExpect(AysMockResultMatchersBuilders.response("notes") - .value(application.getNotes())) - .andExpect(AysMockResultMatchersBuilders.response("createdUser") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.response("createdAt") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.response("updatedUser") - .isEmpty()) - .andExpect(AysMockResultMatchersBuilders.response("updatedAt") - .isEmpty()); - } - - @Test - void givenValidEmergencyEvacuationApplicationRequest_whenApplicationSaved_thenReturnSuccessResponse() - throws Exception { - // Given - String firstName = "Test Application with Applicant"; - EmergencyEvacuationApplicationRequest applicationRequest = new EmergencyEvacuationRequestBuilder() - .withValidValues() - .withFirstName(firstName) - .withHasObstaclePersonExist(false) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/emergency-evacuation-application"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, applicationRequest); - - AysResponse mockResponse = AysResponseBuilder.success(); - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.isSuccess() - .value(true)); - - // Verify - AysPageable pageable = AysPageable.builder() - .page(1) - .pageSize(1000) - .build(); - Optional application = emergencyEvacuationApplicationReadPort - .findAll(pageable, null) - .getContent().stream() - .filter(evacuationApplication -> evacuationApplication.getFirstName() - .equals(applicationRequest.getFirstName())) - .findFirst(); - - Assertions.assertTrue(application.isPresent()); - Assertions.assertNull(application.get().getInstitution()); - Assertions.assertNotNull(application.get().getReferenceNumber()); - Assertions.assertEquals(10, application.get().getReferenceNumber().length()); - Assertions.assertEquals(application.get().getFirstName(), firstName); - Assertions.assertEquals(application.get().getLastName(), applicationRequest.getLastName()); - Assertions.assertEquals(application.get().getPhoneNumber().getCountryCode(), - applicationRequest.getPhoneNumber().getCountryCode()); - Assertions.assertEquals(application.get().getPhoneNumber().getLineNumber(), - applicationRequest.getPhoneNumber().getLineNumber()); - Assertions.assertEquals(application.get().getSourceCity(), applicationRequest.getSourceCity()); - Assertions.assertEquals(application.get().getSourceDistrict(), applicationRequest.getSourceDistrict()); - Assertions.assertEquals(application.get().getAddress(), applicationRequest.getAddress()); - Assertions.assertEquals(application.get().getSeatingCount(), applicationRequest.getSeatingCount()); - Assertions.assertEquals(application.get().getTargetCity(), applicationRequest.getTargetCity()); - Assertions.assertEquals(application.get().getTargetDistrict(), applicationRequest.getTargetDistrict()); - Assertions.assertEquals(EmergencyEvacuationApplicationStatus.PENDING, application.get().getStatus()); - Assertions.assertEquals(application.get().getApplicantFirstName(), - applicationRequest.getApplicantFirstName()); - Assertions.assertEquals(application.get().getApplicantLastName(), - applicationRequest.getApplicantLastName()); - Assertions.assertEquals(application.get().getApplicantPhoneNumber().getCountryCode(), - applicationRequest.getApplicantPhoneNumber().getCountryCode()); - Assertions.assertEquals(application.get().getApplicantPhoneNumber().getLineNumber(), - applicationRequest.getApplicantPhoneNumber().getLineNumber()); - Assertions.assertFalse(application.get().getIsInPerson()); - Assertions.assertFalse(application.get().getHasObstaclePersonExist()); - Assertions.assertNull(application.get().getNotes()); - Assertions.assertEquals("AYS", application.get().getCreatedUser()); - } - - @Test - void givenValidEmergencyEvacuationApplicationRequestWithoutApplicant_whenApplicationSaved_thenReturnSuccessResponse() - throws Exception { - // Given - String firstName = "Test Application without Applicant"; - EmergencyEvacuationApplicationRequest applicationRequest = new EmergencyEvacuationRequestBuilder() - .withValidValues() - .withFirstName(firstName) + .withId(AysValidTestData.Admin.INSTITUTION_ID) + .build() + ) + .withStatus(EmergencyEvacuationApplicationStatus.PENDING) .withoutApplicant() - .withHasObstaclePersonExist(false) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/emergency-evacuation-application"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, applicationRequest); - - AysResponse mockResponse = AysResponseBuilder.success(); - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.isSuccess() - .value(true)); - - // Verify - AysPageable pageable = AysPageable.builder() - .page(1) - .pageSize(1000) - .build(); - Optional application = emergencyEvacuationApplicationReadPort - .findAll(pageable, null) - .getContent().stream() - .filter(evacuationApplication -> evacuationApplication.getFirstName() - .equals(applicationRequest.getFirstName())) - .findFirst(); - - Assertions.assertTrue(application.isPresent()); - Assertions.assertNull(application.get().getInstitution()); - Assertions.assertNotNull(application.get().getReferenceNumber()); - Assertions.assertEquals(10, application.get().getReferenceNumber().length()); - Assertions.assertEquals(application.get().getFirstName(), firstName); - Assertions.assertEquals(application.get().getLastName(), applicationRequest.getLastName()); - Assertions.assertEquals(application.get().getPhoneNumber().getCountryCode(), - applicationRequest.getPhoneNumber().getCountryCode()); - Assertions.assertEquals(application.get().getPhoneNumber().getLineNumber(), - applicationRequest.getPhoneNumber().getLineNumber()); - Assertions.assertEquals(application.get().getSourceCity(), applicationRequest.getSourceCity()); - Assertions.assertEquals(application.get().getSourceDistrict(), applicationRequest.getSourceDistrict()); - Assertions.assertEquals(application.get().getAddress(), applicationRequest.getAddress()); - Assertions.assertEquals(application.get().getSeatingCount(), applicationRequest.getSeatingCount()); - Assertions.assertEquals(application.get().getTargetCity(), applicationRequest.getTargetCity()); - Assertions.assertEquals(application.get().getTargetDistrict(), applicationRequest.getTargetDistrict()); - Assertions.assertEquals(EmergencyEvacuationApplicationStatus.PENDING, application.get().getStatus()); - Assertions.assertEquals(application.get().getApplicantFirstName(), - applicationRequest.getApplicantFirstName()); - Assertions.assertEquals(application.get().getApplicantLastName(), - applicationRequest.getApplicantLastName()); - Assertions.assertNull(application.get().getApplicantPhoneNumber().getCountryCode()); - Assertions.assertNull(application.get().getApplicantPhoneNumber().getLineNumber()); - Assertions.assertTrue(application.get().getIsInPerson()); - Assertions.assertFalse(application.get().getHasObstaclePersonExist()); - Assertions.assertNull(application.get().getNotes()); - Assertions.assertEquals("AYS", application.get().getCreatedUser()); - } - - @Test - void givenValidIdAndValidUpdateRequest_whenApplicationUpdated_thenReturnSuccessResponse() throws Exception { - - // Initialize - emergencyEvacuationApplicationRepository.deleteAll(); - EmergencyEvacuationApplication application = emergencyEvacuationApplicationSavePort.save( - new EmergencyEvacuationApplicationBuilder() - .withValidValues() - .withoutId() - .withoutInstitution() - .withoutApplicant() - .withSeatingCount(5) - .withStatus(EmergencyEvacuationApplicationStatus.PENDING) - .withHasObstaclePersonExist(false) - .withoutNotes() - .build()); - - // Given - String id = application.getId(); - EmergencyEvacuationApplicationUpdateRequest updateRequest = new EmergencyEvacuationApplicationUpdateRequestBuilder() - .withValidValues() - .build(); - - // Then - String endpoint = BASE_PATH.concat("/emergency-evacuation-application/").concat(id); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .put(endpoint, adminToken.getAccessToken(), updateRequest); - - AysResponse mockResponse = AysResponseBuilder.success(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(MockMvcResultMatchers.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()); - - // Verify - Optional applicationFromDatabase = emergencyEvacuationApplicationReadPort - .findById(id); - - Assertions.assertTrue(applicationFromDatabase.isPresent()); - Assertions.assertNotNull(applicationFromDatabase.get().getInstitution()); - Assertions.assertEquals(applicationFromDatabase.get().getSeatingCount(), - updateRequest.getSeatingCount()); - Assertions.assertEquals(applicationFromDatabase.get().getHasObstaclePersonExist(), - updateRequest.getHasObstaclePersonExist()); - Assertions.assertEquals(applicationFromDatabase.get().getStatus(), updateRequest.getStatus()); - Assertions.assertEquals(applicationFromDatabase.get().getNotes(), updateRequest.getNotes()); - Assertions.assertNotNull(applicationFromDatabase.get().getUpdatedUser()); - Assertions.assertNotNull(applicationFromDatabase.get().getUpdatedAt()); - Assertions.assertTrue(UUIDTestUtil.isValid(applicationFromDatabase.get().getUpdatedUser())); - } + .build() + ) + ); + applications.forEach(application -> emergencyEvacuationApplicationSavePort.save(application)); + + // Given + AysPageable mockAysPageable = new AysPageableBuilder() + .withPage(1) + .withPageSize(10) + .build(); + EmergencyEvacuationApplicationListRequest mockListRequest = new EmergencyEvacuationApplicationListRequestBuilder() + .withValidValues() + .withPageable(mockAysPageable) + .withFilter(null) + .withoutOrders() + .build(); + + // Then + String endpoint = BASE_PATH.concat("/emergency-evacuation-applications"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, adminToken.getAccessToken(), mockListRequest); + + AysResponse> mockResponse = AysResponseBuilder.successPage(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.contentSize() + .value(applications.size())) + .andExpect(AysMockResultMatchersBuilders.contents("id") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.contents("referenceNumber") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.contents("firstName") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.contents("lastName") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.contents("phoneNumber.countryCode") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.contents("phoneNumber.lineNumber") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.contents("applicantFirstName") + .exists()) + .andExpect(AysMockResultMatchersBuilders.contents("applicantLastName") + .exists()) + .andExpect(AysMockResultMatchersBuilders.contents("applicantPhoneNumber.countryCode") + .exists()) + .andExpect(AysMockResultMatchersBuilders.contents("applicantPhoneNumber.lineNumber") + .exists()) + .andExpect(AysMockResultMatchersBuilders.contents("seatingCount") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.contents("status") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.contents("isInPerson") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.contents("createdAt") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.response("pageNumber") + .value(1)) + .andExpect(AysMockResultMatchersBuilders.response("pageSize") + .value(applications.size())) + .andExpect(AysMockResultMatchersBuilders.response("totalPageCount") + .value(1)) + .andExpect(AysMockResultMatchersBuilders.response("totalElementCount") + .value(applications.size())) + .andExpect(AysMockResultMatchersBuilders.response("orderedBy") + .isEmpty()) + .andExpect(AysMockResultMatchersBuilders.response("filteredBy") + .isNotEmpty()); + } + + @Test + void givenValidEmergencyEvacuationApplicationListRequest_whenEmergencyEvacuationApplicationsNotFound_thenReturnAysPageResponseOfEmergencyEvacuationApplicationsResponse() throws Exception { + + // Initialize + emergencyEvacuationApplicationRepository.deleteAll(); + emergencyEvacuationApplicationSavePort.save( + new EmergencyEvacuationApplicationBuilder() + .withValidValues() + .withoutId() + .withoutInstitution() + .withStatus(EmergencyEvacuationApplicationStatus.PENDING) + .withoutApplicant() + .build() + ); + + // Given + EmergencyEvacuationApplicationListRequest mockListRequest = new EmergencyEvacuationApplicationListRequestBuilder() + .withValidValues() + .withStatuses(Set.of(EmergencyEvacuationApplicationStatus.COMPLETED)) + .withoutOrders() + .build(); + + // Then + String endpoint = BASE_PATH.concat("/emergency-evacuation-applications"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, adminToken.getAccessToken(), mockListRequest); + + AysResponse> mockResponse = AysResponseBuilder.successPage(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.contentSize() + .value(0)) + .andExpect(AysMockResultMatchersBuilders.contents("id") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("referenceNumber") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("firstName") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("lastName") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("phoneNumber.countryCode") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("phoneNumber.lineNumber") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("applicantFirstName") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("applicantLastName") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("applicantPhoneNumber.countryCode") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("applicantPhoneNumber.lineNumber") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("seatingCount") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("status") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("isInPerson") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("createdAt") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.response("pageNumber") + .value(1)) + .andExpect(AysMockResultMatchersBuilders.response("pageSize") + .value(0)) + .andExpect(AysMockResultMatchersBuilders.response("totalPageCount") + .value(0)) + .andExpect(AysMockResultMatchersBuilders.response("totalElementCount") + .value(0)) + .andExpect(AysMockResultMatchersBuilders.response("orderedBy") + .isEmpty()) + .andExpect(AysMockResultMatchersBuilders.response("filteredBy") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.response("filteredBy.statuses") + .isNotEmpty()); + } + + @Test + void givenValidEmergencyEvacuationApplicationId_whenEmergencyEvacuationApplicationExists_thenReturnEmergencyEvacuationApplicationResponse() throws Exception { + + // Initialize + emergencyEvacuationApplicationRepository.deleteAll(); + EmergencyEvacuationApplication application = emergencyEvacuationApplicationSavePort.save( + new EmergencyEvacuationApplicationBuilder() + .withValidValues() + .withoutId() + .withoutInstitution() + .withoutApplicant() + .build() + ); + + // Given + String applicationId = application.getId(); + + // Then + String endpoint = BASE_PATH.concat("/emergency-evacuation-application/").concat(applicationId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .get(endpoint, adminToken.getAccessToken()); + + EmergencyEvacuationApplicationResponse mockApplicationResponse = emergencyEvacuationApplicationToApplicationResponseMapper + .map(application); + + AysResponse mockResponse = AysResponse + .successOf(mockApplicationResponse); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.response("id") + .value(application.getId())) + .andExpect(AysMockResultMatchersBuilders.response("referenceNumber") + .value(application.getReferenceNumber())) + .andExpect(AysMockResultMatchersBuilders.response("firstName") + .value(application.getFirstName())) + .andExpect(AysMockResultMatchersBuilders.response("lastName") + .value(application.getLastName())) + .andExpect(AysMockResultMatchersBuilders.response("phoneNumber.countryCode") + .value(application.getPhoneNumber().getCountryCode())) + .andExpect(AysMockResultMatchersBuilders.response("phoneNumber.lineNumber") + .value(application.getPhoneNumber().getLineNumber())) + .andExpect(AysMockResultMatchersBuilders.response("sourceCity") + .value(application.getSourceCity())) + .andExpect(AysMockResultMatchersBuilders.response("sourceDistrict") + .value(application.getSourceDistrict())) + .andExpect(AysMockResultMatchersBuilders.response("address") + .value(application.getAddress())) + .andExpect(AysMockResultMatchersBuilders.response("seatingCount") + .value(application.getSeatingCount())) + .andExpect(AysMockResultMatchersBuilders.response("targetCity") + .value(application.getTargetCity())) + .andExpect(AysMockResultMatchersBuilders.response("targetDistrict") + .value(application.getTargetDistrict())) + .andExpect(AysMockResultMatchersBuilders.response("status") + .value(application.getStatus().toString())) + .andExpect(AysMockResultMatchersBuilders.response("isInPerson") + .value(application.getIsInPerson())) + .andExpect(AysMockResultMatchersBuilders.response("hasObstaclePersonExist") + .value(application.getHasObstaclePersonExist())) + .andExpect(AysMockResultMatchersBuilders.response("notes") + .value(application.getNotes())) + .andExpect(AysMockResultMatchersBuilders.response("createdUser") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.response("createdAt") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.response("updatedUser") + .isEmpty()) + .andExpect(AysMockResultMatchersBuilders.response("updatedAt") + .isEmpty()); + } + + + @Test + void givenValidEmergencyEvacuationApplicationRequest_whenApplicationSaved_thenReturnSuccessResponse() throws Exception { + // Given + String firstName = "Test Application with Applicant"; + EmergencyEvacuationApplicationRequest applicationRequest = new EmergencyEvacuationRequestBuilder() + .withValidValues() + .withFirstName(firstName) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/emergency-evacuation-application"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, applicationRequest); + + AysResponse mockResponse = AysResponseBuilder.success(); + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .doesNotExist()); + + // Verify + AysPageable pageable = AysPageable.builder() + .page(1) + .pageSize(1000) + .build(); + Optional application = emergencyEvacuationApplicationReadPort + .findAll(pageable, null) + .getContent().stream() + .filter(evacuationApplication -> evacuationApplication.getFirstName().equals(applicationRequest.getFirstName())) + .findFirst(); + + Assertions.assertTrue(application.isPresent()); + Assertions.assertNull(application.get().getInstitution()); + Assertions.assertNotNull(application.get().getReferenceNumber()); + Assertions.assertEquals(10, application.get().getReferenceNumber().length()); + Assertions.assertEquals(application.get().getFirstName(), firstName); + Assertions.assertEquals(application.get().getLastName(), applicationRequest.getLastName()); + Assertions.assertEquals(application.get().getPhoneNumber().getCountryCode(), applicationRequest.getPhoneNumber().getCountryCode()); + Assertions.assertEquals(application.get().getPhoneNumber().getLineNumber(), applicationRequest.getPhoneNumber().getLineNumber()); + Assertions.assertEquals(application.get().getSourceCity(), applicationRequest.getSourceCity()); + Assertions.assertEquals(application.get().getSourceDistrict(), applicationRequest.getSourceDistrict()); + Assertions.assertEquals(application.get().getAddress(), applicationRequest.getAddress()); + Assertions.assertEquals(application.get().getSeatingCount(), applicationRequest.getSeatingCount()); + Assertions.assertEquals(application.get().getTargetCity(), applicationRequest.getTargetCity()); + Assertions.assertEquals(application.get().getTargetDistrict(), applicationRequest.getTargetDistrict()); + Assertions.assertEquals(EmergencyEvacuationApplicationStatus.PENDING, application.get().getStatus()); + Assertions.assertEquals(application.get().getApplicantFirstName(), applicationRequest.getApplicantFirstName()); + Assertions.assertEquals(application.get().getApplicantLastName(), applicationRequest.getApplicantLastName()); + Assertions.assertEquals(application.get().getApplicantPhoneNumber().getCountryCode(), applicationRequest.getApplicantPhoneNumber().getCountryCode()); + Assertions.assertEquals(application.get().getApplicantPhoneNumber().getLineNumber(), applicationRequest.getApplicantPhoneNumber().getLineNumber()); + Assertions.assertFalse(application.get().getIsInPerson()); + Assertions.assertNull(application.get().getHasObstaclePersonExist()); + Assertions.assertNull(application.get().getNotes()); + Assertions.assertEquals("AYS", application.get().getCreatedUser()); + } + + @Test + void givenValidEmergencyEvacuationApplicationRequestWithoutApplicant_whenApplicationSaved_thenReturnSuccessResponse() throws Exception { + // Given + String firstName = "Test Application without Applicant"; + EmergencyEvacuationApplicationRequest applicationRequest = new EmergencyEvacuationRequestBuilder() + .withValidValues() + .withFirstName(firstName) + .withoutApplicant() + .build(); + + // Then + String endpoint = BASE_PATH.concat("/emergency-evacuation-application"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, applicationRequest); + + AysResponse mockResponse = AysResponseBuilder.success(); + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .doesNotExist()); + + // Verify + AysPageable pageable = AysPageable.builder() + .page(1) + .pageSize(1000) + .build(); + Optional application = emergencyEvacuationApplicationReadPort + .findAll(pageable, null) + .getContent().stream() + .filter(evacuationApplication -> evacuationApplication.getFirstName().equals(applicationRequest.getFirstName())) + .findFirst(); + + Assertions.assertTrue(application.isPresent()); + Assertions.assertNull(application.get().getInstitution()); + Assertions.assertNotNull(application.get().getReferenceNumber()); + Assertions.assertEquals(10, application.get().getReferenceNumber().length()); + Assertions.assertEquals(application.get().getFirstName(), firstName); + Assertions.assertEquals(application.get().getLastName(), applicationRequest.getLastName()); + Assertions.assertEquals(application.get().getPhoneNumber().getCountryCode(), applicationRequest.getPhoneNumber().getCountryCode()); + Assertions.assertEquals(application.get().getPhoneNumber().getLineNumber(), applicationRequest.getPhoneNumber().getLineNumber()); + Assertions.assertEquals(application.get().getSourceCity(), applicationRequest.getSourceCity()); + Assertions.assertEquals(application.get().getSourceDistrict(), applicationRequest.getSourceDistrict()); + Assertions.assertEquals(application.get().getAddress(), applicationRequest.getAddress()); + Assertions.assertEquals(application.get().getSeatingCount(), applicationRequest.getSeatingCount()); + Assertions.assertEquals(application.get().getTargetCity(), applicationRequest.getTargetCity()); + Assertions.assertEquals(application.get().getTargetDistrict(), applicationRequest.getTargetDistrict()); + Assertions.assertEquals(EmergencyEvacuationApplicationStatus.PENDING, application.get().getStatus()); + Assertions.assertEquals(application.get().getApplicantFirstName(), applicationRequest.getApplicantFirstName()); + Assertions.assertEquals(application.get().getApplicantLastName(), applicationRequest.getApplicantLastName()); + Assertions.assertNull(application.get().getApplicantPhoneNumber().getCountryCode()); + Assertions.assertNull(application.get().getApplicantPhoneNumber().getLineNumber()); + Assertions.assertTrue(application.get().getIsInPerson()); + Assertions.assertNull(application.get().getHasObstaclePersonExist()); + Assertions.assertNull(application.get().getNotes()); + Assertions.assertEquals("AYS", application.get().getCreatedUser()); + } + + + @Test + void givenValidIdAndValidUpdateRequest_whenApplicationUpdated_thenReturnSuccessResponse() throws Exception { + + // Initialize + emergencyEvacuationApplicationRepository.deleteAll(); + EmergencyEvacuationApplication application = emergencyEvacuationApplicationSavePort.save( + new EmergencyEvacuationApplicationBuilder() + .withValidValues() + .withoutId() + .withoutInstitution() + .withoutApplicant() + .withSeatingCount(5) + .withStatus(EmergencyEvacuationApplicationStatus.PENDING) + .withoutHasObstaclePersonExist() + .withoutNotes() + .build() + ); + + // Given + String id = application.getId(); + EmergencyEvacuationApplicationUpdateRequest updateRequest = new EmergencyEvacuationApplicationUpdateRequestBuilder() + .withValidValues() + .build(); + + // Then + String endpoint = BASE_PATH.concat("/emergency-evacuation-application/").concat(id); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .put(endpoint, adminToken.getAccessToken(), updateRequest); + + AysResponse mockResponse = AysResponseBuilder.success(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(MockMvcResultMatchers.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Optional applicationFromDatabase = emergencyEvacuationApplicationReadPort + .findById(id); + + Assertions.assertTrue(applicationFromDatabase.isPresent()); + Assertions.assertNotNull(applicationFromDatabase.get().getInstitution()); + Assertions.assertEquals(applicationFromDatabase.get().getSeatingCount(), updateRequest.getSeatingCount()); + Assertions.assertEquals(applicationFromDatabase.get().getHasObstaclePersonExist(), updateRequest.getHasObstaclePersonExist()); + Assertions.assertEquals(applicationFromDatabase.get().getStatus(), updateRequest.getStatus()); + Assertions.assertEquals(applicationFromDatabase.get().getNotes(), updateRequest.getNotes()); + Assertions.assertNotNull(applicationFromDatabase.get().getUpdatedUser()); + Assertions.assertNotNull(applicationFromDatabase.get().getUpdatedAt()); + Assertions.assertTrue(UUIDTestUtil.isValid(applicationFromDatabase.get().getUpdatedUser())); + } } diff --git a/src/test/java/org/ays/emergency_application/model/EmergencyEvacuationApplicationBuilder.java b/src/test/java/org/ays/emergency_application/model/EmergencyEvacuationApplicationBuilder.java index 5bd31e2d4..df3187f12 100644 --- a/src/test/java/org/ays/emergency_application/model/EmergencyEvacuationApplicationBuilder.java +++ b/src/test/java/org/ays/emergency_application/model/EmergencyEvacuationApplicationBuilder.java @@ -58,7 +58,7 @@ public EmergencyEvacuationApplicationBuilder withHasObstaclePersonExist(Boolean } public EmergencyEvacuationApplicationBuilder withoutHasObstaclePersonExist() { - data.setHasObstaclePersonExist(false); + data.setHasObstaclePersonExist(null); return this; } diff --git a/src/test/java/org/ays/emergency_application/model/request/EmergencyEvacuationRequestBuilder.java b/src/test/java/org/ays/emergency_application/model/request/EmergencyEvacuationRequestBuilder.java index d657b7b99..a21411c74 100644 --- a/src/test/java/org/ays/emergency_application/model/request/EmergencyEvacuationRequestBuilder.java +++ b/src/test/java/org/ays/emergency_application/model/request/EmergencyEvacuationRequestBuilder.java @@ -12,17 +12,9 @@ public EmergencyEvacuationRequestBuilder() { public EmergencyEvacuationRequestBuilder withValidValues() { return this - .withFirstName("Test") - .withLastName("User") .withPhoneNumber(new AysPhoneNumberRequestBuilder().withValidValues().build()) - .withSourceCity("Source City") - .withSourceDistrict("Source District") .withAddress("Lorem Ipsum is simply dummy text of the printing and typesetting industry.") - .withSeatingCount(1) - .withTargetCity("Target City") - .withTargetDistrict("Target District") - .withApplicantPhoneNumber(new AysPhoneNumberRequestBuilder().withValidValues().build()) - .withHasObstaclePersonExist(false); + .withApplicantPhoneNumber(new AysPhoneNumberRequestBuilder().withValidValues().build()); } public EmergencyEvacuationRequestBuilder withFirstName(String firstName) { @@ -92,9 +84,4 @@ public EmergencyEvacuationRequestBuilder withoutApplicant() { return this; } - public EmergencyEvacuationRequestBuilder withHasObstaclePersonExist(Boolean hasObstaclePersonExist) { - data.setHasObstaclePersonExist(hasObstaclePersonExist); - return this; - } - } From 3f7154271a37d2e856cf58249df235e4221d288f Mon Sep 17 00:00:00 2001 From: m1erla <116915007+m1erla@users.noreply.github.com> Date: Fri, 17 Jan 2025 18:15:10 +0300 Subject: [PATCH 5/8] AYS-661 | Refactor: revert formatting changes in test files and in some classes, revert withSeatingCount method on withValidValues in EmergencyEvacuationRequestBuilder. --- .../model/EmergencyEvacuationApplication.java | 8 +- .../EmergencyEvacuationApplicationEntity.java | 3 +- ...EmergencyEvacuationApplicationRequest.java | 20 +- ...encyEvacuationApplicationEndToEndTest.java | 880 +++++++++--------- .../EmergencyEvacuationRequestBuilder.java | 1 + 5 files changed, 457 insertions(+), 455 deletions(-) diff --git a/src/main/java/org/ays/emergency_application/model/EmergencyEvacuationApplication.java b/src/main/java/org/ays/emergency_application/model/EmergencyEvacuationApplication.java index b6f883bbb..f1b30bb10 100644 --- a/src/main/java/org/ays/emergency_application/model/EmergencyEvacuationApplication.java +++ b/src/main/java/org/ays/emergency_application/model/EmergencyEvacuationApplication.java @@ -12,8 +12,7 @@ /** * Represents an emergency evacuation application. - * Extends {@link BaseDomainModel} and includes details such as personal and - * contact information, + * Extends {@link BaseDomainModel} and includes details such as personal and contact information, * location details, and application status. */ @Getter @@ -40,7 +39,6 @@ public class EmergencyEvacuationApplication extends BaseDomainModel { private Boolean isInPerson; private Boolean hasObstaclePersonExist; private String notes; - private Institution institution; /** @@ -71,14 +69,12 @@ public void pending() { this.referenceNumber = AysRandomUtil.generateNumber(10).toString(); this.status = EmergencyEvacuationApplicationStatus.PENDING; this.isInPerson = this.applicantPhoneNumber == null; - this.hasObstaclePersonExist = null; } /** * Sets the institution ID for the application. * - * @param institutionId the ID of the institution to associate with the - * application. + * @param institutionId the ID of the institution to associate with the application. */ public void setInstitutionId(final String institutionId) { this.institution = Institution.builder() diff --git a/src/main/java/org/ays/emergency_application/model/entity/EmergencyEvacuationApplicationEntity.java b/src/main/java/org/ays/emergency_application/model/entity/EmergencyEvacuationApplicationEntity.java index cb3defe63..f6419a160 100644 --- a/src/main/java/org/ays/emergency_application/model/entity/EmergencyEvacuationApplicationEntity.java +++ b/src/main/java/org/ays/emergency_application/model/entity/EmergencyEvacuationApplicationEntity.java @@ -21,8 +21,7 @@ /** * A JPA entity class that represents an emergency evacuation entity. - * The emergency evacuation applications are defined in the - * AYS_EMERGENCY_EVACUATION_APPLICATION table in the database. + * The emergency evacuation applications are defined in the AYS_EMERGENCY_EVACUATION_APPLICATION table in the database. */ @Entity @Getter diff --git a/src/main/java/org/ays/emergency_application/model/request/EmergencyEvacuationApplicationRequest.java b/src/main/java/org/ays/emergency_application/model/request/EmergencyEvacuationApplicationRequest.java index 00cbf2d58..cd76245db 100644 --- a/src/main/java/org/ays/emergency_application/model/request/EmergencyEvacuationApplicationRequest.java +++ b/src/main/java/org/ays/emergency_application/model/request/EmergencyEvacuationApplicationRequest.java @@ -14,10 +14,8 @@ import org.hibernate.validator.constraints.Range; /** - * Represents a request to complete emergency evacuation request. The request - * includes fields for the required user - * information, such as the user's phone number, as well as their first and last - * name. + * Represents a request to complete emergency evacuation request. The request includes fields for the required user + * information, such as the user's phone number, as well as their first and last name. */ @Getter @Setter @@ -76,15 +74,13 @@ public class EmergencyEvacuationApplicationRequest { @Valid private AysPhoneNumberRequest applicantPhoneNumber; - private Boolean hasObstaclePersonExist; - @JsonIgnore @AssertTrue(message = "all applicant fields must be filled") @SuppressWarnings("This method is unused by the application directly but Spring is using it in the background.") private boolean isAllApplicantFieldsFilled() { - if (StringUtils.isEmpty(this.applicantFirstName) && StringUtils.isEmpty(this.applicantLastName) - && this.applicantPhoneNumber == null) { + if (StringUtils.isEmpty(this.applicantFirstName) && + StringUtils.isEmpty(this.applicantLastName) && this.applicantPhoneNumber == null) { return true; } @@ -92,8 +88,8 @@ private boolean isAllApplicantFieldsFilled() { && this.applicantPhoneNumber != null && - !(StringUtils.isBlank(this.applicantPhoneNumber.getCountryCode()) - && StringUtils.isBlank(this.applicantPhoneNumber.getLineNumber())); + !(StringUtils.isBlank(this.applicantPhoneNumber.getCountryCode()) && + StringUtils.isBlank(this.applicantPhoneNumber.getLineNumber())); } @JsonIgnore @@ -105,8 +101,8 @@ private boolean isPhoneNumberMustNotBeSameOne() { return true; } - if (StringUtils.isEmpty(this.applicantPhoneNumber.getLineNumber()) - || StringUtils.isEmpty(this.phoneNumber.getLineNumber())) { + if (StringUtils.isEmpty(this.applicantPhoneNumber.getLineNumber()) || + StringUtils.isEmpty(this.phoneNumber.getLineNumber())) { return true; } diff --git a/src/test/java/org/ays/emergency_application/controller/EmergencyEvacuationApplicationEndToEndTest.java b/src/test/java/org/ays/emergency_application/controller/EmergencyEvacuationApplicationEndToEndTest.java index 4d76feb0b..9620bf917 100644 --- a/src/test/java/org/ays/emergency_application/controller/EmergencyEvacuationApplicationEndToEndTest.java +++ b/src/test/java/org/ays/emergency_application/controller/EmergencyEvacuationApplicationEndToEndTest.java @@ -37,445 +37,455 @@ class EmergencyEvacuationApplicationEndToEndTest extends AysEndToEndTest { - @Autowired - private EmergencyEvacuationApplicationSavePort emergencyEvacuationApplicationSavePort; - - @Autowired - private EmergencyEvacuationApplicationReadPort emergencyEvacuationApplicationReadPort; - - @Autowired - private EmergencyEvacuationApplicationRepository emergencyEvacuationApplicationRepository; - - - private final EmergencyEvacuationApplicationToApplicationResponseMapper emergencyEvacuationApplicationToApplicationResponseMapper = EmergencyEvacuationApplicationToApplicationResponseMapper.initialize(); - - - private static final String BASE_PATH = "/api/v1"; - - - @Test - void givenValidEmergencyEvacuationApplicationListRequest_whenApplicationsFound_thenReturnAysPageResponseOfEmergencyEvacuationApplicationsResponse() throws Exception { - - // Initialize - emergencyEvacuationApplicationRepository.deleteAll(); - - List applications = List.of( - emergencyEvacuationApplicationSavePort.save( - new EmergencyEvacuationApplicationBuilder() + @Autowired + private EmergencyEvacuationApplicationSavePort emergencyEvacuationApplicationSavePort; + + @Autowired + private EmergencyEvacuationApplicationReadPort emergencyEvacuationApplicationReadPort; + + @Autowired + private EmergencyEvacuationApplicationRepository emergencyEvacuationApplicationRepository; + + private final EmergencyEvacuationApplicationToApplicationResponseMapper emergencyEvacuationApplicationToApplicationResponseMapper = EmergencyEvacuationApplicationToApplicationResponseMapper + .initialize(); + + private static final String BASE_PATH = "/api/v1"; + + @Test + void givenValidEmergencyEvacuationApplicationListRequest_whenApplicationsFound_thenReturnAysPageResponseOfEmergencyEvacuationApplicationsResponse() + throws Exception { + + // Initialize + emergencyEvacuationApplicationRepository.deleteAll(); + + List applications = List.of( + emergencyEvacuationApplicationSavePort.save( + new EmergencyEvacuationApplicationBuilder() + .withValidValues() + .withoutId() + .withoutInstitution() + .withStatus(EmergencyEvacuationApplicationStatus.PENDING) + .withoutApplicant() + .build()), + emergencyEvacuationApplicationSavePort.save( + new EmergencyEvacuationApplicationBuilder() + .withValidValues() + .withoutId() + .withInstitution( + new InstitutionBuilder() + .withValidValues() + .withId(AysValidTestData.Admin.INSTITUTION_ID) + .build()) + .withStatus(EmergencyEvacuationApplicationStatus.PENDING) + .withoutApplicant() + .build())); + applications.forEach(application -> emergencyEvacuationApplicationSavePort.save(application)); + + // Given + AysPageable mockAysPageable = new AysPageableBuilder() + .withPage(1) + .withPageSize(10) + .build(); + EmergencyEvacuationApplicationListRequest mockListRequest = new EmergencyEvacuationApplicationListRequestBuilder() .withValidValues() - .withoutId() - .withoutInstitution() - .withStatus(EmergencyEvacuationApplicationStatus.PENDING) - .withoutApplicant() - .build() - ), + .withPageable(mockAysPageable) + .withFilter(null) + .withoutOrders() + .build(); + + // Then + String endpoint = BASE_PATH.concat("/emergency-evacuation-applications"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, adminToken.getAccessToken(), mockListRequest); + + AysResponse> mockResponse = AysResponseBuilder + .successPage(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.contentSize() + .value(applications.size())) + .andExpect(AysMockResultMatchersBuilders.contents("id") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.contents("referenceNumber") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.contents("firstName") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.contents("lastName") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.contents("phoneNumber.countryCode") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.contents("phoneNumber.lineNumber") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.contents("applicantFirstName") + .exists()) + .andExpect(AysMockResultMatchersBuilders.contents("applicantLastName") + .exists()) + .andExpect(AysMockResultMatchersBuilders.contents("applicantPhoneNumber.countryCode") + .exists()) + .andExpect(AysMockResultMatchersBuilders.contents("applicantPhoneNumber.lineNumber") + .exists()) + .andExpect(AysMockResultMatchersBuilders.contents("seatingCount") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.contents("status") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.contents("isInPerson") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.contents("createdAt") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.response("pageNumber") + .value(1)) + .andExpect(AysMockResultMatchersBuilders.response("pageSize") + .value(applications.size())) + .andExpect(AysMockResultMatchersBuilders.response("totalPageCount") + .value(1)) + .andExpect(AysMockResultMatchersBuilders.response("totalElementCount") + .value(applications.size())) + .andExpect(AysMockResultMatchersBuilders.response("orderedBy") + .isEmpty()) + .andExpect(AysMockResultMatchersBuilders.response("filteredBy") + .isNotEmpty()); + } + + @Test + void givenValidEmergencyEvacuationApplicationListRequest_whenEmergencyEvacuationApplicationsNotFound_thenReturnAysPageResponseOfEmergencyEvacuationApplicationsResponse() + throws Exception { + + // Initialize + emergencyEvacuationApplicationRepository.deleteAll(); emergencyEvacuationApplicationSavePort.save( - new EmergencyEvacuationApplicationBuilder() + new EmergencyEvacuationApplicationBuilder() + .withValidValues() + .withoutId() + .withoutInstitution() + .withStatus(EmergencyEvacuationApplicationStatus.PENDING) + .withoutApplicant() + .build()); + + // Given + EmergencyEvacuationApplicationListRequest mockListRequest = new EmergencyEvacuationApplicationListRequestBuilder() .withValidValues() - .withoutId() - .withInstitution( - new InstitutionBuilder() + .withStatuses(Set.of(EmergencyEvacuationApplicationStatus.COMPLETED)) + .withoutOrders() + .build(); + + // Then + String endpoint = BASE_PATH.concat("/emergency-evacuation-applications"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, adminToken.getAccessToken(), mockListRequest); + + AysResponse> mockResponse = AysResponseBuilder + .successPage(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.contentSize() + .value(0)) + .andExpect(AysMockResultMatchersBuilders.contents("id") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("referenceNumber") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("firstName") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("lastName") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("phoneNumber.countryCode") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("phoneNumber.lineNumber") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("applicantFirstName") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("applicantLastName") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("applicantPhoneNumber.countryCode") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("applicantPhoneNumber.lineNumber") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("seatingCount") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("status") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("isInPerson") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.contents("createdAt") + .doesNotExist()) + .andExpect(AysMockResultMatchersBuilders.response("pageNumber") + .value(1)) + .andExpect(AysMockResultMatchersBuilders.response("pageSize") + .value(0)) + .andExpect(AysMockResultMatchersBuilders.response("totalPageCount") + .value(0)) + .andExpect(AysMockResultMatchersBuilders.response("totalElementCount") + .value(0)) + .andExpect(AysMockResultMatchersBuilders.response("orderedBy") + .isEmpty()) + .andExpect(AysMockResultMatchersBuilders.response("filteredBy") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.response("filteredBy.statuses") + .isNotEmpty()); + } + + @Test + void givenValidEmergencyEvacuationApplicationId_whenEmergencyEvacuationApplicationExists_thenReturnEmergencyEvacuationApplicationResponse() + throws Exception { + + // Initialize + emergencyEvacuationApplicationRepository.deleteAll(); + EmergencyEvacuationApplication application = emergencyEvacuationApplicationSavePort.save( + new EmergencyEvacuationApplicationBuilder() .withValidValues() - .withId(AysValidTestData.Admin.INSTITUTION_ID) - .build() - ) - .withStatus(EmergencyEvacuationApplicationStatus.PENDING) + .withoutId() + .withoutInstitution() + .withoutApplicant() + .build()); + + // Given + String applicationId = application.getId(); + + // Then + String endpoint = BASE_PATH.concat("/emergency-evacuation-application/").concat(applicationId); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .get(endpoint, adminToken.getAccessToken()); + + EmergencyEvacuationApplicationResponse mockApplicationResponse = emergencyEvacuationApplicationToApplicationResponseMapper + .map(application); + + AysResponse mockResponse = AysResponse + .successOf(mockApplicationResponse); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.response("id") + .value(application.getId())) + .andExpect(AysMockResultMatchersBuilders.response("referenceNumber") + .value(application.getReferenceNumber())) + .andExpect(AysMockResultMatchersBuilders.response("firstName") + .value(application.getFirstName())) + .andExpect(AysMockResultMatchersBuilders.response("lastName") + .value(application.getLastName())) + .andExpect(AysMockResultMatchersBuilders.response("phoneNumber.countryCode") + .value(application.getPhoneNumber().getCountryCode())) + .andExpect(AysMockResultMatchersBuilders.response("phoneNumber.lineNumber") + .value(application.getPhoneNumber().getLineNumber())) + .andExpect(AysMockResultMatchersBuilders.response("sourceCity") + .value(application.getSourceCity())) + .andExpect(AysMockResultMatchersBuilders.response("sourceDistrict") + .value(application.getSourceDistrict())) + .andExpect(AysMockResultMatchersBuilders.response("address") + .value(application.getAddress())) + .andExpect(AysMockResultMatchersBuilders.response("seatingCount") + .value(application.getSeatingCount())) + .andExpect(AysMockResultMatchersBuilders.response("targetCity") + .value(application.getTargetCity())) + .andExpect(AysMockResultMatchersBuilders.response("targetDistrict") + .value(application.getTargetDistrict())) + .andExpect(AysMockResultMatchersBuilders.response("status") + .value(application.getStatus().toString())) + .andExpect(AysMockResultMatchersBuilders.response("isInPerson") + .value(application.getIsInPerson())) + .andExpect(AysMockResultMatchersBuilders.response("hasObstaclePersonExist") + .value(application.getHasObstaclePersonExist())) + .andExpect(AysMockResultMatchersBuilders.response("notes") + .value(application.getNotes())) + .andExpect(AysMockResultMatchersBuilders.response("createdUser") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.response("createdAt") + .isNotEmpty()) + .andExpect(AysMockResultMatchersBuilders.response("updatedUser") + .isEmpty()) + .andExpect(AysMockResultMatchersBuilders.response("updatedAt") + .isEmpty()); + } + + @Test + void givenValidEmergencyEvacuationApplicationRequest_whenApplicationSaved_thenReturnSuccessResponse() + throws Exception { + // Given + String firstName = "Test Application with Applicant"; + EmergencyEvacuationApplicationRequest applicationRequest = new EmergencyEvacuationRequestBuilder() + .withValidValues() + .withFirstName(firstName) + .build(); + + // Then + String endpoint = BASE_PATH.concat("/emergency-evacuation-application"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, applicationRequest); + + AysResponse mockResponse = AysResponseBuilder.success(); + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .doesNotExist()); + + // Verify + AysPageable pageable = AysPageable.builder() + .page(1) + .pageSize(1000) + .build(); + Optional application = emergencyEvacuationApplicationReadPort + .findAll(pageable, null) + .getContent().stream() + .filter(evacuationApplication -> evacuationApplication.getFirstName() + .equals(applicationRequest.getFirstName())) + .findFirst(); + + Assertions.assertTrue(application.isPresent()); + Assertions.assertNull(application.get().getInstitution()); + Assertions.assertNotNull(application.get().getReferenceNumber()); + Assertions.assertEquals(10, application.get().getReferenceNumber().length()); + Assertions.assertEquals(application.get().getFirstName(), firstName); + Assertions.assertEquals(application.get().getLastName(), applicationRequest.getLastName()); + Assertions.assertEquals(application.get().getPhoneNumber().getCountryCode(), + applicationRequest.getPhoneNumber().getCountryCode()); + Assertions.assertEquals(application.get().getPhoneNumber().getLineNumber(), + applicationRequest.getPhoneNumber().getLineNumber()); + Assertions.assertEquals(application.get().getSourceCity(), applicationRequest.getSourceCity()); + Assertions.assertEquals(application.get().getSourceDistrict(), applicationRequest.getSourceDistrict()); + Assertions.assertEquals(application.get().getAddress(), applicationRequest.getAddress()); + Assertions.assertEquals(application.get().getSeatingCount(), applicationRequest.getSeatingCount()); + Assertions.assertEquals(application.get().getTargetCity(), applicationRequest.getTargetCity()); + Assertions.assertEquals(application.get().getTargetDistrict(), applicationRequest.getTargetDistrict()); + Assertions.assertEquals(EmergencyEvacuationApplicationStatus.PENDING, application.get().getStatus()); + Assertions.assertEquals(application.get().getApplicantFirstName(), + applicationRequest.getApplicantFirstName()); + Assertions.assertEquals(application.get().getApplicantLastName(), + applicationRequest.getApplicantLastName()); + Assertions.assertEquals(application.get().getApplicantPhoneNumber().getCountryCode(), + applicationRequest.getApplicantPhoneNumber().getCountryCode()); + Assertions.assertEquals(application.get().getApplicantPhoneNumber().getLineNumber(), + applicationRequest.getApplicantPhoneNumber().getLineNumber()); + Assertions.assertFalse(application.get().getIsInPerson()); + Assertions.assertNull(application.get().getHasObstaclePersonExist()); + Assertions.assertNull(application.get().getNotes()); + Assertions.assertEquals("AYS", application.get().getCreatedUser()); + } + + @Test + void givenValidEmergencyEvacuationApplicationRequestWithoutApplicant_whenApplicationSaved_thenReturnSuccessResponse() + throws Exception { + // Given + String firstName = "Test Application without Applicant"; + EmergencyEvacuationApplicationRequest applicationRequest = new EmergencyEvacuationRequestBuilder() + .withValidValues() + .withFirstName(firstName) .withoutApplicant() - .build() - ) - ); - applications.forEach(application -> emergencyEvacuationApplicationSavePort.save(application)); - - // Given - AysPageable mockAysPageable = new AysPageableBuilder() - .withPage(1) - .withPageSize(10) - .build(); - EmergencyEvacuationApplicationListRequest mockListRequest = new EmergencyEvacuationApplicationListRequestBuilder() - .withValidValues() - .withPageable(mockAysPageable) - .withFilter(null) - .withoutOrders() - .build(); - - // Then - String endpoint = BASE_PATH.concat("/emergency-evacuation-applications"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, adminToken.getAccessToken(), mockListRequest); - - AysResponse> mockResponse = AysResponseBuilder.successPage(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.contentSize() - .value(applications.size())) - .andExpect(AysMockResultMatchersBuilders.contents("id") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.contents("referenceNumber") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.contents("firstName") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.contents("lastName") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.contents("phoneNumber.countryCode") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.contents("phoneNumber.lineNumber") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.contents("applicantFirstName") - .exists()) - .andExpect(AysMockResultMatchersBuilders.contents("applicantLastName") - .exists()) - .andExpect(AysMockResultMatchersBuilders.contents("applicantPhoneNumber.countryCode") - .exists()) - .andExpect(AysMockResultMatchersBuilders.contents("applicantPhoneNumber.lineNumber") - .exists()) - .andExpect(AysMockResultMatchersBuilders.contents("seatingCount") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.contents("status") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.contents("isInPerson") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.contents("createdAt") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.response("pageNumber") - .value(1)) - .andExpect(AysMockResultMatchersBuilders.response("pageSize") - .value(applications.size())) - .andExpect(AysMockResultMatchersBuilders.response("totalPageCount") - .value(1)) - .andExpect(AysMockResultMatchersBuilders.response("totalElementCount") - .value(applications.size())) - .andExpect(AysMockResultMatchersBuilders.response("orderedBy") - .isEmpty()) - .andExpect(AysMockResultMatchersBuilders.response("filteredBy") - .isNotEmpty()); - } - - @Test - void givenValidEmergencyEvacuationApplicationListRequest_whenEmergencyEvacuationApplicationsNotFound_thenReturnAysPageResponseOfEmergencyEvacuationApplicationsResponse() throws Exception { - - // Initialize - emergencyEvacuationApplicationRepository.deleteAll(); - emergencyEvacuationApplicationSavePort.save( - new EmergencyEvacuationApplicationBuilder() - .withValidValues() - .withoutId() - .withoutInstitution() - .withStatus(EmergencyEvacuationApplicationStatus.PENDING) - .withoutApplicant() - .build() - ); - - // Given - EmergencyEvacuationApplicationListRequest mockListRequest = new EmergencyEvacuationApplicationListRequestBuilder() - .withValidValues() - .withStatuses(Set.of(EmergencyEvacuationApplicationStatus.COMPLETED)) - .withoutOrders() - .build(); - - // Then - String endpoint = BASE_PATH.concat("/emergency-evacuation-applications"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, adminToken.getAccessToken(), mockListRequest); - - AysResponse> mockResponse = AysResponseBuilder.successPage(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.contentSize() - .value(0)) - .andExpect(AysMockResultMatchersBuilders.contents("id") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("referenceNumber") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("firstName") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("lastName") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("phoneNumber.countryCode") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("phoneNumber.lineNumber") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("applicantFirstName") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("applicantLastName") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("applicantPhoneNumber.countryCode") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("applicantPhoneNumber.lineNumber") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("seatingCount") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("status") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("isInPerson") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.contents("createdAt") - .doesNotExist()) - .andExpect(AysMockResultMatchersBuilders.response("pageNumber") - .value(1)) - .andExpect(AysMockResultMatchersBuilders.response("pageSize") - .value(0)) - .andExpect(AysMockResultMatchersBuilders.response("totalPageCount") - .value(0)) - .andExpect(AysMockResultMatchersBuilders.response("totalElementCount") - .value(0)) - .andExpect(AysMockResultMatchersBuilders.response("orderedBy") - .isEmpty()) - .andExpect(AysMockResultMatchersBuilders.response("filteredBy") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.response("filteredBy.statuses") - .isNotEmpty()); - } - - @Test - void givenValidEmergencyEvacuationApplicationId_whenEmergencyEvacuationApplicationExists_thenReturnEmergencyEvacuationApplicationResponse() throws Exception { - - // Initialize - emergencyEvacuationApplicationRepository.deleteAll(); - EmergencyEvacuationApplication application = emergencyEvacuationApplicationSavePort.save( - new EmergencyEvacuationApplicationBuilder() - .withValidValues() - .withoutId() - .withoutInstitution() - .withoutApplicant() - .build() - ); - - // Given - String applicationId = application.getId(); - - // Then - String endpoint = BASE_PATH.concat("/emergency-evacuation-application/").concat(applicationId); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .get(endpoint, adminToken.getAccessToken()); - - EmergencyEvacuationApplicationResponse mockApplicationResponse = emergencyEvacuationApplicationToApplicationResponseMapper - .map(application); - - AysResponse mockResponse = AysResponse - .successOf(mockApplicationResponse); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.response("id") - .value(application.getId())) - .andExpect(AysMockResultMatchersBuilders.response("referenceNumber") - .value(application.getReferenceNumber())) - .andExpect(AysMockResultMatchersBuilders.response("firstName") - .value(application.getFirstName())) - .andExpect(AysMockResultMatchersBuilders.response("lastName") - .value(application.getLastName())) - .andExpect(AysMockResultMatchersBuilders.response("phoneNumber.countryCode") - .value(application.getPhoneNumber().getCountryCode())) - .andExpect(AysMockResultMatchersBuilders.response("phoneNumber.lineNumber") - .value(application.getPhoneNumber().getLineNumber())) - .andExpect(AysMockResultMatchersBuilders.response("sourceCity") - .value(application.getSourceCity())) - .andExpect(AysMockResultMatchersBuilders.response("sourceDistrict") - .value(application.getSourceDistrict())) - .andExpect(AysMockResultMatchersBuilders.response("address") - .value(application.getAddress())) - .andExpect(AysMockResultMatchersBuilders.response("seatingCount") - .value(application.getSeatingCount())) - .andExpect(AysMockResultMatchersBuilders.response("targetCity") - .value(application.getTargetCity())) - .andExpect(AysMockResultMatchersBuilders.response("targetDistrict") - .value(application.getTargetDistrict())) - .andExpect(AysMockResultMatchersBuilders.response("status") - .value(application.getStatus().toString())) - .andExpect(AysMockResultMatchersBuilders.response("isInPerson") - .value(application.getIsInPerson())) - .andExpect(AysMockResultMatchersBuilders.response("hasObstaclePersonExist") - .value(application.getHasObstaclePersonExist())) - .andExpect(AysMockResultMatchersBuilders.response("notes") - .value(application.getNotes())) - .andExpect(AysMockResultMatchersBuilders.response("createdUser") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.response("createdAt") - .isNotEmpty()) - .andExpect(AysMockResultMatchersBuilders.response("updatedUser") - .isEmpty()) - .andExpect(AysMockResultMatchersBuilders.response("updatedAt") - .isEmpty()); - } - - - @Test - void givenValidEmergencyEvacuationApplicationRequest_whenApplicationSaved_thenReturnSuccessResponse() throws Exception { - // Given - String firstName = "Test Application with Applicant"; - EmergencyEvacuationApplicationRequest applicationRequest = new EmergencyEvacuationRequestBuilder() - .withValidValues() - .withFirstName(firstName) - .build(); - - // Then - String endpoint = BASE_PATH.concat("/emergency-evacuation-application"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, applicationRequest); - - AysResponse mockResponse = AysResponseBuilder.success(); - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .doesNotExist()); - - // Verify - AysPageable pageable = AysPageable.builder() - .page(1) - .pageSize(1000) - .build(); - Optional application = emergencyEvacuationApplicationReadPort - .findAll(pageable, null) - .getContent().stream() - .filter(evacuationApplication -> evacuationApplication.getFirstName().equals(applicationRequest.getFirstName())) - .findFirst(); - - Assertions.assertTrue(application.isPresent()); - Assertions.assertNull(application.get().getInstitution()); - Assertions.assertNotNull(application.get().getReferenceNumber()); - Assertions.assertEquals(10, application.get().getReferenceNumber().length()); - Assertions.assertEquals(application.get().getFirstName(), firstName); - Assertions.assertEquals(application.get().getLastName(), applicationRequest.getLastName()); - Assertions.assertEquals(application.get().getPhoneNumber().getCountryCode(), applicationRequest.getPhoneNumber().getCountryCode()); - Assertions.assertEquals(application.get().getPhoneNumber().getLineNumber(), applicationRequest.getPhoneNumber().getLineNumber()); - Assertions.assertEquals(application.get().getSourceCity(), applicationRequest.getSourceCity()); - Assertions.assertEquals(application.get().getSourceDistrict(), applicationRequest.getSourceDistrict()); - Assertions.assertEquals(application.get().getAddress(), applicationRequest.getAddress()); - Assertions.assertEquals(application.get().getSeatingCount(), applicationRequest.getSeatingCount()); - Assertions.assertEquals(application.get().getTargetCity(), applicationRequest.getTargetCity()); - Assertions.assertEquals(application.get().getTargetDistrict(), applicationRequest.getTargetDistrict()); - Assertions.assertEquals(EmergencyEvacuationApplicationStatus.PENDING, application.get().getStatus()); - Assertions.assertEquals(application.get().getApplicantFirstName(), applicationRequest.getApplicantFirstName()); - Assertions.assertEquals(application.get().getApplicantLastName(), applicationRequest.getApplicantLastName()); - Assertions.assertEquals(application.get().getApplicantPhoneNumber().getCountryCode(), applicationRequest.getApplicantPhoneNumber().getCountryCode()); - Assertions.assertEquals(application.get().getApplicantPhoneNumber().getLineNumber(), applicationRequest.getApplicantPhoneNumber().getLineNumber()); - Assertions.assertFalse(application.get().getIsInPerson()); - Assertions.assertNull(application.get().getHasObstaclePersonExist()); - Assertions.assertNull(application.get().getNotes()); - Assertions.assertEquals("AYS", application.get().getCreatedUser()); - } - - @Test - void givenValidEmergencyEvacuationApplicationRequestWithoutApplicant_whenApplicationSaved_thenReturnSuccessResponse() throws Exception { - // Given - String firstName = "Test Application without Applicant"; - EmergencyEvacuationApplicationRequest applicationRequest = new EmergencyEvacuationRequestBuilder() - .withValidValues() - .withFirstName(firstName) - .withoutApplicant() - .build(); - - // Then - String endpoint = BASE_PATH.concat("/emergency-evacuation-application"); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .post(endpoint, applicationRequest); - - AysResponse mockResponse = AysResponseBuilder.success(); - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(AysMockResultMatchersBuilders.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.subErrors() - .doesNotExist()); - - // Verify - AysPageable pageable = AysPageable.builder() - .page(1) - .pageSize(1000) - .build(); - Optional application = emergencyEvacuationApplicationReadPort - .findAll(pageable, null) - .getContent().stream() - .filter(evacuationApplication -> evacuationApplication.getFirstName().equals(applicationRequest.getFirstName())) - .findFirst(); - - Assertions.assertTrue(application.isPresent()); - Assertions.assertNull(application.get().getInstitution()); - Assertions.assertNotNull(application.get().getReferenceNumber()); - Assertions.assertEquals(10, application.get().getReferenceNumber().length()); - Assertions.assertEquals(application.get().getFirstName(), firstName); - Assertions.assertEquals(application.get().getLastName(), applicationRequest.getLastName()); - Assertions.assertEquals(application.get().getPhoneNumber().getCountryCode(), applicationRequest.getPhoneNumber().getCountryCode()); - Assertions.assertEquals(application.get().getPhoneNumber().getLineNumber(), applicationRequest.getPhoneNumber().getLineNumber()); - Assertions.assertEquals(application.get().getSourceCity(), applicationRequest.getSourceCity()); - Assertions.assertEquals(application.get().getSourceDistrict(), applicationRequest.getSourceDistrict()); - Assertions.assertEquals(application.get().getAddress(), applicationRequest.getAddress()); - Assertions.assertEquals(application.get().getSeatingCount(), applicationRequest.getSeatingCount()); - Assertions.assertEquals(application.get().getTargetCity(), applicationRequest.getTargetCity()); - Assertions.assertEquals(application.get().getTargetDistrict(), applicationRequest.getTargetDistrict()); - Assertions.assertEquals(EmergencyEvacuationApplicationStatus.PENDING, application.get().getStatus()); - Assertions.assertEquals(application.get().getApplicantFirstName(), applicationRequest.getApplicantFirstName()); - Assertions.assertEquals(application.get().getApplicantLastName(), applicationRequest.getApplicantLastName()); - Assertions.assertNull(application.get().getApplicantPhoneNumber().getCountryCode()); - Assertions.assertNull(application.get().getApplicantPhoneNumber().getLineNumber()); - Assertions.assertTrue(application.get().getIsInPerson()); - Assertions.assertNull(application.get().getHasObstaclePersonExist()); - Assertions.assertNull(application.get().getNotes()); - Assertions.assertEquals("AYS", application.get().getCreatedUser()); - } - - - @Test - void givenValidIdAndValidUpdateRequest_whenApplicationUpdated_thenReturnSuccessResponse() throws Exception { - - // Initialize - emergencyEvacuationApplicationRepository.deleteAll(); - EmergencyEvacuationApplication application = emergencyEvacuationApplicationSavePort.save( - new EmergencyEvacuationApplicationBuilder() - .withValidValues() - .withoutId() - .withoutInstitution() - .withoutApplicant() - .withSeatingCount(5) - .withStatus(EmergencyEvacuationApplicationStatus.PENDING) - .withoutHasObstaclePersonExist() - .withoutNotes() - .build() - ); - - // Given - String id = application.getId(); - EmergencyEvacuationApplicationUpdateRequest updateRequest = new EmergencyEvacuationApplicationUpdateRequestBuilder() - .withValidValues() - .build(); - - // Then - String endpoint = BASE_PATH.concat("/emergency-evacuation-application/").concat(id); - MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders - .put(endpoint, adminToken.getAccessToken(), updateRequest); - - AysResponse mockResponse = AysResponseBuilder.success(); - - aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) - .andExpect(MockMvcResultMatchers.status() - .isOk()) - .andExpect(AysMockResultMatchersBuilders.response() - .doesNotExist()); - - // Verify - Optional applicationFromDatabase = emergencyEvacuationApplicationReadPort - .findById(id); - - Assertions.assertTrue(applicationFromDatabase.isPresent()); - Assertions.assertNotNull(applicationFromDatabase.get().getInstitution()); - Assertions.assertEquals(applicationFromDatabase.get().getSeatingCount(), updateRequest.getSeatingCount()); - Assertions.assertEquals(applicationFromDatabase.get().getHasObstaclePersonExist(), updateRequest.getHasObstaclePersonExist()); - Assertions.assertEquals(applicationFromDatabase.get().getStatus(), updateRequest.getStatus()); - Assertions.assertEquals(applicationFromDatabase.get().getNotes(), updateRequest.getNotes()); - Assertions.assertNotNull(applicationFromDatabase.get().getUpdatedUser()); - Assertions.assertNotNull(applicationFromDatabase.get().getUpdatedAt()); - Assertions.assertTrue(UUIDTestUtil.isValid(applicationFromDatabase.get().getUpdatedUser())); - } + .build(); + + // Then + String endpoint = BASE_PATH.concat("/emergency-evacuation-application"); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .post(endpoint, applicationRequest); + + AysResponse mockResponse = AysResponseBuilder.success(); + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(AysMockResultMatchersBuilders.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.subErrors() + .doesNotExist()); + + // Verify + AysPageable pageable = AysPageable.builder() + .page(1) + .pageSize(1000) + .build(); + Optional application = emergencyEvacuationApplicationReadPort + .findAll(pageable, null) + .getContent().stream() + .filter(evacuationApplication -> evacuationApplication.getFirstName() + .equals(applicationRequest.getFirstName())) + .findFirst(); + + Assertions.assertTrue(application.isPresent()); + Assertions.assertNull(application.get().getInstitution()); + Assertions.assertNotNull(application.get().getReferenceNumber()); + Assertions.assertEquals(10, application.get().getReferenceNumber().length()); + Assertions.assertEquals(application.get().getFirstName(), firstName); + Assertions.assertEquals(application.get().getLastName(), applicationRequest.getLastName()); + Assertions.assertEquals(application.get().getPhoneNumber().getCountryCode(), + applicationRequest.getPhoneNumber().getCountryCode()); + Assertions.assertEquals(application.get().getPhoneNumber().getLineNumber(), + applicationRequest.getPhoneNumber().getLineNumber()); + Assertions.assertEquals(application.get().getSourceCity(), applicationRequest.getSourceCity()); + Assertions.assertEquals(application.get().getSourceDistrict(), applicationRequest.getSourceDistrict()); + Assertions.assertEquals(application.get().getAddress(), applicationRequest.getAddress()); + Assertions.assertEquals(application.get().getSeatingCount(), applicationRequest.getSeatingCount()); + Assertions.assertEquals(application.get().getTargetCity(), applicationRequest.getTargetCity()); + Assertions.assertEquals(application.get().getTargetDistrict(), applicationRequest.getTargetDistrict()); + Assertions.assertEquals(EmergencyEvacuationApplicationStatus.PENDING, application.get().getStatus()); + Assertions.assertEquals(application.get().getApplicantFirstName(), + applicationRequest.getApplicantFirstName()); + Assertions.assertEquals(application.get().getApplicantLastName(), + applicationRequest.getApplicantLastName()); + Assertions.assertNull(application.get().getApplicantPhoneNumber().getCountryCode()); + Assertions.assertNull(application.get().getApplicantPhoneNumber().getLineNumber()); + Assertions.assertTrue(application.get().getIsInPerson()); + Assertions.assertNull(application.get().getHasObstaclePersonExist()); + Assertions.assertNull(application.get().getNotes()); + Assertions.assertEquals("AYS", application.get().getCreatedUser()); + } + + @Test + void givenValidIdAndValidUpdateRequest_whenApplicationUpdated_thenReturnSuccessResponse() throws Exception { + + // Initialize + emergencyEvacuationApplicationRepository.deleteAll(); + EmergencyEvacuationApplication application = emergencyEvacuationApplicationSavePort.save( + new EmergencyEvacuationApplicationBuilder() + .withValidValues() + .withoutId() + .withoutInstitution() + .withoutApplicant() + .withSeatingCount(5) + .withStatus(EmergencyEvacuationApplicationStatus.PENDING) + .withoutHasObstaclePersonExist() + .withoutNotes() + .build()); + + // Given + String id = application.getId(); + EmergencyEvacuationApplicationUpdateRequest updateRequest = new EmergencyEvacuationApplicationUpdateRequestBuilder() + .withValidValues() + .build(); + + // Then + String endpoint = BASE_PATH.concat("/emergency-evacuation-application/").concat(id); + MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders + .put(endpoint, adminToken.getAccessToken(), updateRequest); + + AysResponse mockResponse = AysResponseBuilder.success(); + + aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) + .andExpect(MockMvcResultMatchers.status() + .isOk()) + .andExpect(AysMockResultMatchersBuilders.response() + .doesNotExist()); + + // Verify + Optional applicationFromDatabase = emergencyEvacuationApplicationReadPort + .findById(id); + + Assertions.assertTrue(applicationFromDatabase.isPresent()); + Assertions.assertNotNull(applicationFromDatabase.get().getInstitution()); + Assertions.assertEquals(applicationFromDatabase.get().getSeatingCount(), + updateRequest.getSeatingCount()); + Assertions.assertEquals(applicationFromDatabase.get().getHasObstaclePersonExist(), + updateRequest.getHasObstaclePersonExist()); + Assertions.assertEquals(applicationFromDatabase.get().getStatus(), updateRequest.getStatus()); + Assertions.assertEquals(applicationFromDatabase.get().getNotes(), updateRequest.getNotes()); + Assertions.assertNotNull(applicationFromDatabase.get().getUpdatedUser()); + Assertions.assertNotNull(applicationFromDatabase.get().getUpdatedAt()); + Assertions.assertTrue(UUIDTestUtil.isValid(applicationFromDatabase.get().getUpdatedUser())); + } } diff --git a/src/test/java/org/ays/emergency_application/model/request/EmergencyEvacuationRequestBuilder.java b/src/test/java/org/ays/emergency_application/model/request/EmergencyEvacuationRequestBuilder.java index a21411c74..732b1cd95 100644 --- a/src/test/java/org/ays/emergency_application/model/request/EmergencyEvacuationRequestBuilder.java +++ b/src/test/java/org/ays/emergency_application/model/request/EmergencyEvacuationRequestBuilder.java @@ -14,6 +14,7 @@ public EmergencyEvacuationRequestBuilder withValidValues() { return this .withPhoneNumber(new AysPhoneNumberRequestBuilder().withValidValues().build()) .withAddress("Lorem Ipsum is simply dummy text of the printing and typesetting industry.") + .withSeatingCount(1) .withApplicantPhoneNumber(new AysPhoneNumberRequestBuilder().withValidValues().build()); } From 93d2655f03b80885cbd97d474d255383483b4dcc Mon Sep 17 00:00:00 2001 From: m1erla <116915007+m1erla@users.noreply.github.com> Date: Fri, 17 Jan 2025 19:05:54 +0300 Subject: [PATCH 6/8] AYS-661 | Refactor: revert email validation method in EmailAddressValidator. --- .../org/ays/common/util/validation/EmailAddressValidator.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/org/ays/common/util/validation/EmailAddressValidator.java b/src/main/java/org/ays/common/util/validation/EmailAddressValidator.java index 234d1f9e9..1f05d8fe6 100644 --- a/src/main/java/org/ays/common/util/validation/EmailAddressValidator.java +++ b/src/main/java/org/ays/common/util/validation/EmailAddressValidator.java @@ -69,6 +69,10 @@ public boolean isValid(String email, ConstraintValidatorContext constraintValida return this.buildViolation(constraintValidatorContext, "email is not in a valid format"); } + if (email.length() < 6 || email.length() > 254) { + return this.buildViolation(constraintValidatorContext, "length must be between 6 and 254 characters"); + } + return true; } From 6b82a406ce318274d16978627b24fe9c49016a0b Mon Sep 17 00:00:00 2001 From: m1erla <116915007+m1erla@users.noreply.github.com> Date: Sat, 18 Jan 2025 20:16:51 +0300 Subject: [PATCH 7/8] AYS-661 | Refactor: revert methods on withValidValues and relocated the email's size controller in the EmailAddressValidator. --- .../util/validation/EmailAddressValidator.java | 8 ++++---- .../EmergencyEvacuationApplicationRequest.java | 13 +++++-------- .../EmergencyEvacuationApplicationEndToEndTest.java | 2 +- .../request/EmergencyEvacuationRequestBuilder.java | 9 ++++----- 4 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/ays/common/util/validation/EmailAddressValidator.java b/src/main/java/org/ays/common/util/validation/EmailAddressValidator.java index 1f05d8fe6..159c470ad 100644 --- a/src/main/java/org/ays/common/util/validation/EmailAddressValidator.java +++ b/src/main/java/org/ays/common/util/validation/EmailAddressValidator.java @@ -43,6 +43,10 @@ public boolean isValid(String email, ConstraintValidatorContext constraintValida return true; } + if (email.length() <= 6 || email.length() >= 254) { + return this.buildViolation(constraintValidatorContext, "length must be between 6 and 254 characters"); + } + if (email.startsWith(" ") || email.endsWith(" ")) { return this.buildViolation(constraintValidatorContext, "email must not start or end with whitespace"); } @@ -69,10 +73,6 @@ public boolean isValid(String email, ConstraintValidatorContext constraintValida return this.buildViolation(constraintValidatorContext, "email is not in a valid format"); } - if (email.length() < 6 || email.length() > 254) { - return this.buildViolation(constraintValidatorContext, "length must be between 6 and 254 characters"); - } - return true; } diff --git a/src/main/java/org/ays/emergency_application/model/request/EmergencyEvacuationApplicationRequest.java b/src/main/java/org/ays/emergency_application/model/request/EmergencyEvacuationApplicationRequest.java index cd76245db..eb7294f88 100644 --- a/src/main/java/org/ays/emergency_application/model/request/EmergencyEvacuationApplicationRequest.java +++ b/src/main/java/org/ays/emergency_application/model/request/EmergencyEvacuationApplicationRequest.java @@ -74,13 +74,13 @@ public class EmergencyEvacuationApplicationRequest { @Valid private AysPhoneNumberRequest applicantPhoneNumber; + @JsonIgnore @AssertTrue(message = "all applicant fields must be filled") @SuppressWarnings("This method is unused by the application directly but Spring is using it in the background.") private boolean isAllApplicantFieldsFilled() { - if (StringUtils.isEmpty(this.applicantFirstName) && - StringUtils.isEmpty(this.applicantLastName) && this.applicantPhoneNumber == null) { + if (StringUtils.isEmpty(this.applicantFirstName) && StringUtils.isEmpty(this.applicantLastName) && this.applicantPhoneNumber == null) { return true; } @@ -88,8 +88,7 @@ private boolean isAllApplicantFieldsFilled() { && this.applicantPhoneNumber != null && - !(StringUtils.isBlank(this.applicantPhoneNumber.getCountryCode()) && - StringUtils.isBlank(this.applicantPhoneNumber.getLineNumber())); + !(StringUtils.isBlank(this.applicantPhoneNumber.getCountryCode()) && StringUtils.isBlank(this.applicantPhoneNumber.getLineNumber())); } @JsonIgnore @@ -101,8 +100,7 @@ private boolean isPhoneNumberMustNotBeSameOne() { return true; } - if (StringUtils.isEmpty(this.applicantPhoneNumber.getLineNumber()) || - StringUtils.isEmpty(this.phoneNumber.getLineNumber())) { + if (StringUtils.isEmpty(this.applicantPhoneNumber.getLineNumber()) || StringUtils.isEmpty(this.phoneNumber.getLineNumber())) { return true; } @@ -114,8 +112,7 @@ private boolean isPhoneNumberMustNotBeSameOne() { @SuppressWarnings("This method is unused by the application directly but Spring is using it in the background.") private boolean isSourceCityAndDistrictDifferentFromTargetCityAndDistrict() { - if (this.sourceCity == null || this.sourceDistrict == null || this.targetCity == null - || this.targetDistrict == null) { + if (this.sourceCity == null || this.sourceDistrict == null || this.targetCity == null || this.targetDistrict == null) { return true; } diff --git a/src/test/java/org/ays/emergency_application/controller/EmergencyEvacuationApplicationEndToEndTest.java b/src/test/java/org/ays/emergency_application/controller/EmergencyEvacuationApplicationEndToEndTest.java index 9620bf917..f616f548c 100644 --- a/src/test/java/org/ays/emergency_application/controller/EmergencyEvacuationApplicationEndToEndTest.java +++ b/src/test/java/org/ays/emergency_application/controller/EmergencyEvacuationApplicationEndToEndTest.java @@ -488,4 +488,4 @@ void givenValidIdAndValidUpdateRequest_whenApplicationUpdated_thenReturnSuccessR Assertions.assertTrue(UUIDTestUtil.isValid(applicationFromDatabase.get().getUpdatedUser())); } -} +} \ No newline at end of file diff --git a/src/test/java/org/ays/emergency_application/model/request/EmergencyEvacuationRequestBuilder.java b/src/test/java/org/ays/emergency_application/model/request/EmergencyEvacuationRequestBuilder.java index 732b1cd95..cf03b1e2e 100644 --- a/src/test/java/org/ays/emergency_application/model/request/EmergencyEvacuationRequestBuilder.java +++ b/src/test/java/org/ays/emergency_application/model/request/EmergencyEvacuationRequestBuilder.java @@ -12,12 +12,11 @@ public EmergencyEvacuationRequestBuilder() { public EmergencyEvacuationRequestBuilder withValidValues() { return this - .withPhoneNumber(new AysPhoneNumberRequestBuilder().withValidValues().build()) - .withAddress("Lorem Ipsum is simply dummy text of the printing and typesetting industry.") .withSeatingCount(1) - .withApplicantPhoneNumber(new AysPhoneNumberRequestBuilder().withValidValues().build()); + .withPhoneNumber(new AysPhoneNumberRequestBuilder().withValidValues().build()) + .withApplicantPhoneNumber(new AysPhoneNumberRequestBuilder().withValidValues().build()) + .withAddress("Lorem Ipsum is simply dummy text of the printing and typesetting industry."); } - public EmergencyEvacuationRequestBuilder withFirstName(String firstName) { data.setFirstName(firstName); return this; @@ -85,4 +84,4 @@ public EmergencyEvacuationRequestBuilder withoutApplicant() { return this; } -} +} \ No newline at end of file From ea6d6bf18a9c99963ade29e59056d26d4aa63b33 Mon Sep 17 00:00:00 2001 From: m1erla <116915007+m1erla@users.noreply.github.com> Date: Sat, 18 Jan 2025 20:28:11 +0300 Subject: [PATCH 8/8] AYS-661 | Refactor: reverted format of EmergencyEvacuationApplicationEndToEndTest files. --- .../EmergencyEvacuationApplicationEndToEndTest.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/test/java/org/ays/emergency_application/controller/EmergencyEvacuationApplicationEndToEndTest.java b/src/test/java/org/ays/emergency_application/controller/EmergencyEvacuationApplicationEndToEndTest.java index f616f548c..5ad5cd524 100644 --- a/src/test/java/org/ays/emergency_application/controller/EmergencyEvacuationApplicationEndToEndTest.java +++ b/src/test/java/org/ays/emergency_application/controller/EmergencyEvacuationApplicationEndToEndTest.java @@ -477,10 +477,8 @@ void givenValidIdAndValidUpdateRequest_whenApplicationUpdated_thenReturnSuccessR Assertions.assertTrue(applicationFromDatabase.isPresent()); Assertions.assertNotNull(applicationFromDatabase.get().getInstitution()); - Assertions.assertEquals(applicationFromDatabase.get().getSeatingCount(), - updateRequest.getSeatingCount()); - Assertions.assertEquals(applicationFromDatabase.get().getHasObstaclePersonExist(), - updateRequest.getHasObstaclePersonExist()); + Assertions.assertEquals(applicationFromDatabase.get().getSeatingCount(), updateRequest.getSeatingCount()); + Assertions.assertEquals(applicationFromDatabase.get().getHasObstaclePersonExist(), updateRequest.getHasObstaclePersonExist()); Assertions.assertEquals(applicationFromDatabase.get().getStatus(), updateRequest.getStatus()); Assertions.assertEquals(applicationFromDatabase.get().getNotes(), updateRequest.getNotes()); Assertions.assertNotNull(applicationFromDatabase.get().getUpdatedUser());