TermResourceProvider extends BaseResourceProvider implements ResourceProvider { @Override public void updateResourceById(Request request) throws ResourceNotFoundException, InvalidPayloadException { resourceDefinition.validateUpdatePayload(request); String termName = request.<TermPath>getProperty("termPath").getFullyQualifiedName(); request.getQueryProperties().put("name", termName); AtlasQuery atlasQuery; try { atlasQuery = queryFactory.createTermQuery(request); } catch (InvalidQueryException e) { throw new CatalogRuntimeException("Unable to compile internal Term query: " + e, e); } Map<String, Object> updateProperties = request.getUpdateProperties(); Collection<Map<String, Object>> results = atlasQuery.execute(updateProperties); if (results.isEmpty()) { throw new ResourceNotFoundException(String.format("Term '%s' not found.", termName)); } if (updateProperties.containsKey("description")) { String tagQueryString = String.format("name:%s", termName); Request tagRequest = new CollectionRequest( Collections.<String, Object>singletonMap("id", "*"), tagQueryString, null); AtlasQuery tagQuery; try { tagQuery = queryFactory.createEntityTagQuery(tagRequest); } catch (InvalidQueryException e) { throw new CatalogRuntimeException("Unable to compile internal Entity Tag query: " + e, e); } tagQuery.execute(Collections.singletonMap("description", updateProperties.get("description"))); } } 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 testUpdateResourceById() throws Exception { AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class); QueryFactory queryFactory = createStrictMock(QueryFactory.class); AtlasQuery query = createStrictMock(AtlasQuery.class); Capture<Request> termRequestCapture = newCapture(); Capture<Request> tagRequestCapture = newCapture(); TermPath termPath = new TermPath("testTaxonomy", "termName"); Map<String, Object> requestProperties = new HashMap<>(); requestProperties.put("termPath", termPath); 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.termName"); expect(queryFactory.createTermQuery(capture(termRequestCapture))).andReturn(query); expect(query.execute(requestUpdateProperties)).andReturn(queryResult); expect(queryFactory.createEntityTagQuery(capture(tagRequestCapture))).andReturn(query); expect(query.execute(requestUpdateProperties)).andReturn(null); replay(typeSystem, queryFactory, query); TermResourceProvider provider = new TermResourceProvider(typeSystem); provider.setQueryFactory(queryFactory); provider.updateResourceById(userRequest); Request request = termRequestCapture.getValue(); assertNull(request.getQueryString()); assertTrue(request.getAdditionalSelectProperties().isEmpty()); assertEquals(request.getQueryProperties().size(), 2); assertEquals(request.getQueryProperties().get("termPath"), termPath); assertEquals(request.getQueryProperties().get("name"), termPath.getFullyQualifiedName()); Request tagRequest = tagRequestCapture.getValue(); assertEquals(tagRequest.getQueryString(), "name:testTaxonomy.termName"); assertEquals(tagRequest.getQueryProperties().size(), 1); assertEquals(tagRequest.getQueryProperties().get("id"), "*"); verify(typeSystem, queryFactory, query); }