|
1 | 1 | package com.backbase.oss.codegen.java; |
2 | 2 |
|
| 3 | +import static com.backbase.oss.codegen.java.BoatSpringCodeGen.USE_PROTECTED_FIELDS; |
| 4 | +import static java.util.stream.Collectors.groupingBy; |
| 5 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 6 | +import static org.hamcrest.Matchers.equalTo; |
| 7 | +import static org.hamcrest.Matchers.hasEntry; |
| 8 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 9 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 10 | +import static org.mockito.Mockito.mock; |
| 11 | +import static org.mockito.Mockito.when; |
| 12 | + |
3 | 13 | import com.backbase.oss.codegen.java.BoatSpringCodeGen.NewLineIndent; |
| 14 | +import com.backbase.oss.codegen.java.VerificationRunner.Verification; |
4 | 15 | import com.github.javaparser.StaticJavaParser; |
5 | 16 | import com.github.javaparser.ast.body.MethodDeclaration; |
6 | 17 | import com.github.javaparser.ast.body.Parameter; |
7 | 18 | import com.samskivert.mustache.Template.Fragment; |
8 | 19 | import io.swagger.parser.OpenAPIParser; |
9 | 20 | import io.swagger.v3.oas.models.Operation; |
10 | 21 | import io.swagger.v3.parser.core.models.ParseOptions; |
11 | | -import org.apache.commons.io.FileUtils; |
12 | | -import org.junit.jupiter.api.BeforeAll; |
13 | | -import org.junit.jupiter.api.Test; |
14 | | -import org.openapitools.codegen.*; |
15 | | - |
| 22 | +import jakarta.validation.ConstraintViolation; |
| 23 | +import jakarta.validation.Validation; |
| 24 | +import jakarta.validation.Validator; |
16 | 25 | import java.io.File; |
17 | 26 | import java.io.IOException; |
18 | 27 | import java.io.StringWriter; |
19 | 28 | import java.nio.file.Files; |
20 | 29 | import java.nio.file.Paths; |
21 | 30 | import java.util.Arrays; |
| 31 | +import java.util.Collection; |
22 | 32 | import java.util.List; |
23 | 33 | import java.util.Map; |
24 | | - |
25 | | -import static com.backbase.oss.codegen.java.BoatSpringCodeGen.USE_PROTECTED_FIELDS; |
26 | | -import static java.util.stream.Collectors.groupingBy; |
27 | | -import static org.hamcrest.MatcherAssert.assertThat; |
28 | | -import static org.hamcrest.Matchers.equalTo; |
29 | | -import static org.hamcrest.Matchers.hasEntry; |
30 | | -import static org.junit.jupiter.api.Assertions.assertEquals; |
31 | | -import static org.junit.jupiter.api.Assertions.assertTrue; |
32 | | -import static org.mockito.Mockito.mock; |
33 | | -import static org.mockito.Mockito.when; |
| 34 | +import java.util.Set; |
| 35 | +import org.apache.commons.io.FileUtils; |
| 36 | +import org.apache.commons.lang.UnhandledException; |
| 37 | +import org.hamcrest.Matchers; |
| 38 | +import org.junit.jupiter.api.BeforeAll; |
| 39 | +import org.junit.jupiter.api.Test; |
| 40 | +import org.openapitools.codegen.CliOption; |
| 41 | +import org.openapitools.codegen.ClientOptInput; |
| 42 | +import org.openapitools.codegen.CodegenOperation; |
| 43 | +import org.openapitools.codegen.CodegenProperty; |
| 44 | +import org.openapitools.codegen.DefaultGenerator; |
| 45 | +import org.openapitools.codegen.languages.SpringCodegen; |
34 | 46 |
|
35 | 47 | class BoatSpringCodeGenTests { |
36 | 48 |
|
@@ -133,4 +145,115 @@ void testReplaceBeanValidationCollectionType() { |
133 | 145 | codegenProperty,"Set<@Valid com.backbase.dbs.arrangement.commons.model.TranslationItemDto>"); |
134 | 146 | assertEquals("Set<com.backbase.dbs.arrangement.commons.model.@Valid TranslationItemDto>", result); |
135 | 147 | } |
| 148 | + @Test |
| 149 | + @SuppressWarnings("unchecked") |
| 150 | + void shouldGenerateValidations() throws InterruptedException { |
| 151 | + |
| 152 | + var modelPackage = "com.backbase.model"; |
| 153 | + var input = new File("src/test/resources/boat-spring/openapi.yaml"); |
| 154 | + var output = TEST_OUTPUT + "/shouldGenerateValidations"; |
| 155 | + |
| 156 | + // generate project |
| 157 | + var codegen = new BoatSpringCodeGen(); |
| 158 | + codegen.setLibrary("spring-boot"); |
| 159 | + codegen.setInterfaceOnly(true); |
| 160 | + codegen.setOutputDir(output); |
| 161 | + codegen.setInputSpec(input.getAbsolutePath()); |
| 162 | + codegen.additionalProperties().put(SpringCodegen.USE_SPRING_BOOT3, Boolean.TRUE.toString()); |
| 163 | + codegen.additionalProperties().put(BoatSpringCodeGen.USE_CLASS_LEVEL_BEAN_VALIDATION, Boolean.TRUE.toString()); |
| 164 | + codegen.setModelPackage(modelPackage); |
| 165 | + |
| 166 | + var openApiInput = new OpenAPIParser() |
| 167 | + .readLocation(input.getAbsolutePath(), null, new ParseOptions()) |
| 168 | + .getOpenAPI(); |
| 169 | + var clientOptInput = new ClientOptInput(); |
| 170 | + clientOptInput.config(codegen); |
| 171 | + clientOptInput.openAPI(openApiInput); |
| 172 | + |
| 173 | + List<File> files = new DefaultGenerator().opts(clientOptInput).generate(); |
| 174 | + |
| 175 | + // compile generated project |
| 176 | + var compiler = new MavenProjectCompiler(BoatSpringCodeGenTests.class.getClassLoader()); |
| 177 | + var projectDir = new File(output); |
| 178 | + int compilationStatus = compiler.compile(projectDir); |
| 179 | + assertEquals(0, compilationStatus); |
| 180 | + |
| 181 | + // verify |
| 182 | + ClassLoader projectClassLoader = compiler.getProjectClassLoader(projectDir); |
| 183 | + var verificationRunner = new VerificationRunner(projectClassLoader); |
| 184 | + Validator validator = Validation.buildDefaultValidatorFactory().getValidator(); |
| 185 | + |
| 186 | + Runnable verifyDefaultValues = () -> { |
| 187 | + try { |
| 188 | + var className = modelPackage + ".MultiLinePaymentRequest"; |
| 189 | + Class<?> requestClass = projectClassLoader.loadClass(className); |
| 190 | + Object requestObject = requestClass.getConstructor().newInstance(); |
| 191 | + Set<ConstraintViolation<Object>> violations = validator.validate(requestObject); |
| 192 | + assertThat(violations, Matchers.hasSize(1)); |
| 193 | + assertThat(violations.stream().findFirst().get().getPropertyPath().toString(), Matchers.equalTo("name")); |
| 194 | + assertThat( |
| 195 | + violations.stream().findFirst().get().getMessageTemplate(), |
| 196 | + Matchers.equalTo("{jakarta.validation.constraints.NotNull.message}") |
| 197 | + ); |
| 198 | + } catch (Exception e) { |
| 199 | + throw new UnhandledException(e); |
| 200 | + } |
| 201 | + }; |
| 202 | + verificationRunner.runVerification( |
| 203 | + Verification.builder().runnable(verifyDefaultValues).displayName("validations").build() |
| 204 | + ); |
| 205 | + |
| 206 | + Runnable verifyCollectionItems = () -> { |
| 207 | + try { |
| 208 | + var className = modelPackage + ".MultiLinePaymentRequest"; |
| 209 | + Class<?> requestClass = projectClassLoader.loadClass(className); |
| 210 | + Object requestObject = requestClass.getConstructor().newInstance(); |
| 211 | + |
| 212 | + // set name on MultiLinePaymentRequest |
| 213 | + requestObject.getClass() |
| 214 | + .getDeclaredMethod("setName", String.class) |
| 215 | + .invoke(requestObject, "someName"); |
| 216 | + |
| 217 | + // set arrangement ids |
| 218 | + Collection<String> arrangementIds = (Collection<String>) requestObject.getClass() |
| 219 | + .getDeclaredMethod("getArrangementIds") |
| 220 | + .invoke(requestObject); |
| 221 | + arrangementIds.add("1"); |
| 222 | + arrangementIds.add(""); |
| 223 | + |
| 224 | + // add PaymentRequestLine to lines |
| 225 | + Collection<Object> lines = (Collection<Object>) requestObject.getClass() |
| 226 | + .getDeclaredMethod("getLines") |
| 227 | + .invoke(requestObject); |
| 228 | + Class<?> lineObjectClass = projectClassLoader.loadClass(modelPackage + ".PaymentRequestLine"); |
| 229 | + Object lineObject = lineObjectClass.getConstructor().newInstance(); |
| 230 | + lineObject.getClass() |
| 231 | + .getDeclaredMethod("setAccountId", String.class) |
| 232 | + .invoke(lineObject, "invalidId"); |
| 233 | + lines.add(lineObject); |
| 234 | + |
| 235 | + // validate |
| 236 | + Set<ConstraintViolation<Object>> violations = validator.validate(requestObject); |
| 237 | + assertThat(violations, Matchers.hasSize(3)); |
| 238 | + |
| 239 | + assertViolationsCount(violations, "{jakarta.validation.constraints.Pattern.message}", 1); |
| 240 | + assertViolationsCount(violations, "{jakarta.validation.constraints.Size.message}", 2); |
| 241 | + |
| 242 | + } catch (Exception e) { |
| 243 | + throw new UnhandledException(e); |
| 244 | + } |
| 245 | + }; |
| 246 | + verificationRunner.runVerification( |
| 247 | + Verification.builder().runnable(verifyCollectionItems).displayName("validations").build() |
| 248 | + ); |
| 249 | + } |
| 250 | + |
| 251 | + private static void assertViolationsCount(Set<ConstraintViolation<Object>> violations, String messageTemplate, int count) { |
| 252 | + long actualCount = violations.stream() |
| 253 | + .map(ConstraintViolation::getMessageTemplate) |
| 254 | + .filter(messageTemplate::equals) |
| 255 | + .count(); |
| 256 | + assertEquals(count, actualCount, |
| 257 | + String.format("Number of violations '%s', count mismatch", messageTemplate)); |
| 258 | + } |
136 | 259 | } |
0 commit comments