use of org.apache.atlas.catalog.definition.ResourceDefinition 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.definition.ResourceDefinition 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.definition.ResourceDefinition in project incubator-atlas by apache.
the class TermResourceProviderTest 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<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);
// mock expectations
expect(taxonomyResourceProvider.getResourceById(capture(taxonomyRequestCapture))).andReturn(taxonomyResult);
typeSystem.createTraitType(capture(resourceDefinitionCapture), eq("testTaxonomy.termName"), EasyMock.<String>isNull());
expectLastCall().andThrow(new ResourceAlreadyExistsException(""));
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);
}
use of org.apache.atlas.catalog.definition.ResourceDefinition in project incubator-atlas by apache.
the class AtlasEntityQueryTest method testExecute_Collection_update.
@Test
public void testExecute_Collection_update() throws Exception {
AtlasGraph graph = createStrictMock(AtlasGraph.class);
QueryExpression expression = createStrictMock(QueryExpression.class);
ResourceDefinition resourceDefinition = createStrictMock(ResourceDefinition.class);
Request request = createStrictMock(Request.class);
GremlinPipeline initialPipeline = createStrictMock(GremlinPipeline.class);
Pipe queryPipe = createStrictMock(Pipe.class);
Pipe expressionPipe = createStrictMock(Pipe.class);
Pipe notDeletedPipe = createStrictMock(Pipe.class);
GremlinPipeline rootPipeline = createStrictMock(GremlinPipeline.class);
GremlinPipeline queryPipeline = createStrictMock(GremlinPipeline.class);
GremlinPipeline expressionPipeline = createStrictMock(GremlinPipeline.class);
GremlinPipeline notDeletedPipeline = createStrictMock(GremlinPipeline.class);
Vertex vertex1 = createStrictMock(Vertex.class);
VertexWrapper vertex1Wrapper = createStrictMock(VertexWrapper.class);
Capture<Long> modifiedTimestampCapture = newCapture();
List<Vertex> results = new ArrayList<>();
results.add(vertex1);
Map<String, Object> vertex1PropertyMap = new HashMap<>();
vertex1PropertyMap.put("prop1", "prop1.value1");
vertex1PropertyMap.put("prop2", "prop2.value1");
Map<String, Object> filteredVertex1PropertyMap = new HashMap<>();
filteredVertex1PropertyMap.put("prop1", "prop1.value1");
Map<String, Object> updateProperties = new HashMap<>();
updateProperties.put("prop3", "newValue");
// mock expectations
expect(initialPipeline.add(queryPipe)).andReturn(queryPipeline);
expect(initialPipeline.add(notDeletedPipe)).andReturn(notDeletedPipeline);
expect(initialPipeline.as("root")).andReturn(rootPipeline);
expect(expression.asPipe()).andReturn(expressionPipe);
expect(rootPipeline.add(expressionPipe)).andReturn(expressionPipeline);
expect(expressionPipeline.back("root")).andReturn(rootPipeline);
expect(rootPipeline.toList()).andReturn(results);
graph.commit();
vertex1Wrapper.setProperty("prop3", "newValue");
vertex1Wrapper.setProperty(eq(Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY), capture(modifiedTimestampCapture));
expect(vertex1Wrapper.getPropertyMap()).andReturn(vertex1PropertyMap);
expect(resourceDefinition.filterProperties(request, vertex1PropertyMap)).andReturn(filteredVertex1PropertyMap);
expect(resourceDefinition.resolveHref(filteredVertex1PropertyMap)).andReturn("/foo/bar");
expect(request.getCardinality()).andReturn(Request.Cardinality.COLLECTION);
replay(graph, expression, resourceDefinition, request, initialPipeline, queryPipe, expressionPipe, notDeletedPipe, rootPipeline, queryPipeline, expressionPipeline, notDeletedPipeline, vertex1, vertex1Wrapper);
// end mock expectations
AtlasEntityQuery query = new TestAtlasEntityQuery(expression, resourceDefinition, request, initialPipeline, queryPipe, notDeletedPipe, graph, vertex1Wrapper);
long startTime = System.currentTimeMillis();
// invoke method being tested
Collection<Map<String, Object>> queryResults = query.execute(updateProperties);
long endTime = System.currentTimeMillis();
assertEquals(queryResults.size(), 1);
Map<String, Object> queryResultMap = queryResults.iterator().next();
assertEquals(queryResultMap.size(), 2);
assertEquals(queryResultMap.get("prop1"), "prop1.value1");
assertEquals(queryResultMap.get("href"), "/foo/bar");
long modifiedTimestamp = modifiedTimestampCapture.getValue();
assertTrue(modifiedTimestamp >= startTime && modifiedTimestamp <= endTime);
verify(graph, expression, resourceDefinition, request, initialPipeline, queryPipe, expressionPipe, notDeletedPipe, rootPipeline, queryPipeline, expressionPipeline, notDeletedPipeline, vertex1, vertex1Wrapper);
}
use of org.apache.atlas.catalog.definition.ResourceDefinition in project incubator-atlas by apache.
the class AtlasEntityQueryTest method testExecute_Collection_rollbackOnException.
@Test
public void testExecute_Collection_rollbackOnException() throws Exception {
AtlasGraph graph = createStrictMock(AtlasGraph.class);
QueryExpression expression = createStrictMock(QueryExpression.class);
ResourceDefinition resourceDefinition = createStrictMock(ResourceDefinition.class);
Request request = createStrictMock(Request.class);
GremlinPipeline initialPipeline = createStrictMock(GremlinPipeline.class);
Pipe queryPipe = createStrictMock(Pipe.class);
Pipe expressionPipe = createStrictMock(Pipe.class);
Pipe notDeletedPipe = createStrictMock(Pipe.class);
GremlinPipeline rootPipeline = createStrictMock(GremlinPipeline.class);
GremlinPipeline queryPipeline = createStrictMock(GremlinPipeline.class);
GremlinPipeline expressionPipeline = createStrictMock(GremlinPipeline.class);
GremlinPipeline notDeletedPipeline = createStrictMock(GremlinPipeline.class);
// mock expectations
expect(initialPipeline.add(queryPipe)).andReturn(queryPipeline);
expect(initialPipeline.add(notDeletedPipe)).andReturn(notDeletedPipeline);
expect(initialPipeline.as("root")).andReturn(rootPipeline);
expect(expression.asPipe()).andReturn(expressionPipe);
expect(rootPipeline.add(expressionPipe)).andReturn(expressionPipeline);
expect(expressionPipeline.back("root")).andReturn(rootPipeline);
expect(rootPipeline.toList()).andThrow(new RuntimeException("something bad happened"));
graph.rollback();
replay(graph, expression, resourceDefinition, request, initialPipeline, queryPipe, expressionPipe, notDeletedPipe, rootPipeline, queryPipeline, expressionPipeline, notDeletedPipeline);
// end mock expectations
AtlasEntityQuery query = new TestAtlasEntityQuery(expression, resourceDefinition, request, initialPipeline, queryPipe, notDeletedPipe, graph, null);
try {
// invoke method being tested
query.execute();
fail("expected exception");
} catch (RuntimeException e) {
assertEquals(e.getMessage(), "something bad happened");
}
verify(graph, expression, resourceDefinition, request, initialPipeline, queryPipe, expressionPipe, notDeletedPipe, rootPipeline, queryPipeline, expressionPipeline, notDeletedPipeline);
}
Aggregations