EntityResourceProvider extends BaseResourceProvider implements ResourceProvider { @Override public Result getResourceById(Request request) throws ResourceNotFoundException { AtlasQuery atlasQuery; try { atlasQuery = queryFactory.createEntityQuery(request); } catch (InvalidQueryException e) { throw new CatalogRuntimeException("Unable to compile internal Entity query: " + e, e); } Collection<Map<String, Object>> results = atlasQuery.execute(); if (results.isEmpty()) { throw new ResourceNotFoundException(String.format("Entity '%s' not found.", request.getProperty(resourceDefinition.getIdPropertyName()))); } return new Result(results); } EntityResourceProvider(AtlasTypeSystem typeSystem); @Override Result getResourceById(Request request); @Override Result getResources(Request request); @Override void createResource(Request request); @Override Collection<String> createResources(Request request);  }
@Test public void testGetResource() throws Exception { AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class); QueryFactory queryFactory = createStrictMock(QueryFactory.class); AtlasQuery query = createStrictMock(AtlasQuery.class); Capture<Request> requestCapture = newCapture(); Collection<Map<String, Object>> queryResult = new ArrayList<>(); Map<String, Object> queryResultRow = new HashMap<>(); queryResult.add(queryResultRow); queryResultRow.put("id", "1"); queryResultRow.put("creation_time", "04/20/2016"); expect(queryFactory.createEntityQuery(capture(requestCapture))).andReturn(query); expect(query.execute()).andReturn(queryResult); replay(typeSystem, queryFactory, query); EntityResourceProvider provider = new EntityResourceProvider(typeSystem); provider.setQueryFactory(queryFactory); Map<String, Object> requestProperties = new HashMap<>(); requestProperties.put("id", "1"); Request userRequest = new InstanceRequest(requestProperties); Result result = provider.getResourceById(userRequest); assertEquals(1, result.getPropertyMaps().size()); assertEquals(queryResultRow, result.getPropertyMaps().iterator().next()); Request request = requestCapture.getValue(); assertNull(request.getQueryString()); assertEquals(0, request.getAdditionalSelectProperties().size()); assertEquals(requestProperties, request.getQueryProperties()); verify(typeSystem, queryFactory, query); }
@Test(expectedExceptions = ResourceNotFoundException.class) public void testGetResource_404() throws Exception { AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class); QueryFactory queryFactory = createStrictMock(QueryFactory.class); AtlasQuery query = createStrictMock(AtlasQuery.class); Capture<Request> requestCapture = newCapture(); Collection<Map<String, Object>> emptyResponse = new ArrayList<>(); expect(queryFactory.createEntityQuery(capture(requestCapture))).andReturn(query); expect(query.execute()).andReturn(emptyResponse); replay(typeSystem, queryFactory, query); EntityResourceProvider provider = new EntityResourceProvider(typeSystem); provider.setQueryFactory(queryFactory); Map<String, Object> requestProperties = new HashMap<>(); requestProperties.put("id", "1"); Request request = new InstanceRequest(requestProperties); provider.getResourceById(request); verify(typeSystem, queryFactory, query); }