TermResourceProvider extends BaseResourceProvider implements ResourceProvider { public void createResource(Request request) throws InvalidPayloadException, ResourceAlreadyExistsException, ResourceNotFoundException { TermPath termPath = (TermPath) request.getQueryProperties().remove("termPath"); String qualifiedTermName = termPath.getFullyQualifiedName(); request.getQueryProperties().put("name", qualifiedTermName); resourceDefinition.validateCreatePayload(request); Request taxonomyRequest = new InstanceRequest( Collections.<String, Object>singletonMap("name", termPath.getTaxonomyName())); taxonomyRequest.addAdditionalSelectProperties(Collections.singleton("id")); Result taxonomyResult = getTaxonomyResourceProvider().getResourceById(taxonomyRequest); Map<String, Object> taxonomyPropertyMap = taxonomyResult.getPropertyMaps().iterator().next(); if (! termPath.getPath().equals("/")) { Map<String, Object> parentProperties = new HashMap<>(request.getQueryProperties()); parentProperties.put("termPath", termPath.getParent()); getResourceById(new InstanceRequest(parentProperties)); } typeSystem.createTraitType(resourceDefinition, qualifiedTermName, request.<String>getProperty("description")); typeSystem.createTraitInstance(String.valueOf(taxonomyPropertyMap.get("id")), qualifiedTermName, request.getQueryProperties()); } 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(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); TermPath termPath = new TermPath("testTaxonomy", null); replay(typeSystem, queryFactory, query); Map<String, Object> requestProperties = new HashMap<>(); requestProperties.put("termPath", termPath); Request userRequest = new InstanceRequest(requestProperties); TermResourceProvider provider = new TermResourceProvider(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); Capture<ResourceDefinition> resourceDefinitionCapture = newCapture(); ResourceProvider taxonomyResourceProvider = createStrictMock(TaxonomyResourceProvider.class); Capture<Request> taxonomyRequestCapture = newCapture(); Collection<Map<String, Object>> taxonomyQueryResult = new ArrayList<>(); Map<String, Object> taxonomyQueryResultRow = new HashMap<>(); taxonomyQueryResult.add(taxonomyQueryResultRow); taxonomyQueryResultRow.put("name", "testTaxonomy"); taxonomyQueryResultRow.put("id", "11-22-33"); Result taxonomyResult = new Result(taxonomyQueryResult); Map<String, Object> expectedRequestProps = new HashMap<>(); expectedRequestProps.put("name", "testTaxonomy.termName"); expectedRequestProps.put("available_as_tag", true); expect(taxonomyResourceProvider.getResourceById(capture(taxonomyRequestCapture))).andReturn(taxonomyResult); typeSystem.createTraitType(capture(resourceDefinitionCapture), eq("testTaxonomy.termName"), EasyMock.<String>isNull()); typeSystem.createTraitInstance("11-22-33", "testTaxonomy.termName", expectedRequestProps); replay(typeSystem, queryFactory, query, taxonomyResourceProvider); TermResourceProvider provider = new TestTermResourceProvider(typeSystem, taxonomyResourceProvider); provider.setQueryFactory(queryFactory); TermPath termPath = new TermPath("testTaxonomy", "termName"); Map<String, Object> requestProperties = new HashMap<>(); requestProperties.put("termPath", termPath); Request userRequest = new InstanceRequest(requestProperties); provider.createResource(userRequest); Request taxonomyRequest = taxonomyRequestCapture.getValue(); Map<String, Object> taxonomyRequestProps = taxonomyRequest.getQueryProperties(); assertEquals(taxonomyRequestProps.size(), 1); assertEquals(taxonomyRequestProps.get("name"), "testTaxonomy"); assertEquals(taxonomyRequest.getAdditionalSelectProperties().size(), 1); assertEquals(taxonomyRequest.getAdditionalSelectProperties().iterator().next(), "id"); assertNull(taxonomyRequest.getQueryString()); ResourceDefinition resourceDefinition = resourceDefinitionCapture.getValue(); assertEquals(resourceDefinition.getTypeName(), "Term"); verify(typeSystem, queryFactory, query, taxonomyResourceProvider); }