Search in sources :

Example 1 with AtlasService

use of io.atlasmap.service.AtlasService in project atlasmap by atlasmap.

the class JavaServiceTest method testGetClass.

@Test
public void testGetClass() throws Exception {
    when(mockResourceContext.getResource(AtlasService.class)).thenReturn(new AtlasService());
    ClassInspectionRequest request = new ClassInspectionRequest();
    request.setClassName(ClassInspectionRequest.class.getName());
    byte[] bytes = Json.mapper().writeValueAsBytes(request);
    Response res = javaService.inspectClass(new ByteArrayInputStream(bytes));
    Object entity = res.getEntity();
    assertEquals(byte[].class, entity.getClass());
    ClassInspectionResponse inspectionResponse = Json.mapper().readValue((byte[]) entity, ClassInspectionResponse.class);
    String error = inspectionResponse.getErrorMessage();
    assertTrue(error == null || error.isEmpty(), error);
    assertEquals(ClassInspectionRequest.class.getName(), inspectionResponse.getJavaClass().getClassName());
    request.setClassName(AtlasSession.class.getName());
    bytes = Json.mapper().writeValueAsBytes(request);
    res = javaService.inspectClass(new ByteArrayInputStream(bytes));
    entity = res.getEntity();
    assertEquals(byte[].class, entity.getClass());
    inspectionResponse = Json.mapper().readValue((byte[]) entity, ClassInspectionResponse.class);
    error = inspectionResponse.getErrorMessage();
    assertTrue(error == null || error.isEmpty(), error);
    assertEquals(AtlasSession.class.getName(), inspectionResponse.getJavaClass().getClassName());
    inspectionResponse.getJavaClass().getJavaFields().getJavaField().stream().filter(f -> "properties".equals(f.getName())).forEach(f -> assertEquals("/properties", f.getPath(), "Invalid path: " + f.getPath()));
}
Also used : Response(javax.ws.rs.core.Response) ClassInspectionResponse(io.atlasmap.java.v2.ClassInspectionResponse) ClassInspectionResponse(io.atlasmap.java.v2.ClassInspectionResponse) InjectMocks(org.mockito.InjectMocks) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Mock(org.mockito.Mock) AtlasService(io.atlasmap.service.AtlasService) Mockito.when(org.mockito.Mockito.when) ClassInspectionRequest(io.atlasmap.java.v2.ClassInspectionRequest) Test(org.junit.jupiter.api.Test) Json(io.atlasmap.v2.Json) ByteArrayInputStream(java.io.ByteArrayInputStream) Response(javax.ws.rs.core.Response) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ResourceContext(javax.ws.rs.container.ResourceContext) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) AtlasSession(io.atlasmap.api.AtlasSession) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ClassInspectionResponse(io.atlasmap.java.v2.ClassInspectionResponse) AtlasService(io.atlasmap.service.AtlasService) ByteArrayInputStream(java.io.ByteArrayInputStream) AtlasSession(io.atlasmap.api.AtlasSession) ClassInspectionRequest(io.atlasmap.java.v2.ClassInspectionRequest) Test(org.junit.jupiter.api.Test)

Example 2 with AtlasService

use of io.atlasmap.service.AtlasService in project atlasmap by atlasmap.

the class JavaService method inspectClass.

/**
 * Inspects a Java Class with specified fully qualified class name and return a Document object.
 * @param requestIn request
 * @return {@link ClassInspectionResponse}
 */
@POST
@Path("/class")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
@Operation(summary = "Inspect Class", description = "Inspect a Java Class with specified fully qualified class name and return a Document object")
@RequestBody(description = "ClassInspectionRequest object", content = @Content(schema = @Schema(implementation = ClassInspectionRequest.class)))
@ApiResponses(@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = ClassInspectionResponse.class)), description = "Return a Document object represented by JavaClass"))
public Response inspectClass(InputStream requestIn) {
    ClassInspectionRequest request = fromJson(requestIn, ClassInspectionRequest.class);
    ClassInspectionResponse response = new ClassInspectionResponse();
    ClassInspectionService classInspectionService = new ClassInspectionService();
    classInspectionService.setConversionService(DefaultAtlasConversionService.getInstance());
    configureInspectionService(classInspectionService, request);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Class inspection request: {}", new String(toJson(request)));
    }
    long startTime = System.currentTimeMillis();
    try {
        JavaClass c = null;
        if (request.getClasspath() == null || request.getClasspath().isEmpty()) {
            AtlasService atlasService = resourceContext.getResource(AtlasService.class);
            c = classInspectionService.inspectClass(atlasService.getLibraryLoader(), request.getClassName(), request.getCollectionType(), request.getCollectionClassName());
        } else {
            c = classInspectionService.inspectClass(request.getClassName(), request.getCollectionType(), request.getClasspath());
        }
        response.setJavaClass(c);
    } catch (Throwable e) {
        String msg = String.format("Error inspecting class %s - %s: %s", request.getClassName(), e.getClass().getName(), e.getMessage());
        LOG.error(msg, e);
        response.setErrorMessage(msg);
    } finally {
        response.setExecutionTime(System.currentTimeMillis() - startTime);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Class inspection response: {}", new String(toJson(response)));
    }
    return Response.ok().entity(toJson(response)).build();
}
Also used : ClassInspectionResponse(io.atlasmap.java.v2.ClassInspectionResponse) AtlasService(io.atlasmap.service.AtlasService) JavaClass(io.atlasmap.java.v2.JavaClass) ClassInspectionService(io.atlasmap.java.inspect.ClassInspectionService) ClassInspectionRequest(io.atlasmap.java.v2.ClassInspectionRequest) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Operation(io.swagger.v3.oas.annotations.Operation) ApiResponses(io.swagger.v3.oas.annotations.responses.ApiResponses) RequestBody(io.swagger.v3.oas.annotations.parameters.RequestBody)

Aggregations

ClassInspectionRequest (io.atlasmap.java.v2.ClassInspectionRequest)2 ClassInspectionResponse (io.atlasmap.java.v2.ClassInspectionResponse)2 AtlasService (io.atlasmap.service.AtlasService)2 AtlasSession (io.atlasmap.api.AtlasSession)1 ClassInspectionService (io.atlasmap.java.inspect.ClassInspectionService)1 JavaClass (io.atlasmap.java.v2.JavaClass)1 Json (io.atlasmap.v2.Json)1 Operation (io.swagger.v3.oas.annotations.Operation)1 RequestBody (io.swagger.v3.oas.annotations.parameters.RequestBody)1 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 ResourceContext (javax.ws.rs.container.ResourceContext)1 Response (javax.ws.rs.core.Response)1 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)1 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)1 Test (org.junit.jupiter.api.Test)1