Commit 19751c60 by Shwetha GS

ATLAS-805 Quickstart is failing if run after queries to the business taxonomy…

ATLAS-805 Quickstart is failing if run after queries to the business taxonomy API (jspeidel via shwethags)
parent 7c73f0c4
...@@ -30,7 +30,10 @@ import org.apache.atlas.catalog.projection.Projection; ...@@ -30,7 +30,10 @@ import org.apache.atlas.catalog.projection.Projection;
import org.apache.atlas.catalog.projection.ProjectionResult; import org.apache.atlas.catalog.projection.ProjectionResult;
import org.apache.atlas.repository.graph.TitanGraphProvider; import org.apache.atlas.repository.graph.TitanGraphProvider;
import java.util.*; import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
/** /**
* Base Query implementation. * Base Query implementation.
...@@ -50,7 +53,7 @@ public abstract class BaseQuery implements AtlasQuery { ...@@ -50,7 +53,7 @@ public abstract class BaseQuery implements AtlasQuery {
Collection<Map<String, Object>> resultMaps = new ArrayList<>(); Collection<Map<String, Object>> resultMaps = new ArrayList<>();
for (Vertex vertex : executeQuery()) { for (Vertex vertex : executeQuery()) {
resultMaps.add(processPropertyMap(new VertexWrapper(vertex, resourceDefinition))); resultMaps.add(processPropertyMap(wrapVertex(vertex)));
} }
return resultMaps; return resultMaps;
} }
...@@ -59,19 +62,29 @@ public abstract class BaseQuery implements AtlasQuery { ...@@ -59,19 +62,29 @@ public abstract class BaseQuery implements AtlasQuery {
GremlinPipeline pipeline = getInitialPipeline().as("root"); GremlinPipeline pipeline = getInitialPipeline().as("root");
Pipe adapterPipe = queryExpression.asPipe(); Pipe adapterPipe = queryExpression.asPipe();
//todo: AlwaysQueryAdapter returns null for pipe try {
//todo: Is there a no-op pipe that I could add that wouldn't negatively affect performance // AlwaysQuery returns null for pipe
return adapterPipe == null ? List<Vertex> vertices = adapterPipe == null ? pipeline.toList() :
pipeline.toList() : pipeline.add(adapterPipe).back("root").toList();
pipeline.add(adapterPipe).back("root").toList();
// Even non-mutating queries can result in objects being created in
// the graph such as new fields or property keys. So, it is important
// to commit the implicit query after execution, otherwise the uncommitted
// transaction will still be associated with the thread when it is re-pooled.
getGraph().commit();
return vertices;
} catch (Throwable e) {
getGraph().rollback();
throw e;
}
} }
protected abstract GremlinPipeline getInitialPipeline(); protected abstract GremlinPipeline getInitialPipeline();
// todo: consider getting // todo: consider getting
protected Map<String, Object> processPropertyMap(VertexWrapper vertex) { protected Map<String, Object> processPropertyMap(VertexWrapper vertex) {
Map<String, Object> propertyMap = vertex.getPropertyMap(); Map<String, Object> propertyMap = resourceDefinition.filterProperties(
resourceDefinition.filterProperties(request, propertyMap); request, vertex.getPropertyMap());
addHref(propertyMap); addHref(propertyMap);
return request.getCardinality() == Request.Cardinality.INSTANCE ? return request.getCardinality() == Request.Cardinality.INSTANCE ?
...@@ -118,4 +131,8 @@ public abstract class BaseQuery implements AtlasQuery { ...@@ -118,4 +131,8 @@ public abstract class BaseQuery implements AtlasQuery {
protected TitanGraph getGraph() { protected TitanGraph getGraph() {
return TitanGraphProvider.getGraphInstance(); return TitanGraphProvider.getGraphInstance();
} }
protected VertexWrapper wrapVertex(Vertex v) {
return new VertexWrapper(v, resourceDefinition);
}
} }
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.query;
import com.thinkaurelius.titan.core.TitanGraph;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.gremlin.java.GremlinPipeline;
import com.tinkerpop.pipes.Pipe;
import org.apache.atlas.catalog.Request;
import org.apache.atlas.catalog.VertexWrapper;
import org.apache.atlas.catalog.definition.ResourceDefinition;
import org.testng.annotations.Test;
import java.util.*;
import static org.easymock.EasyMock.*;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;
/**
* Unit tests for AtlasEntityQuery.
*/
@SuppressWarnings("unchecked")
public class AtlasEntityQueryTest {
//todo: add tests for instance query and getInitialPipeline()
@Test
public void testExecute_Collection() throws Exception {
TitanGraph graph = createStrictMock(TitanGraph.class);
QueryExpression expression = createStrictMock(QueryExpression.class);
ResourceDefinition resourceDefinition = createNiceMock(ResourceDefinition.class);
Request request = createNiceMock(Request.class);
Pipe queryExpressionPipe = createStrictMock(Pipe.class);
GremlinPipeline initialPipeline = createStrictMock(GremlinPipeline.class);
GremlinPipeline rootPipeline = createStrictMock(GremlinPipeline.class);
GremlinPipeline expressionPipeline = createStrictMock(GremlinPipeline.class);
Vertex vertex1 = createStrictMock(Vertex.class);
VertexWrapper vertex1Wrapper = createStrictMock(VertexWrapper.class);
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");
// mock expectations
expect(initialPipeline.as("root")).andReturn(rootPipeline);
expect(expression.asPipe()).andReturn(queryExpressionPipe);
expect(rootPipeline.add(queryExpressionPipe)).andReturn(expressionPipeline);
expect(expressionPipeline.back("root")).andReturn(rootPipeline);
expect(rootPipeline.toList()).andReturn(results);
graph.commit();
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, queryExpressionPipe,
initialPipeline, rootPipeline, expressionPipeline, vertex1, vertex1Wrapper);
// end mock expectations
AtlasEntityQuery query = new TestAtlasEntityQuery(
expression, resourceDefinition, request, initialPipeline, graph, vertex1Wrapper);
// invoke method being tested
Collection<Map<String, Object>> queryResults = query.execute();
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");
verify(graph, expression, resourceDefinition, request, queryExpressionPipe,
initialPipeline, rootPipeline, expressionPipeline, vertex1, vertex1Wrapper);
}
@Test
public void testExecute_Collection_rollbackOnException() throws Exception {
TitanGraph graph = createStrictMock(TitanGraph.class);
QueryExpression expression = createStrictMock(QueryExpression.class);
ResourceDefinition resourceDefinition = createNiceMock(ResourceDefinition.class);
Request request = createNiceMock(Request.class);
Pipe queryExpressionPipe = createStrictMock(Pipe.class);
GremlinPipeline initialPipeline = createStrictMock(GremlinPipeline.class);
GremlinPipeline rootPipeline = createStrictMock(GremlinPipeline.class);
GremlinPipeline expressionPipeline = createStrictMock(GremlinPipeline.class);
// mock expectations
expect(initialPipeline.as("root")).andReturn(rootPipeline);
expect(expression.asPipe()).andReturn(queryExpressionPipe);
expect(rootPipeline.add(queryExpressionPipe)).andReturn(expressionPipeline);
expect(expressionPipeline.back("root")).andReturn(rootPipeline);
expect(rootPipeline.toList()).andThrow(new RuntimeException("something bad happened"));
graph.rollback();
replay(graph, expression, resourceDefinition, request, queryExpressionPipe,
initialPipeline, rootPipeline, expressionPipeline);
// end mock expectations
AtlasEntityQuery query = new TestAtlasEntityQuery(
expression, resourceDefinition, request, initialPipeline, 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, queryExpressionPipe,
initialPipeline, rootPipeline, expressionPipeline);
}
private class TestAtlasEntityQuery extends AtlasEntityQuery {
private final GremlinPipeline initialPipeline;
private final TitanGraph graph;
private final VertexWrapper vWrapper;
public TestAtlasEntityQuery(QueryExpression queryExpression,
ResourceDefinition resourceDefinition,
Request request,
GremlinPipeline initialPipeline,
TitanGraph graph,
VertexWrapper vWrapper) {
super(queryExpression, resourceDefinition, request);
this.initialPipeline = initialPipeline;
this.graph = graph;
this.vWrapper = vWrapper;
}
@Override
protected GremlinPipeline getInitialPipeline() {
return initialPipeline;
}
@Override
protected TitanGraph getGraph() {
return graph;
}
@Override
protected VertexWrapper wrapVertex(Vertex v) {
return vWrapper;
}
}
}
...@@ -21,6 +21,7 @@ ATLAS-409 Atlas will not import avro tables with schema read from a file (dosset ...@@ -21,6 +21,7 @@ ATLAS-409 Atlas will not import avro tables with schema read from a file (dosset
ATLAS-379 Create sqoop and falcon metadata addons (venkatnrangan,bvellanki,sowmyaramesh via shwethags) ATLAS-379 Create sqoop and falcon metadata addons (venkatnrangan,bvellanki,sowmyaramesh via shwethags)
ALL CHANGES: ALL CHANGES:
ATLAS-805 Quickstart is failing if run after queries to the business taxonomy API (jspeidel via shwethags)
ATLAS-774 Better error handling from login.jsp (nixonrodrigues via shwethags) ATLAS-774 Better error handling from login.jsp (nixonrodrigues via shwethags)
ATLAS-683 Refactor local type-system cache with cache provider interface (vmadugun via shwethags) ATLAS-683 Refactor local type-system cache with cache provider interface (vmadugun via shwethags)
ATLAS-802 New look UI to show Business Catalog functionalities (kevalbhatt18 via yhemanth) ATLAS-802 New look UI to show Business Catalog functionalities (kevalbhatt18 via yhemanth)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment