TaxonomyResourceProvider extends BaseResourceProvider implements ResourceProvider { @Override public Result getResourceById(Request request) throws ResourceNotFoundException { synchronized (TaxonomyResourceProvider.class) { createDefaultTaxonomyIfNeeded(); } return doGetResourceById(request); } TaxonomyResourceProvider(AtlasTypeSystem typeSystem); @Override Result getResourceById(Request request); @Override Result getResources(Request request); @Override void createResource(Request request); @Override Collection<String> createResources(Request request); @Override void deleteResourceById(Request request); @Override void updateResourceById(Request request); static final String DEFAULT_TAXONOMY_NAME; static final String DEFAULT_TAXONOMY_DESCRIPTION; static final String NAMESPACE_ATTRIBUTE_NAME; static final String TAXONOMY_TERM_TYPE; static final String TAXONOMY_NS; }
@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(); Collection<Map<String, Object>> queryResult = new ArrayList<>(); Map<String, Object> queryResultRow = new HashMap<>(); queryResult.add(queryResultRow); queryResultRow.put("name", "taxonomyName"); queryResultRow.put("description", "test taxonomy description"); queryResultRow.put("creation_time", "04/20/2016"); expect(queryFactory.createTaxonomyQuery(capture(requestCapture))).andReturn(query); expect(query.execute()).andReturn(queryResult); replay(typeSystem, queryFactory, query); TaxonomyResourceProvider provider = new TestTaxonomyResourceProvider(typeSystem); provider.setQueryFactory(queryFactory); Map<String, Object> requestProperties = new HashMap<>(); requestProperties.put("name", "taxonomyName"); 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 public void testGetResourceById_notInitialized_createDefaultTaxonomy() throws Exception { AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class); QueryFactory queryFactory = createStrictMock(QueryFactory.class); AtlasQuery query = createStrictMock(AtlasQuery.class); Capture<Request> checkForAnyTaxonomiesCapture = newCapture(); Capture<Request> createDefaultTaxonomyRequestCapture = newCapture(); Capture<Request> requestCapture = newCapture(); Capture<ResourceDefinition> resourceDefinitionCapture = newCapture(); Collection<Map<String, Object>> queryResult = new ArrayList<>(); Map<String, Object> queryResultRow = new HashMap<>(); queryResult.add(queryResultRow); queryResultRow.put("name", "taxonomyName"); queryResultRow.put("description", "test taxonomy description"); queryResultRow.put("creation_time", "04/20/2016"); expect(queryFactory.createTaxonomyQuery(capture(checkForAnyTaxonomiesCapture))).andReturn(query); expect(query.execute()).andReturn(Collections.<Map<String, Object>>emptySet()); expect(typeSystem.createEntity(capture(resourceDefinitionCapture), capture(createDefaultTaxonomyRequestCapture))).andReturn("testGuid"); expect(queryFactory.createTaxonomyQuery(capture(requestCapture))).andReturn(query); expect(query.execute()).andReturn(queryResult); replay(typeSystem, queryFactory, query); TestTaxonomyResourceProvider provider = new TestTaxonomyResourceProvider(typeSystem); provider.setInitialized(false); provider.setQueryFactory(queryFactory); Map<String, Object> requestProperties = new HashMap<>(); requestProperties.put("name", "taxonomyName"); 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()); Request checkForAnyTaxonomiesRequest = checkForAnyTaxonomiesCapture.getValue(); assertNull(checkForAnyTaxonomiesRequest.getQueryString()); assertEquals(checkForAnyTaxonomiesRequest.getAdditionalSelectProperties().size(), 0); assertEquals(checkForAnyTaxonomiesRequest.getQueryProperties().size(), 0); Request createDefaultTaxonomyRequest = createDefaultTaxonomyRequestCapture.getValue(); assertNull(createDefaultTaxonomyRequest.getQueryString()); assertEquals(createDefaultTaxonomyRequest.getAdditionalSelectProperties().size(), 0); assertEquals(createDefaultTaxonomyRequest.getQueryProperties().size(), 2); assertEquals(createDefaultTaxonomyRequest.getQueryProperties().get("name"), TaxonomyResourceProvider.DEFAULT_TAXONOMY_NAME); assertEquals(createDefaultTaxonomyRequest.getQueryProperties().get("description"), TaxonomyResourceProvider.DEFAULT_TAXONOMY_DESCRIPTION); verify(typeSystem, queryFactory, query); }
@Test public void testGetResourceById_notInitialized_taxonomyAlreadyExists() throws Exception { AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class); QueryFactory queryFactory = createStrictMock(QueryFactory.class); AtlasQuery query = createStrictMock(AtlasQuery.class); Capture<Request> checkForAnyTaxonomiesCapture = newCapture(); Capture<Request> requestCapture = newCapture(); Collection<Map<String, Object>> queryResult = new ArrayList<>(); Map<String, Object> queryResultRow = new HashMap<>(); queryResult.add(queryResultRow); queryResultRow.put("name", "taxonomyName"); queryResultRow.put("description", "test taxonomy description"); queryResultRow.put("creation_time", "04/20/2016"); expect(queryFactory.createTaxonomyQuery(capture(checkForAnyTaxonomiesCapture))).andReturn(query); expect(query.execute()).andReturn(queryResult); expect(queryFactory.createTaxonomyQuery(capture(requestCapture))).andReturn(query); expect(query.execute()).andReturn(queryResult); replay(typeSystem, queryFactory, query); TestTaxonomyResourceProvider provider = new TestTaxonomyResourceProvider(typeSystem); provider.setInitialized(false); provider.setQueryFactory(queryFactory); Map<String, Object> requestProperties = new HashMap<>(); requestProperties.put("name", "taxonomyName"); 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 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.createTaxonomyQuery(capture(requestCapture))).andReturn(query); expect(query.execute()).andReturn(emptyResponse); replay(typeSystem, queryFactory, query); TaxonomyResourceProvider provider = new TestTaxonomyResourceProvider(typeSystem); provider.setQueryFactory(queryFactory); Map<String, Object> requestProperties = new HashMap<>(); requestProperties.put("name", "taxonomyName"); Request request = new InstanceRequest(requestProperties); provider.getResourceById(request); verify(typeSystem, queryFactory, query); }