diff options
14 files changed, 286 insertions, 79 deletions
diff --git a/src/main/java/org/onap/crud/dao/GraphDao.java b/src/main/java/org/onap/crud/dao/GraphDao.java index 6c1bca9..7cb3d4c 100644 --- a/src/main/java/org/onap/crud/dao/GraphDao.java +++ b/src/main/java/org/onap/crud/dao/GraphDao.java @@ -42,10 +42,12 @@ public interface GraphDao { * - The unique identifier of the vertex to retrieve the edges for. * @param queryParams * - query parameters to be passed + * @param txid + * - a DB transaction ID to use (if null, no transactionId is used) * @return - A collection of edges. * @throws CrudException */ - public List<Edge> getVertexEdges(String id, Map<String, String> queryParams) throws CrudException; + public List<Edge> getVertexEdges(String id, Map<String, String> queryParams, String txId) throws CrudException; /** * Retrieve a collection of {@link Vertex} objects which match the supplied @@ -89,7 +91,7 @@ public interface GraphDao { * @throws CrudException */ public OperationResult getEdge(String id, String type, Map<String, String> queryParams) throws CrudException; - + /** * Retrieve a collection of {@link Edge} objects with a given type and which * match a set of supplied filter parameters. @@ -172,7 +174,7 @@ public interface GraphDao { * - The unique identifier of the edge to be deleted. * @throws CrudException */ - public void deleteEdge(String id, String type) throws CrudException; + public void deleteEdge(String id) throws CrudException; public String openTransaction(); @@ -193,7 +195,9 @@ public interface GraphDao { public void deleteVertex(String id, String type, String txId) throws CrudException; - public void deleteEdge(String id, String type, String txId) throws CrudException; + public void deleteEdge(String id, String txId) throws CrudException; - public Edge getEdge(String id, String type, String txId) throws CrudException; + public Edge getEdge(String id, String txId) throws CrudException; + + public Edge getEdge(String id) throws CrudException; } diff --git a/src/main/java/org/onap/crud/dao/champ/ChampDao.java b/src/main/java/org/onap/crud/dao/champ/ChampDao.java index fd5de57..81980cc 100644 --- a/src/main/java/org/onap/crud/dao/champ/ChampDao.java +++ b/src/main/java/org/onap/crud/dao/champ/ChampDao.java @@ -141,26 +141,39 @@ public class ChampDao implements GraphDao { } @Override - public List<Edge> getVertexEdges(String id, Map<String, String> queryParams) throws CrudException { - StringBuilder strBuild = new StringBuilder(baseObjectUrl); - strBuild.append("/relationships/"); - strBuild.append(id); - if(queryParams != null && !queryParams.isEmpty()) - { - strBuild.append("?"); - strBuild.append(URLEncodedUtils.format(convertToNameValuePair(queryParams), Charset.defaultCharset())); - } + public List<Edge> getVertexEdges(String id, Map<String, String> queryParams, String txId) throws CrudException { + StringBuilder strBuild = new StringBuilder(baseObjectUrl); + strBuild.append("/relationships/"); + strBuild.append(id); + + Map<String,String> queryParamsCopy = null; + if (queryParams != null) { + queryParamsCopy = new HashMap<String,String>(queryParams); + } + else { + queryParamsCopy = new HashMap<String,String>(); + } - OperationResult getResult = client.get(strBuild.toString(), createHeader(), MediaType.APPLICATION_JSON_TYPE); + if (txId != null) { + queryParamsCopy.put("transactionId", txId); + } - if (getResult.getResultCode() == 200) { - return champGson.fromJson(getResult.getResult(), new TypeToken<List<Edge>>() { - }.getType()); - } else { - // We didn't find a vertex with the supplied id, so just throw an - // exception. - throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No vertex with id " + id + " found in graph"); - } + if (!queryParamsCopy.isEmpty()) + { + strBuild.append("?"); + strBuild.append(URLEncodedUtils.format(convertToNameValuePair(queryParamsCopy), Charset.defaultCharset())); + } + + OperationResult getResult = client.get(strBuild.toString(), createHeader(), MediaType.APPLICATION_JSON_TYPE); + + if (getResult.getResultCode() == 200) { + return champGson.fromJson(getResult.getResult(), new TypeToken<List<Edge>>() { + }.getType()); + } else { + // We didn't find a vertex with the supplied id, so just throw an + // exception. + throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No vertex with id " + id + " found in graph"); + } } @Override @@ -343,7 +356,7 @@ public class ChampDao implements GraphDao { } @Override - public void deleteEdge(String id, String type) throws CrudException { + public void deleteEdge(String id) throws CrudException { String url = baseRelationshipUrl + "/" + id; OperationResult getResult = client.delete(url, createHeader(), MediaType.APPLICATION_JSON_TYPE); @@ -512,7 +525,7 @@ public class ChampDao implements GraphDao { } @Override - public void deleteEdge(String id, String type, String txId) throws CrudException { + public void deleteEdge(String id, String txId) throws CrudException { String url = baseRelationshipUrl + "/" + id + "?transactionId=" + txId; OperationResult getResult = client.delete(url, createHeader(), MediaType.APPLICATION_JSON_TYPE); @@ -524,19 +537,12 @@ public class ChampDao implements GraphDao { } @Override - public Edge getEdge(String id, String type, String txId) throws CrudException { + public Edge getEdge(String id, String txId) throws CrudException { String url = baseRelationshipUrl + "/" + id + "?transactionId=" + txId; OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE); if (getResult.getResultCode() == 200) { Edge edge = Edge.fromJson(getResult.getResult()); - - if (!edge.getType().equalsIgnoreCase(type)) { - // We didn't find an edge with the supplied type, so just throw an - // exception. - throw new CrudException("No edge with id " + id + " and type " + type + " found in graph", - javax.ws.rs.core.Response.Status.NOT_FOUND); - } return edge; } else { // We didn't find an edge with the supplied id, so just throw an @@ -544,6 +550,20 @@ public class ChampDao implements GraphDao { throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No edge with id " + id + " found in graph"); } } + + @Override + public Edge getEdge(String id) throws CrudException { + String url = baseRelationshipUrl + "/" + id; + OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE); + + if (getResult.getResultCode() == 200) { + Edge edge = Edge.fromJson(getResult.getResult()); + return edge; + } else { + // We didn't find an edge with the supplied id, so just throw an exception. + throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No edge with id " + id + " found in graph"); + } + } public Vertex getVertex(String id, String type, String version, String txId) throws CrudException { String url = baseObjectUrl + "/" + id + "?transactionId=" + txId; diff --git a/src/main/java/org/onap/crud/service/AbstractGraphDataService.java b/src/main/java/org/onap/crud/service/AbstractGraphDataService.java index a5b52bd..59046ab 100644 --- a/src/main/java/org/onap/crud/service/AbstractGraphDataService.java +++ b/src/main/java/org/onap/crud/service/AbstractGraphDataService.java @@ -83,7 +83,7 @@ public abstract class AbstractGraphDataService { type = OxmModelValidator.resolveCollectionType(version, type); OperationResult vertexOpResult = daoForGet.getVertex(id, type, version, queryParams); Vertex vertex = Vertex.fromJson(vertexOpResult.getResult(), version); - List<Edge> edges = daoForGet.getVertexEdges(id, queryParams); + List<Edge> edges = daoForGet.getVertexEdges(id, queryParams, null); EntityTag entityTag = CrudServiceUtil.getETagFromHeader(vertexOpResult.getHeaders()); return new ImmutablePair<>(entityTag, CrudResponseBuilder.buildGetVertexResponse(OxmModelValidator.validateOutgoingPayload(version, vertex), edges, version)); @@ -117,8 +117,7 @@ public abstract class AbstractGraphDataService { EdgePayload edgePayload = EdgePayload.fromJson(item.getValue().getAsJsonObject().toString()); if (opr.getValue().getAsString().equalsIgnoreCase("delete")) { - RelationshipSchemaValidator.validateType(version, edgePayload.getType()); - deleteBulkEdge(edgePayload.getId(), version, edgePayload.getType(), txId); + deleteBulkEdge(edgePayload.getId(), version, txId); } } @@ -233,36 +232,68 @@ public abstract class AbstractGraphDataService { .setTarget("services/inventory/" + version + "/" + target.getType() + "/" + target.getId().get()); } + // If the type isn't set, resolve it based on on the sourece and target vertex types + if (edgePayload.getType() == null || edgePayload.getType().isEmpty()) { + edgePayload.setType(CrudServiceUtil.determineEdgeType(edgePayload, version)); + } + + // TODO: Champ needs to support getting an object's relationships within the context of an existing transaction. + // Currently it doesn't. Disabling multiplicity check until this happens. + + List<Edge> sourceVertexEdges = new ArrayList<Edge>(); + List<Edge> targetVertexEdges = new ArrayList<Edge>(); + + /* List<Edge> sourceVertexEdges = EdgePayloadUtil.filterEdgesByRelatedVertexAndType(EdgePayloadUtil.getVertexNodeType(edgePayload.getSource()), edgePayload.getType(), - daoForGet.getVertexEdges(EdgePayloadUtil.getVertexNodeId(edgePayload.getSource()), null)); - + dao.getVertexEdges(EdgePayloadUtil.getVertexNodeId(edgePayload.getSource()), null, txId)); + List<Edge> targetVertexEdges = EdgePayloadUtil.filterEdgesByRelatedVertexAndType(EdgePayloadUtil.getVertexNodeType(edgePayload.getTarget()), edgePayload.getType(), - daoForGet.getVertexEdges(EdgePayloadUtil.getVertexNodeId(edgePayload.getTarget()), null)); - + dao.getVertexEdges(EdgePayloadUtil.getVertexNodeId(edgePayload.getTarget()), null, txId)); + */ + validatedEdge = RelationshipSchemaValidator.validateIncomingAddPayload(version, edgePayload.getType(), edgePayload, sourceVertexEdges, targetVertexEdges); persistedEdge = addBulkEdge(validatedEdge, version, txId); } else if (opr.getValue().getAsString().equalsIgnoreCase("modify")) { - Edge edge = dao.getEdge(edgePayload.getId(), edgePayload.getType(), txId); + Edge edge = dao.getEdge(edgePayload.getId(), txId); + + // If the type isn't set, resolve it based on on the sourece and target vertex types + if (edgePayload.getType() == null || edgePayload.getType().isEmpty()) { + edgePayload.setType(edge.getType()); + } + // TODO: Champ needs to support getting an object's relationships within the context of an existing transaction. + // Currently it doesn't. Disabling multiplicity check until this happens. + + List<Edge> sourceVertexEdges = new ArrayList<Edge>(); + List<Edge> targetVertexEdges = new ArrayList<Edge>(); + + /* // load source and target vertex relationships for validation List<Edge> sourceVertexEdges = EdgePayloadUtil.filterEdgesByRelatedVertexAndType(EdgePayloadUtil.getVertexNodeType(edgePayload.getSource()), edgePayload.getType(), - daoForGet.getVertexEdges(EdgePayloadUtil.getVertexNodeId(edgePayload.getSource()), null)); + dao.getVertexEdges(EdgePayloadUtil.getVertexNodeId(edgePayload.getSource()), null, txId)); List<Edge> targetVertexEdges = EdgePayloadUtil.filterEdgesByRelatedVertexAndType(EdgePayloadUtil.getVertexNodeType(edgePayload.getTarget()), edgePayload.getType(), - daoForGet.getVertexEdges(EdgePayloadUtil.getVertexNodeId(edgePayload.getTarget()), null)); - + dao.getVertexEdges(EdgePayloadUtil.getVertexNodeId(edgePayload.getTarget()), null, txId)); + */ + validatedEdge = RelationshipSchemaValidator.validateIncomingUpdatePayload(edge, version, edgePayload, edgePayload.getType(), sourceVertexEdges, targetVertexEdges); persistedEdge = updateBulkEdge(validatedEdge, version, txId); } else { - if ( (edgePayload.getId() == null) || (edgePayload.getType() == null) ) { - throw new CrudException("id and type must be specified for patch request", Status.BAD_REQUEST); + if (edgePayload.getId() == null) { + throw new CrudException("id must be specified for patch request", Status.BAD_REQUEST); + } + Edge existingEdge = dao.getEdge(edgePayload.getId(), txId); + + // If the type isn't set, resolve it based on on the sourece and target vertex types + if (edgePayload.getType() == null || edgePayload.getType().isEmpty()) { + edgePayload.setType(existingEdge.getType()); } - Edge existingEdge = dao.getEdge(edgePayload.getId(), edgePayload.getType(), txId); + Edge patchedEdge = RelationshipSchemaValidator.validateIncomingPatchPayload(existingEdge, version, edgePayload); persistedEdge = updateBulkEdge(patchedEdge, version, txId); } @@ -312,6 +343,6 @@ public abstract class AbstractGraphDataService { protected abstract Edge addBulkEdge(Edge edge, String version, String dbTransId) throws CrudException; protected abstract Edge updateBulkEdge(Edge edge, String version, String dbTransId) throws CrudException; - protected abstract void deleteBulkEdge(String id, String version, String type, String dbTransId) throws CrudException; + protected abstract void deleteBulkEdge(String id, String version, String dbTransId) throws CrudException; } diff --git a/src/main/java/org/onap/crud/service/CrudAsyncGraphDataService.java b/src/main/java/org/onap/crud/service/CrudAsyncGraphDataService.java index aa70a66..8d147eb 100644 --- a/src/main/java/org/onap/crud/service/CrudAsyncGraphDataService.java +++ b/src/main/java/org/onap/crud/service/CrudAsyncGraphDataService.java @@ -414,7 +414,18 @@ public class CrudAsyncGraphDataService extends AbstractGraphDataService { } @Override - protected void deleteBulkEdge(String id, String version, String type, String dbTransId) throws CrudException { + protected void deleteBulkEdge(String id, String version, String dbTransId) throws CrudException { + // Get the edge type + String type = null; + try { + Edge edge = daoForGet.getEdge(id); + type = edge.getType(); + } + catch (CrudException ex) { + // Likely the client is trying to delete an edge which isn't present. Just swallow the exception + // and let the bulk request fail via the normal path. + } + GraphEvent event = GraphEvent.builder(GraphEventOperation.DELETE) .edge(new GraphEventEdge(id, version, type, null, null, null)).build(); event.setDbTransactionId(dbTransId); diff --git a/src/main/java/org/onap/crud/service/CrudGraphDataService.java b/src/main/java/org/onap/crud/service/CrudGraphDataService.java index 9ff48c7..ae2d294 100644 --- a/src/main/java/org/onap/crud/service/CrudGraphDataService.java +++ b/src/main/java/org/onap/crud/service/CrudGraphDataService.java @@ -77,11 +77,11 @@ public class CrudGraphDataService extends AbstractGraphDataService { // load source and target vertex relationships for validation List<Edge> sourceVertexEdges = EdgePayloadUtil.filterEdgesByRelatedVertexAndType(EdgePayloadUtil.getVertexNodeType(payload.getSource()), type, - daoForGet.getVertexEdges(EdgePayloadUtil.getVertexNodeId(payload.getSource()), null)); + daoForGet.getVertexEdges(EdgePayloadUtil.getVertexNodeId(payload.getSource()), null, null)); List<Edge> targetVertexEdges = EdgePayloadUtil.filterEdgesByRelatedVertexAndType(EdgePayloadUtil.getVertexNodeType(payload.getTarget()), type, - daoForGet.getVertexEdges(EdgePayloadUtil.getVertexNodeId(payload.getTarget()), null)); + daoForGet.getVertexEdges(EdgePayloadUtil.getVertexNodeId(payload.getTarget()), null, null)); Edge edge = RelationshipSchemaValidator.validateIncomingAddPayload(version, type, payload, sourceVertexEdges, targetVertexEdges); @@ -140,7 +140,7 @@ public class CrudGraphDataService extends AbstractGraphDataService { @Override public String deleteEdge(String version, String id, String type) throws CrudException { RelationshipSchemaValidator.validateType(version, type); - dao.deleteEdge(id, type); + dao.deleteEdge(id); return ""; } @@ -153,11 +153,11 @@ public class CrudGraphDataService extends AbstractGraphDataService { // load source and target vertex relationships for validation List<Edge> sourceVertexEdges = EdgePayloadUtil.filterEdgesByRelatedVertexAndType(EdgePayloadUtil.getVertexNodeType(payload.getSource()), type, - daoForGet.getVertexEdges(EdgePayloadUtil.getVertexNodeId(payload.getSource()), null)); + daoForGet.getVertexEdges(EdgePayloadUtil.getVertexNodeId(payload.getSource()), null, null)); List<Edge> targetVertexEdges = EdgePayloadUtil.filterEdgesByRelatedVertexAndType(EdgePayloadUtil.getVertexNodeType(payload.getTarget()), type, - daoForGet.getVertexEdges(EdgePayloadUtil.getVertexNodeId(payload.getTarget()), null)); + daoForGet.getVertexEdges(EdgePayloadUtil.getVertexNodeId(payload.getTarget()), null, null)); Edge validatedEdge = RelationshipSchemaValidator.validateIncomingUpdatePayload(edge, version, payload, type, sourceVertexEdges, targetVertexEdges); @@ -213,7 +213,7 @@ public class CrudGraphDataService extends AbstractGraphDataService { } @Override - protected void deleteBulkEdge(String id, String version, String type, String dbTransId) throws CrudException { - dao.deleteEdge(id, type, dbTransId); + protected void deleteBulkEdge(String id, String version, String dbTransId) throws CrudException { + dao.deleteEdge(id, dbTransId); } } diff --git a/src/main/java/org/onap/crud/service/CrudRestService.java b/src/main/java/org/onap/crud/service/CrudRestService.java index 25ab43a..3cfc657 100644 --- a/src/main/java/org/onap/crud/service/CrudRestService.java +++ b/src/main/java/org/onap/crud/service/CrudRestService.java @@ -504,10 +504,6 @@ public class CrudRestService { EdgePayload edgePayload = EdgePayload.fromJson(item.getValue().getAsJsonObject().toString()); - if (edgePayload.getType() == null) { - throw new CrudException("Edge Type cannot be null for: " + item.getKey(), Status.BAD_REQUEST); - } - if (!opr.getKey().equalsIgnoreCase("operation")) { throw new CrudException("operation missing in item: " + item.getKey(), Status.BAD_REQUEST); } @@ -697,8 +693,9 @@ public class CrudRestService { } if (payload.getType() == null || payload.getType().isEmpty()) { - throw new CrudException("Missing Edge Type ", Status.BAD_REQUEST); + payload.setType(CrudServiceUtil.determineEdgeType(payload, version)); } + ImmutablePair<EntityTag, String> result = graphDataService.addEdge(version, payload.getType(), payload); response = Response.status(Status.CREATED).entity(result.getValue()).tag(result.getKey()).type(mediaType).build(); } else { diff --git a/src/main/java/org/onap/crud/util/CrudServiceUtil.java b/src/main/java/org/onap/crud/util/CrudServiceUtil.java index ed5fe53..5eeb38e 100644 --- a/src/main/java/org/onap/crud/util/CrudServiceUtil.java +++ b/src/main/java/org/onap/crud/util/CrudServiceUtil.java @@ -31,8 +31,12 @@ import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response.Status; import org.onap.aai.db.props.AAIProperties; import org.onap.crud.exception.CrudException; +import org.onap.crud.parser.EdgePayload; +import org.onap.crud.parser.util.EdgePayloadUtil; import org.onap.schema.EdgeRulesLoader; import org.onap.schema.OxmModelLoader; +import org.onap.schema.RelationshipSchema; + import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonPrimitive; @@ -138,4 +142,25 @@ public class CrudServiceUtil { return entityTag; } + public static String determineEdgeType(EdgePayload payload, String version) throws CrudException { + RelationshipSchema schema = EdgeRulesLoader.getSchemaForVersion(version); + + if (payload.getSource() == null || payload.getTarget() == null) { + throw new CrudException("Source/Target not specified", Status.BAD_REQUEST); + } + + Set<String> edgeTypes = schema.getValidRelationTypes(EdgePayloadUtil.getVertexNodeType(payload.getSource()), + EdgePayloadUtil.getVertexNodeType(payload.getTarget())); + + if (edgeTypes.size() == 0) { + throw new CrudException("No valid relationship types from " + payload.getSource() + " to " + payload.getTarget(), Status.BAD_REQUEST); + } + + if (edgeTypes.size() > 1) { + throw new CrudException("Multiple possible relationship types from " + payload.getSource() + " to " + payload.getTarget(), Status.BAD_REQUEST); + } + + return edgeTypes.iterator().next(); + } + } diff --git a/src/main/java/org/onap/schema/RelationshipSchema.java b/src/main/java/org/onap/schema/RelationshipSchema.java index 2aab7a9..0cf419e 100644 --- a/src/main/java/org/onap/schema/RelationshipSchema.java +++ b/src/main/java/org/onap/schema/RelationshipSchema.java @@ -23,7 +23,9 @@ package org.onap.schema; import java.io.IOException; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; +import java.util.Set; import java.util.stream.Collectors; import javax.ws.rs.core.Response.Status; import org.codehaus.jackson.map.ObjectMapper; @@ -47,6 +49,10 @@ public class RelationshipSchema { private Map<String, Map<String, Class<?>>> relationTypes = new HashMap<>(); private Map<String, EdgeRule> relationshipRules = new HashMap<>(); + + // A map storing the list of valid edge types for a source/target pair + private Map<String, Set<String>> edgeTypesForNodePair = new HashMap<>(); + @SuppressWarnings("unchecked") public RelationshipSchema(Multimap<String, EdgeRule> rules, String props) throws CrudException, IOException { @@ -56,6 +62,16 @@ public class RelationshipSchema { for (EdgeRule rule : rules.values()) { String key = buildRelation(rule.getFrom(), rule.getTo(), rule.getLabel()); relationshipRules.put(key, rule); + + String nodePairKey = buildNodePairKey(rule.getFrom(), rule.getTo()); + if (edgeTypesForNodePair.get(nodePairKey) == null) { + Set<String> typeSet = new HashSet<String>(); + typeSet.add(rule.getLabel()); + edgeTypesForNodePair.put(nodePairKey, typeSet); + } + else { + edgeTypesForNodePair.get(nodePairKey).add(rule.getLabel()); + } } Map<String, Class<?>> edgeProps = properties.entrySet().stream().collect(Collectors.toMap(p -> p.getKey(), p -> { @@ -106,10 +122,23 @@ public class RelationshipSchema { return relationTypes.containsKey(type); } + public Set<String> getValidRelationTypes(String source, String target) { + Set<String> typeList = edgeTypesForNodePair.get(buildNodePairKey(source, target)); + if (typeList == null) { + return new HashSet<String>(); + } + + return typeList; + } + private String buildRelation(String source, String target, String relation) { return source + ":" + target + ":" + relation; } + + private String buildNodePairKey(String source, String target) { + return source + ":" + target; + } private Class<?> resolveClass(String type) throws CrudException, ClassNotFoundException { Class<?> clazz = Class.forName(type); diff --git a/src/test/java/org/onap/crud/service/ChampDaoExceptionsTest.java b/src/test/java/org/onap/crud/service/ChampDaoExceptionsTest.java index 1f0a20b..a7479b5 100644 --- a/src/test/java/org/onap/crud/service/ChampDaoExceptionsTest.java +++ b/src/test/java/org/onap/crud/service/ChampDaoExceptionsTest.java @@ -229,7 +229,7 @@ public class ChampDaoExceptionsTest { buildChampDao(); try { - champDao.getEdge(idNotExists, type, txId); + champDao.getEdge(idNotExists, txId); } catch (CrudException e) { assertEquals(404, e.getHttpStatus().getStatusCode()); assertThat(e.getMessage(), containsString(failureCauseForGetEdge)); @@ -250,7 +250,7 @@ public class ChampDaoExceptionsTest { // Type not matches try { - champDao.getEdge(id, "", txId); + champDao.getEdge(id, txId); } catch (CrudException e) { assertEquals(404, e.getHttpStatus().getStatusCode()); assertThat(e.getMessage(), containsString(failureCauseForGetEdgeTypeNotMatches)); @@ -332,7 +332,7 @@ public class ChampDaoExceptionsTest { buildChampDao(); try { - champDao.getVertexEdges(idNotExists, queryParams); + champDao.getVertexEdges(idNotExists, queryParams, null); } catch (CrudException e) { assertEquals(404, e.getHttpStatus().getStatusCode()); assertThat(e.getMessage(), containsString(failureCauseForGetVertexEdges)); @@ -509,13 +509,13 @@ public class ChampDaoExceptionsTest { buildChampDao(); try { - champDao.deleteEdge(id, type); + champDao.deleteEdge(id); } catch (CrudException e) { assertEquals(400, e.getHttpStatus().getStatusCode()); assertThat(e.getMessage(), containsString(failureCauseFordeleteEdge)); } try { - champDao.deleteEdge(id, type, txId); + champDao.deleteEdge(id, txId); } catch (CrudException e) { assertEquals(400, e.getHttpStatus().getStatusCode()); assertThat(e.getMessage(), containsString(failureCauseFordeleteEdge)); diff --git a/src/test/java/org/onap/crud/service/ChampDaoMockTest.java b/src/test/java/org/onap/crud/service/ChampDaoMockTest.java index 75d5bfd..dadf81a 100644 --- a/src/test/java/org/onap/crud/service/ChampDaoMockTest.java +++ b/src/test/java/org/onap/crud/service/ChampDaoMockTest.java @@ -269,11 +269,13 @@ public class ChampDaoMockTest { mockGetVertex("50bdab41-ad1c-4d00-952c-a0aa5d827811", "?transactionId=", "vserver"); mockGetVertices(queryParamsVertices, "pserver"); mockGetVertices(queryParamsVerticesV13, "pserver"); - mockGetVertexEdges("872dd5df-0be9-4167-95e9-2cf4b21165ed", queryParamsVertex, "tosca.relationships.HostedOn"); - mockGetVertexEdges("872dd5df-0be9-4167-95e9-2cf4b21165ed", queryParamsVertexV13, + mockGetVertexEdges("872dd5df-0be9-4167-95e9-2cf4b21165ed", queryParamsVertex, null, "tosca.relationships.HostedOn"); + mockGetVertexEdges("872dd5df-0be9-4167-95e9-2cf4b21165ed", queryParamsVertexV13, null, "tosca.relationships.HostedOn"); - mockGetVertexEdges("50bdab41-ad1c-4d00-952c-a0aa5d827811", emptyQueryParams, "tosca.relationships.HostedOn"); - mockGetVertexEdges("1d326bc7-b985-492b-9604-0d5d1f06f908", emptyQueryParams, "tosca.relationships.HostedOn"); + mockGetVertexEdges("50bdab41-ad1c-4d00-952c-a0aa5d827811", emptyQueryParams, null, "tosca.relationships.HostedOn"); + mockGetVertexEdges("1d326bc7-b985-492b-9604-0d5d1f06f908", emptyQueryParams, null, "tosca.relationships.HostedOn"); + mockGetVertexEdges("50bdab41-ad1c-4d00-952c-a0aa5d827811", emptyQueryParams, "?transactionId=", "tosca.relationships.HostedOn"); + mockGetVertexEdges("1d326bc7-b985-492b-9604-0d5d1f06f908", emptyQueryParams, "?transactionId=", "tosca.relationships.HostedOn"); mockGetEdges("?", "tosca.relationships.HostedOn"); mockGetEdge("50bdab41-ad1c-4d00-952c-a0aa5d827811", "?transactionId=", "tosca.relationships.HostedOn"); mockGetEdge("872dd5df-0be9-4167-95e9-2cf4b21165ed", emptyQueryParams, "tosca.relationships.HostedOn"); @@ -372,17 +374,24 @@ public class ChampDaoMockTest { .thenReturn(operationResult); } - public void mockGetVertexEdges(String id, Map<String, String> queryParams, String type) { + public void mockGetVertexEdges(String id, Map<String, String> queryParams, String txId, String type) { String edgeResponse = champEdge.replace("edgeType", type); OperationResult operationResult = new OperationResult(); List<String> edgeResponselist = new ArrayList<>(); edgeResponselist.add(edgeResponse); operationResult.setResult(edgeResponselist.toString()); operationResult.setResultCode(200); + String baseUrl = BASE_OBJECT_URL + "/" + RELATIONSHIP_SUB_URL + "/" + id; + String url; + + if (txId != null) { + url = baseUrl + txId; + } + else { + url = appendQueryParams(baseUrl, queryParams).toString(); + } - StringBuilder url = appendQueryParams(BASE_OBJECT_URL + "/" + RELATIONSHIP_SUB_URL + "/" + id, queryParams); - - when(restClientMock.get(url.toString(), createHeaders(), MediaType.APPLICATION_JSON_TYPE)) + when(restClientMock.get(url, createHeaders(), MediaType.APPLICATION_JSON_TYPE)) .thenReturn(operationResult); } diff --git a/src/test/java/org/onap/crud/service/CrudRestServiceTest.java b/src/test/java/org/onap/crud/service/CrudRestServiceTest.java index d8be704..1098284 100644 --- a/src/test/java/org/onap/crud/service/CrudRestServiceTest.java +++ b/src/test/java/org/onap/crud/service/CrudRestServiceTest.java @@ -335,7 +335,8 @@ public class CrudRestServiceTest { response = mockService.addBulk(payloadStr, "v9", "", "services/inventory/v9", new TestHeaders(), null, new TestRequest()); - System.out.println("Bulk Response: " + response.getStatus() + "\n" + response.getEntity().toString()); + System.out.println("Bulk Response: " + response); + System.out.println("Response Entity: " + response.getEntity().toString()); assertTrue(response.getStatus() == 200); } diff --git a/src/test/java/org/onap/crud/service/TestDao.java b/src/test/java/org/onap/crud/service/TestDao.java index 20cd04a..4f1d34e 100644 --- a/src/test/java/org/onap/crud/service/TestDao.java +++ b/src/test/java/org/onap/crud/service/TestDao.java @@ -84,7 +84,7 @@ public class TestDao implements GraphDao { } @Override - public List<Edge> getVertexEdges(String id, Map<String, String> queryParams) throws CrudException { + public List<Edge> getVertexEdges(String id, Map<String, String> queryParams, String txId) throws CrudException { List<Edge> list = new ArrayList<Edge>(); list.add(Edge.fromJson(champEdge)); return list; @@ -159,7 +159,7 @@ public class TestDao implements GraphDao { } @Override - public void deleteEdge(String id, String type) throws CrudException { + public void deleteEdge(String id) throws CrudException { } @@ -217,13 +217,13 @@ public class TestDao implements GraphDao { } @Override - public void deleteEdge(String id, String type, String txId) throws CrudException { + public void deleteEdge(String id, String txId) throws CrudException { // TODO Auto-generated method stub } @Override - public Edge getEdge(String id, String type, String txId) throws CrudException { + public Edge getEdge(String id, String txId) throws CrudException { return Edge.fromJson(champEdge); } @@ -232,4 +232,10 @@ public class TestDao implements GraphDao { headers.add("etag", "test123"); return headers; } + + @Override + public Edge getEdge(String id) throws CrudException { + // TODO Auto-generated method stub + return null; + } }
\ No newline at end of file diff --git a/src/test/java/org/onap/schema/EdgeRulesLoaderTest.java b/src/test/java/org/onap/schema/EdgeRulesLoaderTest.java index 05f998f..aad11fc 100644 --- a/src/test/java/org/onap/schema/EdgeRulesLoaderTest.java +++ b/src/test/java/org/onap/schema/EdgeRulesLoaderTest.java @@ -24,8 +24,13 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; + +import java.util.Set; + import org.junit.Test; import org.onap.crud.exception.CrudException; +import org.onap.crud.parser.EdgePayload; +import org.onap.crud.util.CrudServiceUtil; public class EdgeRulesLoaderTest { @@ -54,6 +59,63 @@ public class EdgeRulesLoaderTest { assertNull(g.lookupRelation("U:W:org.onap.relationships.inventory.groupsResourcesIn")); } + @Test + public void getRelationshipTypeForNodePair() throws Exception { + EdgeRulesLoader.resetSchemaVersionContext(); + EdgeRulesLoader.loadModels("v11"); + RelationshipSchema schema = EdgeRulesLoader.getSchemaForVersion("v11"); + + EdgePayload payload1 = new EdgePayload(); + payload1.setSource("services/inventory/v11/availability-zone/xxx"); + payload1.setTarget("services/inventory/v11/cloud-region/xxx"); + + EdgePayload payload2 = new EdgePayload(); + payload2.setSource("services/inventory/v11/image/xxx"); + payload2.setTarget("services/inventory/v11/pserver/xxx"); + + EdgePayload payload3 = new EdgePayload(); + payload3.setSource("services/inventory/v11/allotted-resource/xxx"); + payload3.setTarget("services/inventory/v11/instance-group/xxx"); + + // Get edge types for node pair with a single possible edge between them + Set<String> typeList = schema.getValidRelationTypes("availability-zone", "cloud-region"); + assertEquals(1, typeList.size()); + assertTrue(typeList.contains("org.onap.relationships.inventory.BelongsTo")); + assertEquals(CrudServiceUtil.determineEdgeType(payload1, "v11"), "org.onap.relationships.inventory.BelongsTo"); + + // Get edge types for node pair with no possible edge between them + typeList = schema.getValidRelationTypes("image", "pserver"); + assertEquals(0, typeList.size()); + typeList = schema.getValidRelationTypes("cloud-region", "availability-zone"); + assertEquals(0, typeList.size()); + + try { + // Should throw an exception here + CrudServiceUtil.determineEdgeType(payload2, "v11"); + assertTrue(false); + } + catch (CrudException ex) { + System.out.println(ex.getMessage()); + } + + typeList = schema.getValidRelationTypes("allotted-resource", "instance-group"); + assertEquals(2, typeList.size()); + assertTrue(typeList.contains("org.onap.relationships.inventory.TestEdge")); + assertTrue(typeList.contains("org.onap.relationships.inventory.MemberOf")); + + for (String type : typeList) { + System.out.println(type); + } + + try { + // Should throw an exception here + CrudServiceUtil.determineEdgeType(payload3, "v11"); + assertTrue(false); + } + catch (CrudException ex) { + System.out.println(ex.getMessage()); + } + } @Test public void getSchemaForVersionFail() throws Exception { diff --git a/src/test/resources/rules/DbEdgeRules_test_two_v11.json b/src/test/resources/rules/DbEdgeRules_test_two_v11.json index f38bd2c..4ff4eb2 100644 --- a/src/test/resources/rules/DbEdgeRules_test_two_v11.json +++ b/src/test/resources/rules/DbEdgeRules_test_two_v11.json @@ -21,6 +21,18 @@ "delete-other-v": "NONE", "SVC-INFRA": "NONE", "prevent-delete": "!${direction}" + }, + { + "from": "allotted-resource", + "to": "instance-group", + "label": "org.onap.relationships.inventory.TestEdge", + "direction": "OUT", + "multiplicity": "MANY2MANY", + "contains-other-v": "NONE", + "delete-other-v": "NONE", + "SVC-INFRA": "NONE", + "prevent-delete": "NONE", + "default": "true" } ] } |