Search in sources :

Example 6 with AtlasQuery

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

the class TermResourceProvider method getResourceById.

@Override
public Result getResourceById(Request request) throws ResourceNotFoundException {
    //todo: shouldn't need to add this here
    request.getQueryProperties().put("name", request.<TermPath>getProperty("termPath").getFullyQualifiedName());
    AtlasQuery atlasQuery;
    try {
        atlasQuery = queryFactory.createTermQuery(request);
    } catch (InvalidQueryException e) {
        throw new CatalogRuntimeException("Unable to compile internal Term query: " + e, e);
    }
    Collection<Map<String, Object>> results = atlasQuery.execute();
    if (results.isEmpty()) {
        throw new ResourceNotFoundException(String.format("Term '%s' not found.", request.<TermPath>getProperty("termPath").getFullyQualifiedName()));
    }
    return new Result(results);
}
Also used : AtlasQuery(org.apache.atlas.catalog.query.AtlasQuery)

Example 7 with AtlasQuery

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

Example 8 with AtlasQuery

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

Example 9 with AtlasQuery

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

Example 10 with AtlasQuery

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);
}
Also used : QueryFactory(org.apache.atlas.catalog.query.QueryFactory) ResourceDefinition(org.apache.atlas.catalog.definition.ResourceDefinition) 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