|
| 1 | +package io.jooby.avaje.validator; |
| 2 | + |
| 3 | +import io.jooby.avaje.validator.app.App; |
| 4 | +import io.jooby.avaje.validator.app.NewAccountRequest; |
| 5 | +import io.jooby.avaje.validator.app.Person; |
| 6 | +import io.jooby.test.JoobyTest; |
| 7 | +import io.jooby.validation.ValidationResult; |
| 8 | +import io.restassured.RestAssured; |
| 9 | +import io.restassured.builder.RequestSpecBuilder; |
| 10 | +import io.restassured.http.ContentType; |
| 11 | +import io.restassured.specification.RequestSpecification; |
| 12 | +import org.assertj.core.api.Assertions; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | + |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +import static io.jooby.StatusCode.UNPROCESSABLE_ENTITY_CODE; |
| 20 | +import static io.jooby.avaje.validator.app.App.DEFAULT_TITLE; |
| 21 | +import static io.restassured.RestAssured.given; |
| 22 | + |
| 23 | +@JoobyTest(value = App.class, port = 8099) |
| 24 | +public class AvajeValidatorModuleTest { |
| 25 | + |
| 26 | + protected static RequestSpecification SPEC = |
| 27 | + new RequestSpecBuilder() |
| 28 | + .setPort(8099) |
| 29 | + .setContentType(ContentType.JSON) |
| 30 | + .setAccept(ContentType.JSON) |
| 31 | + .build(); |
| 32 | + |
| 33 | + static { |
| 34 | + RestAssured.enableLoggingOfRequestAndResponseIfValidationFails(); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void validate_personBean_shouldDetect2Violations() { |
| 39 | + Person person = new Person(null, "Last Name"); |
| 40 | + |
| 41 | + ValidationResult actualResult = |
| 42 | + given() |
| 43 | + .spec(SPEC) |
| 44 | + .with() |
| 45 | + .body(person) |
| 46 | + .post("/create-person") |
| 47 | + .then() |
| 48 | + .assertThat() |
| 49 | + .statusCode(UNPROCESSABLE_ENTITY_CODE) |
| 50 | + .extract() |
| 51 | + .as(ValidationResult.class); |
| 52 | + |
| 53 | + var fieldError = |
| 54 | + new ValidationResult.Error( |
| 55 | + "firstName", List.of("must not be empty"), ValidationResult.ErrorType.FIELD); |
| 56 | + ValidationResult expectedResult = buildResult(List.of(fieldError)); |
| 57 | + |
| 58 | + Assertions.assertThat(expectedResult) |
| 59 | + .usingRecursiveComparison() |
| 60 | + .ignoringCollectionOrderInFieldsMatchingRegexes("errors\\.messages") |
| 61 | + .isEqualTo(actualResult); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void validate_arrayOfPerson_shouldDetect2Violations() { |
| 66 | + Person person1 = new Person("First Name", "Last Name"); |
| 67 | + Person person2 = new Person(null, "Last Name 2"); |
| 68 | + |
| 69 | + ValidationResult actualResult = |
| 70 | + given() |
| 71 | + .spec(SPEC) |
| 72 | + .with() |
| 73 | + .body(new Person[] {person1, person2}) |
| 74 | + .post("/create-array-of-persons") |
| 75 | + .then() |
| 76 | + .assertThat() |
| 77 | + .statusCode(UNPROCESSABLE_ENTITY_CODE) |
| 78 | + .extract() |
| 79 | + .as(ValidationResult.class); |
| 80 | + |
| 81 | + var fieldError = |
| 82 | + new ValidationResult.Error( |
| 83 | + "firstName", List.of("must not be empty"), ValidationResult.ErrorType.FIELD); |
| 84 | + ValidationResult expectedResult = buildResult(List.of(fieldError)); |
| 85 | + |
| 86 | + Assertions.assertThat(expectedResult) |
| 87 | + .usingRecursiveComparison() |
| 88 | + .ignoringCollectionOrderInFieldsMatchingRegexes("errors\\.messages") |
| 89 | + .isEqualTo(actualResult); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void validate_listOfPerson_shouldDetect2Violations() { |
| 94 | + Person person1 = new Person("First Name", "Last Name"); |
| 95 | + Person person2 = new Person(null, "Last Name 2"); |
| 96 | + |
| 97 | + ValidationResult actualResult = |
| 98 | + given() |
| 99 | + .spec(SPEC) |
| 100 | + .with() |
| 101 | + .body(List.of(person1, person2)) |
| 102 | + .post("/create-list-of-persons") |
| 103 | + .then() |
| 104 | + .assertThat() |
| 105 | + .statusCode(UNPROCESSABLE_ENTITY_CODE) |
| 106 | + .extract() |
| 107 | + .as(ValidationResult.class); |
| 108 | + |
| 109 | + var fieldError = |
| 110 | + new ValidationResult.Error( |
| 111 | + "firstName", List.of("must not be empty"), ValidationResult.ErrorType.FIELD); |
| 112 | + ValidationResult expectedResult = buildResult(List.of(fieldError)); |
| 113 | + |
| 114 | + Assertions.assertThat(expectedResult) |
| 115 | + .usingRecursiveComparison() |
| 116 | + .ignoringCollectionOrderInFieldsMatchingRegexes("errors\\.messages") |
| 117 | + .isEqualTo(actualResult); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void validate_mapOfPerson_shouldDetect2Violations() { |
| 122 | + Person person1 = new Person("First Name", "Last Name"); |
| 123 | + Person person2 = new Person(null, "Last Name 2"); |
| 124 | + |
| 125 | + ValidationResult actualResult = |
| 126 | + given() |
| 127 | + .spec(SPEC) |
| 128 | + .with() |
| 129 | + .body(Map.of("1", person1, "2", person2)) |
| 130 | + .post("/create-map-of-persons") |
| 131 | + .then() |
| 132 | + .assertThat() |
| 133 | + .statusCode(UNPROCESSABLE_ENTITY_CODE) |
| 134 | + .extract() |
| 135 | + .as(ValidationResult.class); |
| 136 | + |
| 137 | + var fieldError = |
| 138 | + new ValidationResult.Error( |
| 139 | + "firstName", List.of("must not be empty"), ValidationResult.ErrorType.FIELD); |
| 140 | + ValidationResult expectedResult = buildResult(List.of(fieldError)); |
| 141 | + |
| 142 | + Assertions.assertThat(expectedResult) |
| 143 | + .usingRecursiveComparison() |
| 144 | + .ignoringCollectionOrderInFieldsMatchingRegexes("errors\\.messages") |
| 145 | + .isEqualTo(actualResult); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + public void validate_newAccountBean_shouldDetect6Violations() { |
| 150 | + NewAccountRequest request = new NewAccountRequest(); |
| 151 | + request.setLogin("jk"); |
| 152 | + request.setPassword("123"); |
| 153 | + request.setConfirmPassword("1234"); |
| 154 | + request.setPerson(new Person(null, "Last Name")); |
| 155 | + |
| 156 | + ValidationResult actualResult = |
| 157 | + given() |
| 158 | + .spec(SPEC) |
| 159 | + .with() |
| 160 | + .body(request) |
| 161 | + .post("/create-new-account") |
| 162 | + .then() |
| 163 | + .assertThat() |
| 164 | + .statusCode(UNPROCESSABLE_ENTITY_CODE) |
| 165 | + .extract() |
| 166 | + .as(ValidationResult.class); |
| 167 | + |
| 168 | + List<ValidationResult.Error> errors = |
| 169 | + List.of( |
| 170 | + new ValidationResult.Error( |
| 171 | + "password", |
| 172 | + List.of("length must be between 8 and 24"), |
| 173 | + ValidationResult.ErrorType.FIELD), |
| 174 | + new ValidationResult.Error( |
| 175 | + "person.firstName", List.of("must not be empty"), ValidationResult.ErrorType.FIELD), |
| 176 | + new ValidationResult.Error( |
| 177 | + "confirmPassword", |
| 178 | + List.of("length must be between 8 and 24"), |
| 179 | + ValidationResult.ErrorType.FIELD), |
| 180 | + new ValidationResult.Error( |
| 181 | + "login", |
| 182 | + List.of("length must be between 3 and 16"), |
| 183 | + ValidationResult.ErrorType.FIELD)); |
| 184 | + |
| 185 | + ValidationResult expectedResult = buildResult(errors); |
| 186 | + |
| 187 | + Assertions.assertThat(expectedResult) |
| 188 | + .usingRecursiveComparison() |
| 189 | + .ignoringCollectionOrderInFieldsMatchingRegexes("errors") |
| 190 | + .ignoringCollectionOrderInFieldsMatchingRegexes("errors\\.messages") |
| 191 | + .isEqualTo(actualResult); |
| 192 | + } |
| 193 | + |
| 194 | + private ValidationResult buildResult(List<ValidationResult.Error> errors) { |
| 195 | + return new ValidationResult(DEFAULT_TITLE, UNPROCESSABLE_ENTITY_CODE, errors); |
| 196 | + } |
| 197 | +} |
0 commit comments