Apply multiplicity Rule upon Edge creation
[aai/gizmo.git] / src / test / java / org / onap / schema / validation / MultiplicityValidatorTest.java
1 /**\r
2  * ============LICENSE_START=======================================================\r
3  * org.onap.aai\r
4  * ================================================================================\r
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.\r
6  * Copyright © 2017-2018 Amdocs\r
7  * ================================================================================\r
8  * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * you may not use this file except in compliance with the License.\r
10  * You may obtain a copy of the License at\r
11  *\r
12  *       http://www.apache.org/licenses/LICENSE-2.0\r
13  *\r
14  * Unless required by applicable law or agreed to in writing, software\r
15  * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * See the License for the specific language governing permissions and\r
18  * limitations under the License.\r
19  * ============LICENSE_END=========================================================\r
20  */\r
21 package org.onap.schema.validation;\r
22 \r
23 import java.io.File;\r
24 import java.util.ArrayList;\r
25 import java.util.HashMap;\r
26 import java.util.List;\r
27 import java.util.Map;\r
28 import org.junit.Assert;\r
29 import org.junit.Before;\r
30 import org.junit.Rule;\r
31 import org.junit.Test;\r
32 import org.junit.rules.ExpectedException;\r
33 import org.onap.crud.entity.Edge;\r
34 import org.onap.crud.entity.Vertex;\r
35 import org.onap.crud.exception.CrudException;\r
36 import org.onap.crud.parser.EdgePayload;\r
37 import org.onap.schema.EdgeRulesLoader;\r
38 import org.onap.schema.validation.MultiplicityValidator.MultiplicityType;\r
39 \r
40 public class MultiplicityValidatorTest {\r
41 \r
42     private final String postEdgePayload = "{" + "\"type\": \"tosca.relationships.HostedOn\","\r
43             + "\"source\": \"services/inventory/v12/vserver/50bdab41-ad1c-4d00-952c-a0aa5d827811\","\r
44             + "\"target\": \"services/inventory/v12/pserver/1d326bc7-b985-492b-9604-0d5d1f06f908\","\r
45             + "\"properties\": {" + "\"prevent-delete\": \"NONE\" } }";\r
46 \r
47     @Rule\r
48     public ExpectedException thrown = ExpectedException.none();\r
49 \r
50     @Before\r
51     public void init() {\r
52         ClassLoader classLoader = getClass().getClassLoader();\r
53         File dir = new File(classLoader.getResource("rules").getFile());\r
54         System.setProperty("CONFIG_HOME", dir.getParent());\r
55         EdgeRulesLoader.resetSchemaVersionContext();\r
56     }\r
57 \r
58     @Test\r
59     public void testValidPayloadForMultiplicityRule() throws CrudException {\r
60         Map<String, List<Edge>> vertexMap = getEdgesForVertex(MultiplicityType.MANY2ONE, true);\r
61         MultiplicityValidator.validatePayloadMultiplicity(EdgePayload.fromJson(postEdgePayload),\r
62                 vertexMap.get("source"), vertexMap.get("target"),\r
63                 "tosca.relationships.HostedOn", "v11");\r
64     }\r
65 \r
66     @Test\r
67     public void testInvalidPayloadForMultiplicityRule() throws CrudException {\r
68         thrown.expect(CrudException.class);\r
69         thrown.expectMessage("MANY2ONE multiplicity rule broken for Edge:vserver:pserver:tosca.relationships.HostedOn");\r
70 \r
71         Map<String, List<Edge>> vertexMap = getEdgesForVertex(MultiplicityType.MANY2ONE, false);\r
72         MultiplicityValidator.validatePayloadMultiplicity(EdgePayload.fromJson(postEdgePayload),\r
73                 vertexMap.get("source"), vertexMap.get("target"),\r
74                 "tosca.relationships.HostedOn", "v11");\r
75     }\r
76 \r
77     @Test\r
78     public void testIsVertexValidForMultiplicityType() throws CrudException {\r
79 \r
80         Map<String, List<Edge>> vertexMap = getEdgesForVertex(MultiplicityType.MANY2MANY, true);\r
81         Assert.assertTrue(MultiplicityValidator.isVertexValidForMultiplicityType(vertexMap.get("source"),\r
82                 vertexMap.get("target"), MultiplicityType.MANY2MANY));\r
83 \r
84         vertexMap = getEdgesForVertex(MultiplicityType.MANY2ONE, true);\r
85         Assert.assertTrue(MultiplicityValidator.isVertexValidForMultiplicityType(\r
86                 vertexMap.get("source"), vertexMap.get("target"), MultiplicityType.MANY2ONE));\r
87 \r
88         vertexMap = getEdgesForVertex(MultiplicityType.ONE2MANY, true);\r
89         Assert.assertTrue(MultiplicityValidator.isVertexValidForMultiplicityType(\r
90                 vertexMap.get("source"), vertexMap.get("target"), MultiplicityType.ONE2MANY));\r
91 \r
92         vertexMap = getEdgesForVertex(MultiplicityType.ONE2ONE, true);\r
93         Assert.assertTrue(MultiplicityValidator.isVertexValidForMultiplicityType(\r
94                 vertexMap.get("source"), vertexMap.get("target"), MultiplicityType.ONE2ONE));\r
95 \r
96         vertexMap = getEdgesForVertex(MultiplicityType.ONE2MANY, false);\r
97         Assert.assertFalse(MultiplicityValidator.isVertexValidForMultiplicityType(\r
98                 vertexMap.get("source"), vertexMap.get("target"), MultiplicityType.ONE2MANY));\r
99 \r
100         vertexMap = getEdgesForVertex(MultiplicityType.ONE2ONE, false);\r
101         Assert.assertFalse(MultiplicityValidator.isVertexValidForMultiplicityType(\r
102                 vertexMap.get("source"), vertexMap.get("target"), MultiplicityType.ONE2ONE));\r
103     }\r
104 \r
105     private Map<String, List<Edge>> getEdgesForVertex(MultiplicityType multiplicityType, boolean pass) {\r
106 \r
107         Map<String, List<Edge>> vertexMap = new HashMap<String, List<Edge>>();\r
108         List<Edge> edgesForSourceVertex = new ArrayList<>();\r
109         List<Edge> edgesForTargetVertex = new ArrayList<>();\r
110 \r
111         switch (multiplicityType) {\r
112             case MANY2MANY:\r
113                 if (pass) {\r
114                     Edge edge = new Edge.Builder("type").source(new Vertex.Builder("source").build())\r
115                             .target(new Vertex.Builder("target").build()).build();\r
116                     edgesForSourceVertex.add(edge);\r
117                     edgesForTargetVertex.add(edge);\r
118                 }\r
119                 break;\r
120             case MANY2ONE:\r
121                 if (pass) {\r
122                     Edge edge = new Edge.Builder("type").source(new Vertex.Builder("source").build())\r
123                             .target(new Vertex.Builder("target").build()).build();\r
124                     edgesForTargetVertex.add(edge);\r
125                 } else {\r
126                     Edge edge = new Edge.Builder("type").source(new Vertex.Builder("source").build())\r
127                             .target(new Vertex.Builder("target").build()).build();\r
128                     edgesForSourceVertex.add(edge);\r
129                     edgesForTargetVertex.add(edge);\r
130                 }\r
131                 break;\r
132             case ONE2MANY:\r
133                 if (pass) {\r
134                     Edge edge = new Edge.Builder("type").source(new Vertex.Builder("source").build())\r
135                             .target(new Vertex.Builder("target").build()).build();\r
136                     edgesForSourceVertex.add(edge);\r
137                 } else {\r
138                     Edge edge = new Edge.Builder("type").source(new Vertex.Builder("source").build())\r
139                             .target(new Vertex.Builder("target").build()).build();\r
140                     edgesForSourceVertex.add(edge);\r
141                     edgesForTargetVertex.add(edge);\r
142                 }\r
143                 break;\r
144             case ONE2ONE:\r
145                 if (!pass) {\r
146                     Edge edge = new Edge.Builder("type").source(new Vertex.Builder("source").build())\r
147                             .target(new Vertex.Builder("target").build()).build();\r
148                     edgesForSourceVertex.add(edge);\r
149                     edgesForTargetVertex.add(edge);\r
150                 }\r
151                 break;\r
152         }\r
153         vertexMap.put("source", edgesForSourceVertex);\r
154         vertexMap.put("target", edgesForTargetVertex);\r
155 \r
156         return vertexMap;\r
157     }\r
158 \r
159 }\r