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