-/**\r
- * ============LICENSE_START=======================================================\r
- * org.onap.aai\r
- * ================================================================================\r
- * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.\r
- * Copyright © 2017-2018 Amdocs\r
- * ================================================================================\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- * ============LICENSE_END=========================================================\r
- */\r
-package org.onap.schema.validation;\r
-\r
-import java.util.ArrayList;\r
-import java.util.HashMap;\r
-import java.util.List;\r
-import java.util.Map;\r
-import org.junit.Assert;\r
-import org.junit.Before;\r
-import org.junit.Rule;\r
-import org.junit.Test;\r
-import org.junit.rules.ExpectedException;\r
-import org.onap.crud.entity.Edge;\r
-import org.onap.crud.entity.Vertex;\r
-import org.onap.crud.exception.CrudException;\r
-import org.onap.crud.parser.EdgePayload;\r
-import org.onap.schema.EdgeRulesLoader;\r
-import org.onap.schema.validation.MultiplicityValidator.MultiplicityType;\r
-\r
-import org.junit.runner.RunWith;\r
-import org.mockito.junit.MockitoJUnitRunner;\r
-import org.onap.crud.OXMModelLoaderSetup;\r
-\r
-@RunWith(MockitoJUnitRunner.Silent.class)\r
-public class MultiplicityValidatorTest extends OXMModelLoaderSetup{\r
-\r
- private final String postEdgePayload = "{" + "\"type\": \"tosca.relationships.HostedOn\","\r
- + "\"source\": \"services/inventory/v12/vserver/50bdab41-ad1c-4d00-952c-a0aa5d827811\","\r
- + "\"target\": \"services/inventory/v12/pserver/1d326bc7-b985-492b-9604-0d5d1f06f908\","\r
- + "\"properties\": {" + "\"prevent-delete\": \"NONE\" } }";\r
-\r
- @Rule\r
- public ExpectedException thrown = ExpectedException.none();\r
-\r
- @Before\r
- public void init() {\r
- System.setProperty("CONFIG_HOME", "src/test/resources");\r
- EdgeRulesLoader.resetSchemaVersionContext();\r
- }\r
-\r
- @Test\r
- public void testValidPayloadForMultiplicityRule() throws CrudException {\r
- Map<String, List<Edge>> vertexMap = getEdgesForVertex(MultiplicityType.MANY2ONE, true);\r
- MultiplicityValidator.validatePayloadMultiplicity(EdgePayload.fromJson(postEdgePayload),\r
- vertexMap.get("source"), vertexMap.get("target"),\r
- "tosca.relationships.HostedOn", "v11");\r
- }\r
-\r
- @Test\r
- public void testInvalidPayloadForMultiplicityRule() throws CrudException {\r
- thrown.expect(CrudException.class);\r
- thrown.expectMessage("MANY2ONE multiplicity rule broken for Edge:vserver:pserver:tosca.relationships.HostedOn");\r
-\r
- Map<String, List<Edge>> vertexMap = getEdgesForVertex(MultiplicityType.MANY2ONE, false);\r
- MultiplicityValidator.validatePayloadMultiplicity(EdgePayload.fromJson(postEdgePayload),\r
- vertexMap.get("source"), vertexMap.get("target"),\r
- "tosca.relationships.HostedOn", "v11");\r
- }\r
-\r
- @Test\r
- public void testIsVertexValidForMultiplicityType() throws CrudException {\r
-\r
- Map<String, List<Edge>> vertexMap = getEdgesForVertex(MultiplicityType.MANY2MANY, true);\r
- Assert.assertTrue(MultiplicityValidator.isVertexValidForMultiplicityType(vertexMap.get("source"),\r
- vertexMap.get("target"), MultiplicityType.MANY2MANY));\r
-\r
- vertexMap = getEdgesForVertex(MultiplicityType.MANY2ONE, true);\r
- Assert.assertTrue(MultiplicityValidator.isVertexValidForMultiplicityType(\r
- vertexMap.get("source"), vertexMap.get("target"), MultiplicityType.MANY2ONE));\r
-\r
- vertexMap = getEdgesForVertex(MultiplicityType.ONE2MANY, true);\r
- Assert.assertTrue(MultiplicityValidator.isVertexValidForMultiplicityType(\r
- vertexMap.get("source"), vertexMap.get("target"), MultiplicityType.ONE2MANY));\r
-\r
- vertexMap = getEdgesForVertex(MultiplicityType.ONE2ONE, true);\r
- Assert.assertTrue(MultiplicityValidator.isVertexValidForMultiplicityType(\r
- vertexMap.get("source"), vertexMap.get("target"), MultiplicityType.ONE2ONE));\r
-\r
- vertexMap = getEdgesForVertex(MultiplicityType.ONE2MANY, false);\r
- Assert.assertFalse(MultiplicityValidator.isVertexValidForMultiplicityType(\r
- vertexMap.get("source"), vertexMap.get("target"), MultiplicityType.ONE2MANY));\r
-\r
- vertexMap = getEdgesForVertex(MultiplicityType.ONE2ONE, false);\r
- Assert.assertFalse(MultiplicityValidator.isVertexValidForMultiplicityType(\r
- vertexMap.get("source"), vertexMap.get("target"), MultiplicityType.ONE2ONE));\r
- }\r
-\r
- private Map<String, List<Edge>> getEdgesForVertex(MultiplicityType multiplicityType, boolean pass) {\r
-\r
- Map<String, List<Edge>> vertexMap = new HashMap<String, List<Edge>>();\r
- List<Edge> edgesForSourceVertex = new ArrayList<>();\r
- List<Edge> edgesForTargetVertex = new ArrayList<>();\r
-\r
- switch (multiplicityType) {\r
- case MANY2MANY:\r
- if (pass) {\r
- Edge edge = new Edge.Builder("type").source(new Vertex.Builder("source").build())\r
- .target(new Vertex.Builder("target").build()).build();\r
- edgesForSourceVertex.add(edge);\r
- edgesForTargetVertex.add(edge);\r
- }\r
- break;\r
- case MANY2ONE:\r
- if (pass) {\r
- Edge edge = new Edge.Builder("type").source(new Vertex.Builder("source").build())\r
- .target(new Vertex.Builder("target").build()).build();\r
- edgesForTargetVertex.add(edge);\r
- } else {\r
- Edge edge = new Edge.Builder("type").source(new Vertex.Builder("source").build())\r
- .target(new Vertex.Builder("target").build()).build();\r
- edgesForSourceVertex.add(edge);\r
- edgesForTargetVertex.add(edge);\r
- }\r
- break;\r
- case ONE2MANY:\r
- if (pass) {\r
- Edge edge = new Edge.Builder("type").source(new Vertex.Builder("source").build())\r
- .target(new Vertex.Builder("target").build()).build();\r
- edgesForSourceVertex.add(edge);\r
- } else {\r
- Edge edge = new Edge.Builder("type").source(new Vertex.Builder("source").build())\r
- .target(new Vertex.Builder("target").build()).build();\r
- edgesForSourceVertex.add(edge);\r
- edgesForTargetVertex.add(edge);\r
- }\r
- break;\r
- case ONE2ONE:\r
- if (!pass) {\r
- Edge edge = new Edge.Builder("type").source(new Vertex.Builder("source").build())\r
- .target(new Vertex.Builder("target").build()).build();\r
- edgesForSourceVertex.add(edge);\r
- edgesForTargetVertex.add(edge);\r
- }\r
- break;\r
- }\r
- vertexMap.put("source", edgesForSourceVertex);\r
- vertexMap.put("target", edgesForTargetVertex);\r
-\r
- return vertexMap;\r
- }\r
-\r
-}\r