TermResourceProvider extends BaseResourceProvider implements ResourceProvider { @Override public Result getResourceById(Request request) throws ResourceNotFoundException { request.getQueryProperties().put("name", request.<TermPath>getProperty("termPath").getFullyQualifiedName()); AtlasQuery atlasQuery; try { atlasQuery = queryFactory.createTermQuery(request); } catch (InvalidQueryException e) { throw new CatalogRuntimeException("Unable to compile internal Term query: " + e, e); } Collection<Map<String, Object>> results = atlasQuery.execute(); if (results.isEmpty()) { throw new ResourceNotFoundException(String.format("Term '%s' not found.", request.<TermPath>getProperty("termPath").getFullyQualifiedName())); } return new Result(results); } TermResourceProvider(AtlasTypeSystem typeSystem); @Override Result getResourceById(Request request); Result getResources(Request request); void createResource(Request request); @Override Collection<String> createResources(Request request); @Override void updateResourceById(Request request); @Override void deleteResourceById(Request request);  }
@Test public void testGetResourceById() throws Exception { AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class); QueryFactory queryFactory = createStrictMock(QueryFactory.class); AtlasQuery query = createStrictMock(AtlasQuery.class); Capture<Request> requestCapture = newCapture(); TermPath termPath = new TermPath("testTaxonomy", "termName"); Collection<Map<String, Object>> queryResult = new ArrayList<>(); Map<String, Object> queryResultRow = new HashMap<>(); queryResult.add(queryResultRow); queryResultRow.put("name", "testTaxonomy.termName"); queryResultRow.put("description", "test term description"); queryResultRow.put("creation_time", "04/20/2016"); queryResultRow.put("acceptable_use", "anything"); queryResultRow.put("available_as_tag", true); Map<String, Object> hierarchyMap = new HashMap<>(); queryResultRow.put("hierarchy", hierarchyMap); hierarchyMap.put("path", "/"); hierarchyMap.put("short_name", "termName"); hierarchyMap.put("taxonomy", "testTaxonomy"); expect(queryFactory.createTermQuery(capture(requestCapture))).andReturn(query); expect(query.execute()).andReturn(queryResult); replay(typeSystem, queryFactory, query); TermResourceProvider provider = new TermResourceProvider(typeSystem); provider.setQueryFactory(queryFactory); Map<String, Object> requestProperties = new HashMap<>(); requestProperties.put("termPath", termPath); Request userRequest = new InstanceRequest(requestProperties); Result result = provider.getResourceById(userRequest); assertEquals(result.getPropertyMaps().size(), 1); assertEquals(result.getPropertyMaps().iterator().next(), queryResultRow); Request request = requestCapture.getValue(); assertNull(request.getQueryString()); assertEquals(request.getAdditionalSelectProperties().size(), 0); assertEquals(request.getQueryProperties().size(), 2); assertEquals(request.getQueryProperties().get("termPath"), termPath); assertEquals(request.getQueryProperties().get("name"), termPath.getFullyQualifiedName()); verify(typeSystem, queryFactory, query); }
@Test(expectedExceptions = ResourceNotFoundException.class) public void testGetResourceById_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.createTermQuery(capture(requestCapture))).andReturn(query); expect(query.execute()).andReturn(emptyResponse); replay(typeSystem, queryFactory, query); TermResourceProvider provider = new TermResourceProvider(typeSystem); provider.setQueryFactory(queryFactory); Map<String, Object> requestProperties = new HashMap<>(); requestProperties.put("termPath", new TermPath("taxonomyName.badTermName")); Request request = new InstanceRequest(requestProperties); provider.getResourceById(request); }