use of org.apache.atlas.catalog.query.AtlasQuery in project incubator-atlas by apache.
the class TaxonomyResourceProviderTest method testGetResources_noResults.
@Test
public void testGetResources_noResults() throws Exception {
AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class);
QueryFactory queryFactory = createStrictMock(QueryFactory.class);
AtlasQuery query = createStrictMock(AtlasQuery.class);
Capture<Request> requestCapture = newCapture();
// empty result shouldn't result in exception for collection query
Collection<Map<String, Object>> queryResult = new ArrayList<>();
// mock expectations
expect(queryFactory.createTaxonomyQuery(capture(requestCapture))).andReturn(query);
expect(query.execute()).andReturn(queryResult);
replay(typeSystem, queryFactory, query);
TaxonomyResourceProvider provider = new TestTaxonomyResourceProvider(typeSystem);
provider.setQueryFactory(queryFactory);
Request userRequest = new CollectionRequest(Collections.<String, Object>emptyMap(), "name:taxonomy*");
Result result = provider.getResources(userRequest);
assertEquals(0, result.getPropertyMaps().size());
Request request = requestCapture.getValue();
assertEquals("name:taxonomy*", request.getQueryString());
assertEquals(0, request.getAdditionalSelectProperties().size());
assertEquals(0, request.getQueryProperties().size());
verify(typeSystem, queryFactory, query);
}
use of org.apache.atlas.catalog.query.AtlasQuery in project incubator-atlas by apache.
the class TaxonomyResourceProviderTest method testGetResourceById_notInitialized_createDefaultTaxonomy.
@Test
public void testGetResourceById_notInitialized_createDefaultTaxonomy() throws Exception {
AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class);
QueryFactory queryFactory = createStrictMock(QueryFactory.class);
AtlasQuery query = createStrictMock(AtlasQuery.class);
Capture<Request> checkForAnyTaxonomiesCapture = newCapture();
Capture<Request> createDefaultTaxonomyRequestCapture = newCapture();
Capture<Request> requestCapture = newCapture();
Capture<ResourceDefinition> resourceDefinitionCapture = newCapture();
Collection<Map<String, Object>> queryResult = new ArrayList<>();
Map<String, Object> queryResultRow = new HashMap<>();
queryResult.add(queryResultRow);
queryResultRow.put("name", "taxonomyName");
queryResultRow.put("description", "test taxonomy description");
queryResultRow.put("creation_time", "04/20/2016");
// mock expectations
expect(queryFactory.createTaxonomyQuery(capture(checkForAnyTaxonomiesCapture))).andReturn(query);
expect(query.execute()).andReturn(Collections.<Map<String, Object>>emptySet());
expect(typeSystem.createEntity(capture(resourceDefinitionCapture), capture(createDefaultTaxonomyRequestCapture))).andReturn("testGuid");
expect(queryFactory.createTaxonomyQuery(capture(requestCapture))).andReturn(query);
expect(query.execute()).andReturn(queryResult);
replay(typeSystem, queryFactory, query);
TestTaxonomyResourceProvider provider = new TestTaxonomyResourceProvider(typeSystem);
provider.setInitialized(false);
provider.setQueryFactory(queryFactory);
Map<String, Object> requestProperties = new HashMap<>();
requestProperties.put("name", "taxonomyName");
Request userRequest = new InstanceRequest(requestProperties);
Result result = provider.getResourceById(userRequest);
assertEquals(1, result.getPropertyMaps().size());
assertEquals(queryResultRow, result.getPropertyMaps().iterator().next());
Request request = requestCapture.getValue();
assertNull(request.getQueryString());
assertEquals(0, request.getAdditionalSelectProperties().size());
assertEquals(requestProperties, request.getQueryProperties());
Request checkForAnyTaxonomiesRequest = checkForAnyTaxonomiesCapture.getValue();
assertNull(checkForAnyTaxonomiesRequest.getQueryString());
assertEquals(checkForAnyTaxonomiesRequest.getAdditionalSelectProperties().size(), 0);
assertEquals(checkForAnyTaxonomiesRequest.getQueryProperties().size(), 0);
Request createDefaultTaxonomyRequest = createDefaultTaxonomyRequestCapture.getValue();
assertNull(createDefaultTaxonomyRequest.getQueryString());
assertEquals(createDefaultTaxonomyRequest.getAdditionalSelectProperties().size(), 0);
assertEquals(createDefaultTaxonomyRequest.getQueryProperties().size(), 2);
assertEquals(createDefaultTaxonomyRequest.getQueryProperties().get("name"), TaxonomyResourceProvider.DEFAULT_TAXONOMY_NAME);
assertEquals(createDefaultTaxonomyRequest.getQueryProperties().get("description"), TaxonomyResourceProvider.DEFAULT_TAXONOMY_DESCRIPTION);
verify(typeSystem, queryFactory, query);
}
use of org.apache.atlas.catalog.query.AtlasQuery in project incubator-atlas by apache.
the class TermResourceProviderTest method testGetResources.
@Test
public void testGetResources() throws Exception {
AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class);
QueryFactory queryFactory = createStrictMock(QueryFactory.class);
AtlasQuery query = createStrictMock(AtlasQuery.class);
Capture<Request> requestCapture = newCapture();
TermPath termPath = new TermPath("testTaxonomy", null);
Collection<Map<String, Object>> queryResult = new ArrayList<>();
Map<String, Object> queryResultRow1 = new HashMap<>();
queryResult.add(queryResultRow1);
queryResultRow1.put("name", "testTaxonomy.termName");
queryResultRow1.put("description", "test term description");
queryResultRow1.put("creation_time", "04/20/2016");
queryResultRow1.put("acceptable_use", "anything");
queryResultRow1.put("available_as_tag", true);
Map<String, Object> hierarchyMap = new HashMap<>();
queryResultRow1.put("hierarchy", hierarchyMap);
hierarchyMap.put("path", "/");
hierarchyMap.put("short_name", "termName");
hierarchyMap.put("taxonomy", "testTaxonomy");
Map<String, Object> queryResultRow2 = new HashMap<>();
queryResult.add(queryResultRow2);
queryResultRow2.put("name", "testTaxonomy.termName2");
queryResultRow2.put("description", "test term 2 description");
queryResultRow2.put("creation_time", "04/21/2016");
queryResultRow2.put("acceptable_use", "anything");
queryResultRow2.put("available_as_tag", true);
Map<String, Object> hierarchyMap2 = new HashMap<>();
queryResultRow2.put("hierarchy", hierarchyMap2);
hierarchyMap2.put("path", "/");
hierarchyMap2.put("short_name", "termName2");
hierarchyMap2.put("taxonomy", "testTaxonomy");
// mock expectations
expect(queryFactory.createTermQuery(capture(requestCapture))).andReturn(query);
expect(query.execute()).andReturn(queryResult);
replay(typeSystem, queryFactory, query);
TermResourceProvider provider = new TermResourceProvider(typeSystem);
provider.setQueryFactory(queryFactory);
Map<String, Object> requestProperties = new HashMap<>();
requestProperties.put("termPath", termPath);
Request userRequest = new CollectionRequest(requestProperties, "name:taxonomy*");
// invoke test method
Result result = provider.getResources(userRequest);
assertEquals(result.getPropertyMaps().size(), 2);
assertTrue(result.getPropertyMaps().contains(queryResultRow1));
assertTrue(result.getPropertyMaps().contains(queryResultRow2));
Request request = requestCapture.getValue();
assertEquals(request.getQueryString(), "name:taxonomy*");
assertEquals(request.getAdditionalSelectProperties().size(), 0);
assertEquals(request.getQueryProperties().size(), 1);
verify(typeSystem, queryFactory, query);
}
use of org.apache.atlas.catalog.query.AtlasQuery in project incubator-atlas by apache.
the class TermResourceProviderTest method testCreateResource.
@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");
// when not specified, the default value of 'true' should be set
expectedRequestProps.put("available_as_tag", true);
// mock expectations
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);
}
use of org.apache.atlas.catalog.query.AtlasQuery in project incubator-atlas by apache.
the class TermResourceProviderTest method testDeleteResourceById.
@Test
public void testDeleteResourceById() throws Exception {
ResourceProvider taxonomyResourceProvider = createStrictMock(ResourceProvider.class);
ResourceProvider entityResourceProvider = createStrictMock(ResourceProvider.class);
ResourceProvider entityTagResourceProvider = createStrictMock(ResourceProvider.class);
AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class);
QueryFactory queryFactory = createStrictMock(QueryFactory.class);
AtlasQuery query = createStrictMock(AtlasQuery.class);
Capture<Request> taxonomyRequestCapture = newCapture();
Capture<Request> termRequestCapture = newCapture();
// root term being deleted
TermPath termPath = new TermPath("testTaxonomy.termName");
// entity requests to get id's of entities tagged with terms
Request entityRequest1 = new CollectionRequest(Collections.<String, Object>emptyMap(), "tags/name:testTaxonomy.termName.child1");
Request entityRequest2 = new CollectionRequest(Collections.<String, Object>emptyMap(), "tags/name:testTaxonomy.termName.child2");
Request entityRequest3 = new CollectionRequest(Collections.<String, Object>emptyMap(), "tags/name:testTaxonomy.termName");
// entity tag requests to delete entity tags
Map<String, Object> entityTagRequestMap1 = new HashMap<>();
entityTagRequestMap1.put("id", "111");
entityTagRequestMap1.put("name", "testTaxonomy.termName.child1");
Request entityTagRequest1 = new InstanceRequest(entityTagRequestMap1);
Map<String, Object> entityTagRequestMap2 = new HashMap<>();
entityTagRequestMap2.put("id", "222");
entityTagRequestMap2.put("name", "testTaxonomy.termName.child1");
Request entityTagRequest2 = new InstanceRequest(entityTagRequestMap2);
Map<String, Object> entityTagRequestMap3 = new HashMap<>();
entityTagRequestMap3.put("id", "333");
entityTagRequestMap3.put("name", "testTaxonomy.termName.child2");
Request entityTagRequest3 = new InstanceRequest(entityTagRequestMap3);
Map<String, Object> requestProperties = new HashMap<>();
requestProperties.put("termPath", termPath);
Request userRequest = new InstanceRequest(requestProperties);
Collection<Map<String, Object>> queryResult = new ArrayList<>();
Map<String, Object> queryResultRow = new HashMap<>();
queryResult.add(queryResultRow);
queryResultRow.put("name", "testTaxonomy.termName");
queryResultRow.put("id", "111-222-333");
Collection<Map<String, Object>> taxonomyResultMaps = new ArrayList<>();
Map<String, Object> taxonomyResultMap = new HashMap<>();
taxonomyResultMap.put("name", "testTaxonomy");
taxonomyResultMap.put("id", "12345");
taxonomyResultMaps.add(taxonomyResultMap);
Result taxonomyResult = new Result(taxonomyResultMaps);
Collection<Map<String, Object>> childResult = new ArrayList<>();
Map<String, Object> childResultRow = new HashMap<>();
childResult.add(childResultRow);
childResultRow.put("name", "testTaxonomy.termName.child1");
childResultRow.put("id", "1-1-1");
Map<String, Object> childResultRow2 = new HashMap<>();
childResult.add(childResultRow2);
childResultRow2.put("name", "testTaxonomy.termName.child2");
childResultRow2.put("id", "2-2-2");
Collection<Map<String, Object>> entityResults1 = new ArrayList<>();
Map<String, Object> entityResult1Map1 = new HashMap<>();
entityResult1Map1.put("name", "entity1");
entityResult1Map1.put("id", "111");
entityResults1.add(entityResult1Map1);
Map<String, Object> entityResult1Map2 = new HashMap<>();
entityResult1Map2.put("name", "entity2");
entityResult1Map2.put("id", "222");
entityResults1.add(entityResult1Map2);
Result entityResult1 = new Result(entityResults1);
Collection<Map<String, Object>> entityResults2 = new ArrayList<>();
Map<String, Object> entityResult2Map = new HashMap<>();
entityResult2Map.put("name", "entity3");
entityResult2Map.put("id", "333");
entityResults2.add(entityResult2Map);
Result entityResult2 = new Result(entityResults2);
// mock expectations
// ensure term exists
expect(queryFactory.createTermQuery(userRequest)).andReturn(query);
expect(query.execute()).andReturn(queryResult);
// taxonomy query
expect(taxonomyResourceProvider.getResourceById(capture(taxonomyRequestCapture))).andReturn(taxonomyResult);
// get term children
expect(queryFactory.createTermQuery(capture(termRequestCapture))).andReturn(query);
expect(query.execute()).andReturn(childResult);
// entities with child1 tag
expect(entityResourceProvider.getResources(eq(entityRequest1))).andReturn(entityResult1);
// typeSystem.deleteTag("111", "testTaxonomy.termName.child1");
// typeSystem.deleteTag("222", "testTaxonomy.termName.child1");
entityTagResourceProvider.deleteResourceById(entityTagRequest1);
entityTagResourceProvider.deleteResourceById(entityTagRequest2);
// delete child1 from taxonomy
typeSystem.deleteTag("12345", "testTaxonomy.termName.child1");
// entities with child2 tag
expect(entityResourceProvider.getResources(eq(entityRequest2))).andReturn(entityResult2);
//typeSystem.deleteTag("333", "testTaxonomy.termName.child2");
entityTagResourceProvider.deleteResourceById(entityTagRequest3);
// delete child2 from taxonomy
typeSystem.deleteTag("12345", "testTaxonomy.termName.child2");
// root term being deleted which has no associated tags
expect(entityResourceProvider.getResources(eq(entityRequest3))).andReturn(new Result(Collections.<Map<String, Object>>emptyList()));
// delete root term from taxonomy
typeSystem.deleteTag("12345", "testTaxonomy.termName");
replay(taxonomyResourceProvider, entityResourceProvider, entityTagResourceProvider, typeSystem, queryFactory, query);
TermResourceProvider provider = new TestTermResourceProvider(typeSystem, taxonomyResourceProvider, entityResourceProvider, entityTagResourceProvider);
provider.setQueryFactory(queryFactory);
// invoke method being tested
provider.deleteResourceById(userRequest);
Request taxonomyRequest = taxonomyRequestCapture.getValue();
assertEquals(taxonomyRequest.getQueryProperties().get("name"), "testTaxonomy");
assertEquals(taxonomyRequest.getAdditionalSelectProperties().size(), 1);
assertTrue(taxonomyRequest.getAdditionalSelectProperties().contains("id"));
Request childTermRequest = termRequestCapture.getValue();
assertEquals(childTermRequest.<TermPath>getProperty("termPath").getFullyQualifiedName(), "testTaxonomy.termName.");
verify(taxonomyResourceProvider, entityResourceProvider, entityTagResourceProvider, typeSystem, queryFactory, query);
}
Aggregations