[Java] Java class assert (#11738)

* [Java] java file assert

* [Java] java file assert
This commit is contained in:
Oleh Kurpiak 2022-02-28 01:59:41 +02:00 committed by GitHub
parent 433d130b1b
commit 5b9efb6b5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 359 additions and 0 deletions

View File

@ -0,0 +1,53 @@
package org.openapitools.codegen.java.assertions;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import org.assertj.core.api.ListAssert;
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.expr.MemberValuePair;
import com.github.javaparser.ast.nodeTypes.NodeWithSimpleName;
public abstract class AbstractAnnotationAssert<ACTUAL extends AbstractAnnotationAssert<ACTUAL>> extends ListAssert<AnnotationExpr> {
protected AbstractAnnotationAssert(final List<AnnotationExpr> annotationExpr) {
super(annotationExpr);
}
public ACTUAL hasSize(final int size) {
super.hasSize(size);
return myself();
}
public ACTUAL containsWithName(final String name) {
super
.withFailMessage("Should have annotation with name: " + name)
.anyMatch(annotation -> annotation.getNameAsString().equals(name));
return myself();
}
public ACTUAL containsWithNameAndAttributes(final String name, final Map<String, String> attributes) {
super
.withFailMessage("Should have annotation with name: " + name + " and attributes: " + attributes + ", but was: " + actual)
.anyMatch(annotation -> annotation.getNameAsString().equals(name) && hasAttributes(annotation, attributes));
return myself();
}
private static boolean hasAttributes(final AnnotationExpr annotation, final Map<String, String> expectedAttributesToContains) {
final Map<String, String> actualAttributes = annotation.getChildNodes().stream()
.filter(MemberValuePair.class::isInstance)
.map(MemberValuePair.class::cast)
.collect(Collectors.toMap(NodeWithSimpleName::getNameAsString, pair -> pair.getValue().toString()));
return expectedAttributesToContains.entrySet().stream()
.allMatch(expected -> Objects.equals(actualAttributes.get(expected.getKey()), expected.getValue()));
}
@SuppressWarnings("unchecked")
private ACTUAL myself() {
return (ACTUAL) this;
}
}

View File

@ -0,0 +1,78 @@
package org.openapitools.codegen.java.assertions;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.Assertions;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.nodeTypes.NodeWithName;
public class JavaFileAssert extends AbstractAssert<JavaFileAssert, CompilationUnit> {
private JavaFileAssert(final CompilationUnit actual) {
super(actual, JavaFileAssert.class);
}
public static JavaFileAssert assertThat(final String source) {
return new JavaFileAssert(StaticJavaParser.parse(source));
}
public static JavaFileAssert assertThat(final Path path) {
try {
return new JavaFileAssert(StaticJavaParser.parse(path));
} catch (IOException e) {
throw new RuntimeException("Exception while reading file: " + path, e);
}
}
public MethodAssert assertMethod(final String methodName, final String... paramTypes) {
List<MethodDeclaration> methods = paramTypes.length == 0
? actual.getType(0).getMethodsByName(methodName)
: actual.getType(0).getMethodsBySignature(methodName, paramTypes);
String message = paramTypes.length == 0
? "Expected to be a single method %s, but found " + methods.size()
: "Expected to be a single method %s with parameter(s) %s, but found " + methods.size();
Assertions.assertThat(methods)
.withFailMessage(message, methodName, Arrays.toString(paramTypes))
.hasSize(1);
return new MethodAssert(this, methods.get(0));
}
public PropertyAssert hasProperty(final String propertyName) {
Optional<FieldDeclaration> fieldOptional = actual.getType(0).getMembers().stream()
.filter(FieldDeclaration.class::isInstance)
.map(FieldDeclaration.class::cast)
.filter(field -> field.getVariables().getFirst().map(var -> var.getNameAsString().equals(propertyName)).orElse(Boolean.FALSE))
.findFirst();
Assertions.assertThat(fieldOptional)
.withFailMessage("Should have field with name %s", propertyName)
.isPresent();
return new PropertyAssert(this, fieldOptional.get());
}
public JavaFileAssert hasImports(final String... imports) {
Assertions.assertThat(actual.getImports().stream().map(NodeWithName::getNameAsString))
.containsAll(Arrays.asList(imports));
return this;
}
public JavaFileAssert printFileContent() {
System.out.println(actual);
return this;
}
public TypeAnnotationAssert assertTypeAnnotations() {
return new TypeAnnotationAssert(this, actual.getType(0).getAnnotations());
}
}

View File

@ -0,0 +1,19 @@
package org.openapitools.codegen.java.assertions;
import java.util.List;
import com.github.javaparser.ast.expr.AnnotationExpr;
public class MethodAnnotationAssert extends AbstractAnnotationAssert<MethodAnnotationAssert> {
private final MethodAssert methodAssert;
protected MethodAnnotationAssert(final MethodAssert methodAssert, final List<AnnotationExpr> annotationExpr) {
super(annotationExpr);
this.methodAssert = methodAssert;
}
public MethodAssert toMethod() {
return methodAssert;
}
}

View File

@ -0,0 +1,42 @@
package org.openapitools.codegen.java.assertions;
import java.util.Optional;
import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.Assertions;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.Parameter;
public class MethodAssert extends AbstractAssert<MethodAssert, MethodDeclaration> {
private final JavaFileAssert fileAssert;
MethodAssert(final JavaFileAssert fileAssert, final MethodDeclaration methodDeclaration) {
super(methodDeclaration, MethodAssert.class);
this.fileAssert = fileAssert;
}
public JavaFileAssert and() {
return fileAssert;
}
public MethodAnnotationAssert assertMethodAnnotations() {
return new MethodAnnotationAssert(this, actual.getAnnotations());
}
public MethodAssert hasReturnType(final String returnType) {
Assertions.assertThat(actual.getType().toString())
.isEqualTo(returnType);
return this;
}
public ParameterAssert hasParameter(final String paramName) {
final Optional<Parameter> parameter = actual.getParameterByName(paramName);
Assertions.assertThat(parameter)
.withFailMessage("Method %s should have parameter %s, but it doesn't", actual.getNameAsString(), paramName)
.isPresent();
return new ParameterAssert(this, parameter.get());
}
}

View File

@ -0,0 +1,19 @@
package org.openapitools.codegen.java.assertions;
import java.util.List;
import com.github.javaparser.ast.expr.AnnotationExpr;
public class ParameterAnnotationAssert extends AbstractAnnotationAssert<ParameterAnnotationAssert> {
private final ParameterAssert parameterAssert;
protected ParameterAnnotationAssert(final ParameterAssert parameterAssert, final List<AnnotationExpr> annotationExpr) {
super(annotationExpr);
this.parameterAssert = parameterAssert;
}
public ParameterAssert toParameter() {
return parameterAssert;
}
}

View File

@ -0,0 +1,31 @@
package org.openapitools.codegen.java.assertions;
import org.assertj.core.api.Assertions;
import org.assertj.core.api.ObjectAssert;
import com.github.javaparser.ast.body.Parameter;
public class ParameterAssert extends ObjectAssert<Parameter> {
private final MethodAssert methodAssert;
protected ParameterAssert(final MethodAssert methodAssert, final Parameter parameter) {
super(parameter);
this.methodAssert = methodAssert;
}
public MethodAssert toMethod() {
return methodAssert;
}
public ParameterAssert withType(final String expectedType) {
Assertions.assertThat(actual.getTypeAsString())
.withFailMessage("Expected parameter to have type %s, but was %s", expectedType, actual.getTypeAsString())
.isEqualTo(expectedType);
return this;
}
public ParameterAnnotationAssert assertParameterAnnotations() {
return new ParameterAnnotationAssert(this, actual.getAnnotations());
}
}

View File

@ -0,0 +1,19 @@
package org.openapitools.codegen.java.assertions;
import java.util.List;
import com.github.javaparser.ast.expr.AnnotationExpr;
public class PropertyAnnotationAssert extends AbstractAnnotationAssert<PropertyAnnotationAssert> {
private final PropertyAssert propertyAssert;
protected PropertyAnnotationAssert(final PropertyAssert propertyAssert, final List<AnnotationExpr> annotationExpr) {
super(annotationExpr);
this.propertyAssert = propertyAssert;
}
public PropertyAssert toProperty() {
return propertyAssert;
}
}

View File

@ -0,0 +1,32 @@
package org.openapitools.codegen.java.assertions;
import org.assertj.core.api.Assertions;
import org.assertj.core.api.ObjectAssert;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.Parameter;
public class PropertyAssert extends ObjectAssert<FieldDeclaration> {
private final JavaFileAssert javaFileAssert;
protected PropertyAssert(final JavaFileAssert javaFileAssert, final FieldDeclaration fieldDeclaration) {
super(fieldDeclaration);
this.javaFileAssert = javaFileAssert;
}
public JavaFileAssert toType() {
return javaFileAssert;
}
public PropertyAssert withType(final String expectedType) {
Assertions.assertThat(actual.getElementType().toString())
.withFailMessage("Expected property %s to have type %s, but was %s", actual.getVariable(0).getNameAsString(), expectedType, actual.getElementType().toString())
.isEqualTo(expectedType);
return this;
}
public PropertyAnnotationAssert assertPropertyAnnotations() {
return new PropertyAnnotationAssert(this, actual.getAnnotations());
}
}

View File

@ -0,0 +1,19 @@
package org.openapitools.codegen.java.assertions;
import java.util.List;
import com.github.javaparser.ast.expr.AnnotationExpr;
public class TypeAnnotationAssert extends AbstractAnnotationAssert<TypeAnnotationAssert> {
private final JavaFileAssert fileAssert;
protected TypeAnnotationAssert(final JavaFileAssert fileAssert, final List<AnnotationExpr> annotationExpr) {
super(annotationExpr);
this.fileAssert = fileAssert;
}
public JavaFileAssert toType() {
return fileAssert;
}
}

View File

@ -40,6 +40,7 @@ import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import org.openapitools.codegen.java.assertions.JavaFileAssert;
import org.openapitools.codegen.CliOption;
import org.openapitools.codegen.ClientOptInput;
import org.openapitools.codegen.CodegenConstants;
@ -58,6 +59,8 @@ import org.testng.annotations.DataProvider;
import org.testng.annotations.Ignore;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap;
public class SpringCodegenTest {
@Test
@ -95,9 +98,53 @@ public class SpringCodegenTest {
generator.setGeneratorPropertyDefault(CodegenConstants.SUPPORTING_FILES, "false");
generator.opts(input).generate();
JavaFileAssert.assertThat(Paths.get(outputPath + "/src/main/java/org/openapitools/api/ZebrasApi.java"))
.assertTypeAnnotations()
.hasSize(3)
.containsWithName("Validated")
.containsWithName("Generated")
.containsWithNameAndAttributes("Generated", ImmutableMap.of(
"value", "\"org.openapitools.codegen.languages.SpringCodegen\""
))
.containsWithNameAndAttributes("Tag", ImmutableMap.of(
"name", "\"zebras\""
))
.toType()
.assertMethod("getZebras")
.hasReturnType("ResponseEntity<Void>")
.assertMethodAnnotations()
.hasSize(2)
.containsWithNameAndAttributes("Operation", ImmutableMap.of("operationId", "\"getZebras\""))
.containsWithNameAndAttributes("RequestMapping", ImmutableMap.of(
"method", "RequestMethod.GET",
"value", "\"/zebras\""
))
.toMethod()
.hasParameter("limit").withType("BigDecimal")
.assertParameterAnnotations()
.containsWithName("Valid")
.containsWithNameAndAttributes("Parameter", ImmutableMap.of("name", "\"limit\""))
.containsWithNameAndAttributes("RequestParam", ImmutableMap.of("required", "false", "value", "\"limit\""))
.toParameter()
.toMethod()
.hasParameter("animalParams").withType("AnimalParams");
// todo: to remove
assertFileContains(Paths.get(outputPath + "/src/main/java/org/openapitools/api/ZebrasApi.java"),
"AnimalParams");
JavaFileAssert.assertThat(Paths.get(outputPath + "/src/main/java/org/openapitools/model/AnimalParams.java"))
.hasImports("org.springframework.format.annotation.DateTimeFormat")
.hasProperty("born").withType("LocalDate")
.assertPropertyAnnotations()
.containsWithNameAndAttributes("DateTimeFormat", ImmutableMap.of("iso", "DateTimeFormat.ISO.DATE"))
.toProperty()
.toType()
.hasProperty("lastSeen").withType("OffsetDateTime")
.assertPropertyAnnotations()
.containsWithNameAndAttributes("DateTimeFormat", ImmutableMap.of("iso", "DateTimeFormat.ISO.DATE_TIME"));
// todo: to remove
assertFileContains(Paths.get(outputPath + "/src/main/java/org/openapitools/model/AnimalParams.java"),
"@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)", "@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)");