BAEL-6801, Applied review comments (#14567)
* BAEL-6801 Validate string length with Java validation annotations * Bael-6801, applied Review comments * Bael-6801, applied Review comments, deleting the springboot implementation
This commit is contained in:
parent
06f473f5e6
commit
674b3d3453
@ -0,0 +1,4 @@
|
||||
package com.baeldung.javaxval.listvalidation;
|
||||
|
||||
public interface AllLevels {
|
||||
}
|
@ -1,12 +1,7 @@
|
||||
package com.baeldung.listvalidation.domain;
|
||||
package com.baeldung.javaxval.listvalidation;
|
||||
|
||||
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 jakarta.validation.constraints.*;
|
||||
|
||||
import javax.validation.constraints.*;
|
||||
import java.util.Date;
|
||||
|
||||
public class JobAspirant {
|
||||
@ -14,7 +9,6 @@ public class JobAspirant {
|
||||
@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),
|
||||
@ -22,18 +16,8 @@ public class JobAspirant {
|
||||
@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),
|
||||
@ -47,18 +31,16 @@ public class JobAspirant {
|
||||
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)
|
||||
@AssertTrue(message = "Terms and Conditions consent missing for Senior Level Job", groups = Senior.class),
|
||||
@AssertTrue(message = "Terms and Conditions consent missing for Mid-Senior Level Job", groups = MidSenior.class),
|
||||
@AssertTrue(message = "Terms and Conditions consent missing for Junior Level Job", 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)
|
||||
@Future(message = "Active passport is mandatory for Senior Level Job", groups = Senior.class),
|
||||
@Future(message = "Active passport is mandatory for Mid-Senior Level Job", groups = MidSenior.class),
|
||||
@Future(message = "Active passport is mandatory for Junior Level Job", groups = Junior.class)
|
||||
})
|
||||
private Date passportExpiryDate;
|
||||
|
||||
@ -70,8 +52,6 @@ public class JobAspirant {
|
||||
@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() {
|
||||
@ -104,4 +84,12 @@ public class JobAspirant {
|
||||
public void setPassportExpiryDate(Date passportExpiryDate) {
|
||||
this.passportExpiryDate = passportExpiryDate;
|
||||
}
|
||||
|
||||
public Integer getExperience() {
|
||||
return experience;
|
||||
}
|
||||
|
||||
public void setExperience(Integer experience) {
|
||||
this.experience = experience;
|
||||
}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
package com.baeldung.javaxval.listvalidation;
|
||||
|
||||
public interface Junior {
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
package com.baeldung.javaxval.listvalidation;
|
||||
|
||||
public interface MidSenior {
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
package com.baeldung.javaxval.listvalidation;
|
||||
|
||||
public interface Senior {
|
||||
}
|
@ -0,0 +1,209 @@
|
||||
package com.baeldung.javaxval.listvalidation;
|
||||
|
||||
import jakarta.validation.ConstraintViolation;
|
||||
import jakarta.validation.Validation;
|
||||
import jakarta.validation.Validator;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class JobAspirantUnitTest {
|
||||
private static Validator validator;
|
||||
@BeforeClass
|
||||
public static void setupValidatorInstance() {
|
||||
validator = Validation.buildDefaultValidatorFactory().getValidator();
|
||||
}
|
||||
@Test
|
||||
public void givenJobLevelJunior_whenInValidMinExperience_thenExpectErrors() throws ParseException {
|
||||
JobAspirant jobAspirant = getJobAspirant("Junior", "John Adam", "2025-12-31", 3, true);
|
||||
Set<ConstraintViolation<JobAspirant>> violations = validator.validate(jobAspirant, Junior.class);
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
violations.forEach(action -> {
|
||||
assertThat(action.getPropertyPath().toString()).isEqualTo("experience");
|
||||
assertThat(action.getMessage()).isEqualTo("Years of experience cannot be less than 5 Years");
|
||||
});
|
||||
}
|
||||
@Test
|
||||
public void givenJobLevelMidSenior_whenInvalidMinExperience_thenExpectErrors() throws ParseException {
|
||||
JobAspirant jobAspirant = getJobAspirant("MidSenior", "John Adam", "2025-12-31", 8, true);
|
||||
Set<ConstraintViolation<JobAspirant>> violations = validator.validate(jobAspirant, MidSenior.class);
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
violations.forEach(action -> {
|
||||
assertThat(action.getPropertyPath().toString()).isEqualTo("experience");
|
||||
assertThat(action.getMessage()).isEqualTo("Years of experience cannot be less than 10 Years");
|
||||
});
|
||||
}
|
||||
@Test
|
||||
public void givenJobLevelSenior_whenInvalidMinExperience_thenExpectErrors() throws ParseException {
|
||||
JobAspirant jobAspirant = getJobAspirant("Senior", "John Adam", "2025-12-31", 13, true);
|
||||
Set<ConstraintViolation<JobAspirant>> violations = validator.validate(jobAspirant, Senior.class);
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
violations.forEach(action -> {
|
||||
assertThat(action.getPropertyPath().toString()).isEqualTo("experience");
|
||||
assertThat(action.getMessage()).isEqualTo("Years of experience cannot be less than 15 Years");
|
||||
});
|
||||
}
|
||||
@Test
|
||||
public void givenJobLevelJunior_whenInValidMaxExperience_thenExpectErrors() throws ParseException {
|
||||
JobAspirant jobAspirant = getJobAspirant("Junior", "John Adam", "2025-12-31", 11, true);
|
||||
Set<ConstraintViolation<JobAspirant>> violations = validator.validate(jobAspirant, Junior.class);
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
violations.forEach(action -> {
|
||||
assertThat(action.getPropertyPath().toString()).isEqualTo("experience");
|
||||
assertThat(action.getMessage()).isEqualTo("Years of experience cannot be more than 10 Years");
|
||||
});
|
||||
}
|
||||
@Test
|
||||
public void givenJobLevelMidSenior_whenInvalidMaxExperience_thenExpectErrors() throws ParseException {
|
||||
JobAspirant jobAspirant = getJobAspirant("MidSenior", "John Adam", "2025-12-31", 16, true);
|
||||
Set<ConstraintViolation<JobAspirant>> violations = validator.validate(jobAspirant, MidSenior.class);
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
violations.forEach(action -> {
|
||||
assertThat(action.getPropertyPath().toString()).isEqualTo("experience");
|
||||
assertThat(action.getMessage()).isEqualTo("Years of experience cannot be more than 15 Years");
|
||||
});
|
||||
}
|
||||
@Test
|
||||
public void givenJobLevelSenior_whenInvalidMaxExperience_thenExpectErrors() throws ParseException {
|
||||
JobAspirant jobAspirant = getJobAspirant("Senior", "John Adam", "2025-12-31", 23, true);
|
||||
Set<ConstraintViolation<JobAspirant>> violations = validator.validate(jobAspirant, Senior.class);
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
violations.forEach(action -> {
|
||||
assertThat(action.getPropertyPath().toString()).isEqualTo("experience");
|
||||
assertThat(action.getMessage()).isEqualTo("Years of experience cannot be more than 20 Years");
|
||||
});
|
||||
}
|
||||
@Test
|
||||
public void whenInvalidName_thenExpectErrors() throws ParseException {
|
||||
JobAspirant jobAspirant = getJobAspirant("Senior", "John Adam", "2025-12-31", 17, true);
|
||||
Set<ConstraintViolation<JobAspirant>> violations = validator.validate(jobAspirant, Senior.class, AllLevels.class);
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
violations.forEach(action -> {
|
||||
assertThat(action.getPropertyPath().toString()).isEqualTo("name");
|
||||
assertThat(action.getMessage()).isEqualTo("Name should not contain consecutive spaces");
|
||||
});
|
||||
}
|
||||
@Test
|
||||
public void givenJuniorLevel_whenInvalidAgreement_thenExpectErrors() throws ParseException {
|
||||
JobAspirant jobAspirant = getJobAspirant("Junior", "John Adam", "2025-12-31", 7, false);
|
||||
Set<ConstraintViolation<JobAspirant>> violations = validator.validate(jobAspirant, Junior.class);
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
violations.forEach(action -> {
|
||||
assertThat(action.getPropertyPath().toString()).isEqualTo("agreement");
|
||||
assertThat(action.getMessage()).isEqualTo("Terms and Conditions consent missing for Junior Level Job");
|
||||
});
|
||||
}
|
||||
@Test
|
||||
public void givenSeniorLevel_whenInvalidAgreement_thenExpectErrors() throws ParseException {
|
||||
JobAspirant jobAspirant = getJobAspirant("Senior", "John Adam", "2025-12-31", 17, false);
|
||||
Set<ConstraintViolation<JobAspirant>> violations = validator.validate(jobAspirant, Senior.class);
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
violations.forEach(action -> {
|
||||
assertThat(action.getPropertyPath().toString()).isEqualTo("agreement");
|
||||
assertThat(action.getMessage()).isEqualTo("Terms and Conditions consent missing for Senior Level Job");
|
||||
});
|
||||
}
|
||||
@Test
|
||||
public void givenJobLevelMidSenior_whenInvalidPassport_thenExpectErrors() throws ParseException {
|
||||
JobAspirant jobAspirant = getJobAspirant("MidSenior", "John Adam", "2021-12-31", 12, true);
|
||||
Set<ConstraintViolation<JobAspirant>> violations = validator.validate(jobAspirant, MidSenior.class);
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
violations.forEach(action -> {
|
||||
assertThat(action.getPropertyPath().toString()).isEqualTo("passportExpiryDate");
|
||||
assertThat(action.getMessage()).isEqualTo("Active passport is mandatory for Mid-Senior Level Job");
|
||||
});
|
||||
}
|
||||
@Test
|
||||
public void givenJobLevelSenior_whenInvalidPassport_thenExpectErrors() throws ParseException {
|
||||
JobAspirant jobAspirant = getJobAspirant("Senior", "John Adam", "2021-12-31", 18, true);
|
||||
Set<ConstraintViolation<JobAspirant>> violations = validator.validate(jobAspirant, Senior.class);
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
violations.forEach(action -> {
|
||||
assertThat(action.getPropertyPath().toString()).isEqualTo("passportExpiryDate");
|
||||
assertThat(action.getMessage()).isEqualTo("Active passport is mandatory for Senior Level Job");
|
||||
});
|
||||
}
|
||||
@Test
|
||||
public void givenJobLevelSenior_whenAllFieldsValid_thenNoErrors() throws ParseException {
|
||||
JobAspirant jobAspirant = getJobAspirant("Senior", "John Adam", "2025-12-31", 17, true);
|
||||
Set<ConstraintViolation<JobAspirant>> violations = validator.validate(jobAspirant, Senior.class, AllLevels.class);
|
||||
assertThat(violations.size()).isEqualTo(0);
|
||||
}
|
||||
@Test
|
||||
public void givenJobLevelMidSenior_whenAllFieldsValid_thenNoErrors() throws ParseException {
|
||||
JobAspirant jobAspirant = getJobAspirant("MidSenior", "John Adam", "2025-12-31", 12, true);
|
||||
Set<ConstraintViolation<JobAspirant>> violations = validator.validate(jobAspirant, MidSenior.class, AllLevels.class);
|
||||
assertThat(violations.size()).isEqualTo(0);
|
||||
}
|
||||
@Test
|
||||
public void givenJobLevelJunior_whenAllFieldsValid_thenNoErrors() throws ParseException {
|
||||
JobAspirant jobAspirant = getJobAspirant("Junior", "John Adam", "2025-12-31", 7, true);
|
||||
Set<ConstraintViolation<JobAspirant>> violations = validator.validate(jobAspirant, Junior.class, AllLevels.class);
|
||||
assertThat(violations.size()).isEqualTo(0);
|
||||
}
|
||||
@Test
|
||||
public void givenJobLevelJunior_whenAllFieldsInValid_thenExpectErrors() throws ParseException {
|
||||
JobAspirant jobAspirant = getJobAspirant("Junior", " John Adam", "2022-12-31", 3, false);
|
||||
Set<ConstraintViolation<JobAspirant>> violations = validator.validate(jobAspirant, Junior.class, AllLevels.class);
|
||||
assertThat(violations.size()).isEqualTo(4);
|
||||
violations.forEach(action -> {
|
||||
String fieldName = action.getPropertyPath().toString();
|
||||
switch(fieldName) {
|
||||
case "name":
|
||||
assertThat(action.getMessage()).isEqualTo("Name should not start with space");
|
||||
break;
|
||||
case "passportExpiryDate":
|
||||
assertThat(action.getMessage()).isEqualTo("Active passport is mandatory for Junior Level Job");
|
||||
break;
|
||||
case "experience":
|
||||
assertThat(action.getMessage()).isEqualTo("Years of experience cannot be less than 5 Years");
|
||||
break;
|
||||
case "agreement":
|
||||
assertThat(action.getMessage()).isEqualTo("Terms and Conditions consent missing for Junior Level Job");
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
@Test
|
||||
public void givenJobLevelSenior_whenAllFieldsInValid_thenExpectErrors() throws ParseException {
|
||||
JobAspirant jobAspirant = getJobAspirant("Senior", "John Adam ", "2022-12-31", 12, false);
|
||||
Set<ConstraintViolation<JobAspirant>> violations = validator.validate(jobAspirant, Senior.class, AllLevels.class);
|
||||
assertThat(violations.size()).isEqualTo(4);
|
||||
violations.forEach(action -> {
|
||||
String fieldName = action.getPropertyPath().toString();
|
||||
switch(fieldName) {
|
||||
case "name":
|
||||
assertThat(action.getMessage()).isEqualTo("Name should not end with space");
|
||||
break;
|
||||
case "passportExpiryDate":
|
||||
assertThat(action.getMessage()).isEqualTo("Active passport is mandatory for Senior Level Job");
|
||||
break;
|
||||
case "experience":
|
||||
assertThat(action.getMessage()).isEqualTo("Years of experience cannot be less than 15 Years");
|
||||
break;
|
||||
case "agreement":
|
||||
assertThat(action.getMessage()).isEqualTo("Terms and Conditions consent missing for Senior Level Job");
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
private JobAspirant getJobAspirant(String jobLevel, String name, String passportExpDate, int exp, boolean agmt) throws ParseException {
|
||||
JobAspirant jobAspirant = new JobAspirant();
|
||||
jobAspirant.setName(name);
|
||||
jobAspirant.setPassportExpiryDate(convertStringToDate(passportExpDate));
|
||||
jobAspirant.setJobLevel(jobLevel);
|
||||
jobAspirant.setExperience(exp);
|
||||
jobAspirant.setAgreement(agmt);
|
||||
return jobAspirant;
|
||||
}
|
||||
private Date convertStringToDate(String date) throws ParseException {
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
||||
return formatter.parse(date);
|
||||
}
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
@ -1,49 +0,0 @@
|
||||
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<String> applyLevelJunior(@Validated({Junior.class, AllLevels.class}) @RequestBody JobAspirant user) {
|
||||
|
||||
return ResponseEntity.ok("Application submitted successfully");
|
||||
}
|
||||
|
||||
@PostMapping("/applyLevelMidSenior")
|
||||
public ResponseEntity<String> applyLevelMidSenior(@Validated({MidSenior.class, AllLevels.class}) @RequestBody JobAspirant user) {
|
||||
|
||||
return ResponseEntity.ok("Application submitted successfully");
|
||||
}
|
||||
|
||||
@PostMapping("/applyLevelSenior")
|
||||
public ResponseEntity<String> applyLevelSenior(@Validated({Senior.class, AllLevels.class}) @RequestBody JobAspirant user) {
|
||||
|
||||
return ResponseEntity.ok("Application submitted successfully");
|
||||
}
|
||||
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public Map<String, String> handleValidationExceptions(MethodArgumentNotValidException ex) {
|
||||
Map<String, String> errors = new HashMap<>();
|
||||
ex.getBindingResult().getAllErrors().forEach((error) -> {
|
||||
String fieldName = ((FieldError) error).getField();
|
||||
String errorMessage = error.getDefaultMessage();
|
||||
errors.put(fieldName, errorMessage);
|
||||
});
|
||||
return errors;
|
||||
}
|
||||
}
|
@ -1,4 +0,0 @@
|
||||
package com.baeldung.listvalidation.groups;
|
||||
|
||||
public interface AllLevels {
|
||||
}
|
@ -1,4 +0,0 @@
|
||||
package com.baeldung.listvalidation.groups;
|
||||
|
||||
public interface Junior {
|
||||
}
|
@ -1,4 +0,0 @@
|
||||
package com.baeldung.listvalidation.groups;
|
||||
|
||||
public interface MidSenior {
|
||||
}
|
@ -1,4 +0,0 @@
|
||||
package com.baeldung.listvalidation.groups;
|
||||
|
||||
public interface Senior {
|
||||
}
|
@ -1,289 +0,0 @@
|
||||
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<String,String> 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<String, String> 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<String, String[]> 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<String, String> 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<String, String> 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<String, String[]> 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<String, String> 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<String, String[]> 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) + "\"}"));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user