Search in sources :

Example 21 with AtlasQuery

use of org.apache.atlas.catalog.query.AtlasQuery 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 22 with AtlasQuery

use of org.apache.atlas.catalog.query.AtlasQuery 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 23 with AtlasQuery

use of org.apache.atlas.catalog.query.AtlasQuery 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)

Example 24 with AtlasQuery

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

the class EntityResourceProviderTest method testCreateResource.

@Test(expectedExceptions = UnsupportedOperationException.class)
public void testCreateResource() throws Exception {
    AtlasTypeSystem typeSystem = createStrictMock(AtlasTypeSystem.class);
    QueryFactory queryFactory = createStrictMock(QueryFactory.class);
    AtlasQuery query = createStrictMock(AtlasQuery.class);
    // mock expectations
    replay(typeSystem, queryFactory, query);
    Map<String, Object> requestProperties = new HashMap<>();
    requestProperties.put("id", "1");
    Request userRequest = new InstanceRequest(requestProperties);
    EntityResourceProvider provider = new EntityResourceProvider(typeSystem);
    provider.setQueryFactory(queryFactory);
    provider.createResource(userRequest);
}
Also used : AtlasQuery(org.apache.atlas.catalog.query.AtlasQuery) QueryFactory(org.apache.atlas.catalog.query.QueryFactory) Test(org.testng.annotations.Test)

Example 25 with AtlasQuery

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

the class EntityTagResourceProvider method getResourceById.

@Override
public Result getResourceById(Request request) throws ResourceNotFoundException {
    AtlasQuery atlasQuery;
    try {
        atlasQuery = queryFactory.createEntityTagQuery(request);
    } catch (InvalidQueryException e) {
        throw new CatalogRuntimeException("Unable to compile internal Entity Tag query: " + e, e);
    }
    Collection<Map<String, Object>> results = atlasQuery.execute();
    if (results.isEmpty()) {
        throw new ResourceNotFoundException(String.format("Tag '%s' not found.", request.getProperty(resourceDefinition.getIdPropertyName())));
    }
    return new Result(results);
}
Also used : AtlasQuery(org.apache.atlas.catalog.query.AtlasQuery)

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