From 035ad742f60bdb375ee4e2da4345d404949a19b4 Mon Sep 17 00:00:00 2001 From: vasraz Date: Wed, 11 Jan 2023 19:19:05 +0000 Subject: Improve test coverage - extract duplicated code - remove redundant and unused code Signed-off-by: Vasyl Razinkov Change-Id: I7ae3592951f53bbebf10a80f7cce22d54b2940b6 Issue-ID: SDC-4317 --- .../sdc/be/dao/janusgraph/JanusGraphDao.java | 406 +++++++++------------ .../be/dao/janusgraph/JanusGraphDaoMockTest.java | 12 +- .../sdc/be/dao/janusgraph/JanusGraphDaoTest.java | 15 +- 3 files changed, 182 insertions(+), 251 deletions(-) (limited to 'catalog-dao/src') diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/janusgraph/JanusGraphDao.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/janusgraph/JanusGraphDao.java index ed69992715..38dc522247 100644 --- a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/janusgraph/JanusGraphDao.java +++ b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/janusgraph/JanusGraphDao.java @@ -31,11 +31,13 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Optional; import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.StreamSupport; -import java.util.Optional; +import lombok.Getter; import org.apache.commons.collections.MapUtils; +import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.tinkerpop.gremlin.structure.Direction; @@ -72,7 +74,9 @@ import org.springframework.beans.factory.annotation.Qualifier; public class JanusGraphDao { private static final Logger logger = Logger.getLogger(JanusGraphDao.class); - JanusGraphClient janusGraphClient; + private static final String FAILED_TO_GET_BY_CRITERIA_FOR_TYPE_AND_PROPERTIES = "Failed to get by criteria for type '{}' and properties '{}'"; + @Getter + private final JanusGraphClient janusGraphClient; public JanusGraphDao(@Qualifier("janusgraph-client") JanusGraphClient janusGraphClient) { this.janusGraphClient = janusGraphClient; @@ -89,10 +93,6 @@ public class JanusGraphDao { return janusGraphClient.rollback(); } - public Either getGraph() { - return janusGraphClient.getGraph(); - } - /** * @param graphVertex * @return @@ -157,28 +157,22 @@ public class JanusGraphDao { if (graph.isLeft()) { try { JanusGraph tGraph = graph.left().value(); - @SuppressWarnings("unchecked") Iterable vertecies = tGraph.query().has(name.getProperty(), value) + @SuppressWarnings("unchecked") Iterable vertices = tGraph.query().has(name.getProperty(), value) .has(GraphPropertyEnum.LABEL.getProperty(), label.getName()).vertices(); - java.util.Iterator iterator = vertecies.iterator(); + java.util.Iterator iterator = vertices.iterator(); if (iterator.hasNext()) { JanusGraphVertex vertex = iterator.next(); GraphVertex graphVertex = createAndFill(vertex, parseFlag); return Either.left(graphVertex); } - if (logger.isDebugEnabled()) { - logger.debug("No vertex in graph for key = {} and value = {} label = {}", name, value, label); - } + logger.debug("No vertex in graph for key = {} and value = {} label = {}", name, value, label); return Either.right(JanusGraphOperationStatus.NOT_FOUND); } catch (Exception e) { - if (logger.isDebugEnabled()) { - logger.debug("Failed to get vertex in graph for key ={} and value = {} label = {}", name, value, label); - } + logger.debug("Failed to get vertex in graph for key ={} and value = {} label = {}", name, value, label); return Either.right(JanusGraphClient.handleJanusGraphException(e)); } } else { - if (logger.isDebugEnabled()) { - logger.debug("No vertex in graph for key ={} and value = {} label = {} error :{}", name, value, label, graph.right().value()); - } + logger.debug("No vertex in graph for key ={} and value = {} label = {} error :{}", name, value, label, graph.right().value()); return Either.right(graph.right().value()); } } @@ -199,37 +193,29 @@ public class JanusGraphDao { public Either getVertexById(String id, JsonParseFlagEnum parseFlag) { Either graph = janusGraphClient.getGraph(); if (id == null) { - if (logger.isDebugEnabled()) { - logger.debug("No vertex in graph for id = {} ", id); - } + logger.debug("No vertex in graph for id = {} ", id); return Either.right(JanusGraphOperationStatus.NOT_FOUND); } if (graph.isLeft()) { try { JanusGraph tGraph = graph.left().value(); - @SuppressWarnings("unchecked") Iterable vertecies = tGraph.query() + @SuppressWarnings("unchecked") Iterable vertices = tGraph.query() .has(GraphPropertyEnum.UNIQUE_ID.getProperty(), id).vertices(); - java.util.Iterator iterator = vertecies.iterator(); + java.util.Iterator iterator = vertices.iterator(); if (iterator.hasNext()) { JanusGraphVertex vertex = iterator.next(); GraphVertex graphVertex = createAndFill(vertex, parseFlag); return Either.left(graphVertex); } else { - if (logger.isDebugEnabled()) { - logger.debug("No vertex in graph for id = {}", id); - } + logger.debug("No vertex in graph for id = {}", id); return Either.right(JanusGraphOperationStatus.NOT_FOUND); } } catch (Exception e) { - if (logger.isDebugEnabled()) { - logger.debug("Failed to get vertex in graph for id {} ", id); - } + logger.debug("Failed to get vertex in graph for id {} ", id); return Either.right(JanusGraphClient.handleJanusGraphException(e)); } } else { - if (logger.isDebugEnabled()) { - logger.debug("No vertex in graph for id {} error : {}", id, graph.right().value()); - } + logger.debug("No vertex in graph for id {} error : {}", id, graph.right().value()); return Either.right(graph.right().value()); } } @@ -255,7 +241,7 @@ public class JanusGraphDao { } } - public void setVertexProperties(Vertex vertex, Map properties) throws IOException { + public void setVertexProperties(Vertex vertex, Map properties) { for (Map.Entry entry : properties.entrySet()) { if (entry.getValue() != null) { vertex.property(entry.getKey(), entry.getValue()); @@ -315,11 +301,9 @@ public class JanusGraphDao { } public JanusGraphOperationStatus createEdge(Vertex from, Vertex to, EdgeLabelEnum label, Map properties) { - if (logger.isTraceEnabled()) { - logger.trace("Try to connect {} with {} label {} properties {}", - from == null ? "NULL" : from.property(GraphPropertyEnum.UNIQUE_ID.getProperty()), - to == null ? "NULL" : to.property(GraphPropertyEnum.UNIQUE_ID.getProperty()), label, properties); - } + logger.trace("Try to connect {} with {} label {} properties {}", + from == null ? "NULL" : from.property(GraphPropertyEnum.UNIQUE_ID.getProperty()), + to == null ? "NULL" : to.property(GraphPropertyEnum.UNIQUE_ID.getProperty()), label, properties); if (from == null || to == null) { logger.trace("No JanusGraph vertex for id from {} or id to {}", from == null ? "NULL" : from.property(GraphPropertyEnum.UNIQUE_ID.getProperty()), @@ -340,7 +324,7 @@ public class JanusGraphDao { public Map getVertexProperties(Element element) { Map result = new HashMap<>(); - if (element != null && element.keys() != null && element.keys().size() > 0) { + if (element != null && CollectionUtils.isNotEmpty(element.keys())) { Map propertyMap = ElementHelper.propertyMap(element, element.keys().toArray(new String[element.keys().size()])); for (Entry entry : propertyMap.entrySet()) { String key = entry.getKey(); @@ -357,7 +341,7 @@ public class JanusGraphDao { public Map getEdgeProperties(Element element) { Map result = new HashMap<>(); - if (element != null && element.keys() != null && element.keys().size() > 0) { + if (element != null && CollectionUtils.isNotEmpty(element.keys())) { Map propertyMap = ElementHelper.propertyMap(element, element.keys().toArray(new String[element.keys().size()])); for (Entry entry : propertyMap.entrySet()) { String key = entry.getKey(); @@ -421,36 +405,29 @@ public class JanusGraphDao { List result = new ArrayList<>(); while (iterator.hasNext()) { JanusGraphVertex vertex = iterator.next(); - Map newProp = getVertexProperties(vertex); + getVertexProperties(vertex); GraphVertex graphVertex = createAndFill(vertex, parseFlag); result.add(graphVertex); } - if (logger.isDebugEnabled()) { - logger - .debug("Number of fetced nodes in graph for criteria : from type = {} and properties = {} is {}", type, props, result.size()); - } + logger.debug("Number of fetched nodes in graph for criteria : from type = {} and properties = {} is {}", type, props, result.size()); if (result.size() == 0) { return Either.right(JanusGraphOperationStatus.NOT_FOUND); } return Either.left(result); } catch (Exception e) { - if (logger.isDebugEnabled()) { - logger.debug("Failed get by criteria for type = {} and properties = {}", type, props, e); - } + logger.debug("Failed get by criteria for type = {} and properties = {}", type, props, e); return Either.right(JanusGraphClient.handleJanusGraphException(e)); } } else { - if (logger.isDebugEnabled()) { - logger.debug("Failed get by criteria for type ={} and properties = {} error : {}", type, props, graph.right().value()); - } + logger.debug("Failed get by criteria for type ={} and properties = {} error : {}", type, props, graph.right().value()); return Either.right(graph.right().value()); } } - + public Either, JanusGraphOperationStatus> getByCriteria(final VertexTypeEnum type, final Map props, - final Map hasNotProps, - final JsonParseFlagEnum parseFlag, - final String model) { + final Map hasNotProps, + final JsonParseFlagEnum parseFlag, + final String model) { return getByCriteria(type, props, hasNotProps, null, parseFlag, model, false); } @@ -461,14 +438,14 @@ public class JanusGraphDao { final boolean includeNormativeExtensionModels) { return getByCriteria(type, props, hasNotProps, null, parseFlag, model, includeNormativeExtensionModels); } - + public Either, JanusGraphOperationStatus> getByCriteria(final VertexTypeEnum type, - final Map hasProps, - final Map hasNotProps, - final Map> predicates, - final JsonParseFlagEnum parseFlag, - final String model) { - return getByCriteria(type, hasProps, hasNotProps, predicates, parseFlag, model, false); + final Map hasProps, + final Map hasNotProps, + final Map> predicates, + final JsonParseFlagEnum parseFlag, + final String model) { + return getByCriteria(type, hasProps, hasNotProps, predicates, parseFlag, model, false); } public Either, JanusGraphOperationStatus> getByCriteria(final VertexTypeEnum type, @@ -481,137 +458,74 @@ public class JanusGraphDao { Either graph = janusGraphClient.getGraph(); if (graph.isLeft()) { try { - JanusGraph tGraph = graph.left().value(); - JanusGraphQuery query = tGraph.query(); - - if (type != null) { - query = query.has(GraphPropertyEnum.LABEL.getProperty(), type.getName()); - } - if (hasProps != null && !hasProps.isEmpty()) { - for (Map.Entry entry : hasProps.entrySet()) { - query = query.has(entry.getKey().getProperty(), entry.getValue()); - } - } - if (hasNotProps != null && !hasNotProps.isEmpty()) { - for (Map.Entry entry : hasNotProps.entrySet()) { - if (entry.getValue() instanceof List) { - buildMultipleNegateQueryFromList(entry, query); - } else { - query = query.hasNot(entry.getKey().getProperty(), entry.getValue()); - } - } - } - if (predicates != null && !predicates.isEmpty()) { - for (Map.Entry> entry : predicates.entrySet()) { - JanusGraphPredicate predicate = entry.getValue().getKey(); - Object object = entry.getValue().getValue(); - query = query.has(entry.getKey(), predicate, object); - } - } - Iterable vertices = query.vertices(); + JanusGraphQuery query = getJanusGraphQuery(type, hasProps, hasNotProps, predicates, graph.left().value()); + final Iterable vertices = query.vertices(); if (vertices == null || !vertices.iterator().hasNext()) { return Either.right(JanusGraphOperationStatus.NOT_FOUND); } - List result = new ArrayList<>(); - final Predicate filterPredicate = StringUtils.isEmpty(model) ? this::vertexNotConnectedToAnyModel : vertex -> vertexValidForModel(vertex, model, includeNormativeExtensionModels); - final List verticesForModel = StreamSupport.stream(vertices.spliterator(), false).filter(filterPredicate).collect(Collectors.toList()); - if (verticesForModel == null || verticesForModel.size() == 0) { + final Predicate filterPredicate = StringUtils.isEmpty(model) ? this::vertexNotConnectedToAnyModel + : vertex -> vertexValidForModel(vertex, model, includeNormativeExtensionModels); + final List verticesForModel = StreamSupport.stream(vertices.spliterator(), false).filter(filterPredicate) + .collect(Collectors.toList()); + if (CollectionUtils.isEmpty(verticesForModel)) { return Either.right(JanusGraphOperationStatus.NOT_FOUND); } - - verticesForModel.forEach(vertex -> result.add(createAndFill(vertex, parseFlag))); - if (logger.isDebugEnabled()) { - logger.debug("Number of fetched nodes in graph for criteria : from type '{}' and properties '{}' is '{}'", type, hasProps, - result.size()); - } + + final List result = new ArrayList<>(); + verticesForModel.forEach(vertex -> result.add(createAndFill(vertex, parseFlag))); + logger.debug("Number of fetched nodes in graph for criteria : from type '{}' and properties '{}' is '{}'", type, hasProps, + result.size()); return Either.left(result); } catch (Exception e) { - if (logger.isDebugEnabled()) { - logger.debug("Failed to get by criteria for type '{}' and properties '{}'", type, hasProps, e); - } + logger.debug(FAILED_TO_GET_BY_CRITERIA_FOR_TYPE_AND_PROPERTIES, type, hasProps, e); return Either.right(JanusGraphClient.handleJanusGraphException(e)); } } else { - if (logger.isDebugEnabled()) { - logger.debug("Failed to get by criteria for type '{}' and properties '{}'. Error : '{}'", type, hasProps, graph.right().value()); - } + logger.debug("Failed to get by criteria for type '{}' and properties '{}'. Error : '{}'", type, hasProps, graph.right().value()); return Either.right(graph.right().value()); } } - + public Either, JanusGraphOperationStatus> getByCriteria(VertexTypeEnum type, - Map props, Map hasNotProps, - JsonParseFlagEnum parseFlag) { + Map props, + Map hasNotProps, + JsonParseFlagEnum parseFlag) { return getByCriteria(type, props, hasNotProps, null, parseFlag); } public Either, JanusGraphOperationStatus> getByCriteria(final VertexTypeEnum type, - final Map hasProps, final Map hasNotProps, - final Map> predicates, final JsonParseFlagEnum parseFlag) { - Either graph = janusGraphClient.getGraph(); + final Map hasProps, + final Map hasNotProps, + final Map> predicates, + final JsonParseFlagEnum parseFlag) { + final Either graph = janusGraphClient.getGraph(); if (graph.isLeft()) { try { - JanusGraph tGraph = graph.left().value(); - JanusGraphQuery query = tGraph.query(); - - if (type != null) { - query = query.has(GraphPropertyEnum.LABEL.getProperty(), type.getName()); - } - if (hasProps != null && !hasProps.isEmpty()) { - for (Map.Entry entry : hasProps.entrySet()) { - query = query.has(entry.getKey().getProperty(), entry.getValue()); - } - } - if (hasNotProps != null && !hasNotProps.isEmpty()) { - for (Map.Entry entry : hasNotProps.entrySet()) { - if (entry.getValue() instanceof List) { - buildMultipleNegateQueryFromList(entry, query); - } else { - query = query.hasNot(entry.getKey().getProperty(), entry.getValue()); - } - } - } - if (predicates != null && !predicates.isEmpty()) { - for (Map.Entry> entry : predicates.entrySet()) { - JanusGraphPredicate predicate = entry.getValue().getKey(); - Object object = entry.getValue().getValue(); - query = query.has(entry.getKey(), predicate, object); - } - } + JanusGraphQuery query = getJanusGraphQuery(type, hasProps, hasNotProps, predicates, graph.left().value()); Iterable vertices = query.vertices(); if (vertices == null || !vertices.iterator().hasNext()) { return Either.right(JanusGraphOperationStatus.NOT_FOUND); } List result = new ArrayList<>(); - vertices.forEach(vertex -> result.add(createAndFill(vertex, parseFlag))); - if (logger.isDebugEnabled()) { - logger.debug( - "Number of fetched nodes in graph for criteria : from type '{}' and properties '{}' is '{}'", - type, hasProps, result.size()); - } + logger.debug("Number of fetched nodes in graph for criteria : from type '{}' and properties '{}' is '{}'", type, hasProps, + result.size()); return Either.left(result); } catch (Exception e) { - if (logger.isDebugEnabled()) { - logger.debug("Failed to get by criteria for type '{}' and properties '{}'", type, hasProps, e); - } + logger.debug(FAILED_TO_GET_BY_CRITERIA_FOR_TYPE_AND_PROPERTIES, type, hasProps, e); return Either.right(JanusGraphClient.handleJanusGraphException(e)); } } else { - if (logger.isDebugEnabled()) { - logger.debug("Failed to get by criteria for type '{}' and properties '{}'. Error : '{}'", type, - hasProps, graph.right().value()); - } + logger.debug("Failed to get by criteria for type '{}' and properties '{}'. Error : '{}'", type, hasProps, graph.right().value()); return Either.right(graph.right().value()); } } - /** * Finds the vertices that have the given invariant id and any additional property provided. * - * @param invariantUuid the invariant uuid + * @param invariantUuid the invariant uuid * @param additionalPropertiesToMatch any additional property to match along with the {@link GraphPropertyEnum#INVARIANT_UUID} * @return the list of vertex that has the given invariant uuid * @throws JanusGraphException if the find operation was returned an error status @@ -624,7 +538,7 @@ public class JanusGraphDao { } propertiesToMatch.put(GraphPropertyEnum.INVARIANT_UUID, invariantUuid); final Either, JanusGraphOperationStatus> vertexEither = - getByCriteria(null, propertiesToMatch, JsonParseFlagEnum.ParseMetadata); + getByCriteria(null, propertiesToMatch, JsonParseFlagEnum.ParseMetadata); if (vertexEither.isRight()) { final JanusGraphOperationStatus status = vertexEither.right().value(); if (status == JanusGraphOperationStatus.NOT_FOUND) { @@ -641,49 +555,52 @@ public class JanusGraphDao { } private boolean vertexValidForModel(final JanusGraphVertex vertex, final String model, final boolean includeNormativeExtensions) { - final String vertexLabel = (String)vertex.property(GraphPropertyEnum.LABEL.getProperty()).value(); + final String vertexLabel = (String) vertex.property(GraphPropertyEnum.LABEL.getProperty()).value(); final VertexTypeEnum vertexType = VertexTypeEnum.getByName(vertexLabel); - final GraphEdgeLabels edgeLabel = vertexType.equals(VertexTypeEnum.TOPOLOGY_TEMPLATE) ? GraphEdgeLabels.MODEL : GraphEdgeLabels.MODEL_ELEMENT; - final Either>, JanusGraphOperationStatus> modelVertices = getParentVerticies(vertex, edgeLabel); + final GraphEdgeLabels edgeLabel = VertexTypeEnum.TOPOLOGY_TEMPLATE.equals(vertexType) ? GraphEdgeLabels.MODEL : GraphEdgeLabels.MODEL_ELEMENT; + final Either>, JanusGraphOperationStatus> modelVertices = getParentVertices(vertex, edgeLabel); - return modelVertices.isLeft() && modelVertices.left().value().stream().anyMatch(vertexPair -> modelVertexMatchesModel(vertexPair.getLeft(), model, includeNormativeExtensions)); + return modelVertices.isLeft() && modelVertices.left().value().stream() + .anyMatch(vertexPair -> modelVertexMatchesModel(vertexPair.getLeft(), model, includeNormativeExtensions)); } - + private boolean modelVertexMatchesModel(final JanusGraphVertex modelVertex, final String model, final boolean includeNormativeExtensions) { - if (model.equals((String)modelVertex.property("name").value())) { + if (model.equals((String) modelVertex.property("name").value())) { return true; } final Either>, JanusGraphOperationStatus> derivedModels = - getParentVerticies(modelVertex, GraphEdgeLabels.DERIVED_FROM); - if (derivedModels.isLeft() && derivedModels.left().value().stream().anyMatch(derivedModel ->modelVertexMatchesModel(derivedModel.left, model, includeNormativeExtensions))) { + getParentVertices(modelVertex, GraphEdgeLabels.DERIVED_FROM); + if (derivedModels.isLeft() && derivedModels.left().value().stream() + .anyMatch(derivedModel -> modelVertexMatchesModel(derivedModel.left, model, includeNormativeExtensions))) { return true; } - + if (includeNormativeExtensions && isANormativeExtension(modelVertex)) { - final Either,JanusGraphOperationStatus> derivedFromModels = - getChildrenVertices(modelVertex, EdgeLabelEnum.DERIVED_FROM, JsonParseFlagEnum.ParseAll); - return derivedFromModels.isLeft() && derivedFromModels.left().value().stream().anyMatch(derivedFromModel -> model.equals((String)derivedFromModel.property("name").value())); + final Either, JanusGraphOperationStatus> derivedFromModels = + getChildrenVertices(modelVertex, EdgeLabelEnum.DERIVED_FROM); + return derivedFromModels.isLeft() && derivedFromModels.left().value().stream() + .anyMatch(derivedFromModel -> model.equals((String) derivedFromModel.property("name").value())); } return false; } - + private boolean isANormativeExtension(final JanusGraphVertex modelVertex) { - return ModelTypeEnum.NORMATIVE_EXTENSION.getValue().equals((String)modelVertex.property(GraphPropertyEnum.MODEL_TYPE.getProperty()).value()); + return ModelTypeEnum.NORMATIVE_EXTENSION.getValue().equals((String) modelVertex.property(GraphPropertyEnum.MODEL_TYPE.getProperty()).value()); } - - private Either>, JanusGraphOperationStatus> getParentVerticies( - final JanusGraphVertex rootVertex, final GraphEdgeLabels edgeType) { - return getEdgeVerticies(rootVertex, Direction.IN, edgeType); + + private Either>, JanusGraphOperationStatus> getParentVertices( + final JanusGraphVertex rootVertex, final GraphEdgeLabels edgeType) { + return getEdgeVertices(rootVertex, Direction.IN, edgeType); } - - private Either>, JanusGraphOperationStatus> getEdgeVerticies( - final JanusGraphVertex rootVertex, final Direction direction, final GraphEdgeLabels edgeType) { + + private Either>, JanusGraphOperationStatus> getEdgeVertices( + final JanusGraphVertex rootVertex, final Direction direction, final GraphEdgeLabels edgeType) { final List> immutablePairs = new ArrayList<>(); final Iterator edgesCreatorIterator = rootVertex.edges(direction, edgeType.getProperty()); if (edgesCreatorIterator != null) { while (edgesCreatorIterator.hasNext()) { Edge edge = edgesCreatorIterator.next(); - JanusGraphVertex vertex = Direction.OUT.equals(direction)? (JanusGraphVertex) edge.inVertex() : (JanusGraphVertex) edge.outVertex(); + JanusGraphVertex vertex = Direction.OUT.equals(direction) ? (JanusGraphVertex) edge.inVertex() : (JanusGraphVertex) edge.outVertex(); ImmutablePair immutablePair = new ImmutablePair<>(vertex, edge); immutablePairs.add(immutablePair); } @@ -693,15 +610,15 @@ public class JanusGraphDao { } return Either.left(immutablePairs); } - + private boolean vertexNotConnectedToAnyModel(final JanusGraphVertex vertex) { - String vt = (String)vertex.property(GraphPropertyEnum.LABEL.getProperty()).value(); + String vt = (String) vertex.property(GraphPropertyEnum.LABEL.getProperty()).value(); VertexTypeEnum vertexType = VertexTypeEnum.getByName(vt); - EdgeLabelEnum edgeLabel = vertexType.equals(VertexTypeEnum.TOPOLOGY_TEMPLATE) ? EdgeLabelEnum.MODEL : EdgeLabelEnum.MODEL_ELEMENT; + EdgeLabelEnum edgeLabel = VertexTypeEnum.TOPOLOGY_TEMPLATE.equals(vertexType) ? EdgeLabelEnum.MODEL : EdgeLabelEnum.MODEL_ELEMENT; return !vertex.edges(Direction.IN, edgeLabel.name()).hasNext(); } - public Either, JanusGraphOperationStatus> getCatalogOrArchiveVerticies(boolean isCatalog) { + public Either, JanusGraphOperationStatus> getCatalogOrArchiveVertices(boolean isCatalog) { Either graph = janusGraphClient.getGraph(); if (graph.isLeft()) { try { @@ -721,22 +638,17 @@ public class JanusGraphDao { Iterator vertices = catalogV.vertices(Direction.OUT, edgeLabel); return Either.left(vertices); } catch (Exception e) { - if (logger.isDebugEnabled()) { - logger.debug("Failed get by criteria: ", e); - } + logger.debug("Failed get by criteria: ", e); return Either.right(JanusGraphClient.handleJanusGraphException(e)); } } else { - if (logger.isDebugEnabled()) { - logger.debug("Failed get by criteria : ", graph.right().value()); - } + logger.debug("Failed get by criteria : {}", graph.right().value()); return Either.right(graph.right().value()); } } private void buildMultipleNegateQueryFromList(Map.Entry entry, JanusGraphQuery query) { - List negateList = (List) entry.getValue(); - for (Object listItem : negateList) { + for (Object listItem : (List) entry.getValue()) { query.hasNot(entry.getKey().getProperty(), listItem); } } @@ -749,48 +661,47 @@ public class JanusGraphDao { */ public Either getChildVertex(GraphVertex parentVertex, EdgeLabelEnum edgeLabel, JsonParseFlagEnum parseFlag) { - Either, JanusGraphOperationStatus> childrenVertecies = getChildrenVertices(parentVertex, edgeLabel, parseFlag); - if (childrenVertecies.isRight()) { - return Either.right(childrenVertecies.right().value()); + Either, JanusGraphOperationStatus> childrenVertices = getChildrenVertices(parentVertex, edgeLabel, parseFlag); + if (childrenVertices.isRight()) { + return Either.right(childrenVertices.right().value()); } - return Either.left(childrenVertecies.left().value().get(0)); + return Either.left(childrenVertices.left().value().get(0)); } /** * @param parentVertex * @param edgeLabel - * @param parseFlag * @return */ - public Either getChildVertex(Vertex parentVertex, EdgeLabelEnum edgeLabel, JsonParseFlagEnum parseFlag) { - Either, JanusGraphOperationStatus> childrenVertecies = getChildrenVertices(parentVertex, edgeLabel, parseFlag); - if (childrenVertecies.isRight()) { - return Either.right(childrenVertecies.right().value()); + public Either getChildVertex(Vertex parentVertex, EdgeLabelEnum edgeLabel) { + Either, JanusGraphOperationStatus> childrenVertices = getChildrenVertices(parentVertex, edgeLabel); + if (childrenVertices.isRight()) { + return Either.right(childrenVertices.right().value()); } - return Either.left(childrenVertecies.left().value().get(0)); + return Either.left(childrenVertices.left().value().get(0)); } public Either getParentVertex(GraphVertex parentVertex, EdgeLabelEnum edgeLabel, JsonParseFlagEnum parseFlag) { - Either, JanusGraphOperationStatus> childrenVertecies = getParentVertices(parentVertex, edgeLabel, parseFlag); - if (childrenVertecies.isRight()) { - return Either.right(childrenVertecies.right().value()); + Either, JanusGraphOperationStatus> childrenVertices = getParentVertices(parentVertex, edgeLabel, parseFlag); + if (childrenVertices.isRight()) { + return Either.right(childrenVertices.right().value()); } - if (isEmpty(childrenVertecies.left().value())) { + if (isEmpty(childrenVertices.left().value())) { return Either.right(JanusGraphOperationStatus.NOT_FOUND); } - return Either.left(childrenVertecies.left().value().get(0)); + return Either.left(childrenVertices.left().value().get(0)); } - public Either getParentVertex(Vertex parentVertex, EdgeLabelEnum edgeLabel, JsonParseFlagEnum parseFlag) { - Either, JanusGraphOperationStatus> childrenVertecies = getParentVertices(parentVertex, edgeLabel, parseFlag); - if (childrenVertecies.isRight()) { - return Either.right(childrenVertecies.right().value()); + public Either getParentVertex(Vertex parentVertex, EdgeLabelEnum edgeLabel) { + Either, JanusGraphOperationStatus> childrenVertices = getParentVertices(parentVertex, edgeLabel); + if (childrenVertices.isRight()) { + return Either.right(childrenVertices.right().value()); } - if (isEmpty(childrenVertecies.left().value())) { + if (isEmpty(childrenVertices.left().value())) { return Either.right(JanusGraphOperationStatus.NOT_FOUND); } - return Either.left(childrenVertecies.left().value().get(0)); + return Either.left(childrenVertices.left().value().get(0)); } /** @@ -809,13 +720,11 @@ public class JanusGraphDao { return getAdjacentVertices(parentVertex, edgeLabel, parseFlag, Direction.IN); } - public Either, JanusGraphOperationStatus> getParentVertices(Vertex parentVertex, EdgeLabelEnum edgeLabel, - JsonParseFlagEnum parseFlag) { - return getAdjacentVertices(parentVertex, edgeLabel, parseFlag, Direction.IN); + public Either, JanusGraphOperationStatus> getParentVertices(Vertex parentVertex, EdgeLabelEnum edgeLabel) { + return getAdjacentVertices(parentVertex, edgeLabel, Direction.IN); } - private Either, JanusGraphOperationStatus> getAdjacentVertices(Vertex parentVertex, EdgeLabelEnum edgeLabel, - JsonParseFlagEnum parseFlag, Direction direction) { + private Either, JanusGraphOperationStatus> getAdjacentVertices(Vertex parentVertex, EdgeLabelEnum edgeLabel, Direction direction) { List list = new ArrayList<>(); try { Either graphRes = janusGraphClient.getGraph(); @@ -833,7 +742,6 @@ public class JanusGraphDao { } else { vertex = (JanusGraphVertex) edge.inVertex(); } - // GraphVertex graphVertex = createAndFill(vertex, parseFlag); list.add(vertex); } } @@ -850,25 +758,21 @@ public class JanusGraphDao { /** * @param parentVertex * @param edgeLabel - * @param parseFlag * @return */ - public Either, JanusGraphOperationStatus> getChildrenVertices(Vertex parentVertex, EdgeLabelEnum edgeLabel, - JsonParseFlagEnum parseFlag) { - return getAdjacentVertices(parentVertex, edgeLabel, parseFlag, Direction.OUT); + public Either, JanusGraphOperationStatus> getChildrenVertices(Vertex parentVertex, EdgeLabelEnum edgeLabel) { + return getAdjacentVertices(parentVertex, edgeLabel, Direction.OUT); } private Either, JanusGraphOperationStatus> getAdjacentVertices(GraphVertex parentVertex, EdgeLabelEnum edgeLabel, JsonParseFlagEnum parseFlag, Direction direction) { List list = new ArrayList<>(); - Either, JanusGraphOperationStatus> adjacentVerticies = getAdjacentVertices(parentVertex.getVertex(), edgeLabel, parseFlag, + Either, JanusGraphOperationStatus> adjacentVertices = getAdjacentVertices(parentVertex.getVertex(), edgeLabel, direction); - if (adjacentVerticies.isRight()) { - return Either.right(adjacentVerticies.right().value()); + if (adjacentVertices.isRight()) { + return Either.right(adjacentVertices.right().value()); } - adjacentVerticies.left().value().stream().forEach(vertex -> { - list.add(createAndFill((JanusGraphVertex) vertex, parseFlag)); - }); + adjacentVertices.left().value().stream().forEach(vertex -> list.add(createAndFill((JanusGraphVertex) vertex, parseFlag))); return Either.left(list); } @@ -990,15 +894,15 @@ public class JanusGraphDao { * @return */ public Either deleteEdge(GraphVertex fromVertex, GraphVertex toVertex, EdgeLabelEnum label) { - return deleteEdge(fromVertex.getVertex(), toVertex.getVertex(), label, fromVertex.getUniqueId(), toVertex.getUniqueId(), false); + return deleteEdge(fromVertex.getVertex(), label, fromVertex.getUniqueId(), toVertex.getUniqueId(), false); } public Either deleteAllEdges(GraphVertex fromVertex, GraphVertex toVertex, EdgeLabelEnum label) { - return deleteEdge(fromVertex.getVertex(), toVertex.getVertex(), label, fromVertex.getUniqueId(), toVertex.getUniqueId(), true); + return deleteEdge(fromVertex.getVertex(), label, fromVertex.getUniqueId(), toVertex.getUniqueId(), true); } - public Either deleteEdge(JanusGraphVertex fromVertex, JanusGraphVertex toVertex, EdgeLabelEnum label, - String uniqueIdFrom, String uniqueIdTo, boolean deleteAll) { + private Either deleteEdge(JanusGraphVertex fromVertex, EdgeLabelEnum label, + String uniqueIdFrom, String uniqueIdTo, boolean deleteAll) { Either result = null; Vertex problemV = null; try { @@ -1012,16 +916,15 @@ public class JanusGraphDao { currVertexUniqueId = edge.inVertex().value(GraphPropertyEnum.UNIQUE_ID.getProperty()); } catch (Exception e) { // AutoHealing procedure - logger.info("Corrupted vertex and edge were found and deleted {}", e); + logger.info("Corrupted vertex and edge were found and deleted ", e); if (problemV != null) { Map props = getVertexProperties(problemV); logger.debug("problematic Vertex properties:"); logger.debug("props size: {}", props.size()); for (Map.Entry entry : props.entrySet()) { - logger.debug("{}{}", entry.getKey() + ":" + entry.getValue()); + logger.debug("{}:{}", entry.getKey(), entry.getValue()); } - Either, JanusGraphOperationStatus> childrenVertices = getChildrenVertices(problemV, EdgeLabelEnum.VERSION, - JsonParseFlagEnum.NoParse); + Either, JanusGraphOperationStatus> childrenVertices = getChildrenVertices(problemV, EdgeLabelEnum.VERSION); if (childrenVertices.isLeft()) { childrenVertices.left().value().size(); logger.debug("number of children that problematic Vertex has: {}", props.size()); @@ -1029,12 +932,12 @@ public class JanusGraphDao { try { edge.remove(); } catch (Exception e1) { - logger.debug("failed to remove problematic edge. {}", e1); + logger.debug("failed to remove problematic edge.", e1); } try { problemV.remove(); } catch (Exception e2) { - logger.debug("failed to remove problematic vertex . {}", e2); + logger.debug("failed to remove problematic vertex.", e2); } } continue; @@ -1275,7 +1178,7 @@ public class JanusGraphDao { public JanusGraphOperationStatus moveEdge(GraphVertex vertexA, GraphVertex vertexB, EdgeLabelEnum label, Direction direction) { JanusGraphOperationStatus result = deleteEdgeByDirection(vertexA, direction, label); if (result != JanusGraphOperationStatus.OK) { - logger.error("Failed to diassociate {} from element {}. error {} ", label, vertexA.getUniqueId(), result); + logger.error("Failed to disassociate {} from element {}. error {} ", label, vertexA.getUniqueId(), result); return result; } JanusGraphOperationStatus createRelation; @@ -1298,4 +1201,35 @@ public class JanusGraphDao { } return getBelongingEdgeByCriteria(getVertexRes.left().value(), label, properties); } + + private JanusGraphQuery getJanusGraphQuery(final VertexTypeEnum type, final Map hasProps, + final Map hasNotProps, + final Map> predicates, + final JanusGraph graph) { + JanusGraphQuery query = graph.query(); + + if (type != null) { + query = query.has(GraphPropertyEnum.LABEL.getProperty(), type.getName()); + } + if (hasProps != null && !hasProps.isEmpty()) { + for (Entry entry : hasProps.entrySet()) { + query = query.has(entry.getKey().getProperty(), entry.getValue()); + } + } + if (hasNotProps != null && !hasNotProps.isEmpty()) { + for (Entry entry : hasNotProps.entrySet()) { + if (entry.getValue() instanceof List) { + buildMultipleNegateQueryFromList(entry, query); + } else { + query = query.hasNot(entry.getKey().getProperty(), entry.getValue()); + } + } + } + if (predicates != null && !predicates.isEmpty()) { + for (Entry> entry : predicates.entrySet()) { + query = query.has(entry.getKey(), entry.getValue().getKey(), entry.getValue().getValue()); + } + } + return query; + } } diff --git a/catalog-dao/src/test/java/org/openecomp/sdc/be/dao/janusgraph/JanusGraphDaoMockTest.java b/catalog-dao/src/test/java/org/openecomp/sdc/be/dao/janusgraph/JanusGraphDaoMockTest.java index 7a0152df60..c896db6a45 100644 --- a/catalog-dao/src/test/java/org/openecomp/sdc/be/dao/janusgraph/JanusGraphDaoMockTest.java +++ b/catalog-dao/src/test/java/org/openecomp/sdc/be/dao/janusgraph/JanusGraphDaoMockTest.java @@ -82,7 +82,7 @@ class JanusGraphDaoMockTest extends DAOConfDependentTest { // default test - result = testSubject.getGraph(); + result = testSubject.getJanusGraphClient().getGraph(); } @Test @@ -268,7 +268,7 @@ class JanusGraphDaoMockTest extends DAOConfDependentTest { Mockito.when(janusGraphClient.getGraph()).thenReturn(value); // default test - result = testSubject.getCatalogOrArchiveVerticies(true); + result = testSubject.getCatalogOrArchiveVertices(true); } @Test @@ -300,7 +300,7 @@ class JanusGraphDaoMockTest extends DAOConfDependentTest { Mockito.when(janusGraphClient.getGraph()).thenReturn(value); // default test - result = testSubject.getChildVertex(parentVertex, edgeLabel, parseFlag); + result = testSubject.getChildVertex(parentVertex, edgeLabel); } @Test @@ -313,7 +313,7 @@ class JanusGraphDaoMockTest extends DAOConfDependentTest { // default test - result = testSubject.getParentVertex(parentVertex, edgeLabel, parseFlag); + result = testSubject.getParentVertex(parentVertex, edgeLabel); } @Test @@ -326,7 +326,7 @@ class JanusGraphDaoMockTest extends DAOConfDependentTest { // default test - result = testSubject.getParentVertices(parentVertex, edgeLabel, parseFlag); + result = testSubject.getParentVertices(parentVertex, edgeLabel); } @Test @@ -339,7 +339,7 @@ class JanusGraphDaoMockTest extends DAOConfDependentTest { // default test - result = testSubject.getChildrenVertices(parentVertex, edgeLabel, parseFlag); + result = testSubject.getChildrenVertices(parentVertex, edgeLabel); } @Test diff --git a/catalog-dao/src/test/java/org/openecomp/sdc/be/dao/janusgraph/JanusGraphDaoTest.java b/catalog-dao/src/test/java/org/openecomp/sdc/be/dao/janusgraph/JanusGraphDaoTest.java index fae514a2df..9faee6c390 100644 --- a/catalog-dao/src/test/java/org/openecomp/sdc/be/dao/janusgraph/JanusGraphDaoTest.java +++ b/catalog-dao/src/test/java/org/openecomp/sdc/be/dao/janusgraph/JanusGraphDaoTest.java @@ -41,22 +41,19 @@ import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum; import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum; import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; import org.openecomp.sdc.be.utils.DAOConfDependentTest; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; class JanusGraphDaoTest extends DAOConfDependentTest { - private static final Logger logger = LoggerFactory.getLogger(JanusGraphDaoTest.class); private final JanusGraphDao dao = new JanusGraphDao(new JanusGraphClient(new DAOJanusGraphStrategy())); @BeforeEach void init() { - dao.janusGraphClient.createGraph(); + dao.getJanusGraphClient().createGraph(); } @AfterEach void end() { - dao.janusGraphClient.cleanupGraph(); + dao.getJanusGraphClient().cleanupGraph(); } @Test @@ -106,7 +103,7 @@ class JanusGraphDaoTest extends DAOConfDependentTest { // default test - result = dao.getGraph(); + result = dao.getJanusGraphClient().getGraph(); } @Test @@ -237,7 +234,7 @@ class JanusGraphDaoTest extends DAOConfDependentTest { // default test - result = dao.getCatalogOrArchiveVerticies(true); + result = dao.getCatalogOrArchiveVertices(true); } @Test @@ -250,7 +247,7 @@ class JanusGraphDaoTest extends DAOConfDependentTest { // default test - result = dao.getParentVertices(parentVertex, edgeLabel, parseFlag); + result = dao.getParentVertices(parentVertex, edgeLabel); } @Test @@ -263,7 +260,7 @@ class JanusGraphDaoTest extends DAOConfDependentTest { // default test - result = dao.getChildrenVertices(parentVertex, edgeLabel, parseFlag); + result = dao.getChildrenVertices(parentVertex, edgeLabel); } @Test -- cgit 1.2.3-korg