EntityTagResourceProvider extends BaseResourceProvider implements ResourceProvider { @Override public void createResource(Request request) throws InvalidPayloadException, ResourceAlreadyExistsException, ResourceNotFoundException { String entityId = String.valueOf(request.getQueryProperties().remove("id")); resourceDefinition.validateCreatePayload(request); Result termResult = getTermQueryResult(request.<String>getProperty("name")); Map<String, Object> termProperties = termResult.getPropertyMaps().iterator().next(); if (String.valueOf(termProperties.get("available_as_tag")).equals("false")) { throw new InvalidPayloadException( "Attempted to tag an entity with a term which is not available to be tagged"); } tagEntities(Collections.singleton(entityId), termProperties); } 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(expectedExceptions = InvalidPayloadException.class) public void testCreateResource_invalidRequest__noName() throws Exception { AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class); QueryFactory queryFactory = createStrictMock(QueryFactory.class); AtlasQuery query = createStrictMock(AtlasQuery.class); replay(typeSystem, queryFactory, query); Map<String, Object> requestProperties = new HashMap<>(); requestProperties.put("description", "description"); Request userRequest = new InstanceRequest(requestProperties); EntityTagResourceProvider provider = new EntityTagResourceProvider(typeSystem); provider.setQueryFactory(queryFactory); provider.createResource(userRequest); }
@Test public void testCreateResource() throws Exception { AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class); QueryFactory queryFactory = createStrictMock(QueryFactory.class); AtlasQuery query = createStrictMock(AtlasQuery.class); ResourceProvider termResourceProvider = createStrictMock(TermResourceProvider.class); Capture<Request> termRequestCapture = newCapture(); Collection<Map<String, Object>> termQueryResult = new ArrayList<>(); Map<String, Object> termQueryResultRow = new HashMap<>(); termQueryResult.add(termQueryResultRow); termQueryResultRow.put("name", "testTaxonomy.termName"); termQueryResultRow.put("type", "testTaxonomy.termName"); termQueryResultRow.put("available_as_tag", true); termQueryResultRow.put("description", "term description"); Result termResult = new Result(termQueryResult); expect(termResourceProvider.getResourceById(capture(termRequestCapture))).andReturn(termResult); Map<String, Object> tagProperties = new HashMap<>(); tagProperties.put("name", "testTaxonomy.termName"); tagProperties.put("description", "term description"); typeSystem.createTraitInstance("11-22-33", "testTaxonomy.termName", tagProperties); replay(typeSystem, queryFactory, query, termResourceProvider); EntityTagResourceProvider provider = new TestEntityTagResourceProvider(typeSystem, termResourceProvider); provider.setQueryFactory(queryFactory); Map<String, Object> requestProperties = new HashMap<>(); requestProperties.put("name", "testTaxonomy.termName"); requestProperties.put("id", "11-22-33"); Request userRequest = new InstanceRequest(requestProperties); provider.createResource(userRequest); Request termRequest = termRequestCapture.getValue(); Map<String, Object> termRequestProps = termRequest.getQueryProperties(); assertEquals(1, termRequestProps.size()); TermPath termPath = (TermPath) termRequestProps.get("termPath"); assertEquals("testTaxonomy.termName", termPath.getFullyQualifiedName()); assertEquals(1, termRequest.getAdditionalSelectProperties().size()); assertEquals("type", termRequest.getAdditionalSelectProperties().iterator().next()); assertNull(termRequest.getQueryString()); verify(typeSystem, queryFactory, query, termResourceProvider); }