Search in sources :

Example 16 with QueryFactory

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

the class TaxonomyResourceProviderTest method testCreateResource_invalidRequest__alreadyExists.

@Test(expectedExceptions = ResourceAlreadyExistsException.class)
public void testCreateResource_invalidRequest__alreadyExists() throws Exception {
    AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class);
    QueryFactory queryFactory = createStrictMock(QueryFactory.class);
    AtlasQuery query = createStrictMock(AtlasQuery.class);
    Capture<Request> requestCapture = newCapture();
    // query is executed to see if resource already exists
    Collection<Map<String, Object>> queryResult = new ArrayList<>();
    Map<String, Object> queryResultRow = new HashMap<>();
    queryResult.add(queryResultRow);
    queryResultRow.put("mame", "taxonomyName");
    queryResultRow.put("description", "test taxonomy description");
    queryResultRow.put("creation_time", "04/20/2016");
    // mock expectations
    expect(queryFactory.createTaxonomyQuery(capture(requestCapture))).andReturn(query);
    // returning result for query should result in ResourceAlreadyExistsException
    expect(query.execute()).andReturn(queryResult);
    replay(typeSystem, queryFactory, query);
    // taxonomy create request must contain 'name' property
    Map<String, Object> requestProperties = new HashMap<>();
    requestProperties.put("name", "taxonomyName");
    Request userRequest = new InstanceRequest(requestProperties);
    TaxonomyResourceProvider provider = new TestTaxonomyResourceProvider(typeSystem);
    provider.setQueryFactory(queryFactory);
    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 17 with QueryFactory

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

the class EntityTagResourceProviderTest 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();
    Collection<Map<String, Object>> queryResult = new ArrayList<>();
    // 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(0, result.getPropertyMaps().size());
    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)

Example 18 with QueryFactory

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

the class EntityTagResourceProviderTest method testCreateResources.

@Test
public void testCreateResources() throws Exception {
    AtlasTypeSystem typeSystem = createMock(AtlasTypeSystem.class);
    QueryFactory queryFactory = createStrictMock(QueryFactory.class);
    AtlasQuery entityQuery = createMock(AtlasQuery.class);
    ResourceProvider termResourceProvider = createMock(TermResourceProvider.class);
    Capture<Request> entityRequestCapture = newCapture();
    Capture<Request> termRequestCapture1 = newCapture();
    Capture<Request> termRequestCapture2 = newCapture();
    Collection<Map<String, Object>> entityQueryResult = new ArrayList<>();
    Map<String, Object> entityQueryResultRow = new HashMap<>();
    entityQueryResultRow.put("id", "1");
    entityQueryResult.add(entityQueryResultRow);
    Map<String, Object> entityQueryResultRow2 = new HashMap<>();
    entityQueryResultRow2.put("id", "2");
    entityQueryResult.add(entityQueryResultRow2);
    Collection<Map<String, Object>> termQueryResult1 = new ArrayList<>();
    Map<String, Object> termQueryResultRow1 = new HashMap<>();
    termQueryResult1.add(termQueryResultRow1);
    termQueryResultRow1.put("name", "testTaxonomy.termName1");
    termQueryResultRow1.put("type", "testTaxonomy.termName1");
    termQueryResultRow1.put("available_as_tag", true);
    termQueryResultRow1.put("description", "term description");
    Result termResult1 = new Result(termQueryResult1);
    Collection<Map<String, Object>> termQueryResult2 = new ArrayList<>();
    Map<String, Object> termQueryResultRow2 = new HashMap<>();
    termQueryResult2.add(termQueryResultRow2);
    termQueryResultRow2.put("name", "testTaxonomy.termName2");
    termQueryResultRow2.put("type", "testTaxonomy.termName2");
    termQueryResultRow2.put("available_as_tag", true);
    termQueryResultRow2.put("description", "term 2 description");
    Result termResult2 = new Result(termQueryResult2);
    // mock expectations
    expect(queryFactory.createEntityQuery(capture(entityRequestCapture))).andReturn(entityQuery);
    expect(entityQuery.execute()).andReturn(entityQueryResult);
    expect(termResourceProvider.getResourceById(capture(termRequestCapture1))).andReturn(termResult1);
    expect(termResourceProvider.getResourceById(capture(termRequestCapture2))).andReturn(termResult2);
    Map<String, Object> tagProperties1 = new HashMap<>();
    tagProperties1.put("name", "testTaxonomy.termName1");
    tagProperties1.put("description", "term description");
    // each tag is associated with each entity
    typeSystem.createTraitInstance("1", "testTaxonomy.termName1", tagProperties1);
    typeSystem.createTraitInstance("2", "testTaxonomy.termName1", tagProperties1);
    Map<String, Object> tagProperties2 = new HashMap<>();
    tagProperties2.put("name", "testTaxonomy.termName2");
    tagProperties2.put("description", "term 2 description");
    // each tag is associated with each entity
    typeSystem.createTraitInstance("1", "testTaxonomy.termName2", tagProperties2);
    typeSystem.createTraitInstance("2", "testTaxonomy.termName2", tagProperties2);
    replay(typeSystem, queryFactory, entityQuery, termResourceProvider);
    // end mock expectations
    EntityTagResourceProvider provider = new TestEntityTagResourceProvider(typeSystem, termResourceProvider);
    provider.setQueryFactory(queryFactory);
    Map<String, Object> requestProps = new HashMap<>();
    Collection<Map<String, String>> tagMaps = new ArrayList<>();
    requestProps.put("tags", tagMaps);
    Map<String, String> tagMap1 = new HashMap<>();
    tagMap1.put("name", "testTaxonomy.termName1");
    tagMaps.add(tagMap1);
    Map<String, String> tagMap2 = new HashMap<>();
    tagMap2.put("name", "testTaxonomy.termName2");
    tagMaps.add(tagMap2);
    Request userRequest = new CollectionRequest(requestProps, "name:foo*");
    // invoke method being tested
    Collection<String> createResult = provider.createResources(userRequest);
    assertEquals(4, createResult.size());
    assertTrue(createResult.contains("v1/entities/1/tags/testTaxonomy.termName1"));
    assertTrue(createResult.contains("v1/entities/1/tags/testTaxonomy.termName2"));
    assertTrue(createResult.contains("v1/entities/2/tags/testTaxonomy.termName1"));
    assertTrue(createResult.contains("v1/entities/2/tags/testTaxonomy.termName2"));
    Request entityRequest = entityRequestCapture.getValue();
    assertEquals("name:foo*", entityRequest.getQueryString());
    assertEquals(Request.Cardinality.COLLECTION, entityRequest.getCardinality());
    Request termRequest1 = termRequestCapture1.getValue();
    assertNull(termRequest1.getQueryString());
    assertEquals(Request.Cardinality.INSTANCE, termRequest1.getCardinality());
    Map<String, Object> termRequestProps = termRequest1.getQueryProperties();
    assertEquals(1, termRequestProps.size());
    TermPath termPath = (TermPath) termRequestProps.get("termPath");
    assertEquals("testTaxonomy.termName1", termPath.getFullyQualifiedName());
    Request termRequest2 = termRequestCapture2.getValue();
    assertNull(termRequest2.getQueryString());
    assertEquals(Request.Cardinality.INSTANCE, termRequest2.getCardinality());
    Map<String, Object> termRequestProps2 = termRequest2.getQueryProperties();
    assertEquals(1, termRequestProps2.size());
    TermPath termPath2 = (TermPath) termRequestProps2.get("termPath");
    assertEquals("testTaxonomy.termName2", termPath2.getFullyQualifiedName());
    verify(typeSystem, queryFactory, entityQuery, termResourceProvider);
}
Also used : QueryFactory(org.apache.atlas.catalog.query.QueryFactory) AtlasQuery(org.apache.atlas.catalog.query.AtlasQuery) Test(org.testng.annotations.Test)

