TaxonomyResourceProvider extends BaseResourceProvider implements ResourceProvider { @Override public void updateResourceById(Request request) throws ResourceNotFoundException, InvalidPayloadException { resourceDefinition.validateUpdatePayload(request); AtlasQuery atlasQuery; try { atlasQuery = queryFactory.createTaxonomyQuery(request); } catch (InvalidQueryException e) { throw new CatalogRuntimeException("Unable to compile internal Term query: " + e, e); } synchronized (TaxonomyResourceProvider.class) { createDefaultTaxonomyIfNeeded(); if (atlasQuery.execute(request.getUpdateProperties()).isEmpty()) { throw new ResourceNotFoundException(String.format("Taxonomy '%s' not found.", request.getQueryProperties().get("name"))); } } } 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 testUpdateResourceById() throws Exception { AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class); QueryFactory queryFactory = createStrictMock(QueryFactory.class); AtlasQuery query = createStrictMock(AtlasQuery.class); Capture<Request> taxonomyRequestCapture = newCapture(); Map<String, Object> requestProperties = new HashMap<>(); requestProperties.put("name", "testTaxonomy"); Map<String, Object> requestUpdateProperties = new HashMap<>(); requestUpdateProperties.put("description", "updatedValue"); Request userRequest = new InstanceRequest(requestProperties, requestUpdateProperties); Collection<Map<String, Object>> queryResult = new ArrayList<>(); Map<String, Object> queryResultRow = new HashMap<>(); queryResult.add(queryResultRow); queryResultRow.put("name", "testTaxonomy"); expect(queryFactory.createTaxonomyQuery(capture(taxonomyRequestCapture))).andReturn(query); expect(query.execute(requestUpdateProperties)).andReturn(queryResult); replay(typeSystem, queryFactory, query); TaxonomyResourceProvider provider = new TestTaxonomyResourceProvider(typeSystem); provider.setQueryFactory(queryFactory); provider.updateResourceById(userRequest); Request request = taxonomyRequestCapture.getValue(); assertNull(request.getQueryString()); assertEquals(request.getQueryProperties().size(), 1); assertEquals(request.getQueryProperties().get("name"), "testTaxonomy"); assertEquals(request.getUpdateProperties().size(), 1); assertEquals(request.getUpdateProperties().get("description"), "updatedValue"); verify(typeSystem, queryFactory, query); }
@Test(expectedExceptions = InvalidPayloadException.class) public void testUpdateResourceById_attemptNameChange() throws Exception { AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class); QueryFactory queryFactory = createStrictMock(QueryFactory.class); AtlasQuery query = createStrictMock(AtlasQuery.class); Capture<Request> taxonomyRequestCapture = newCapture(); Map<String, Object> requestProperties = new HashMap<>(); requestProperties.put("name", "testTaxonomy"); Map<String, Object> requestUpdateProperties = new HashMap<>(); requestUpdateProperties.put("name", "notCurrentlySupported"); Request userRequest = new InstanceRequest(requestProperties, requestUpdateProperties); Collection<Map<String, Object>> queryResult = new ArrayList<>(); Map<String, Object> queryResultRow = new HashMap<>(); queryResult.add(queryResultRow); queryResultRow.put("name", "testTaxonomy"); expect(queryFactory.createTaxonomyQuery(capture(taxonomyRequestCapture))).andReturn(query); expect(query.execute(requestUpdateProperties)).andReturn(queryResult); replay(typeSystem, queryFactory, query); TaxonomyResourceProvider provider = new TestTaxonomyResourceProvider(typeSystem); provider.setQueryFactory(queryFactory); provider.updateResourceById(userRequest); verify(typeSystem, queryFactory, query); }
@Test(expectedExceptions = ResourceNotFoundException.class) public void testUpdateResourceById_404() throws Exception { AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class); QueryFactory queryFactory = createStrictMock(QueryFactory.class); AtlasQuery query = createStrictMock(AtlasQuery.class); Capture<Request> taxonomyRequestCapture = newCapture(); Map<String, Object> requestProperties = new HashMap<>(); requestProperties.put("name", "testTaxonomy"); Map<String, Object> requestUpdateProperties = new HashMap<>(); requestUpdateProperties.put("description", "updated"); Request userRequest = new InstanceRequest(requestProperties, requestUpdateProperties); expect(queryFactory.createTaxonomyQuery(capture(taxonomyRequestCapture))).andReturn(query); expect(query.execute(requestUpdateProperties)).andReturn(Collections.<Map<String, Object>>emptyList()); replay(typeSystem, queryFactory, query); TaxonomyResourceProvider provider = new TestTaxonomyResourceProvider(typeSystem); provider.setQueryFactory(queryFactory); provider.updateResourceById(userRequest); verify(typeSystem, queryFactory, query); }