From d41ef90610aadb5aa3372d5922155e4fc4e0a407 Mon Sep 17 00:00:00 2001 From: sblimkie Date: Thu, 15 Mar 2018 13:53:15 -0400 Subject: Add junit tests Add junit tests Change-Id: Iaa22b0faa206114ef39648d451a5275205d14d9a Issue-ID: AAI-885 Signed-off-by: sblimkie --- .../java/org/onap/crud/event/GraphEventTest.java | 130 ++++++ .../onap/crud/service/AaiResourceServiceTest.java | 216 ++++++++++ .../org/onap/crud/service/BulkPayloadTest.java | 73 ++++ .../org/onap/crud/service/CrudRestServiceTest.java | 241 +++++++++++ src/test/java/org/onap/crud/service/TestDao.java | 194 +++++++++ .../java/org/onap/crud/service/TestHeaders.java | 91 ++++ .../java/org/onap/crud/service/TestRequest.java | 459 +++++++++++++++++++++ .../java/org/onap/crud/service/TestUriInfo.java | 148 +++++++ .../org/onap/schema/AaiResourceServiceTest.java | 217 ---------- .../org/onap/schema/RelationshipSchemaTest.java | 4 +- 10 files changed, 1554 insertions(+), 219 deletions(-) create mode 100644 src/test/java/org/onap/crud/event/GraphEventTest.java create mode 100644 src/test/java/org/onap/crud/service/AaiResourceServiceTest.java create mode 100644 src/test/java/org/onap/crud/service/BulkPayloadTest.java create mode 100644 src/test/java/org/onap/crud/service/CrudRestServiceTest.java create mode 100644 src/test/java/org/onap/crud/service/TestDao.java create mode 100644 src/test/java/org/onap/crud/service/TestHeaders.java create mode 100644 src/test/java/org/onap/crud/service/TestRequest.java create mode 100644 src/test/java/org/onap/crud/service/TestUriInfo.java delete mode 100644 src/test/java/org/onap/schema/AaiResourceServiceTest.java (limited to 'src/test/java') diff --git a/src/test/java/org/onap/crud/event/GraphEventTest.java b/src/test/java/org/onap/crud/event/GraphEventTest.java new file mode 100644 index 0000000..f9a4c6c --- /dev/null +++ b/src/test/java/org/onap/crud/event/GraphEventTest.java @@ -0,0 +1,130 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2018 Amdocs + * ================================================================================ + * Licensed 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * ============LICENSE_END========================================================= + */ +package org.onap.crud.event; + +import static org.junit.Assert.*; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +import javax.ws.rs.core.Response.Status; + +import org.junit.Test; +import org.onap.crud.entity.Edge; +import org.onap.crud.entity.Vertex; +import org.onap.crud.event.GraphEvent; +import org.onap.crud.event.GraphEvent.GraphEventOperation; +import org.onap.crud.event.GraphEvent.GraphEventResult; +import org.onap.crud.event.GraphEventVertex; +import org.onap.crud.exception.CrudException; + +public class GraphEventTest { + private final String vertexPayload = "{" + + "\"key\": \"test-uuid\"," + + "\"type\": \"pserver\"," + + "\"properties\": {" + + "\"fqdn\": \"myhost.onap.com\"," + + "\"hostname\": \"myhost\" } }"; + + private final String edgePayload = "{" + + "\"key\": \"test-uuid\"," + + "\"type\": \"tosca.relationships.HostedOn\"," + + "\"properties\": {" + + "\"prevent-delete\": \"NONE\" }," + + "\"source\": {" + + "\"key\": \"50bdab41-ad1c-4d00-952c-a0aa5d827811\", \"type\": \"vserver\"}," + + "\"target\": {" + + "\"key\": \"1d326bc7-b985-492b-9604-0d5d1f06f908\", \"type\": \"pserver\"}" + + " }"; + + @Test + public void validateGraphEvent() throws CrudException, IOException { + // Test building event from json + File file = new File("src/test/resources/payloads/graphVertexEvent.json"); + String payloadStr = readFileToString(file); + GraphEvent event = GraphEvent.fromJson(payloadStr); + assertTrue(event.getOperation() == GraphEventOperation.UPDATE); + assertTrue(event.getDbTransactionId().equals("b3e2853e-f643-47a3-a0c3-cb54cc997ad3")); + assertTrue(event.getTimestamp() == Long.parseLong("1514927928167")); + assertTrue(event.getTransactionId().equals("c0a81fa7-5ef4-49cd-ab39-e42c53c9b9a4")); + assertTrue(event.getObjectKey().equals("mykey")); + assertTrue(event.getObjectType().equals("vertex->pserver")); + assertTrue(event.getVertex().getId().equals("mykey")); + assertTrue(event.getVertex().getModelVersion().equals("v11")); + assertTrue(event.getVertex().getType().equals("pserver")); + assertTrue(event.getVertex().getProperties() != null); + assertTrue(event.getVertex().toVertex() != null); + assertTrue(event.getVertex().toJson() != null); + + // Test building event from vertex + Vertex vertex = Vertex.fromJson(vertexPayload, "v11"); + event = GraphEvent.builder(GraphEventOperation.CREATE).vertex(GraphEventVertex.fromVertex(vertex, "v11")).build(); + assertTrue(event.getOperation() == GraphEventOperation.CREATE); + + + // Test building event from edge + Edge edge = Edge.fromJson(edgePayload); + event = GraphEvent.builder(GraphEventOperation.UPDATE).edge(GraphEventEdge.fromEdge(edge, "v11")).build(); + assertTrue(event.getOperation() == GraphEventOperation.UPDATE); + assertTrue(event.getObjectKey().equals("test-uuid")); + assertTrue(event.getObjectType().equals("edge->tosca.relationships.HostedOn")); + assertTrue(event.getEdge().getId().equals("test-uuid")); + assertTrue(event.getEdge().getType().equals("tosca.relationships.HostedOn")); + assertTrue(event.getEdge().getProperties() != null); + assertTrue(event.getEdge().toEdge() != null); + assertTrue(event.getEdge().toJson() != null); + + // Test Getters/Setters + event.setDbTransactionId("a"); + assertTrue(event.getDbTransactionId().equals("a")); + event.setErrorMessage("error"); + assertTrue(event.getErrorMessage().equals("error")); + event.setResult(GraphEventResult.FAILURE); + assertTrue(event.getResult() == GraphEventResult.FAILURE); + event.setHttpErrorStatus(Status.BAD_REQUEST); + assertTrue(event.getHttpErrorStatus() == Status.BAD_REQUEST); + event.setTimestamp(1234567); + assertTrue(event.getTimestamp() == Long.parseLong("1234567")); + } + + public static String readFileToString(File aFile) throws IOException { + BufferedReader br = new BufferedReader(new FileReader(aFile)); + try { + StringBuilder sb = new StringBuilder(); + String line = br.readLine(); + + while (line != null) { + sb.append(line); + line = br.readLine(); + } + + return sb.toString().replaceAll("\\s+", ""); + } finally { + try { + br.close(); + } catch (IOException e) { + fail("Unexpected IOException: " + e.getMessage()); + } + } + } +} diff --git a/src/test/java/org/onap/crud/service/AaiResourceServiceTest.java b/src/test/java/org/onap/crud/service/AaiResourceServiceTest.java new file mode 100644 index 0000000..5eb8a68 --- /dev/null +++ b/src/test/java/org/onap/crud/service/AaiResourceServiceTest.java @@ -0,0 +1,216 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2018 Amdocs + * ================================================================================ + * Licensed 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * ============LICENSE_END========================================================= + */ +package org.onap.crud.service; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.Map; + +import org.junit.Before; +import org.junit.Test; +import org.onap.aai.exceptions.AAIException; +import org.onap.aai.serialization.db.EdgeProperty; +import org.onap.aai.serialization.db.EdgeRule; +import org.onap.aai.serialization.db.EdgeRules; +import org.onap.aai.serialization.db.EdgeType; +import org.onap.crud.exception.CrudException; +import org.onap.crud.service.EdgePayload; + +import com.google.gson.JsonElement; + +public class AaiResourceServiceTest { + + public AaiResourceService aaiResSvc = null; + + + @Before + public void setup() { + System.setProperty("AJSC_HOME", "."); + System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local"); + + aaiResSvc = new AaiResourceService(); + } + + + /** + * This test validates that we can apply db edge rules against an edge request + * payload and have the properties defined in the edge rules merged into the + * payload. + * + * @throws CrudException + * @throws AAIException + */ + @Test + public void applyEdgeRulesToPayloadTest() throws CrudException, AAIException { + + String content = "{" + + "\"source\": \"services/inventory/v8/l-interface/369553424\", " + + "\"target\": \"services/inventory/v8/logical-link/573444128\"," + + "\"properties\": {" + + "}" + + "}"; + + // Convert our simulated payload to an EdgePayload object. + EdgePayload payload = EdgePayload.fromJson(content); + + // Now, apply the db edge rules against our edge payload. + EdgePayload payloadAfterEdgeRules = aaiResSvc.applyEdgeRulesToPayload(payload); + + EdgeRules rules = EdgeRules.getInstance(); + EdgeRule rule = rules.getEdgeRule(EdgeType.COUSIN, "l-interface", "logical-link"); + Map edgeProps = rule.getEdgeProperties(); + + // Validate that the properties defined in the DB edge rules show up in our + // final payload. + for(EdgeProperty key : edgeProps.keySet()) { + assertTrue(payloadAfterEdgeRules.toString().contains(key.toString())); + } + } + + + /** + * This test validates that trying to apply edge rules where there is no + * db edge rules entry for the supplied source and target vertex types + * produces an exception. + * + * @throws CrudException + */ + @Test + public void noRuleForEdgeTest() throws CrudException { + + String content = "{" + + "\"source\": \"services/inventory/v8/commodore-64/12345\", " + + "\"target\": \"services/inventory/v8/jumpman/67890\"," + + "\"properties\": {" + + "}" + + "}"; + + // Convert our simulated payload to an EdgePayload object. + EdgePayload payload = EdgePayload.fromJson(content); + + // Now, apply the db edge rules against our edge payload. + try { + aaiResSvc.applyEdgeRulesToPayload(payload); + + } catch (CrudException e) { + + // We expected an exception since there is no rule for our made up vertices.. + assertTrue(e.getMessage().contains("No edge rules for")); + return; + } + + // If we're here then something unexpected happened... + fail(); + } + + + /** + * This test validates that it is possible to merge client supplied and edge rule + * supplied properties into one edge property list. + * + * @throws Exception + */ + @Test + public void mergeEdgePropertiesTest() throws Exception { + + String content = "{" + + "\"source\": \"services/inventory/v8/l-interface/369553424\", " + + "\"target\": \"services/inventory/v8/logical-link/573444128\"," + + "\"properties\": {" + + "\"multiplicity\": \"many\"," + + "\"is-parent\": true," + + "\"uses-resource\": \"true\"," + + "\"has-del-target\": \"true\"" + + "}" + + "}"; + + EdgePayload payload = EdgePayload.fromJson(content); + EdgeRules rules = EdgeRules.getInstance(); + EdgeRule rule = rules.getEdgeRule(EdgeType.COUSIN, "l-interface", "logical-link"); + Map edgeProps = rule.getEdgeProperties(); + + // Merge the client supplied properties with the properties defined in the DB edge rules. + JsonElement mergedProperties = + aaiResSvc.mergeProperties(payload.getProperties(), rule.getEdgeProperties()); + + // Now, validate that the resulting set of properties contains both the client and edge + // rule supplied properties. + String mergedPropertiesString = mergedProperties.toString(); + assertTrue("Client supplied property 'multiplicity' is missing from merged properties set", + mergedPropertiesString.contains("multiplicity")); + assertTrue("Client supplied property 'is-parent' is missing from merged properties set", + mergedPropertiesString.contains("is-parent")); + assertTrue("Client supplied property 'uses-resource' is missing from merged properties set", + mergedPropertiesString.contains("uses-resource")); + assertTrue("Client supplied property 'has-del-target' is missing from merged properties set", + mergedPropertiesString.contains("has-del-target")); + + for(EdgeProperty key : edgeProps.keySet()) { + assertTrue("Edge rule supplied property '" + key.toString() + "' is missing from merged properties set", + mergedPropertiesString.contains(key.toString())); + } + } + + /** + * This test validates that if we try to merge client supplied edge properties + * with the properties defined in the db edge rules, and there is a conflict, + * then the merge will fail. + * + * @throws Exception + */ + @Test + public void mergeEdgePropertiesConflictTest() throws Exception { + + String content = "{" + + "\"source\": \"services/inventory/v8/l-interface/369553424\", " + + "\"target\": \"services/inventory/v8/logical-link/573444128\"," + + "\"properties\": {" + + "\"contains-other-v\": \"OUT\"" + + "}" + + "}"; + + EdgePayload payload = EdgePayload.fromJson(content); + EdgeRules rules = EdgeRules.getInstance(); + EdgeRule rule = rules.getEdgeRule(EdgeType.COUSIN, "l-interface", "logical-link"); + + try { + + // Try to merge our client supplied properties with the properties defined + // in the db edge rules. + aaiResSvc.mergeProperties(payload.getProperties(), rule.getEdgeProperties()); + + } catch (CrudException e) { + + // We should have gotten an exception because we are trying to set a parameter which is + // already defined in the db edge rules, so if we're here then we are good. + return; + } + + // If we made it here then we were allowed to set a property that is already defined + // in the db edge rules, which we should not have... + fail(); + } + + + + +} diff --git a/src/test/java/org/onap/crud/service/BulkPayloadTest.java b/src/test/java/org/onap/crud/service/BulkPayloadTest.java new file mode 100644 index 0000000..89c2269 --- /dev/null +++ b/src/test/java/org/onap/crud/service/BulkPayloadTest.java @@ -0,0 +1,73 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2018 Amdocs + * ================================================================================ + * Licensed 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * ============LICENSE_END========================================================= + */ +package org.onap.crud.service; + +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Set; + + +public class BulkPayloadTest { + + @Test + public void testBulkPayload() throws Exception { + BulkPayload p = new BulkPayload(); + JsonObject root = new JsonObject(); + JsonArray vertices = new JsonArray(); + JsonObject v1 = new JsonObject(); + JsonObject v2 = new JsonObject(); + JsonObject prop = new JsonObject(); + + prop.addProperty("p1", "value1"); + prop.addProperty("p2", "value2"); + v1.add("v1", prop); + v2.add("v2", prop); + + vertices.add(v1); + vertices.add(v2); + + root.add("objects", vertices); + + String s = "{\"objects\":[{\"v1\":{\"p1\":\"value1\",\"p2\":\"value2\"}},{\"v2\":{\"p1\":\"value1\",\"p2\":\"value2\"}}]}"; + + p = BulkPayload.fromJson(s); + + List po = p.getObjects(); + List ids = new ArrayList(); + for (JsonElement e : po) { + Set> entries = e.getAsJsonObject().entrySet(); + + for (Map.Entry entry : entries) { + ids.add(entry.getKey()); + } + } + + System.out.println("root: " + root.toString()); + System.out.println("payload ids: " + ids.toString()); + } +} \ No newline at end of file diff --git a/src/test/java/org/onap/crud/service/CrudRestServiceTest.java b/src/test/java/org/onap/crud/service/CrudRestServiceTest.java new file mode 100644 index 0000000..029fd52 --- /dev/null +++ b/src/test/java/org/onap/crud/service/CrudRestServiceTest.java @@ -0,0 +1,241 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2018 Amdocs + * ================================================================================ + * Licensed 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * ============LICENSE_END========================================================= + */ +package org.onap.crud.service; + +import static org.junit.Assert.*; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.Response; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.onap.crud.exception.CrudException; +import org.onap.schema.RelationshipSchemaLoader; + + + +public class CrudRestServiceTest { + private final String putVertexPayload = "{" + + "\"id\": \"test-uuid\"," + + "\"type\": \"pserver\"," + + "\"properties\": {" + + "\"fqdn\": \"myhost.onap.com\"," + + "\"hostname\": \"myhost\" } }"; + + private final String postVertexPayload = "{" + + "\"type\": \"pserver\"," + + "\"properties\": {" + + "\"fqdn\": \"myhost.onap.com\"," + + "\"hostname\": \"myhost\" } }"; + + private final String postMissingPropVertexPayload = "{" + + "\"type\": \"pserver\"," + + "\"properties\": {" + + "\"fqdn\": \"myhost.onap.com\"," + + "\"equip-type\": \"box\" } }"; + + private final String postEdgePayload = "{" + + "\"type\": \"tosca.relationships.HostedOn\"," + + "\"source\": \"services/inventory/v12/vserver/50bdab41-ad1c-4d00-952c-a0aa5d827811\"," + + "\"target\": \"services/inventory/v12/pserver/1d326bc7-b985-492b-9604-0d5d1f06f908\"," + + "\"properties\": {" + + "\"prevent-delete\": \"NONE\" } }"; + + + private CrudRestService mockService; + + @Before + public void init() throws Exception { + ClassLoader classLoader = getClass().getClassLoader(); + File dir = new File(classLoader.getResource("model").getFile()); + System.setProperty("CONFIG_HOME", dir.getParent()); + RelationshipSchemaLoader.resetVersionContextMap(); + + CrudGraphDataService service = new CrudGraphDataService(new TestDao()); + CrudRestService restService = new CrudRestService(service, null); + mockService = Mockito.spy(restService); + + Mockito.doReturn(true).when(mockService).validateRequest(Mockito.any(HttpServletRequest.class), + Mockito.anyString(), Mockito.anyString(), Mockito.any(CrudRestService.Action.class), Mockito.anyString(), + Mockito.any(HttpHeaders.class)); + } + + @Test + public void testDelete() throws CrudException { + Response response; + + response = mockService.deleteVertex("", "v11", "pserver", "872dd5df-0be9-4167-95e9-2cf4b21165ed", + "services/inventory/v11", new TestHeaders(), null, new TestRequest()); + assertTrue(response.getStatus() == 200); + + response = mockService.deleteEdge("", "v11", "tosca.relationships.HostedOn", "872dd5df-0be9-4167-95e9-2cf4b21165ed", + "services/inventory/v11", new TestHeaders(), null, new TestRequest()); + assertTrue(response.getStatus() == 200); + } + + @Test + public void testAddVertex() throws CrudException { + Response response; + + response = mockService.addVertex(postMissingPropVertexPayload, "v11", "services/inventory/v11", + new TestHeaders(), null, new TestRequest()); + System.out.println("Response: " + response.getStatus() + "\n" + response.getEntity().toString()); + assertTrue(response.getStatus() == 400); + + response = mockService.addVertex(postVertexPayload, "v11", "services/inventory/v11", + new TestHeaders(), null, new TestRequest()); + System.out.println("Response: " + response.getStatus() + "\n" + response.getEntity().toString()); + assertTrue(response.getStatus() == 201); + + response = mockService.addVertex(postMissingPropVertexPayload, "v11", "pserver", "services/inventory/v11", + new TestHeaders(), null, new TestRequest()); + System.out.println("Response: " + response.getStatus() + "\n" + response.getEntity().toString()); + assertTrue(response.getStatus() == 400); + + response = mockService.addVertex(postVertexPayload, "v11", "pserver", "services/inventory/v11", + new TestHeaders(), null, new TestRequest()); + System.out.println("Response: " + response.getStatus() + "\n" + response.getEntity().toString()); + assertTrue(response.getStatus() == 201); + } + + @Test + public void testAddEdge() throws CrudException { + Response response; + + response = mockService.addEdge(postEdgePayload, "v11", "services/inventory/v11", + new TestHeaders(), null, new TestRequest()); + System.out.println("Response: " + response.getStatus() + "\n" + response.getEntity().toString()); + assertTrue(response.getStatus() == 201); + + response = mockService.addEdge(postEdgePayload, "v11", "tosca.relationships.HostedOn", "services/inventory/v11", + new TestHeaders(), null, new TestRequest()); + System.out.println("Response: " + response.getStatus() + "\n" + response.getEntity().toString()); + assertTrue(response.getStatus() == 201); + } + + @Test + public void testUpdateVertex() throws CrudException { + Response response; + + // Test ID mismatch + response = mockService.updateVertex(putVertexPayload, "v11", "pserver", "bad-id", + "services/inventory/v11", new TestHeaders(), null, new TestRequest()); + System.out.println("Response: " + response.getStatus() + "\n" + response.getEntity().toString()); + assertTrue(response.getStatus() == 400); + + // Success case + response = mockService.updateVertex(putVertexPayload, "v11", "pserver", "test-uuid", + "services/inventory/v11", new TestHeaders(), null, new TestRequest()); + System.out.println("Response: " + response.getStatus() + "\n" + response.getEntity().toString()); + assertTrue(response.getStatus() == 200); + + // Patch + response = mockService.patchVertex(putVertexPayload, "v11", "pserver", "test-uuid", + "services/inventory/v11", new TestHeaders(), null, new TestRequest()); + System.out.println("Response: " + response.getStatus() + "\n" + response.getEntity().toString()); + assertTrue(response.getStatus() == 200); + } + + @Test + public void testUpdateEdge() throws CrudException { + Response response; + + response = mockService.updateEdge(postEdgePayload, "v11", "tosca.relationships.HostedOn", "my-uuid", + "services/inventory/v11", new TestHeaders(), null, new TestRequest()); + System.out.println("Response: " + response.getStatus() + "\n" + response.getEntity().toString()); + assertTrue(response.getStatus() == 200); + + // Patch + response = mockService.patchEdge(postEdgePayload, "v11", "tosca.relationships.HostedOn", "my-uuid", + "services/inventory/v11", new TestHeaders(), null, new TestRequest()); + System.out.println("Response: " + response.getStatus() + "\n" + response.getEntity().toString()); + assertTrue(response.getStatus() == 200); + } + + @Test + public void testGet() throws CrudException { + Response response; + + response = mockService.getVertex("", "v11", "pserver", "872dd5df-0be9-4167-95e9-2cf4b21165ed", + "services/inventory/v11", new TestHeaders(), new TestUriInfo(), new TestRequest()); + System.out.println("Response: " + response.getStatus() + "\n" + response.getEntity().toString()); + assertTrue(response.getStatus() == 200); + + response = mockService.getEdge("", "v11", "tosca.relationships.HostedOn", "872dd5df-0be9-4167-95e9-2cf4b21165ed", + "services/inventory/v11", new TestHeaders(), new TestUriInfo(), new TestRequest()); + System.out.println("Response: " + response.getStatus() + "\n" + response.getEntity().toString()); + assertTrue(response.getStatus() == 200); + + response = mockService.getVertices("", "v11", "pserver", + "services/inventory/v11", new TestHeaders(), new TestUriInfo(), new TestRequest()); + System.out.println("Response: " + response.getStatus() + "\n" + response.getEntity().toString()); + assertTrue(response.getStatus() == 200); + + response = mockService.getEdges("", "v11", "tosca.relationships.HostedOn", + "services/inventory/v11", new TestHeaders(), new TestUriInfo(), new TestRequest()); + System.out.println("Response: " + response.getStatus() + "\n" + response.getEntity().toString()); + assertTrue(response.getStatus() == 200); + } + + @Test + public void testBulk() throws CrudException, IOException { + Response response; + + File bulkFile = new File("src/test/resources/payloads/bulk.json"); + String payloadStr = readFileToString(bulkFile); + System.out.println(payloadStr); + + response = mockService.addBulk(payloadStr, "v11", "", + "services/inventory/v11", new TestHeaders(), null, new TestRequest()); + System.out.println("Bulk Response: " + response.getStatus() + "\n" + response.getEntity().toString()); + assertTrue(response.getStatus() == 200); + } + + public static String readFileToString(File aFile) throws IOException { + + BufferedReader br = new BufferedReader(new FileReader(aFile)); + try { + StringBuilder sb = new StringBuilder(); + String line = br.readLine(); + + while (line != null) { + sb.append(line); + line = br.readLine(); + } + + return sb.toString().replaceAll("\\s+", ""); + } finally { + try { + br.close(); + } catch (IOException e) { + fail("Unexpected IOException: " + e.getMessage()); + } + } + } + +} diff --git a/src/test/java/org/onap/crud/service/TestDao.java b/src/test/java/org/onap/crud/service/TestDao.java new file mode 100644 index 0000000..5af85f0 --- /dev/null +++ b/src/test/java/org/onap/crud/service/TestDao.java @@ -0,0 +1,194 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2018 Amdocs + * ================================================================================ + * Licensed 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * ============LICENSE_END========================================================= + */ +package org.onap.crud.service; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Map; + +import org.onap.crud.dao.GraphDao; +import org.onap.crud.entity.Edge; +import org.onap.crud.entity.Vertex; +import org.onap.crud.exception.CrudException; + +public class TestDao implements GraphDao { + + private final String champVertex = "{" + + "\"key\": \"test-uuid\"," + + "\"type\": \"pserver\"," + + "\"properties\": {" + + "\"fqdn\": \"myhost.onap.com\"," + + "\"hostname\": \"myhost\" } }"; + + private final String champEdge = "{" + + "\"key\": \"test-uuid\"," + + "\"type\": \"tosca.relationships.HostedOn\"," + + "\"properties\": {" + + "\"prevent-delete\": \"NONE\" }," + + "\"source\": {" + + "\"key\": \"50bdab41-ad1c-4d00-952c-a0aa5d827811\", \"type\": \"vserver\"}," + + "\"target\": {" + + "\"key\": \"1d326bc7-b985-492b-9604-0d5d1f06f908\", \"type\": \"pserver\"}" + + " }"; + + @Override + public Vertex getVertex(String id, String version) throws CrudException { + return Vertex.fromJson(champVertex, "v11"); + } + + @Override + public Vertex getVertex(String id, String type, String version, Map queryParams) + throws CrudException { + return Vertex.fromJson(champVertex, "v11"); + } + + @Override + public List getVertexEdges(String id, Map queryParams) throws CrudException { + List list = new ArrayList(); + list.add(Edge.fromJson(champEdge)); + return list; + } + + @Override + public List getVertices(String type, Map filter, String version) throws CrudException { + List list = new ArrayList(); + list.add(Vertex.fromJson(champVertex, "v11")); + return list; + } + + @Override + public List getVertices(String type, Map filter, HashSet properties, String version) + throws CrudException { + List list = new ArrayList(); + list.add(Vertex.fromJson(champVertex, "v11")); + return list; + } + + @Override + public Edge getEdge(String id, String type, Map queryParams) throws CrudException { + return Edge.fromJson(champEdge); + } + + @Override + public List getEdges(String type, Map filter) throws CrudException { + List list = new ArrayList(); + list.add(Edge.fromJson(champEdge)); + return list; + } + + @Override + public Vertex addVertex(String type, Map properties, String version) throws CrudException { + return Vertex.fromJson(champVertex, "v11"); + } + + @Override + public Vertex updateVertex(String id, String type, Map properties, String version) + throws CrudException { + return Vertex.fromJson(champVertex, "v11"); + } + + @Override + public void deleteVertex(String id, String type) throws CrudException { + + } + + @Override + public Edge addEdge(String type, Vertex source, Vertex target, Map properties, String version) + throws CrudException { + return Edge.fromJson(champEdge); + } + + @Override + public Edge updateEdge(Edge edge) throws CrudException { + return Edge.fromJson(champEdge); + } + + @Override + public void deleteEdge(String id, String type) throws CrudException { + + } + + @Override + public String openTransaction() { + // TODO Auto-generated method stub + return null; + } + + @Override + public void commitTransaction(String id) throws CrudException { + // TODO Auto-generated method stub + + } + + @Override + public void rollbackTransaction(String id) throws CrudException { + // TODO Auto-generated method stub + + } + + @Override + public boolean transactionExists(String id) throws CrudException { + // TODO Auto-generated method stub + return false; + } + + @Override + public Vertex addVertex(String type, Map properties, String version, String txId) + throws CrudException { + return Vertex.fromJson(champVertex, "v11"); + } + + @Override + public Edge addEdge(String type, Vertex source, Vertex target, Map properties, String version, + String txId) throws CrudException { + return Edge.fromJson(champEdge); + } + + @Override + public Vertex updateVertex(String id, String type, Map properties, String version, String txId) + throws CrudException { + return Vertex.fromJson(champVertex, "v11"); + } + + @Override + public Edge updateEdge(Edge edge, String txId) throws CrudException { + return Edge.fromJson(champEdge); + } + + @Override + public void deleteVertex(String id, String type, String txId) throws CrudException { + // TODO Auto-generated method stub + + } + + @Override + public void deleteEdge(String id, String type, String txId) throws CrudException { + // TODO Auto-generated method stub + + } + + @Override + public Edge getEdge(String id, String type, String txId) throws CrudException { + return Edge.fromJson(champEdge); + } + +} diff --git a/src/test/java/org/onap/crud/service/TestHeaders.java b/src/test/java/org/onap/crud/service/TestHeaders.java new file mode 100644 index 0000000..6e30471 --- /dev/null +++ b/src/test/java/org/onap/crud/service/TestHeaders.java @@ -0,0 +1,91 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2018 Amdocs + * ================================================================================ + * Licensed 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * ============LICENSE_END========================================================= + */ +package org.onap.crud.service; + +import java.util.Date; +import java.util.List; +import java.util.Locale; +import java.util.Map; + +import javax.ws.rs.core.Cookie; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedHashMap; +import javax.ws.rs.core.MultivaluedMap; + +public class TestHeaders implements HttpHeaders { + + @Override + public List getAcceptableLanguages() { + return null; + } + + @Override + public List getAcceptableMediaTypes() { + return null; + } + + @Override + public Map getCookies() { + return null; + } + + @Override + public Date getDate() { + return null; + } + + @Override + public String getHeaderString(String arg0) { + return null; + } + + @Override + public Locale getLanguage() { + return null; + } + + @Override + public int getLength() { + return 0; + } + + @Override + public MediaType getMediaType() { + // TODO Auto-generated method stub + return null; + } + + @Override + public List getRequestHeader(String arg0) { + // TODO Auto-generated method stub + return null; + } + + @Override + public MultivaluedMap getRequestHeaders() { + MultivaluedMap map = new MultivaluedHashMap(); + map.add("X-FromAppId", "test-app"); + map.add("X-TransactionId", "65f7e29c-57fd-45b2-bfd5-19e25c59110e"); + return map; + } + +} diff --git a/src/test/java/org/onap/crud/service/TestRequest.java b/src/test/java/org/onap/crud/service/TestRequest.java new file mode 100644 index 0000000..f5ee7d6 --- /dev/null +++ b/src/test/java/org/onap/crud/service/TestRequest.java @@ -0,0 +1,459 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2018 Amdocs + * ================================================================================ + * Licensed 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * ============LICENSE_END========================================================= + */ +package org.onap.crud.service; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.security.Principal; +import java.util.Collection; +import java.util.Enumeration; +import java.util.Locale; +import java.util.Map; + +import javax.servlet.AsyncContext; +import javax.servlet.DispatcherType; +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.ServletInputStream; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import javax.servlet.http.HttpUpgradeHandler; +import javax.servlet.http.Part; + +public class TestRequest implements HttpServletRequest { + + @Override + public AsyncContext getAsyncContext() { + // TODO Auto-generated method stub + return null; + } + + @Override + public Object getAttribute(String arg0) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Enumeration getAttributeNames() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getCharacterEncoding() { + // TODO Auto-generated method stub + return null; + } + + @Override + public int getContentLength() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public long getContentLengthLong() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public String getContentType() { + // TODO Auto-generated method stub + return null; + } + + @Override + public DispatcherType getDispatcherType() { + // TODO Auto-generated method stub + return null; + } + + @Override + public ServletInputStream getInputStream() throws IOException { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getLocalAddr() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getLocalName() { + // TODO Auto-generated method stub + return null; + } + + @Override + public int getLocalPort() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public Locale getLocale() { + // TODO Auto-generated method stub + return null; + } + + @Override + public Enumeration getLocales() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getParameter(String arg0) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Map getParameterMap() { + // TODO Auto-generated method stub + return null; + } + + @Override + public Enumeration getParameterNames() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String[] getParameterValues(String arg0) { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getProtocol() { + // TODO Auto-generated method stub + return null; + } + + @Override + public BufferedReader getReader() throws IOException { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getRealPath(String arg0) { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getRemoteAddr() { + return "127.0.0.1"; + } + + @Override + public String getRemoteHost() { + return "127.0.0.1"; + } + + @Override + public int getRemotePort() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public RequestDispatcher getRequestDispatcher(String arg0) { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getScheme() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getServerName() { + // TODO Auto-generated method stub + return null; + } + + @Override + public int getServerPort() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public ServletContext getServletContext() { + // TODO Auto-generated method stub + return null; + } + + @Override + public boolean isAsyncStarted() { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean isAsyncSupported() { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean isSecure() { + // TODO Auto-generated method stub + return false; + } + + @Override + public void removeAttribute(String arg0) { + // TODO Auto-generated method stub + + } + + @Override + public void setAttribute(String arg0, Object arg1) { + // TODO Auto-generated method stub + + } + + @Override + public void setCharacterEncoding(String arg0) throws UnsupportedEncodingException { + // TODO Auto-generated method stub + + } + + @Override + public AsyncContext startAsync() throws IllegalStateException { + // TODO Auto-generated method stub + return null; + } + + @Override + public AsyncContext startAsync(ServletRequest arg0, ServletResponse arg1) throws IllegalStateException { + // TODO Auto-generated method stub + return null; + } + + @Override + public boolean authenticate(HttpServletResponse arg0) throws IOException, ServletException { + // TODO Auto-generated method stub + return false; + } + + @Override + public String changeSessionId() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getAuthType() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getContextPath() { + // TODO Auto-generated method stub + return null; + } + + @Override + public Cookie[] getCookies() { + // TODO Auto-generated method stub + return null; + } + + @Override + public long getDateHeader(String arg0) { + // TODO Auto-generated method stub + return 0; + } + + @Override + public String getHeader(String arg0) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Enumeration getHeaderNames() { + // TODO Auto-generated method stub + return null; + } + + @Override + public Enumeration getHeaders(String arg0) { + // TODO Auto-generated method stub + return null; + } + + @Override + public int getIntHeader(String arg0) { + // TODO Auto-generated method stub + return 0; + } + + @Override + public String getMethod() { + return "OP"; + } + + @Override + public Part getPart(String arg0) throws IOException, ServletException { + // TODO Auto-generated method stub + return null; + } + + @Override + public Collection getParts() throws IOException, ServletException { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getPathInfo() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getPathTranslated() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getQueryString() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getRemoteUser() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getRequestURI() { + // TODO Auto-generated method stub + return null; + } + + @Override + public StringBuffer getRequestURL() { + return new StringBuffer(); + } + + @Override + public String getRequestedSessionId() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getServletPath() { + // TODO Auto-generated method stub + return null; + } + + @Override + public HttpSession getSession() { + // TODO Auto-generated method stub + return null; + } + + @Override + public HttpSession getSession(boolean arg0) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Principal getUserPrincipal() { + // TODO Auto-generated method stub + return null; + } + + @Override + public boolean isRequestedSessionIdFromCookie() { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean isRequestedSessionIdFromURL() { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean isRequestedSessionIdFromUrl() { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean isRequestedSessionIdValid() { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean isUserInRole(String arg0) { + // TODO Auto-generated method stub + return false; + } + + @Override + public void login(String arg0, String arg1) throws ServletException { + // TODO Auto-generated method stub + + } + + @Override + public void logout() throws ServletException { + // TODO Auto-generated method stub + + } + + @Override + public T upgrade(Class arg0) throws IOException, ServletException { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/src/test/java/org/onap/crud/service/TestUriInfo.java b/src/test/java/org/onap/crud/service/TestUriInfo.java new file mode 100644 index 0000000..980f0da --- /dev/null +++ b/src/test/java/org/onap/crud/service/TestUriInfo.java @@ -0,0 +1,148 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2018 Amdocs + * ================================================================================ + * Licensed 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * ============LICENSE_END========================================================= + */ +package org.onap.crud.service; + +import java.net.URI; +import java.util.List; + +import javax.ws.rs.core.MultivaluedHashMap; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.PathSegment; +import javax.ws.rs.core.UriBuilder; +import javax.ws.rs.core.UriInfo; + +public class TestUriInfo implements UriInfo { + + @Override + public URI getAbsolutePath() { + // TODO Auto-generated method stub + return null; + } + + @Override + public UriBuilder getAbsolutePathBuilder() { + // TODO Auto-generated method stub + return null; + } + + @Override + public URI getBaseUri() { + // TODO Auto-generated method stub + return null; + } + + @Override + public UriBuilder getBaseUriBuilder() { + // TODO Auto-generated method stub + return null; + } + + @Override + public List getMatchedResources() { + // TODO Auto-generated method stub + return null; + } + + @Override + public List getMatchedURIs() { + // TODO Auto-generated method stub + return null; + } + + @Override + public List getMatchedURIs(boolean arg0) { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getPath() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getPath(boolean arg0) { + // TODO Auto-generated method stub + return null; + } + + @Override + public MultivaluedMap getPathParameters() { + // TODO Auto-generated method stub + return null; + } + + @Override + public MultivaluedMap getPathParameters(boolean arg0) { + // TODO Auto-generated method stub + return null; + } + + @Override + public List getPathSegments() { + // TODO Auto-generated method stub + return null; + } + + @Override + public List getPathSegments(boolean arg0) { + // TODO Auto-generated method stub + return null; + } + + @Override + public MultivaluedMap getQueryParameters() { + MultivaluedMap map = new MultivaluedHashMap(); + map.add("hostname", "myhost"); + return map; + } + + @Override + public MultivaluedMap getQueryParameters(boolean arg0) { + return null; + } + + @Override + public URI getRequestUri() { + // TODO Auto-generated method stub + return null; + } + + @Override + public UriBuilder getRequestUriBuilder() { + // TODO Auto-generated method stub + return null; + } + + @Override + public URI relativize(URI arg0) { + // TODO Auto-generated method stub + return null; + } + + @Override + public URI resolve(URI arg0) { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/src/test/java/org/onap/schema/AaiResourceServiceTest.java b/src/test/java/org/onap/schema/AaiResourceServiceTest.java deleted file mode 100644 index 9fc1446..0000000 --- a/src/test/java/org/onap/schema/AaiResourceServiceTest.java +++ /dev/null @@ -1,217 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. - * Copyright © 2017-2018 Amdocs - * ================================================================================ - * Licensed 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 - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * 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. - * ============LICENSE_END========================================================= - */ -package org.onap.schema; - -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import java.util.Map; - -import org.junit.Before; -import org.junit.Test; -import org.onap.aai.exceptions.AAIException; -import org.onap.aai.serialization.db.EdgeProperty; -import org.onap.aai.serialization.db.EdgeRule; -import org.onap.aai.serialization.db.EdgeRules; -import org.onap.aai.serialization.db.EdgeType; -import org.onap.crud.exception.CrudException; -import org.onap.crud.service.AaiResourceService; -import org.onap.crud.service.EdgePayload; - -import com.google.gson.JsonElement; - -public class AaiResourceServiceTest { - - public AaiResourceService aaiResSvc = null; - - - @Before - public void setup() { - System.setProperty("AJSC_HOME", "."); - System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local"); - - aaiResSvc = new AaiResourceService(); - } - - - /** - * This test validates that we can apply db edge rules against an edge request - * payload and have the properties defined in the edge rules merged into the - * payload. - * - * @throws CrudException - * @throws AAIException - */ - @Test - public void applyEdgeRulesToPayloadTest() throws CrudException, AAIException { - - String content = "{" + - "\"source\": \"services/inventory/v8/l-interface/369553424\", " + - "\"target\": \"services/inventory/v8/logical-link/573444128\"," + - "\"properties\": {" + - "}" + - "}"; - - // Convert our simulated payload to an EdgePayload object. - EdgePayload payload = EdgePayload.fromJson(content); - - // Now, apply the db edge rules against our edge payload. - EdgePayload payloadAfterEdgeRules = aaiResSvc.applyEdgeRulesToPayload(payload); - - EdgeRules rules = EdgeRules.getInstance(); - EdgeRule rule = rules.getEdgeRule(EdgeType.COUSIN, "l-interface", "logical-link"); - Map edgeProps = rule.getEdgeProperties(); - - // Validate that the properties defined in the DB edge rules show up in our - // final payload. - for(EdgeProperty key : edgeProps.keySet()) { - assertTrue(payloadAfterEdgeRules.toString().contains(key.toString())); - } - } - - - /** - * This test validates that trying to apply edge rules where there is no - * db edge rules entry for the supplied source and target vertex types - * produces an exception. - * - * @throws CrudException - */ - @Test - public void noRuleForEdgeTest() throws CrudException { - - String content = "{" + - "\"source\": \"services/inventory/v8/commodore-64/12345\", " + - "\"target\": \"services/inventory/v8/jumpman/67890\"," + - "\"properties\": {" + - "}" + - "}"; - - // Convert our simulated payload to an EdgePayload object. - EdgePayload payload = EdgePayload.fromJson(content); - - // Now, apply the db edge rules against our edge payload. - try { - aaiResSvc.applyEdgeRulesToPayload(payload); - - } catch (CrudException e) { - - // We expected an exception since there is no rule for our made up vertices.. - assertTrue(e.getMessage().contains("No edge rules for")); - return; - } - - // If we're here then something unexpected happened... - fail(); - } - - - /** - * This test validates that it is possible to merge client supplied and edge rule - * supplied properties into one edge property list. - * - * @throws Exception - */ - @Test - public void mergeEdgePropertiesTest() throws Exception { - - String content = "{" + - "\"source\": \"services/inventory/v8/l-interface/369553424\", " + - "\"target\": \"services/inventory/v8/logical-link/573444128\"," + - "\"properties\": {" + - "\"multiplicity\": \"many\"," + - "\"is-parent\": true," + - "\"uses-resource\": \"true\"," + - "\"has-del-target\": \"true\"" + - "}" + - "}"; - - EdgePayload payload = EdgePayload.fromJson(content); - EdgeRules rules = EdgeRules.getInstance(); - EdgeRule rule = rules.getEdgeRule(EdgeType.COUSIN, "l-interface", "logical-link"); - Map edgeProps = rule.getEdgeProperties(); - - // Merge the client supplied properties with the properties defined in the DB edge rules. - JsonElement mergedProperties = - aaiResSvc.mergeProperties(payload.getProperties(), rule.getEdgeProperties()); - - // Now, validate that the resulting set of properties contains both the client and edge - // rule supplied properties. - String mergedPropertiesString = mergedProperties.toString(); - assertTrue("Client supplied property 'multiplicity' is missing from merged properties set", - mergedPropertiesString.contains("multiplicity")); - assertTrue("Client supplied property 'is-parent' is missing from merged properties set", - mergedPropertiesString.contains("is-parent")); - assertTrue("Client supplied property 'uses-resource' is missing from merged properties set", - mergedPropertiesString.contains("uses-resource")); - assertTrue("Client supplied property 'has-del-target' is missing from merged properties set", - mergedPropertiesString.contains("has-del-target")); - - for(EdgeProperty key : edgeProps.keySet()) { - assertTrue("Edge rule supplied property '" + key.toString() + "' is missing from merged properties set", - mergedPropertiesString.contains(key.toString())); - } - } - - /** - * This test validates that if we try to merge client supplied edge properties - * with the properties defined in the db edge rules, and there is a conflict, - * then the merge will fail. - * - * @throws Exception - */ - @Test - public void mergeEdgePropertiesConflictTest() throws Exception { - - String content = "{" + - "\"source\": \"services/inventory/v8/l-interface/369553424\", " + - "\"target\": \"services/inventory/v8/logical-link/573444128\"," + - "\"properties\": {" + - "\"contains-other-v\": \"OUT\"" + - "}" + - "}"; - - EdgePayload payload = EdgePayload.fromJson(content); - EdgeRules rules = EdgeRules.getInstance(); - EdgeRule rule = rules.getEdgeRule(EdgeType.COUSIN, "l-interface", "logical-link"); - - try { - - // Try to merge our client supplied properties with the properties defined - // in the db edge rules. - aaiResSvc.mergeProperties(payload.getProperties(), rule.getEdgeProperties()); - - } catch (CrudException e) { - - // We should have gotten an exception because we are trying to set a parameter which is - // already defined in the db edge rules, so if we're here then we are good. - return; - } - - // If we made it here then we were allowed to set a property that is already defined - // in the db edge rules, which we should not have... - fail(); - } - - - - -} diff --git a/src/test/java/org/onap/schema/RelationshipSchemaTest.java b/src/test/java/org/onap/schema/RelationshipSchemaTest.java index c5fbf9d..7d54a30 100644 --- a/src/test/java/org/onap/schema/RelationshipSchemaTest.java +++ b/src/test/java/org/onap/schema/RelationshipSchemaTest.java @@ -71,7 +71,7 @@ public class RelationshipSchemaTest { loadRelations(versionContextMap); assertNotNull(versionContextMap.get("v11").lookupRelation("availability-zone:complex:groupsResourcesIn")); assertTrue(versionContextMap.get("v11") - .lookupRelation("availability-zone:complex:groupsResourcesIn").containsKey("usesResource")); + .lookupRelation("availability-zone:complex:groupsResourcesIn").containsKey("prevent-delete")); } @Test @@ -80,7 +80,7 @@ public class RelationshipSchemaTest { loadRelations(versionContextMap); assertNotNull(versionContextMap.get("v11").lookupRelationType("groupsResourcesIn")); assertTrue(versionContextMap.get("v11") - .lookupRelation("availability-zone:complex:groupsResourcesIn").containsKey("usesResource")); + .lookupRelation("availability-zone:complex:groupsResourcesIn").containsKey("prevent-delete")); } private void loadRelations(Map map){ -- cgit 1.2.3-korg