Search in sources :

Example 1 with Endpoint

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

the class GeneratorUtilsTest method should_BeConsideredAsHavingAnAnnotation_When_GivenClassHavsAnnotationDeclarationAndTheImport.

@Test
public void should_BeConsideredAsHavingAnAnnotation_When_GivenClassHavsAnnotationDeclarationAndTheImport() {
    NodeWithAnnotations<?> declarationWithEndpointAnnotation = Mockito.mock(NodeWithAnnotations.class);
    CompilationUnit compilationUnitWithVaadinEndpointImport = 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(Endpoint.class.getName()).when(importDeclaration).getNameAsString();
    imports.add(importDeclaration);
    Mockito.doReturn(imports).when(compilationUnitWithVaadinEndpointImport).getImports();
    Assert.assertTrue("A class with a Vaadin Endpoint should be considered as an Endpoint", GeneratorUtils.hasAnnotation(declarationWithEndpointAnnotation, compilationUnitWithVaadinEndpointImport, 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 2 with Endpoint

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

the class AbstractEndpointGenerationTest method assertClassPathsRecursive.

private int assertClassPathsRecursive(Paths actualPaths, Class<?> testEndpointClass, Class<?> testMethodsClass, HashMap<String, Class<?>> typeArguments) {
    if (!testMethodsClass.equals(testEndpointClass) && !testMethodsClass.isAnnotationPresent(EndpointExposed.class)) {
        return 0;
    }
    int pathCount = 0;
    for (Method expectedEndpointMethod : testMethodsClass.getDeclaredMethods()) {
        if (!Modifier.isPublic(expectedEndpointMethod.getModifiers()) || accessChecker.getAccessAnnotationChecker().getSecurityTarget(expectedEndpointMethod).isAnnotationPresent(DenyAll.class)) {
            continue;
        }
        pathCount++;
        String expectedEndpointUrl = String.format("/%s/%s", getEndpointName(testEndpointClass), expectedEndpointMethod.getName());
        PathItem actualPath = actualPaths.get(expectedEndpointUrl);
        assertNotNull(String.format("Expected to find a path '%s' for the endpoint method '%s' in the class '%s'", expectedEndpointUrl, expectedEndpointMethod, testEndpointClass), actualPath);
        assertPath(testEndpointClass, expectedEndpointMethod, actualPath, typeArguments);
    }
    Type genericSuperClass = testMethodsClass.getGenericSuperclass();
    pathCount += assertClassPathsRecursive(actualPaths, testEndpointClass, applyTypeArguments(genericSuperClass, typeArguments), extractTypeArguments(genericSuperClass, typeArguments));
    for (Type genericInterface : testMethodsClass.getGenericInterfaces()) {
        pathCount += assertClassPathsRecursive(actualPaths, testEndpointClass, applyTypeArguments(genericInterface, typeArguments), extractTypeArguments(genericInterface, typeArguments));
    }
    return pathCount;
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) DenyAll(javax.annotation.security.DenyAll) GenericArrayType(java.lang.reflect.GenericArrayType) Type(java.lang.reflect.Type) ParameterizedType(java.lang.reflect.ParameterizedType) Method(java.lang.reflect.Method) Endpoint(dev.hilla.Endpoint)

Example 3 with Endpoint

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

the class AbstractEndpointGenerationTest method assertComponentSchemas.

private void assertComponentSchemas(Map<String, Schema> actualSchemas, List<Class<?>> testEndpointClasses) {
    int schemasCount = 0;
    for (Class<?> expectedSchemaClass : testEndpointClasses) {
        schemasCount++;
        Schema actualSchema = actualSchemas.get(expectedSchemaClass.getCanonicalName());
        assertNotNull(String.format("Expected to have a schema defined for a class '%s'", expectedSchemaClass), actualSchema);
        assertSchema(actualSchema, expectedSchemaClass, new HashMap<>());
    }
    assertEquals("Expected to have all endpoint classes defined in schemas", schemasCount, actualSchemas.size());
}
Also used : 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) Endpoint(dev.hilla.Endpoint)

Example 4 with Endpoint

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

the class AbstractEndpointGenerationTest method assertRequestSchema.

private void assertRequestSchema(Schema requestSchema, Class<?>[] parameterTypes, List<HashMap<String, Class<?>>> parameterTypeArguments, Parameter[] parameters) {
    Map<String, Schema> properties = requestSchema.getProperties();
    assertEquals("Request schema should have the same amount of properties as the corresponding endpoint method parameters number", parameterTypes.length, properties.size());
    int index = 0;
    for (Map.Entry<String, Schema> stringSchemaEntry : properties.entrySet()) {
        assertSchema(stringSchemaEntry.getValue(), parameterTypes[index], parameterTypeArguments.get(index));
        List requiredList = requestSchema.getRequired();
        assertTrue(requiredList.contains(stringSchemaEntry.getKey()));
        index++;
    }
}
Also used : 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) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Endpoint(dev.hilla.Endpoint)

Example 5 with Endpoint

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

the class GeneratorUtilsTest method should_Not_BeConsideredAsHavingAnAnnotation_When_GivenClassDoesNotHaveAnnotationDeclarationNorImport.

@Test
public void should_Not_BeConsideredAsHavingAnAnnotation_When_GivenClassDoesNotHaveAnnotationDeclarationNorImport() {
    NodeWithAnnotations<?> declarationWithoutEndpointAnnotation = Mockito.mock(NodeWithAnnotations.class);
    CompilationUnit compilationUnitWithoutEndpointImport = Mockito.mock(CompilationUnit.class);
    Mockito.doReturn(Optional.empty()).when(declarationWithoutEndpointAnnotation).getAnnotationByClass(Endpoint.class);
    Mockito.doReturn(new NodeList<>()).when(compilationUnitWithoutEndpointImport).getImports();
    Assert.assertFalse("A class without Endpoint annotation nor import should not be considered as an Endpoint", GeneratorUtils.hasAnnotation(declarationWithoutEndpointAnnotation, compilationUnitWithoutEndpointImport, Endpoint.class));
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Endpoint(dev.hilla.Endpoint) Test(org.junit.Test)

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