Remove Multiplicity feature
[aai/gizmo.git] / src / test / java / org / onap / schema / RelationshipSchemaValidatorTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.schema;
22
23 import static org.hamcrest.CoreMatchers.is;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertThat;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.onap.crud.entity.Edge;
29 import org.onap.crud.exception.CrudException;
30 import org.onap.crud.service.EdgePayload;
31 import org.junit.runner.RunWith;
32 import org.mockito.junit.MockitoJUnitRunner;
33 import org.onap.crud.OXMModelLoaderSetup;
34
35 @RunWith(MockitoJUnitRunner.Silent.class)
36 public class RelationshipSchemaValidatorTest extends OXMModelLoaderSetup{
37     // @formatter:off
38           private final String edgePayload = "{" +
39                       "\"type\": \"tosca.relationships.HostedOn\"," +
40                       "\"source\": \"services/inventory/v12/vserver/50bdab41-ad1c-4d00-952c-a0aa5d827811\"," +
41                       "\"target\": \"services/inventory/v12/pserver/1d326bc7-b985-492b-9604-0d5d1f06f908\"," +
42                       "\"properties\": {" +
43                       "\"prevent-delete\": \"NONE\" } }";
44           
45           private final String champEdge = "{" +
46                       "\"key\": \"test-uuid\"," +
47                       "\"type\": \"edgeType\"," +
48                       "\"properties\": {" +
49                       "\"prevent-delete\": \"NONE\" }," +
50                       "\"source\": {" +
51                       "\"key\": \"50bdab41-ad1c-4d00-952c-a0aa5d827811\", \"type\": \"vserver\"}," +
52                       "\"target\": {" +
53                       "\"key\": \"1d326bc7-b985-492b-9604-0d5d1f06f908\", \"type\": \"pserver\"}" +
54                       " }";
55         // @formatter:on
56
57     @Before
58     public void init() throws Exception {
59         System.setProperty("CONFIG_HOME", "src/test/resources");
60     }
61
62     @Test
63     public void testValidateIncomingUpdatePayloadMissingSource() throws CrudException {
64         String type = "tosca.relationships.HostedOn";
65         String version = "v11";
66         String jsonString;
67         EdgePayload payload;
68
69         String champJson = champEdge.replace("edgeType", type);
70         Edge edge = Edge.fromJson(champJson);
71
72         jsonString = edgePayload.replace("services/inventory/v12/vserver/50bdab41-ad1c-4d00-952c-a0aa5d827811", "");
73         payload = EdgePayload.fromJson(jsonString);
74
75         try {
76             RelationshipSchemaValidator.validateIncomingUpdatePayload(edge, version, payload);
77         } catch (CrudException e) {
78             assertEquals(400, e.getHttpStatus().getStatusCode());
79             assertThat(e.getMessage(), is("Invalid Source Urls"));
80         }
81     }
82
83     @Test
84     public void testValidateIncomingUpdatePayloadInvalidSource() throws CrudException {
85         String type = "tosca.relationships.HostedOn";
86         String version = "v11";
87         String jsonString;
88         EdgePayload payload;
89
90         String champJson = champEdge.replace("edgeType", type);
91         Edge edge = Edge.fromJson(champJson);
92
93         jsonString = edgePayload.replace("1d326bc7-b985-492b-9604-0d5d1f06f908", "invalidId");
94         payload = EdgePayload.fromJson(jsonString);
95
96         try {
97             RelationshipSchemaValidator.validateIncomingUpdatePayload(edge, version, payload);
98         } catch (CrudException e) {
99             assertEquals(400, e.getHttpStatus().getStatusCode());
100             assertThat(e.getMessage(), is("Target can't be updated"));
101         }
102     }
103
104     @Test
105     public void testValidateIncomingUpdatePayloadMissingTarget() throws CrudException {
106         String type = "tosca.relationships.HostedOn";
107         String version = "v11";
108         String jsonString;
109         EdgePayload payload;
110
111         String champJson = champEdge.replace("edgeType", type);
112         Edge edge = Edge.fromJson(champJson);
113
114         jsonString = edgePayload.replace("services/inventory/v12/pserver/1d326bc7-b985-492b-9604-0d5d1f06f908", "");
115         payload = EdgePayload.fromJson(jsonString);
116
117         try {
118             RelationshipSchemaValidator.validateIncomingUpdatePayload(edge, version, payload);
119         } catch (CrudException e) {
120             assertEquals(400, e.getHttpStatus().getStatusCode());
121             assertThat(e.getMessage(), is("Invalid Target Urls"));
122         }
123     }
124
125     @Test
126     public void testValidateIncomingUpdatePayloadInvalidTarget() throws CrudException {
127         String type = "tosca.relationships.HostedOn";
128         String version = "v11";
129         String jsonString;
130         EdgePayload payload;
131
132         String champJson = champEdge.replace("edgeType", type);
133         Edge edge = Edge.fromJson(champJson);
134
135         jsonString = edgePayload.replace("50bdab41-ad1c-4d00-952c-a0aa5d827811", "invalidId");
136         payload = EdgePayload.fromJson(jsonString);
137
138         try {
139             RelationshipSchemaValidator.validateIncomingUpdatePayload(edge, version, payload);
140         } catch (CrudException e) {
141             assertEquals(400, e.getHttpStatus().getStatusCode());
142             assertThat(e.getMessage(), is("Source can't be updated"));
143         }
144     }
145
146     @Test
147     public void testValidateIncomingAddPayloadExceptionHandling() throws CrudException {
148         String type = "tosca.relationships.HostedOn";
149         String version = "v11";
150         String jsonString;
151
152         EdgePayload payload;
153         jsonString = edgePayload.replace("services/inventory/v12/vserver/50bdab41-ad1c-4d00-952c-a0aa5d827811", "");
154         payload = EdgePayload.fromJson(jsonString);
155
156         try {
157             RelationshipSchemaValidator.validateIncomingAddPayload(version, type, payload);
158         } catch (CrudException e) {
159             assertEquals(400, e.getHttpStatus().getStatusCode());
160             assertThat(e.getMessage(), is("Invalid Source/Target Urls"));
161         }
162
163         jsonString = edgePayload;
164         payload = EdgePayload.fromJson(jsonString);
165         type = "tosca.relationships.invalidType";
166         try {
167             RelationshipSchemaValidator.validateIncomingAddPayload(version, type, payload);
168         } catch (CrudException e) {
169             assertEquals(400, e.getHttpStatus().getStatusCode());
170             assertThat(e.getMessage(), is("Invalid source/target/relationship type: vserver:pserver:" + type));
171         }
172     }
173
174     @Test
175     public void testValidateIncomingPatchPayloadMissingSource() throws CrudException {
176         String type = "tosca.relationships.HostedOn";
177         String version = "v11";
178         String jsonString;
179
180         String champJson = champEdge.replace("edgeType", type);
181         Edge edge = Edge.fromJson(champJson);
182
183         EdgePayload payload;
184
185         jsonString = edgePayload.replace("services/inventory/v12/vserver/50bdab41-ad1c-4d00-952c-a0aa5d827811", "");
186         payload = EdgePayload.fromJson(jsonString);
187
188         try {
189             RelationshipSchemaValidator.validateIncomingPatchPayload(edge, version, payload);
190         } catch (CrudException e) {
191             assertEquals(400, e.getHttpStatus().getStatusCode());
192             assertThat(e.getMessage(), is("Invalid Source Urls"));
193         }
194     }
195
196     @Test
197     public void testValidateIncomingPatchPayloadInvalidSource() throws CrudException {
198         String type = "tosca.relationships.HostedOn";
199         String version = "v11";
200         String jsonString;
201
202         String champJson = champEdge.replace("edgeType", type);
203         Edge edge = Edge.fromJson(champJson);
204
205         EdgePayload payload;
206
207         jsonString = edgePayload.replace("50bdab41-ad1c-4d00-952c-a0aa5d827811", "invalidId");
208         payload = EdgePayload.fromJson(jsonString);
209         try {
210             RelationshipSchemaValidator.validateIncomingPatchPayload(edge, version, payload);
211         } catch (CrudException e) {
212             assertEquals(400, e.getHttpStatus().getStatusCode());
213             assertThat(e.getMessage(), is("Source can't be updated"));
214         }
215     }
216
217     @Test
218     public void testValidateIncomingPatchPayloadMissingTarget() throws CrudException {
219         String type = "tosca.relationships.HostedOn";
220         String version = "v11";
221         String jsonString;
222
223         String champJson = champEdge.replace("edgeType", type);
224         Edge edge = Edge.fromJson(champJson);
225
226         EdgePayload payload;
227
228         jsonString = edgePayload.replace("services/inventory/v12/pserver/1d326bc7-b985-492b-9604-0d5d1f06f908", "");
229         payload = EdgePayload.fromJson(jsonString);
230
231         try {
232             RelationshipSchemaValidator.validateIncomingPatchPayload(edge, version, payload);
233         } catch (CrudException e) {
234             assertEquals(400, e.getHttpStatus().getStatusCode());
235             assertThat(e.getMessage(), is("Invalid Target Urls"));
236         }
237     }
238
239     @Test
240     public void testValidateIncomingPatchPayloadInvalidTarget() throws CrudException {
241         String type = "tosca.relationships.HostedOn";
242         String version = "v11";
243         String jsonString;
244
245         String champJson = champEdge.replace("edgeType", type);
246         Edge edge = Edge.fromJson(champJson);
247
248         EdgePayload payload;
249
250         jsonString = edgePayload.replace("1d326bc7-b985-492b-9604-0d5d1f06f908", "invalidId");
251         payload = EdgePayload.fromJson(jsonString);
252         try {
253             RelationshipSchemaValidator.validateIncomingPatchPayload(edge, version, payload);
254         } catch (CrudException e) {
255             assertEquals(400, e.getHttpStatus().getStatusCode());
256             assertThat(e.getMessage(), is("Target can't be updated"));
257         }
258     }
259
260     @Test
261     public void testValidateTypeExceptionHandling() {
262         String version = "v11";
263         String type = "tosca.relationships.invalidType";
264
265         try {
266             RelationshipSchemaValidator.validateType(version, type);
267         } catch (CrudException e) {
268             assertEquals(400, e.getHttpStatus().getStatusCode());
269             assertThat(e.getMessage(), is("Invalid " + RelationshipSchema.SCHEMA_RELATIONSHIP_TYPE + ": " + type));
270         }
271     }
272 }