EntityTagResourceProvider extends BaseResourceProvider implements ResourceProvider { @Override public Collection<String> createResources(Request request) throws InvalidQueryException, ResourceNotFoundException, ResourceAlreadyExistsException { Collection<String> relativeUrls = new ArrayList<>(); AtlasQuery atlasQuery = queryFactory.createEntityQuery(request); Collection<String> guids = new ArrayList<>(); for (Map<String, Object> entityMap: atlasQuery.execute()) { guids.add(String.valueOf(entityMap.get("id"))); } Collection<Map<String, String>> tagMaps = request.getProperty("tags"); for (Map<String, String> tagMap : tagMaps) { Result termResult = getTermQueryResult(tagMap.get("name")); relativeUrls.addAll(tagEntities(guids, termResult.getPropertyMaps().iterator().next())); } return relativeUrls; } EntityTagResourceProvider(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);  }
@Test public void testCreateResources() throws Exception { AtlasTypeSystem typeSystem = createMock(AtlasTypeSystem.class); QueryFactory queryFactory = createStrictMock(QueryFactory.class); AtlasQuery entityQuery = createMock(AtlasQuery.class); ResourceProvider termResourceProvider = createMock(TermResourceProvider.class); Capture<Request> entityRequestCapture = newCapture(); Capture<Request> termRequestCapture1 = newCapture(); Capture<Request> termRequestCapture2 = newCapture(); Collection<Map<String, Object>> entityQueryResult = new ArrayList<>(); Map<String, Object> entityQueryResultRow = new HashMap<>(); entityQueryResultRow.put("id", "1"); entityQueryResult.add(entityQueryResultRow); Map<String, Object> entityQueryResultRow2 = new HashMap<>(); entityQueryResultRow2.put("id", "2"); entityQueryResult.add(entityQueryResultRow2); Collection<Map<String, Object>> termQueryResult1 = new ArrayList<>(); Map<String, Object> termQueryResultRow1 = new HashMap<>(); termQueryResult1.add(termQueryResultRow1); termQueryResultRow1.put("name", "testTaxonomy.termName1"); termQueryResultRow1.put("type", "testTaxonomy.termName1"); termQueryResultRow1.put("available_as_tag", true); termQueryResultRow1.put("description", "term description"); Result termResult1 = new Result(termQueryResult1); Collection<Map<String, Object>> termQueryResult2 = new ArrayList<>(); Map<String, Object> termQueryResultRow2 = new HashMap<>(); termQueryResult2.add(termQueryResultRow2); termQueryResultRow2.put("name", "testTaxonomy.termName2"); termQueryResultRow2.put("type", "testTaxonomy.termName2"); termQueryResultRow2.put("available_as_tag", true); termQueryResultRow2.put("description", "term 2 description"); Result termResult2 = new Result(termQueryResult2); expect(queryFactory.createEntityQuery(capture(entityRequestCapture))).andReturn(entityQuery); expect(entityQuery.execute()).andReturn(entityQueryResult); expect(termResourceProvider.getResourceById(capture(termRequestCapture1))).andReturn(termResult1); expect(termResourceProvider.getResourceById(capture(termRequestCapture2))).andReturn(termResult2); Map<String, Object> tagProperties1 = new HashMap<>(); tagProperties1.put("name", "testTaxonomy.termName1"); tagProperties1.put("description", "term description"); typeSystem.createTraitInstance("1", "testTaxonomy.termName1", tagProperties1); typeSystem.createTraitInstance("2", "testTaxonomy.termName1", tagProperties1); Map<String, Object> tagProperties2 = new HashMap<>(); tagProperties2.put("name", "testTaxonomy.termName2"); tagProperties2.put("description", "term 2 description"); typeSystem.createTraitInstance("1", "testTaxonomy.termName2", tagProperties2); typeSystem.createTraitInstance("2", "testTaxonomy.termName2", tagProperties2); replay(typeSystem, queryFactory, entityQuery, termResourceProvider); EntityTagResourceProvider provider = new TestEntityTagResourceProvider(typeSystem, termResourceProvider); provider.setQueryFactory(queryFactory); Map<String, Object> requestProps = new HashMap<>(); Collection<Map<String, String>> tagMaps = new ArrayList<>(); requestProps.put("tags", tagMaps); Map<String, String> tagMap1 = new HashMap<>(); tagMap1.put("name", "testTaxonomy.termName1"); tagMaps.add(tagMap1); Map<String, String> tagMap2 = new HashMap<>(); tagMap2.put("name", "testTaxonomy.termName2"); tagMaps.add(tagMap2); Request userRequest = new CollectionRequest(requestProps, "name:foo*"); Collection<String> createResult = provider.createResources(userRequest); assertEquals(4, createResult.size()); assertTrue(createResult.contains("v1/entities/1/tags/testTaxonomy.termName1")); assertTrue(createResult.contains("v1/entities/1/tags/testTaxonomy.termName2")); assertTrue(createResult.contains("v1/entities/2/tags/testTaxonomy.termName1")); assertTrue(createResult.contains("v1/entities/2/tags/testTaxonomy.termName2")); Request entityRequest = entityRequestCapture.getValue(); assertEquals("name:foo*", entityRequest.getQueryString()); assertEquals(Request.Cardinality.COLLECTION, entityRequest.getCardinality()); Request termRequest1 = termRequestCapture1.getValue(); assertNull(termRequest1.getQueryString()); assertEquals(Request.Cardinality.INSTANCE, termRequest1.getCardinality()); Map<String, Object> termRequestProps = termRequest1.getQueryProperties(); assertEquals(1, termRequestProps.size()); TermPath termPath = (TermPath) termRequestProps.get("termPath"); assertEquals("testTaxonomy.termName1", termPath.getFullyQualifiedName()); Request termRequest2 = termRequestCapture2.getValue(); assertNull(termRequest2.getQueryString()); assertEquals(Request.Cardinality.INSTANCE, termRequest2.getCardinality()); Map<String, Object> termRequestProps2 = termRequest2.getQueryProperties(); assertEquals(1, termRequestProps2.size()); TermPath termPath2 = (TermPath) termRequestProps2.get("termPath"); assertEquals("testTaxonomy.termName2", termPath2.getFullyQualifiedName()); verify(typeSystem, queryFactory, entityQuery, termResourceProvider); }