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()));
}
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();
}
Aggregations