Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/workflows/samples-jaxrs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,9 @@ jobs:
- samples/server/petstore/jaxrs-resteasy/eap-joda
- samples/server/petstore/jaxrs-resteasy/eap-java8
- samples/server/petstore/jaxrs-resteasy/joda
- samples/server/petstore/jaxrs-resteasy/default-value
- samples/server/petstore/jaxrs-cxf
- samples/server/petstore/jaxrs-cxf-annotated-base-path
- samples/server/petstore/jaxrs-cxf-cdi
- samples/server/petstore/jaxrs-cxf-cdi-default-value
- samples/server/petstore/jaxrs-cxf-non-spring-app
steps:
- uses: actions/checkout@v3
Expand Down
7 changes: 0 additions & 7 deletions bin/configs/jaxrs-cxf-cdi-default-values.yaml

This file was deleted.

7 changes: 0 additions & 7 deletions bin/configs/jaxrs-resteasy-default-values.yaml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.openapitools.codegen.java.assertions;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand All @@ -8,8 +9,11 @@
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.expr.MarkerAnnotationExpr;
import com.github.javaparser.ast.expr.NormalAnnotationExpr;
import com.github.javaparser.ast.expr.SingleMemberAnnotationExpr;
import com.github.javaparser.ast.nodeTypes.NodeWithSimpleName;
import com.google.common.collect.ImmutableMap;

public abstract class AbstractAnnotationAssert<ACTUAL extends AbstractAnnotationAssert<ACTUAL>> extends ListAssert<AnnotationExpr> {

Expand Down Expand Up @@ -37,10 +41,19 @@ public ACTUAL containsWithNameAndAttributes(final String name, final Map<String,
}

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()));
final Map<String, String> actualAttributes;
if (annotation instanceof SingleMemberAnnotationExpr) {
actualAttributes = ImmutableMap.of(
"value", ((SingleMemberAnnotationExpr) annotation).getMemberValue().toString()
);
} else if (annotation instanceof NormalAnnotationExpr) {
actualAttributes = ((NormalAnnotationExpr) annotation).getPairs().stream()
.collect(Collectors.toMap(NodeWithSimpleName::getNameAsString, pair -> pair.getValue().toString()));
} else if (annotation instanceof MarkerAnnotationExpr) {
actualAttributes = new HashMap<>();
} else {
throw new IllegalArgumentException("Unexpected annotation expression type for: " + annotation);
}

