Search in sources :

Example 11 with AtlasQuery

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);
}
Also used : QueryFactory(org.apache.atlas.catalog.query.QueryFactory) AtlasQuery(org.apache.atlas.catalog.query.AtlasQuery) Test(org.testng.annotations.Test)

Example 12 with AtlasQuery

use of org.apache.atlas.catalog.query.AtlasQuery in project incubator-atlas by apache.

the class EntityResourceProviderTest method testGetResource.

@Test
public void testGetResource() throws Exception {
    AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class);
    QueryFactory queryFactory = createStrictMock(QueryFactory.class);
    AtlasQuery query = createStrictMock(AtlasQuery.class);
    Capture<Request> requestCapture = newCapture();
    Collection<Map<String, Object>> queryResult = new ArrayList<>();
    Map<String, Object> queryResultRow = new HashMap<>();
    queryResult.add(queryResultRow);
    queryResultRow.put("id", "1");
    queryResultRow.put("creation_time", "04/20/2016");
    // mock expectations
    expect(queryFactory.createEntityQuery(capture(requestCapture))).andReturn(query);
    expect(query.execute()).andReturn(queryResult);
    replay(typeSystem, queryFactory, query);
    EntityResourceProvider provider = new EntityResourceProvider(typeSystem);
    provider.setQueryFactory(queryFactory);
    Map<String, Object> requestProperties = new HashMap<>();
    requestProperties.put("id", "1");
    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());
    verify(typeSystem, queryFactory, query);
}
Also used : QueryFactory(org.apache.atlas.catalog.query.QueryFactory) AtlasQuery(org.apache.atlas.catalog.query.AtlasQuery) Test(org.testng.annotations.Test)

Example 13 with AtlasQuery

use of org.apache.atlas.catalog.query.AtlasQuery in project incubator-atlas by apache.

the class EntityTagResourceProviderTest method testGetResource.

@Test
public void testGetResource() throws Exception {
    AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class);
    QueryFactory queryFactory = createStrictMock(QueryFactory.class);
    AtlasQuery query = createStrictMock(AtlasQuery.class);
    Capture<Request> requestCapture = newCapture();
    Collection<Map<String, Object>> queryResult = new ArrayList<>();
    Map<String, Object> queryResultRow = new HashMap<>();
    queryResult.add(queryResultRow);
    queryResultRow.put("name", "taxonomyName.termName");
    queryResultRow.put("description", "test term description");
    // mock expectations
    expect(queryFactory.createEntityTagQuery(capture(requestCapture))).andReturn(query);
    expect(query.execute()).andReturn(queryResult);
    replay(typeSystem, queryFactory, query);
    EntityTagResourceProvider provider = new EntityTagResourceProvider(typeSystem);
    provider.setQueryFactory(queryFactory);
    Map<String, Object> requestProperties = new HashMap<>();
    requestProperties.put("name", "taxonomyName.termName");
    requestProperties.put("id", "1");
    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(2, request.getQueryProperties().size());
    assertEquals("taxonomyName.termName", request.getQueryProperties().get("name"));
    assertEquals(Request.Cardinality.INSTANCE, request.getCardinality());
    verify(typeSystem, queryFactory, query);
}
Also used : QueryFactory(org.apache.atlas.catalog.query.QueryFactory) AtlasQuery(org.apache.atlas.catalog.query.AtlasQuery) Test(org.testng.annotations.Test)

Example 14 with AtlasQuery

use of org.apache.atlas.catalog.query.AtlasQuery in project incubator-atlas by apache.

the class EntityTagResourceProviderTest method testCreateResource_invalidRequest__termNotAvailableForTagging.

@Test(expectedExceptions = CatalogException.class)
public void testCreateResource_invalidRequest__termNotAvailableForTagging() 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");
    // false value for 'available_as_tag' should result in an exception
    termQueryResultRow.put("available_as_tag", false);
    termQueryResultRow.put("description", "term description");
    Result termResult = new Result(termQueryResult);
    // mock expectations
    expect(termResourceProvider.getResourceById(capture(termRequestCapture))).andReturn(termResult);
    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);
}
Also used : QueryFactory(org.apache.atlas.catalog.query.QueryFactory) AtlasQuery(org.apache.atlas.catalog.query.AtlasQuery) Test(org.testng.annotations.Test)

Example 15 with AtlasQuery

use of org.apache.atlas.catalog.query.AtlasQuery in project incubator-atlas by apache.

the class EntityTagResourceProviderTest 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();
    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");
    Map<String, Object> queryResultRow2 = new HashMap<>();
    queryResult.add(queryResultRow2);
    queryResultRow2.put("name", "testTaxonomy.termName2");
    queryResultRow2.put("description", "test term 2 description");
    // mock expectations
    expect(queryFactory.createEntityTagQuery(capture(requestCapture))).andReturn(query);
    expect(query.execute()).andReturn(queryResult);
    replay(typeSystem, queryFactory, query);
    EntityTagResourceProvider provider = new EntityTagResourceProvider(typeSystem);
    provider.setQueryFactory(queryFactory);
    Map<String, Object> requestProperties = new HashMap<>();
    requestProperties.put("id", "1");
    Request userRequest = new CollectionRequest(requestProperties, "name:testTaxonomy.*");
    // invoke test method
    Result result = provider.getResources(userRequest);
    assertEquals(2, result.getPropertyMaps().size());
    assertTrue(result.getPropertyMaps().contains(queryResultRow1));
    assertTrue(result.getPropertyMaps().contains(queryResultRow2));
    Request request = requestCapture.getValue();
    assertEquals("name:testTaxonomy.*", request.getQueryString());
    assertEquals(0, request.getAdditionalSelectProperties().size());
    verify(typeSystem, queryFactory, query);
}
Also used : QueryFactory(org.apache.atlas.catalog.query.QueryFactory) AtlasQuery(org.apache.atlas.catalog.query.AtlasQuery) Test(org.testng.annotations.Test)

Aggregations

AtlasQuery (org.apache.atlas.catalog.query.AtlasQuery)47 QueryFactory (org.apache.atlas.catalog.query.QueryFactory)38 Test (org.testng.annotations.Test)38 ResourceDefinition (org.apache.atlas.catalog.definition.ResourceDefinition)5 TaxonomyResourceDefinition (org.apache.atlas.catalog.definition.TaxonomyResourceDefinition)3 ResourceAlreadyExistsException (org.apache.atlas.catalog.exception.ResourceAlreadyExistsException)2 ResourceNotFoundException (org.apache.atlas.catalog.exception.ResourceNotFoundException)1