From 811df0013038bc22539646689afad7fb44e512c1 Mon Sep 17 00:00:00 2001 From: parthiv39731 <70740707+parthiv39731@users.noreply.github.com> Date: Tue, 8 Aug 2023 11:05:59 +0530 Subject: [PATCH] BAEL-6801 (#14545) Validate string length with Java validation annotations --- .../SpringJobApplicationDemoApp.java | 12 + .../controller/ApplyJobController.java | 49 +++ .../listvalidation/domain/JobAspirant.java | 107 +++++++ .../listvalidation/groups/AllLevels.java | 4 + .../listvalidation/groups/Junior.java | 4 + .../listvalidation/groups/MidSenior.java | 4 + .../listvalidation/groups/Senior.java | 4 + .../ApplyJobControllerIntegrationTest.java | 289 ++++++++++++++++++ 8 files changed, 473 insertions(+) create mode 100644 spring-boot-modules/spring-boot-validation/src/main/java/com/baeldung/listvalidation/SpringJobApplicationDemoApp.java create mode 100644 spring-boot-modules/spring-boot-validation/src/main/java/com/baeldung/listvalidation/controller/ApplyJobController.java create mode 100644 spring-boot-modules/spring-boot-validation/src/main/java/com/baeldung/listvalidation/domain/JobAspirant.java create mode 100644 spring-boot-modules/spring-boot-validation/src/main/java/com/baeldung/listvalidation/groups/AllLevels.java create mode 100644 spring-boot-modules/spring-boot-validation/src/main/java/com/baeldung/listvalidation/groups/Junior.java create mode 100644 spring-boot-modules/spring-boot-validation/src/main/java/com/baeldung/listvalidation/groups/MidSenior.java create mode 100644 spring-boot-modules/spring-boot-validation/src/main/java/com/baeldung/listvalidation/groups/Senior.java create mode 100644 spring-boot-modules/spring-boot-validation/src/test/java/com/baeldung/listvalidation/application/ApplyJobControllerIntegrationTest.java diff --git a/spring-boot-modules/spring-boot-validation/src/main/java/com/baeldung/listvalidation/SpringJobApplicationDemoApp.java b/spring-boot-modules/spring-boot-validation/src/main/java/com/baeldung/listvalidation/SpringJobApplicationDemoApp.java new file mode 100644 index 0000000000..a7967c73cb --- /dev/null +++ b/spring-boot-modules/spring-boot-validation/src/main/java/com/baeldung/listvalidation/SpringJobApplicationDemoApp.java @@ -0,0 +1,12 @@ +package com.baeldung.listvalidation; + + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringJobApplicationDemoApp { + public static void main(String[] args) { + SpringApplication.run(SpringJobApplicationDemoApp.class, args); + } +} diff --git a/spring-boot-modules/spring-boot-validation/src/main/java/com/baeldung/listvalidation/controller/ApplyJobController.java b/spring-boot-modules/spring-boot-validation/src/main/java/com/baeldung/listvalidation/controller/ApplyJobController.java new file mode 100644 index 0000000000..e3877f09b0 --- /dev/null +++ b/spring-boot-modules/spring-boot-validation/src/main/java/com/baeldung/listvalidation/controller/ApplyJobController.java @@ -0,0 +1,49 @@ +package com.baeldung.listvalidation.controller; + +import com.baeldung.listvalidation.domain.JobAspirant; +import com.baeldung.listvalidation.groups.AllLevels; +import com.baeldung.listvalidation.groups.Junior; +import com.baeldung.listvalidation.groups.MidSenior; +import com.baeldung.listvalidation.groups.Senior; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.validation.FieldError; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.*; + +import java.util.HashMap; +import java.util.Map; + +@RestController +public class ApplyJobController { + @PostMapping("/applyLevelJunior") + public ResponseEntity applyLevelJunior(@Validated({Junior.class, AllLevels.class}) @RequestBody JobAspirant user) { + + return ResponseEntity.ok("Application submitted successfully"); + } + + @PostMapping("/applyLevelMidSenior") + public ResponseEntity applyLevelMidSenior(@Validated({MidSenior.class, AllLevels.class}) @RequestBody JobAspirant user) { + + return ResponseEntity.ok("Application submitted successfully"); + } + + @PostMapping("/applyLevelSenior") + public ResponseEntity applyLevelSenior(@Validated({Senior.class, AllLevels.class}) @RequestBody JobAspirant user) { + + return ResponseEntity.ok("Application submitted successfully"); + } + + @ResponseStatus(HttpStatus.BAD_REQUEST) + @ExceptionHandler(MethodArgumentNotValidException.class) + public Map handleValidationExceptions(MethodArgumentNotValidException ex) { + Map errors = new HashMap<>(); + ex.getBindingResult().getAllErrors().forEach((error) -> { + String fieldName = ((FieldError) error).getField(); + String errorMessage = error.getDefaultMessage(); + errors.put(fieldName, errorMessage); + }); + return errors; + } +} diff --git a/spring-boot-modules/spring-boot-validation/src/main/java/com/baeldung/listvalidation/domain/JobAspirant.java b/spring-boot-modules/spring-boot-validation/src/main/java/com/baeldung/listvalidation/domain/JobAspirant.java new file mode 100644 index 0000000000..4191625b4f --- /dev/null +++ b/spring-boot-modules/spring-boot-validation/src/main/java/com/baeldung/listvalidation/domain/JobAspirant.java @@ -0,0 +1,107 @@ +package com.baeldung.listvalidation.domain; + +import com.baeldung.listvalidation.groups.AllLevels; +import com.baeldung.listvalidation.groups.Junior; +import com.baeldung.listvalidation.groups.MidSenior; +import com.baeldung.listvalidation.groups.Senior; +import org.springframework.lang.Nullable; + +import javax.validation.constraints.*; +import java.util.Date; + +public class JobAspirant { + @Size.List({ + @Size(min = 5, message = "Name should have at least 5 characters", groups = AllLevels.class), + @Size(max = 20, message = "Name should have at most 20 characters", groups = AllLevels.class) + }) + + @Pattern.List({ + @Pattern(regexp = "^[\\p{Alpha} ]*$", message = "Name should contain only alphabets and space", groups = AllLevels.class), + @Pattern(regexp = "^[^\\s].*$", message = "Name should not start with space", groups = AllLevels.class), + @Pattern(regexp = "^.*[^\\s]$", message = "Name should not end with space", groups = AllLevels.class), + @Pattern(regexp = "^((?! ).)*$", message = "Name should not contain consecutive spaces", groups = AllLevels.class), + @Pattern(regexp = "^[^a-z].*$", message = "Name should not start with a lower case character", groups = AllLevels.class) + }) + + + private String name; + + public Integer getExperience() { + return experience; + } + + public void setExperience(Integer experience) { + this.experience = experience; + } + + @Min.List({ + @Min(value = 15, message = "Years of experience cannot be less than 15 Years", groups = Senior.class), + @Min(value = 10, message = "Years of experience cannot be less than 10 Years", groups = MidSenior.class), + @Min(value = 5, message = "Years of experience cannot be less than 5 Years", groups = Junior.class) + }) + @Max.List({ + @Max(value = 20, message = "Years of experience cannot be more than 20 Years", groups = Senior.class), + @Max(value = 15, message = "Years of experience cannot be more than 15 Years", groups = MidSenior.class), + @Max(value = 10, message = "Years of experience cannot be more than 10 Years", groups = Junior.class) + }) + private Integer experience; + + @AssertTrue.List({ + @AssertTrue(message = "Terms and Conditions consent missing for Senior Level Job Application", groups = Senior.class), + @AssertTrue(message = "Terms and Conditions consent missing for Mid-Senior Level Job Application", groups = MidSenior.class), + @AssertTrue(message = "Terms and Conditions consent missing for Junior Level Job Application", groups = Junior.class) + }) + @Nullable + private Boolean agreement; + + @Nullable + @Future.List({ + @Future(message = "Active passport is mandatory for Senior Level Job Application", groups = Senior.class), + @Future(message = "Active passport is mandatory for Mid-Senior Level Job Application", groups = MidSenior.class), + @Future(message = "Active passport is mandatory for Junior Level Job Application", groups = Junior.class) + }) + private Date passportExpiryDate; + + @Pattern.List({ + @Pattern(regexp = "^(Senior)$", message = "Job level should be Senior" + ,flags = Pattern.Flag.CASE_INSENSITIVE, groups = Senior.class), + @Pattern(regexp = "^(MidSenior)$", message = "Job level should be MidSenior" + ,flags = Pattern.Flag.CASE_INSENSITIVE, groups = MidSenior.class), + @Pattern(regexp = "^(Junior)$", message = "Job level should be Junior" + ,flags = Pattern.Flag.CASE_INSENSITIVE, groups = Junior.class) + }) +// @Pattern(regexp = "^(Senior|MidSenior|Junior)$", message = "Job level should be Senior, MidSenior or Junior" +// ,flags = Pattern.Flag.CASE_INSENSITIVE, groups = AllLevels.class) + private String jobLevel; + + public String getJobLevel() { + return jobLevel; + } + + public String getName() { + return name; + } + + public Boolean getAgreement() { + return agreement; + } + public Date getPassportExpiryDate() { + return passportExpiryDate; + } + + public void setAgreement(Boolean agreement) { + this.agreement = agreement; + } + + public void setName(String name) { + this.name = name; + } + + public void setJobLevel(String jobLevel) { + this.jobLevel = jobLevel; + } + + public void setPassportExpiryDate(Date passportExpiryDate) { + this.passportExpiryDate = passportExpiryDate; + } +} diff --git a/spring-boot-modules/spring-boot-validation/src/main/java/com/baeldung/listvalidation/groups/AllLevels.java b/spring-boot-modules/spring-boot-validation/src/main/java/com/baeldung/listvalidation/groups/AllLevels.java new file mode 100644 index 0000000000..8e3f8cc7ef --- /dev/null +++ b/spring-boot-modules/spring-boot-validation/src/main/java/com/baeldung/listvalidation/groups/AllLevels.java @@ -0,0 +1,4 @@ +package com.baeldung.listvalidation.groups; + +public interface AllLevels { +} diff --git a/spring-boot-modules/spring-boot-validation/src/main/java/com/baeldung/listvalidation/groups/Junior.java b/spring-boot-modules/spring-boot-validation/src/main/java/com/baeldung/listvalidation/groups/Junior.java new file mode 100644 index 0000000000..8aca819f18 --- /dev/null +++ b/spring-boot-modules/spring-boot-validation/src/main/java/com/baeldung/listvalidation/groups/Junior.java @@ -0,0 +1,4 @@ +package com.baeldung.listvalidation.groups; + +public interface Junior { +} diff --git a/spring-boot-modules/spring-boot-validation/src/main/java/com/baeldung/listvalidation/groups/MidSenior.java b/spring-boot-modules/spring-boot-validation/src/main/java/com/baeldung/listvalidation/groups/MidSenior.java new file mode 100644 index 0000000000..ddea95f5e2 --- /dev/null +++ b/spring-boot-modules/spring-boot-validation/src/main/java/com/baeldung/listvalidation/groups/MidSenior.java @@ -0,0 +1,4 @@ +package com.baeldung.listvalidation.groups; + +public interface MidSenior { +} diff --git a/spring-boot-modules/spring-boot-validation/src/main/java/com/baeldung/listvalidation/groups/Senior.java b/spring-boot-modules/spring-boot-validation/src/main/java/com/baeldung/listvalidation/groups/Senior.java new file mode 100644 index 0000000000..d09d3ea5d5 --- /dev/null +++ b/spring-boot-modules/spring-boot-validation/src/main/java/com/baeldung/listvalidation/groups/Senior.java @@ -0,0 +1,4 @@ +package com.baeldung.listvalidation.groups; + +public interface Senior { +} diff --git a/spring-boot-modules/spring-boot-validation/src/test/java/com/baeldung/listvalidation/application/ApplyJobControllerIntegrationTest.java b/spring-boot-modules/spring-boot-validation/src/test/java/com/baeldung/listvalidation/application/ApplyJobControllerIntegrationTest.java new file mode 100644 index 0000000000..978110633c --- /dev/null +++ b/spring-boot-modules/spring-boot-validation/src/test/java/com/baeldung/listvalidation/application/ApplyJobControllerIntegrationTest.java @@ -0,0 +1,289 @@ +package com.baeldung.listvalidation.application; + + +import org.json.JSONObject; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; + +import java.nio.charset.StandardCharsets; +import java.util.Map; + +@RunWith(SpringRunner.class) +@WebMvcTest +@AutoConfigureMockMvc +public class ApplyJobControllerIntegrationTest { + + @Autowired + private MockMvc mockMvc; + private static final String[] CORRECT_PASSPORT_EXPIRY_DATES = {"2025-01-01", "2026-02-29", "2024-03-31"}; + private static final String[] INCORRECT_PASSPORT_EXPIRY_DATES = {"2020-01-01", "2021-02-29", "2022-03-31"}; + + private static final Map MAP_OF_JOB_LEVEL_TO_PASSPORT_ERRMSG = Map.of( + "Junior", "Active passport is mandatory for Junior Level Job Application" + , "MidSenior", "Active passport is mandatory for Mid-Senior Level Job Application" + , "Senior", "Active passport is mandatory for Senior Level Job Application" + ); + private static final Map MAP_OF_JOB_LEVEL_TO_AGREEMENT_ERRMSG = Map.of( + "Junior", "Terms and Conditions consent missing for Junior Level Job Application" + , "MidSenior", "Terms and Conditions consent missing for Mid-Senior Level Job Application" + , "Senior", "Terms and Conditions consent missing for Senior Level Job Application" + ); + private static final String[] USERS_INCORRECT_NAMES = {"Bob", "bob Marley", "Bob John Federik Marley", "Bob M@rley" + , " Bob Marley", "Bob Marley ", "Bob Marley"}; + + private static final String[] USERS_CORRECT_NAMES = {"Bob Marley", "Bob John Marley", "Bobby"}; + + private static final Map MAP_OF_JOBLEVEL_TO_INCORRECT_MIN_EXPERIENCES = Map.of( + "Junior", new String[]{"1", "2", "3", "4"} + , "MidSenior", new String[]{"6", "7", "8", "9"} + , "Senior", new String[]{"11", "12", "13", "14"} + ); + private static final Map MAP_OF_JOBLEVEL_TO_MIN_ERRMSG = Map.of( + "Junior", "Years of experience cannot be less than 5 Years" + , "MidSenior", "Years of experience cannot be less than 10 Years" + , "Senior", "Years of experience cannot be less than 15 Years" + ); + private static final Map MAP_OF_JOBLEVEL_TO_MAX_ERRMSG = Map.of( + "Junior", "Years of experience cannot be more than 10 Years" + , "MidSenior", "Years of experience cannot be more than 15 Years" + , "Senior", "Years of experience cannot be more than 20 Years" + ); + private static final Map MAP_OF_JOBLEVEL_TO_INCORRECT_MAX_EXPERIENCES = Map.of( + "Junior", new String[]{"11", "14", "20", "30"} + , "MidSenior", new String[]{"16", "20", "25", "40"} + , "Senior", new String[]{"21", "25", "30", "35"} + ); + private static final Map MAP_OF_INCORRECT_NAMES_TO_ERRMSG = Map.of( + "Bob", "Name should have at least 5 characters" + , "bob Marley", "Name should not start with a lower case character" + , "Bob John Federik Marley", "Name should have at most 20 characters" + , "Bob M@rley", "Name should contain only alphabets and space" + , " Bob Marley", "Name should not start with space" + , "Bob Marley ", "Name should not end with space" + , "Bob Marley", "Name should not contain consecutive spaces" + ); + + private static final Map MAP_OF_JOBLEVEL_TO_CORRECT_EXPERIENCES = Map.of( + "Junior", new String[]{"6", "7", "8", "9", "10"} + , "MidSenior", new String[]{"11", "12", "13", "14", "15"} + , "Senior", new String[]{"16", "17", "18", "19", "20"} + ); + + + private static String[] USERS_VALID_NAMES_INVALID_MAXEXP_VALIDPSPT_VALIDAGMT_JSONS = null; + private static String[] USERS_VALID_NAMES_INVALID_MINEXP_VALIDPSPT_VALIDAGMT_JSONS = null; + private static String[] USERS_INVALID_NAMES_INVALID_MAXEXP_VALIDPSPT_VALIDAGMT_JSONS = null; + private static String[] USERS_INVALID_NAMES_INVALID_MINEXP_VALIDPSPT_VALIDAGMT_JSONS = null; + private static String[] USERS_ALL_FIELDS_VALID_EXCEPT_FOR_PSPT_AND_AGMT_JSONS = null; + private static String[] USERS_ALL_FIELDS_VALID_JSONS = null; + + @Before + public void prepareAllValidFieldsExceptForPsptAndAgmt() { + USERS_ALL_FIELDS_VALID_EXCEPT_FOR_PSPT_AND_AGMT_JSONS = new String[USERS_CORRECT_NAMES.length + * MAP_OF_JOBLEVEL_TO_CORRECT_EXPERIENCES.size() * 5 * INCORRECT_PASSPORT_EXPIRY_DATES.length]; + int i = 0; + for (String user : USERS_CORRECT_NAMES) { + for (String jobLevel : MAP_OF_JOBLEVEL_TO_CORRECT_EXPERIENCES.keySet()) { + for (String experience : MAP_OF_JOBLEVEL_TO_CORRECT_EXPERIENCES.get(jobLevel)) { + for (String passportExpiryDate : INCORRECT_PASSPORT_EXPIRY_DATES) { + USERS_ALL_FIELDS_VALID_EXCEPT_FOR_PSPT_AND_AGMT_JSONS[i++] = "{\"name\": \"" + user + + "\", \"experience\": \"" + experience + "\", \"jobLevel\": \"" + jobLevel + + "\", \"passportExpiryDate\": \"" + passportExpiryDate + + "\", \"agreement\": \"" + false + "\"}"; + } + } + } + } + + } + @Before + public void prepareUserWithAllFieldsValid() { + USERS_ALL_FIELDS_VALID_JSONS = new String[USERS_CORRECT_NAMES.length + * MAP_OF_JOBLEVEL_TO_CORRECT_EXPERIENCES.size() * 5 * CORRECT_PASSPORT_EXPIRY_DATES.length]; + int i = 0; + for (String user : USERS_CORRECT_NAMES) { + for (String jobLevel : MAP_OF_JOBLEVEL_TO_CORRECT_EXPERIENCES.keySet()) { + for (String experience : MAP_OF_JOBLEVEL_TO_CORRECT_EXPERIENCES.get(jobLevel)) { + for (String passportExpiryDate : CORRECT_PASSPORT_EXPIRY_DATES) { + USERS_ALL_FIELDS_VALID_JSONS[i++] = "{\"name\": \"" + user + + "\", \"experience\": \"" + experience + "\", \"jobLevel\": \"" + jobLevel + + "\", \"passportExpiryDate\": \"" + passportExpiryDate + + "\", \"agreement\": \"" + true + "\"}"; + } + } + } + } + } + @Before + public void prepareUsersInvalidNamesInValidMaxExpValidPassportValidAgreementJsons() { + USERS_INVALID_NAMES_INVALID_MAXEXP_VALIDPSPT_VALIDAGMT_JSONS = new String[USERS_INCORRECT_NAMES.length + * MAP_OF_JOBLEVEL_TO_INCORRECT_MAX_EXPERIENCES.size() * 4 * CORRECT_PASSPORT_EXPIRY_DATES.length]; + int i = 0; + for (String user : USERS_INCORRECT_NAMES) { + for (String jobLevel : MAP_OF_JOBLEVEL_TO_INCORRECT_MAX_EXPERIENCES.keySet()) { + for (String experience : MAP_OF_JOBLEVEL_TO_INCORRECT_MAX_EXPERIENCES.get(jobLevel)) { + for (String passportExpiryDate : CORRECT_PASSPORT_EXPIRY_DATES) { + USERS_INVALID_NAMES_INVALID_MAXEXP_VALIDPSPT_VALIDAGMT_JSONS[i++] = "{\"name\": \"" + user + + "\", \"experience\": \"" + experience + "\", \"jobLevel\": \"" + jobLevel + + "\", \"passportExpiryDate\": \"" + passportExpiryDate + + "\", \"agreement\": \"" + true + "\"}"; + } + } + } + } + } + @Before + public void prepareUsersCorrectNamesIncorrectMinExpJsons() { + USERS_VALID_NAMES_INVALID_MINEXP_VALIDPSPT_VALIDAGMT_JSONS = new String[USERS_CORRECT_NAMES.length + * MAP_OF_JOBLEVEL_TO_INCORRECT_MIN_EXPERIENCES.size() * 4 * CORRECT_PASSPORT_EXPIRY_DATES.length]; + int i = 0; + for (String user : USERS_CORRECT_NAMES) { + for (String jobLevel : MAP_OF_JOBLEVEL_TO_INCORRECT_MIN_EXPERIENCES.keySet()) { + for (String experience : MAP_OF_JOBLEVEL_TO_INCORRECT_MIN_EXPERIENCES.get(jobLevel)) { + for (String passportExpiryDate : CORRECT_PASSPORT_EXPIRY_DATES) { + USERS_VALID_NAMES_INVALID_MINEXP_VALIDPSPT_VALIDAGMT_JSONS[i++] = "{\"name\": \"" + user + + "\", \"experience\": \"" + experience + "\", \"jobLevel\": \"" + jobLevel + + "\", \"passportExpiryDate\": \"" + passportExpiryDate + + "\", \"agreement\": \"" + true + "\"}"; + } + } + } + } + } + @Before + public void prepareUsersIncorrectNamesIncorrectMinExpJsons() { + USERS_INVALID_NAMES_INVALID_MINEXP_VALIDPSPT_VALIDAGMT_JSONS = new String[USERS_INCORRECT_NAMES.length + * MAP_OF_JOBLEVEL_TO_INCORRECT_MIN_EXPERIENCES.size() * 4 * CORRECT_PASSPORT_EXPIRY_DATES.length]; + int i = 0; + for (String user : USERS_INCORRECT_NAMES) { + for (String jobLevel : MAP_OF_JOBLEVEL_TO_INCORRECT_MIN_EXPERIENCES.keySet()) { + for (String experience : MAP_OF_JOBLEVEL_TO_INCORRECT_MIN_EXPERIENCES.get(jobLevel)) { + for (String passportExpiryDate : CORRECT_PASSPORT_EXPIRY_DATES) { + USERS_INVALID_NAMES_INVALID_MINEXP_VALIDPSPT_VALIDAGMT_JSONS[i++] = "{\"name\": \"" + user + + "\", \"experience\": \"" + experience + "\", \"jobLevel\": \"" + jobLevel + + "\", \"passportExpiryDate\": \"" + passportExpiryDate + + "\", \"agreement\": \"" + true + "\"}"; + } + } + } + } + } + @Before + public void prepareUsersCorrectNamesIncorrectMaxExpJsons() { + USERS_VALID_NAMES_INVALID_MAXEXP_VALIDPSPT_VALIDAGMT_JSONS = new String[USERS_CORRECT_NAMES.length + * MAP_OF_JOBLEVEL_TO_INCORRECT_MAX_EXPERIENCES.size() * 4 * CORRECT_PASSPORT_EXPIRY_DATES.length]; + int i = 0; + for (String user : USERS_CORRECT_NAMES) { + for (String jobLevel : MAP_OF_JOBLEVEL_TO_INCORRECT_MAX_EXPERIENCES.keySet()) { + for (String experience : MAP_OF_JOBLEVEL_TO_INCORRECT_MAX_EXPERIENCES.get(jobLevel)) { + for (String passportExpiryDate : CORRECT_PASSPORT_EXPIRY_DATES) { + USERS_VALID_NAMES_INVALID_MAXEXP_VALIDPSPT_VALIDAGMT_JSONS[i++] = "{\"name\": \"" + user + + "\", \"experience\": \"" + experience + "\", \"jobLevel\": \"" + jobLevel + + "\", \"passportExpiryDate\": \"" + passportExpiryDate + + "\", \"agreement\": \"" + true + "\"}"; + } + } + } + } + } + @Test + public void whenAllFieldsValidExceptForPsptAndAgmt_thenBadRequestResponse() throws Exception { + String[] users = USERS_ALL_FIELDS_VALID_EXCEPT_FOR_PSPT_AND_AGMT_JSONS; + for (String user : users) { + JSONObject jsonObject = new JSONObject(user); + String jobLevel = jsonObject.getString("jobLevel"); + mockMvc.perform(MockMvcRequestBuilders.post("/applyLevel" + jobLevel) + .content(user) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(MockMvcResultMatchers.status().isBadRequest()) + .andExpect(MockMvcResultMatchers.content() + .json("{\"agreement\":\"" + MAP_OF_JOB_LEVEL_TO_AGREEMENT_ERRMSG.get(jobLevel) + + "\",\"passportExpiryDate\":\"" + MAP_OF_JOB_LEVEL_TO_PASSPORT_ERRMSG.get(jobLevel) + "\"}")); + } + } + @Test + public void whenUserWithAllFieldsValid_thenCorrectResponse() throws Exception { + MediaType textPlainUtf8 = new MediaType(MediaType.TEXT_PLAIN, StandardCharsets.UTF_8); + String[] users = USERS_ALL_FIELDS_VALID_JSONS; + for (String user : users) { + JSONObject jsonObject = new JSONObject(user); + String jobLevel = jsonObject.getString("jobLevel"); + mockMvc.perform(MockMvcRequestBuilders.post("/applyLevel" + jobLevel) + .content(user) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.content().contentType(textPlainUtf8)) + .andExpect(MockMvcResultMatchers.content().string("Application submitted successfully")); + } + } + @Test + public void whenValidUserNamesInValidMaxExpValidPsptValidAgmt_thenBadRequestResponse() throws Exception { + String[] users = USERS_VALID_NAMES_INVALID_MAXEXP_VALIDPSPT_VALIDAGMT_JSONS; + for (String user : users) { + JSONObject jsonObject = new JSONObject(user); + String jobLevel = jsonObject.getString("jobLevel"); + mockMvc.perform(MockMvcRequestBuilders.post("/applyLevel" + jobLevel) + .content(user) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(MockMvcResultMatchers.status().isBadRequest()) + .andExpect(MockMvcResultMatchers.content() + .json("{\"experience\":\"" + MAP_OF_JOBLEVEL_TO_MAX_ERRMSG.get(jobLevel) + + "\"}")); + } + } + @Test + public void whenInvalidUserNamesInValidMaxExpValidPsptValidAgmt_thenBadRequestResponse() throws Exception { + String[] users = USERS_INVALID_NAMES_INVALID_MAXEXP_VALIDPSPT_VALIDAGMT_JSONS; + for (String user : users) { + JSONObject jsonObject = new JSONObject(user); + String jobLevel = jsonObject.getString("jobLevel"); + String name = jsonObject.getString("name"); + mockMvc.perform(MockMvcRequestBuilders.post("/applyLevel" + jobLevel) + .content(user) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(MockMvcResultMatchers.status().isBadRequest()) + .andExpect(MockMvcResultMatchers.content() + .json("{\"name\":\"" + MAP_OF_INCORRECT_NAMES_TO_ERRMSG.get(name) + + "\",\"experience\":\"" + MAP_OF_JOBLEVEL_TO_MAX_ERRMSG.get(jobLevel) + "\"}")); + } + } + @Test + public void whenValidUserNamesInValidMinExpValidPsptValidAgmt_thenBadRequestResponse() throws Exception { + String[] users = USERS_VALID_NAMES_INVALID_MINEXP_VALIDPSPT_VALIDAGMT_JSONS; + for (String user : users) { + JSONObject jsonObject = new JSONObject(user); + String jobLevel = jsonObject.getString("jobLevel"); + mockMvc.perform(MockMvcRequestBuilders.post("/applyLevel" + jobLevel) + .content(user) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(MockMvcResultMatchers.status().isBadRequest()) + .andExpect(MockMvcResultMatchers.content() + .json("{\"experience\":\"" + MAP_OF_JOBLEVEL_TO_MIN_ERRMSG.get(jobLevel) + "\"}")); + } + } + @Test + public void whenInValidUserNamesInValidMinExpValidPsptValidAgmt_thenBadRequestResponse() throws Exception { + String[] users = USERS_INVALID_NAMES_INVALID_MINEXP_VALIDPSPT_VALIDAGMT_JSONS; + for (String user : users) { + JSONObject jsonObject = new JSONObject(user); + String jobLevel = jsonObject.getString("jobLevel"); + String name = jsonObject.getString("name"); + mockMvc.perform(MockMvcRequestBuilders.post("/applyLevel" + jobLevel) + .content(user) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(MockMvcResultMatchers.status().isBadRequest()) + .andExpect(MockMvcResultMatchers.content() + .json("{\"name\":\"" + MAP_OF_INCORRECT_NAMES_TO_ERRMSG.get(name) + + "\",\"experience\":\"" + MAP_OF_JOBLEVEL_TO_MIN_ERRMSG.get(jobLevel) + "\"}")); + } + } +}