TermResourceProvider extends BaseResourceProvider implements ResourceProvider { @Override public void deleteResourceById(Request request) throws ResourceNotFoundException, InvalidPayloadException { getResourceById(request); TermPath termPath = (TermPath) request.getQueryProperties().get("termPath"); String taxonomyId = getTaxonomyId(termPath); deleteChildren(taxonomyId, termPath); deleteTerm(taxonomyId, termPath); } 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 testDeleteResourceById() throws Exception { ResourceProvider taxonomyResourceProvider = createStrictMock(ResourceProvider.class); ResourceProvider entityResourceProvider = createStrictMock(ResourceProvider.class); ResourceProvider entityTagResourceProvider = createStrictMock(ResourceProvider.class); AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class); QueryFactory queryFactory = createStrictMock(QueryFactory.class); AtlasQuery query = createStrictMock(AtlasQuery.class); Capture<Request> taxonomyRequestCapture = newCapture(); Capture<Request> termRequestCapture = newCapture(); TermPath termPath = new TermPath("testTaxonomy.termName"); Request entityRequest1 = new CollectionRequest(Collections.<String, Object>emptyMap(), "tags/name:testTaxonomy.termName.child1"); Request entityRequest2 = new CollectionRequest(Collections.<String, Object>emptyMap(), "tags/name:testTaxonomy.termName.child2"); Request entityRequest3 = new CollectionRequest(Collections.<String, Object>emptyMap(), "tags/name:testTaxonomy.termName"); Map<String, Object> entityTagRequestMap1 = new HashMap<>(); entityTagRequestMap1.put("id", "111"); entityTagRequestMap1.put("name", "testTaxonomy.termName.child1"); Request entityTagRequest1 = new InstanceRequest(entityTagRequestMap1); Map<String, Object> entityTagRequestMap2 = new HashMap<>(); entityTagRequestMap2.put("id", "222"); entityTagRequestMap2.put("name", "testTaxonomy.termName.child1"); Request entityTagRequest2 = new InstanceRequest(entityTagRequestMap2); Map<String, Object> entityTagRequestMap3 = new HashMap<>(); entityTagRequestMap3.put("id", "333"); entityTagRequestMap3.put("name", "testTaxonomy.termName.child2"); Request entityTagRequest3 = new InstanceRequest(entityTagRequestMap3); Map<String, Object> requestProperties = new HashMap<>(); requestProperties.put("termPath", termPath); Request userRequest = new InstanceRequest(requestProperties); Collection<Map<String, Object>> queryResult = new ArrayList<>(); Map<String, Object> queryResultRow = new HashMap<>(); queryResult.add(queryResultRow); queryResultRow.put("name", "testTaxonomy.termName"); queryResultRow.put("id", "111-222-333"); Collection<Map<String, Object>> taxonomyResultMaps = new ArrayList<>(); Map<String, Object> taxonomyResultMap = new HashMap<>(); taxonomyResultMap.put("name", "testTaxonomy"); taxonomyResultMap.put("id", "12345"); taxonomyResultMaps.add(taxonomyResultMap); Result taxonomyResult = new Result(taxonomyResultMaps); Collection<Map<String, Object>> childResult = new ArrayList<>(); Map<String, Object> childResultRow = new HashMap<>(); childResult.add(childResultRow); childResultRow.put("name", "testTaxonomy.termName.child1"); childResultRow.put("id", "1-1-1"); Map<String, Object> childResultRow2 = new HashMap<>(); childResult.add(childResultRow2); childResultRow2.put("name", "testTaxonomy.termName.child2"); childResultRow2.put("id", "2-2-2"); Collection<Map<String, Object>> entityResults1 = new ArrayList<>(); Map<String, Object> entityResult1Map1 = new HashMap<>(); entityResult1Map1.put("name", "entity1"); entityResult1Map1.put("id", "111"); entityResults1.add(entityResult1Map1); Map<String, Object> entityResult1Map2 = new HashMap<>(); entityResult1Map2.put("name", "entity2"); entityResult1Map2.put("id", "222"); entityResults1.add(entityResult1Map2); Result entityResult1 = new Result(entityResults1); Collection<Map<String, Object>> entityResults2 = new ArrayList<>(); Map<String, Object> entityResult2Map = new HashMap<>(); entityResult2Map.put("name", "entity3"); entityResult2Map.put("id", "333"); entityResults2.add(entityResult2Map); Result entityResult2 = new Result(entityResults2); expect(queryFactory.createTermQuery(userRequest)).andReturn(query); expect(query.execute()).andReturn(queryResult); expect(taxonomyResourceProvider.getResourceById(capture(taxonomyRequestCapture))).andReturn(taxonomyResult); expect(queryFactory.createTermQuery(capture(termRequestCapture))).andReturn(query); expect(query.execute()).andReturn(childResult); expect(entityResourceProvider.getResources(eq(entityRequest1))).andReturn(entityResult1); entityTagResourceProvider.deleteResourceById(entityTagRequest1); entityTagResourceProvider.deleteResourceById(entityTagRequest2); typeSystem.deleteTag("12345", "testTaxonomy.termName.child1"); expect(entityResourceProvider.getResources(eq(entityRequest2))).andReturn(entityResult2); entityTagResourceProvider.deleteResourceById(entityTagRequest3); typeSystem.deleteTag("12345", "testTaxonomy.termName.child2"); expect(entityResourceProvider.getResources(eq(entityRequest3))).andReturn( new Result(Collections.<Map<String, Object>>emptyList())); typeSystem.deleteTag("12345", "testTaxonomy.termName"); replay(taxonomyResourceProvider, entityResourceProvider, entityTagResourceProvider, typeSystem, queryFactory, query); TermResourceProvider provider = new TestTermResourceProvider( typeSystem, taxonomyResourceProvider, entityResourceProvider, entityTagResourceProvider); provider.setQueryFactory(queryFactory); provider.deleteResourceById(userRequest); Request taxonomyRequest = taxonomyRequestCapture.getValue(); assertEquals(taxonomyRequest.getQueryProperties().get("name"), "testTaxonomy"); assertEquals(taxonomyRequest.getAdditionalSelectProperties().size(), 1); assertTrue(taxonomyRequest.getAdditionalSelectProperties().contains("id")); Request childTermRequest = termRequestCapture.getValue(); assertEquals(childTermRequest.<TermPath>getProperty("termPath").getFullyQualifiedName(), "testTaxonomy.termName."); verify(taxonomyResourceProvider, entityResourceProvider, entityTagResourceProvider, typeSystem, queryFactory, query); }