Example 19 with QueryFactory

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

the class EntityTagResourceProviderTest method testGetResource_404.

@Test(expectedExceptions = ResourceNotFoundException.class)
public void testGetResource_404() throws Exception {
    AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class);
    QueryFactory queryFactory = createStrictMock(QueryFactory.class);
    AtlasQuery query = createStrictMock(AtlasQuery.class);
    Capture<Request> requestCapture = newCapture();
    // empty response should result in a ResourceNotFoundException
    Collection<Map<String, Object>> emptyResponse = new ArrayList<>();
    // mock expectations
    expect(queryFactory.createEntityTagQuery(capture(requestCapture))).andReturn(query);
    expect(query.execute()).andReturn(emptyResponse);
    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 request = new InstanceRequest(requestProperties);
    provider.getResourceById(request);
}
Also used : QueryFactory(org.apache.atlas.catalog.query.QueryFactory) AtlasQuery(org.apache.atlas.catalog.query.AtlasQuery) Test(org.testng.annotations.Test)

Example 20 with QueryFactory

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

the class EntityTagResourceProviderTest method testCreateResource_invalidRequest__alreadyExists.

@Test(expectedExceptions = ResourceAlreadyExistsException.class)
public void testCreateResource_invalidRequest__alreadyExists() 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");
    termQueryResultRow.put("available_as_tag", true);
    termQueryResultRow.put("description", "term description");
    Result termResult = new Result(termQueryResult);
    // mock expectations
    expect(termResourceProvider.getResourceById(capture(termRequestCapture))).andReturn(termResult);
    Map<String, Object> tagProperties = new HashMap<>();
    tagProperties.put("name", "testTaxonomy.termName");
    tagProperties.put("description", "term description");
    typeSystem.createTraitInstance("11-22-33", "testTaxonomy.termName", tagProperties);
    expectLastCall().andThrow(new ResourceAlreadyExistsException(""));
    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) ResourceAlreadyExistsException(org.apache.atlas.catalog.exception.ResourceAlreadyExistsException) AtlasQuery(org.apache.atlas.catalog.query.AtlasQuery) Test(org.testng.annotations.Test)

Aggregations

QueryFactory (org.apache.atlas.catalog.query.QueryFactory)40 Test (org.testng.annotations.Test)40 AtlasQuery (org.apache.atlas.catalog.query.AtlasQuery)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