return expectedAttributesToContains.entrySet().stream()
.allMatch(expected -> Objects.equals(actualAttributes.get(expected.getKey()), expected.getValue()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.openapitools.codegen.java.assertions;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.Assertions;
Expand Down Expand Up @@ -33,6 +35,14 @@ public static JavaFileAssert assertThat(final Path path) {
}
}

public static JavaFileAssert assertThat(final File file) {
try {
return new JavaFileAssert(StaticJavaParser.parse(file));
} catch (IOException e) {
throw new RuntimeException("Exception while reading file: " + file, e);
}
}

public MethodAssert assertMethod(final String methodName, final String... paramTypes) {
List<MethodDeclaration> methods = paramTypes.length == 0
? actual.getType(0).getMethodsByName(methodName)
Expand Down Expand Up @@ -71,6 +81,20 @@ public JavaFileAssert printFileContent() {
return this;
}

public JavaFileAssert fileContains(final String... lines) {
final String actualBody = actual.getTokenRange()
.orElseThrow(() -> new IllegalStateException("Empty file"))
.toString();
Assertions.assertThat(actualBody)
.withFailMessage(
"File should contains lines\n====\n%s\n====\nbut actually was\n====\n%s\n====",
Arrays.stream(lines).collect(Collectors.joining(System.lineSeparator())), actualBody
)
.contains(lines);

return this;
}

public TypeAnnotationAssert assertTypeAnnotations() {
return new TypeAnnotationAssert(this, actual.getType(0).getAnnotations());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
package org.openapitools.codegen.java.assertions;

import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Collectors;

import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.Assertions;

import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.Parameter;
import com.github.javaparser.ast.nodeTypes.NodeWithName;

public class MethodAssert extends AbstractAssert<MethodAssert, MethodDeclaration> {

private final JavaFileAssert fileAssert;
private final String methodSignature;

MethodAssert(final JavaFileAssert fileAssert, final MethodDeclaration methodDeclaration) {
super(methodDeclaration, MethodAssert.class);
this.fileAssert = fileAssert;
this.methodSignature = methodDeclaration.getDeclarationAsString();
}

public JavaFileAssert and() {
public JavaFileAssert toFileAssert() {
return fileAssert;
}

Expand All @@ -34,9 +41,83 @@ public MethodAssert hasReturnType(final String returnType) {
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)
.withFailMessage("Method %s should have parameter %s, but it doesn't", methodSignature, paramName)
.isPresent();
return new ParameterAssert(this, parameter.get());
}

public MethodAssert doesNotHaveParameter(final String paramName) {
Assertions.assertThat(actual.getParameterByName(paramName))
.withFailMessage("Method %s shouldn't have parameter %s, but it does", methodSignature, paramName)
.isEmpty();
return this;
}

public MethodAssert bodyContainsLines(final String... lines) {
Assertions.assertThat(isWithImplementation())
.withFailMessage("Method %s is abstract", methodSignature)
.isTrue();
final String actualBody = actual.getTokenRange()
.orElseThrow(() -> new IllegalStateException("Not-abstract method doesn't have body"))
.toString();
Assertions.assertThat(actualBody)
.withFailMessage(
"Method's %s body should contains lines\n====\n%s\n====\nbut actually was\n====\n%s\n====",
methodSignature, Arrays.stream(lines).collect(Collectors.joining(System.lineSeparator())), actualBody
)
.contains(lines);

return this;
}

public MethodAssert doesNotHaveImplementation() {
Assertions.assertThat(isWithImplementation())
.withFailMessage("Method %s should be abstract", methodSignature)
.isFalse();
return this;
}

public MethodAssert doesNotHaveComment() {
Assertions.assertThat(actual.getJavadocComment())
.withFailMessage("Method %s shouldn't contains comment, but it does", methodSignature)
.isEmpty();
return this;
}

public MethodAssert commentContainsLines(final String... lines) {
Assertions.assertThat(actual.getJavadocComment())
.withFailMessage("Method %s should contains comment, but it doesn't", methodSignature)
.isPresent();
final String actualComment = actual.getJavadocComment().get().getContent();
Assertions.assertThat(actualComment)
.withFailMessage(
"Method's %s comment should contains lines\n====\n%s\n====\nbut actually was\n====%s\n====",
methodSignature, Arrays.stream(lines).collect(Collectors.joining(System.lineSeparator())), actualComment
)
.contains(lines);

return this;
}

public MethodAssert noneOfParameterHasAnnotation(final String annotationName) {
actual.getParameters()
.forEach(
param -> Assertions.assertThat(param.getAnnotations())
.withFailMessage("Parameter %s contains annotation %s while it shouldn't", param.getNameAsString(), annotationName)
.extracting(NodeWithName::getNameAsString)
.doesNotContain(annotationName)
);

return this;
}

private boolean isWithImplementation() {
final boolean isInterface = actual.getParentNode()
.filter(ClassOrInterfaceDeclaration.class::isInstance)
.map(ClassOrInterfaceDeclaration.class::cast)
.map(ClassOrInterfaceDeclaration::isInterface)
.orElse(false);
return !(actual.isAbstract() || (isInterface && !actual.isDefault()));
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,101 @@
package org.openapitools.codegen.java.jaxrs;

import java.io.File;
import java.nio.file.Files;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.openapitools.codegen.ClientOptInput;
import org.openapitools.codegen.DefaultGenerator;
import org.openapitools.codegen.java.assertions.JavaFileAssert;
import org.openapitools.codegen.languages.JavaJAXRSCXFCDIServerCodegen;
import org.openapitools.codegen.languages.features.CXFServerFeatures;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import com.google.common.collect.ImmutableMap;

import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.parser.core.models.ParseOptions;

public class JavaJAXRSCXFCDIServerCodegenTest extends JavaJaxrsBaseTest {

@BeforeMethod
public void beforeMethod() {
codegen = new JavaJAXRSCXFCDIServerCodegen();
}

@Test
public void testHandleDefaultValue_issue8535() throws Exception {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();

OpenAPI openAPI = new OpenAPIParser()
.readLocation("src/test/resources/3_0/issue_8535.yaml", null, new ParseOptions()).getOpenAPI();

codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(CXFServerFeatures.LOAD_TEST_DATA_FROM_FILE, "true");

ClientOptInput input = new ClientOptInput()
.openAPI(openAPI)
.config(codegen);

DefaultGenerator generator = new DefaultGenerator();
Map<String, File> files = generator.opts(input).generate().stream()
.collect(Collectors.toMap(File::getName, Function.identity()));

JavaFileAssert.assertThat(files.get("TestHeadersApi.java"))
.assertMethod("headersTest")
.hasParameter("headerNumber").withType("BigDecimal")
.assertParameterAnnotations()
.containsWithNameAndAttributes("ApiParam", ImmutableMap.of("defaultValue", "\"11.2\""))
.toParameter().toMethod()
.hasParameter("headerString").withType("String")
.assertParameterAnnotations()
.containsWithNameAndAttributes("ApiParam", ImmutableMap.of("defaultValue", "\"qwerty\""))
.toParameter().toMethod()
.hasParameter("headerStringWrapped").withType("String")
.assertParameterAnnotations()
.containsWithNameAndAttributes("ApiParam", ImmutableMap.of("defaultValue", "\"qwerty\""))
.toParameter().toMethod()
.hasParameter("headerStringQuotes").withType("String")
.assertParameterAnnotations()
.containsWithNameAndAttributes("ApiParam", ImmutableMap.of("defaultValue", "\"qwerty\\\"with quotes\\\" test\""))
.toParameter().toMethod()
.hasParameter("headerStringQuotesWrapped").withType("String")
.assertParameterAnnotations()
.containsWithNameAndAttributes("ApiParam", ImmutableMap.of("defaultValue", "\"qwerty\\\"with quotes\\\" test\""))
.toParameter().toMethod()
.hasParameter("headerBoolean").withType("Boolean")
.assertParameterAnnotations()
.containsWithNameAndAttributes("ApiParam", ImmutableMap.of("defaultValue", "\"true\""));

JavaFileAssert.assertThat(files.get("TestQueryParamsApi.java"))
.assertMethod("queryParamsTest")
.hasParameter("queryNumber").withType("BigDecimal")
.assertParameterAnnotations()
.containsWithNameAndAttributes("ApiParam", ImmutableMap.of("defaultValue", "\"11.2\""))
.toParameter().toMethod()
.hasParameter("queryString").withType("String")
.assertParameterAnnotations()
.containsWithNameAndAttributes("ApiParam", ImmutableMap.of("defaultValue", "\"qwerty\""))
.toParameter().toMethod()
.hasParameter("queryStringWrapped").withType("String")
.assertParameterAnnotations()
.containsWithNameAndAttributes("ApiParam", ImmutableMap.of("defaultValue", "\"qwerty\""))
.toParameter().toMethod()
.hasParameter("queryStringQuotes").withType("String")
.assertParameterAnnotations()
.containsWithNameAndAttributes("ApiParam", ImmutableMap.of("defaultValue", "\"qwerty\\\"with quotes\\\" test\""))
.toParameter().toMethod()
.hasParameter("queryStringQuotesWrapped").withType("String")
.assertParameterAnnotations()
.containsWithNameAndAttributes("ApiParam", ImmutableMap.of("defaultValue", "\"qwerty\\\"with quotes\\\" test\""))
.toParameter().toMethod()
.hasParameter("queryBoolean").withType("Boolean")
.assertParameterAnnotations()
.containsWithNameAndAttributes("ApiParam", ImmutableMap.of("defaultValue", "\"true\""));
}
}
Loading