Search in sources :

Example 6 with Endpoint

use of dev.hilla.Endpoint in project flow by vaadin.

the class GeneratorUtilsTest method should_Not_BeConsideredAsHavingAnAnnotation_When_GivenClassHavsAnnotationDeclarationButWithDifferentImport.

@Test
public void should_Not_BeConsideredAsHavingAnAnnotation_When_GivenClassHavsAnnotationDeclarationButWithDifferentImport() {
    NodeWithAnnotations<?> declarationWithEndpointAnnotation = Mockito.mock(NodeWithAnnotations.class);
    CompilationUnit compilationUnitWithNonVaadinEndpointImport = Mockito.mock(CompilationUnit.class);
    AnnotationExpr endpointAnnotation = Mockito.mock(AnnotationExpr.class);
    Mockito.doReturn(Optional.of(endpointAnnotation)).when(declarationWithEndpointAnnotation).getAnnotationByClass(Endpoint.class);
    NodeList<ImportDeclaration> imports = new NodeList<>();
    ImportDeclaration importDeclaration = Mockito.mock(ImportDeclaration.class);
    Mockito.doReturn("some.non.vaadin.Endpoint").when(importDeclaration).getNameAsString();
    imports.add(importDeclaration);
    Mockito.doReturn(imports).when(compilationUnitWithNonVaadinEndpointImport).getImports();
    Assert.assertFalse("A class with a non Vaadin Endpoint should not be considered as an Endpoint", GeneratorUtils.hasAnnotation(declarationWithEndpointAnnotation, compilationUnitWithNonVaadinEndpointImport, Endpoint.class));
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr) Endpoint(dev.hilla.Endpoint) NodeList(com.github.javaparser.ast.NodeList) ImportDeclaration(com.github.javaparser.ast.ImportDeclaration) Test(org.junit.Test)

Example 7 with Endpoint

use of dev.hilla.Endpoint in project flow by vaadin.

the class AbstractEndpointGenerationTest method assertPath.

private void assertPath(Class<?> testEndpointClass, Method expectedEndpointMethod, PathItem actualPath, HashMap<String, Class<?>> typeArguments) {
    Operation actualOperation = actualPath.getPost();
    assertEquals("Unexpected tag in the OpenAPI spec", Collections.singletonList(testEndpointClass.getSimpleName()), actualOperation.getTags());
    assertTrue(String.format("Unexpected OpenAPI operation id: does not contain the endpoint name of the class '%s'", testEndpointClass.getSimpleName()), actualOperation.getOperationId().contains(getEndpointName(testEndpointClass)));
    assertTrue(String.format("Unexpected OpenAPI operation id: does not contain the name of the endpoint method '%s'", expectedEndpointMethod.getName()), actualOperation.getOperationId().contains(expectedEndpointMethod.getName()));
    if (expectedEndpointMethod.getParameterCount() > 0) {
        Schema requestSchema = extractSchema(actualOperation.getRequestBody().getContent());
        Type[] genericParameterTypes = expectedEndpointMethod.getGenericParameterTypes();
        Class<?>[] parameterTypes = new Class<?>[genericParameterTypes.length];
        List<HashMap<String, Class<?>>> parameterTypeArguments = new ArrayList<>();
        for (int i = 0; i < genericParameterTypes.length; i++) {
            parameterTypes[i] = applyTypeArguments(genericParameterTypes[i], typeArguments);
            parameterTypeArguments.add(i, extractTypeArguments(genericParameterTypes[i], typeArguments));
        }
        assertRequestSchema(requestSchema, parameterTypes, parameterTypeArguments, expectedEndpointMethod.getParameters());
    } else {
        assertNull(String.format("No request body should be present in path schema for endpoint method with no parameters, method: '%s'", expectedEndpointMethod), actualOperation.getRequestBody());
    }
    ApiResponses responses = actualOperation.getResponses();
    assertEquals("Every operation is expected to have a single '200' response", 1, responses.size());
    ApiResponse apiResponse = responses.get("200");
    assertNotNull("Every operation is expected to have a single '200' response", apiResponse);
    Type genericReturnType = expectedEndpointMethod.getGenericReturnType();
    Class<?> returnType = applyTypeArguments(genericReturnType, typeArguments);
    Class<?> mappedType = new EndpointTransferMapper().getTransferType(returnType);
    if (mappedType != null) {
        returnType = mappedType;
    }
    if (returnType != void.class) {
        assertSchema(extractSchema(apiResponse.getContent()), returnType, extractTypeArguments(genericReturnType, typeArguments));
    } else {
        assertNull(String.format("No response is expected to be present for void method '%s'", expectedEndpointMethod), apiResponse.getContent());
    }
    assertNotNull("Non-anonymous endpoint method should have a security data defined for it in the schema", actualOperation.getSecurity());
}
Also used : HashMap(java.util.HashMap) BooleanSchema(io.swagger.v3.oas.models.media.BooleanSchema) DateSchema(io.swagger.v3.oas.models.media.DateSchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) DateTimeSchema(io.swagger.v3.oas.models.media.DateTimeSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) NumberSchema(io.swagger.v3.oas.models.media.NumberSchema) Schema(io.swagger.v3.oas.models.media.Schema) MapSchema(io.swagger.v3.oas.models.media.MapSchema) ArrayList(java.util.ArrayList) Operation(io.swagger.v3.oas.models.Operation) Endpoint(dev.hilla.Endpoint) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) EndpointTransferMapper(dev.hilla.endpointransfermapper.EndpointTransferMapper) GenericArrayType(java.lang.reflect.GenericArrayType) Type(java.lang.reflect.Type) ParameterizedType(java.lang.reflect.ParameterizedType) ApiResponses(io.swagger.v3.oas.models.responses.ApiResponses)

Aggregations

Endpoint (dev.hilla.Endpoint)7 CompilationUnit (com.github.javaparser.ast.CompilationUnit)3 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)3 BooleanSchema (io.swagger.v3.oas.models.media.BooleanSchema)3 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)3 DateSchema (io.swagger.v3.oas.models.media.DateSchema)3 DateTimeSchema (io.swagger.v3.oas.models.media.DateTimeSchema)3 MapSchema (io.swagger.v3.oas.models.media.MapSchema)3 NumberSchema (io.swagger.v3.oas.models.media.NumberSchema)3 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)3 Schema (io.swagger.v3.oas.models.media.Schema)3 StringSchema (io.swagger.v3.oas.models.media.StringSchema)3 Test (org.junit.Test)3 ImportDeclaration (com.github.javaparser.ast.ImportDeclaration)2 NodeList (com.github.javaparser.ast.NodeList)2 AnnotationExpr (com.github.javaparser.ast.expr.AnnotationExpr)2 GenericArrayType (java.lang.reflect.GenericArrayType)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 Type (java.lang.reflect.Type)2 ArrayList (java.util.ArrayList)2