Increase code coverage
[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.util.ArrayList;\r
24 import java.util.HashMap;\r
25 import java.util.List;\r
26 import java.util.Map;\r
27 import org.junit.Assert;\r
28 import org.junit.Before;\r
29 import org.junit.Rule;\r
30 import org.junit.Test;\r
31 import org.junit.rules.ExpectedException;\r
32 import org.onap.crud.entity.Edge;\r
33 import org.onap.crud.entity.Vertex;\r
34 import org.onap.crud.exception.CrudException;\r
35 import org.onap.crud.parser.EdgePayload;\r
36 import org.onap.schema.EdgeRulesLoader;\r
37 import org.onap.schema.validation.MultiplicityValidator.MultiplicityType;\r
38 \r
39 public class MultiplicityValidatorTest {\r
40 \r
41     private final String postEdgePayload = "{" + "\"type\": \"tosca.relationships.HostedOn\","\r
42             + "\"source\": \"services/inventory/v12/vserver/50bdab41-ad1c-4d00-952c-a0aa5d827811\","\r
43             + "\"target\": \"services/inventory/v12/pserver/1d326bc7-b985-492b-9604-0d5d1f06f908\","\r
44             + "\"properties\": {" + "\"prevent-delete\": \"NONE\" } }";\r
45 \r
46     @Rule\r
47     public ExpectedException thrown = ExpectedException.none();\r
48 \r
49     @Before\r
50     public void init() {\r
51         System.setProperty("CONFIG_HOME", "src/test/resources");\r
52         EdgeRulesLoader.resetSchemaVersionContext();\r
53     }\r
54 \r
55     @Test\r
56     public void testValidPayloadForMultiplicityRule() throws CrudException {\r
57         Map<String, List<Edge>> vertexMap = getEdgesForVertex(MultiplicityType.MANY2ONE, true);\r
58         MultiplicityValidator.validatePayloadMultiplicity(EdgePayload.fromJson(postEdgePayload),\r
59                 vertexMap.get("source"), vertexMap.get("target"),\r
60                 "tosca.relationships.HostedOn", "v11");\r
61     }\r
62 \r
63     @Test\r
64     public void testInvalidPayloadForMultiplicityRule() throws CrudException {\r
65         thrown.expect(CrudException.class);\r
66         thrown.expectMessage("MANY2ONE multiplicity rule broken for Edge:vserver:pserver:tosca.relationships.HostedOn");\r
67 \r
68         Map<String, List<Edge>> vertexMap = getEdgesForVertex(MultiplicityType.MANY2ONE, false);\r
69         MultiplicityValidator.validatePayloadMultiplicity(EdgePayload.fromJson(postEdgePayload),\r
70                 vertexMap.get("source"), vertexMap.get("target"),\r
71                 "tosca.relationships.HostedOn", "v11");\r
72     }\r
73 \r
74     @Test\r
75     public void testIsVertexValidForMultiplicityType() throws CrudException {\r
76 \r
77         Map<String, List<Edge>> vertexMap = getEdgesForVertex(MultiplicityType.MANY2MANY, true);\r
78         Assert.assertTrue(MultiplicityValidator.isVertexValidForMultiplicityType(vertexMap.get("source"),\r
79                 vertexMap.get("target"), MultiplicityType.MANY2MANY));\r
80 \r
81         vertexMap = getEdgesForVertex(MultiplicityType.MANY2ONE, true);\r
82         Assert.assertTrue(MultiplicityValidator.isVertexValidForMultiplicityType(\r
83                 vertexMap.get("source"), vertexMap.get("target"), MultiplicityType.MANY2ONE));\r
84 \r
85         vertexMap = getEdgesForVertex(MultiplicityType.ONE2MANY, true);\r
86         Assert.assertTrue(MultiplicityValidator.isVertexValidForMultiplicityType(\r
87                 vertexMap.get("source"), vertexMap.get("target"), MultiplicityType.ONE2MANY));\r
88 \r
89         vertexMap = getEdgesForVertex(MultiplicityType.ONE2ONE, true);\r
90         Assert.assertTrue(MultiplicityValidator.isVertexValidForMultiplicityType(\r
91                 vertexMap.get("source"), vertexMap.get("target"), MultiplicityType.ONE2ONE));\r
92 \r
93         vertexMap = getEdgesForVertex(MultiplicityType.ONE2MANY, false);\r
94         Assert.assertFalse(MultiplicityValidator.isVertexValidForMultiplicityType(\r
95                 vertexMap.get("source"), vertexMap.get("target"), MultiplicityType.ONE2MANY));\r
96 \r
97         vertexMap = getEdgesForVertex(MultiplicityType.ONE2ONE, false);\r
98         Assert.assertFalse(MultiplicityValidator.isVertexValidForMultiplicityType(\r
99                 vertexMap.get("source"), vertexMap.get("target"), MultiplicityType.ONE2ONE));\r
100     }\r
101 \r
102     private Map<String, List<Edge>> getEdgesForVertex(MultiplicityType multiplicityType, boolean pass) {\r
103 \r
104         Map<String, List<Edge>> vertexMap = new HashMap<String, List<Edge>>();\r
105         List<Edge> edgesForSourceVertex = new ArrayList<>();\r
106         List<Edge> edgesForTargetVertex = new ArrayList<>();\r
107 \r
108         switch (multiplicityType) {\r
109             case MANY2MANY:\r
110                 if (pass) {\r
111                     Edge edge = new Edge.Builder("type").source(new Vertex.Builder("source").build())\r
112                             .target(new Vertex.Builder("target").build()).build();\r
113                     edgesForSourceVertex.add(edge);\r
114                     edgesForTargetVertex.add(edge);\r
115                 }\r
116                 break;\r
117             case MANY2ONE:\r
118                 if (pass) {\r
119                     Edge edge = new Edge.Builder("type").source(new Vertex.Builder("source").build())\r
120                             .target(new Vertex.Builder("target").build()).build();\r
121                     edgesForTargetVertex.add(edge);\r
122                 } else {\r
123                     Edge edge = new Edge.Builder("type").source(new Vertex.Builder("source").build())\r
124                             .target(new Vertex.Builder("target").build()).build();\r
125                     edgesForSourceVertex.add(edge);\r
126                     edgesForTargetVertex.add(edge);\r
127                 }\r
128                 break;\r
129             case ONE2MANY:\r
130                 if (pass) {\r
131                     Edge edge = new Edge.Builder("type").source(new Vertex.Builder("source").build())\r
132                             .target(new Vertex.Builder("target").build()).build();\r
133                     edgesForSourceVertex.add(edge);\r
134                 } else {\r
135                     Edge edge = new Edge.Builder("type").source(new Vertex.Builder("source").build())\r
136                             .target(new Vertex.Builder("target").build()).build();\r
137                     edgesForSourceVertex.add(edge);\r
138                     edgesForTargetVertex.add(edge);\r
139                 }\r
140                 break;\r
141             case ONE2ONE:\r
142                 if (!pass) {\r
143                     Edge edge = new Edge.Builder("type").source(new Vertex.Builder("source").build())\r
144                             .target(new Vertex.Builder("target").build()).build();\r
145                     edgesForSourceVertex.add(edge);\r
146                     edgesForTargetVertex.add(edge);\r
147                 }\r
148                 break;\r
149         }\r
150         vertexMap.put("source", edgesForSourceVertex);\r
151         vertexMap.put("target", edgesForTargetVertex);\r
152 \r
153         return vertexMap;\r
154     }\r
155 \r
156 }\r