Upgrade version of aai-common
[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.parser.EdgePayload;
31 import org.onap.schema.validation.RelationshipSchemaValidator;
32
33 import org.junit.runner.RunWith;
34 import org.mockito.junit.MockitoJUnitRunner;
35 import org.onap.crud.OXMModelLoaderSetup;
36
37 @RunWith(MockitoJUnitRunner.Silent.class)
38 public class RelationshipSchemaValidatorTest extends OXMModelLoaderSetup{
39     // @formatter:off
40           private final String edgePayload = "{" +
41                       "\"type\": \"tosca.relationships.HostedOn\"," +
42                       "\"source\": \"services/inventory/v12/vserver/50bdab41-ad1c-4d00-952c-a0aa5d827811\"," +
43                       "\"target\": \"services/inventory/v12/pserver/1d326bc7-b985-492b-9604-0d5d1f06f908\"," +
44                       "\"properties\": {" +
45                       "\"prevent-delete\": \"NONE\" } }";
46           
47           private final String champEdge = "{" +
48                       "\"key\": \"test-uuid\"," +
49                       "\"type\": \"edgeType\"," +
50                       "\"properties\": {" +
51                       "\"prevent-delete\": \"NONE\" }," +
52                       "\"source\": {" +
53                       "\"key\": \"50bdab41-ad1c-4d00-952c-a0aa5d827811\", \"type\": \"vserver\"}," +
54                       "\"target\": {" +
55                       "\"key\": \"1d326bc7-b985-492b-9604-0d5d1f06f908\", \"type\": \"pserver\"}" +
56                       " }";
57         // @formatter:on
58
59     @Before
60     public void init() throws Exception {
61         System.setProperty("CONFIG_HOME", "src/test/resources");
62     }
63
64     @Test
65     public void testValidateIncomingUpdatePayloadMissingSource() throws CrudException {
66         String type = "tosca.relationships.HostedOn";
67         String version = "v11";
68         String jsonString;
69         EdgePayload payload;
70
71         String champJson = champEdge.replace("edgeType", type);
72         Edge edge = Edge.fromJson(champJson);
73
74         jsonString = edgePayload.replace("services/inventory/v12/vserver/50bdab41-ad1c-4d00-952c-a0aa5d827811", "");
75         payload = EdgePayload.fromJson(jsonString);
76
77         try {
78             RelationshipSchemaValidator.validateIncomingUpdatePayload(edge, version, payload);
79         } catch (CrudException e) {
80             assertEquals(400, e.getHttpStatus().getStatusCode());
81             assertThat(e.getMessage(), is("Invalid Source/Target Urls"));
82         }
83     }
84
85     @Test
86     public void testValidateIncomingUpdatePayloadInvalidSource() throws CrudException {
87         String type = "tosca.relationships.HostedOn";
88         String version = "v11";
89         String jsonString;
90         EdgePayload payload;
91
92         String champJson = champEdge.replace("edgeType", type);
93         Edge edge = Edge.fromJson(champJson);
94
95         jsonString = edgePayload.replace("1d326bc7-b985-492b-9604-0d5d1f06f908", "invalidId");
96         payload = EdgePayload.fromJson(jsonString);
97
98         try {
99             RelationshipSchemaValidator.validateIncomingUpdatePayload(edge, version, payload);
100         } catch (CrudException e) {
101             assertEquals(400, e.getHttpStatus().getStatusCode());
102             assertThat(e.getMessage(), is("Target can't be updated"));
103         }
104     }
105
106     @Test
107     public void testValidateIncomingUpdatePayloadMissingTarget() throws CrudException {
108         String type = "tosca.relationships.HostedOn";
109         String version = "v11";
110         String jsonString;
111         EdgePayload payload;
112
113         String champJson = champEdge.replace("edgeType", type);
114         Edge edge = Edge.fromJson(champJson);
115
116         jsonString = edgePayload.replace("services/inventory/v12/pserver/1d326bc7-b985-492b-9604-0d5d1f06f908", "");
117         payload = EdgePayload.fromJson(jsonString);
118
119         try {
120             RelationshipSchemaValidator.validateIncomingUpdatePayload(edge, version, payload);
121         } catch (CrudException e) {
122             assertEquals(400, e.getHttpStatus().getStatusCode());
123             assertThat(e.getMessage(), is("Invalid Source/Target Urls"));
124         }
125     }
126
127     @Test
128     public void testValidateIncomingUpdatePayloadInvalidTarget() throws CrudException {
129         String type = "tosca.relationships.HostedOn";
130         String version = "v11";
131         String jsonString;
132         EdgePayload payload;
133
134         String champJson = champEdge.replace("edgeType", type);
135         Edge edge = Edge.fromJson(champJson);
136
137         jsonString = edgePayload.replace("50bdab41-ad1c-4d00-952c-a0aa5d827811", "invalidId");
138         payload = EdgePayload.fromJson(jsonString);
139
140         try {
141             RelationshipSchemaValidator.validateIncomingUpdatePayload(edge, version, payload);
142         } catch (CrudException e) {
143             assertEquals(400, e.getHttpStatus().getStatusCode());
144             assertThat(e.getMessage(), is("Source can't be updated"));
145         }
146     }
147
148     @Test
149     public void testValidateIncomingAddPayloadExceptionHandling() throws CrudException {
150         String type = "tosca.relationships.HostedOn";
151         String version = "v11";
152         String jsonString;
153
154         EdgePayload payload;
155         jsonString = edgePayload.replace("services/inventory/v12/vserver/50bdab41-ad1c-4d00-952c-a0aa5d827811", "");
156         payload = EdgePayload.fromJson(jsonString);
157
158         try {
159             RelationshipSchemaValidator.validateIncomingAddPayload(version, type, payload);
160         } catch (CrudException e) {
161             assertEquals(400, e.getHttpStatus().getStatusCode());
162             assertThat(e.getMessage(), is("Invalid Source/Target Urls"));
163         }
164
165         jsonString = edgePayload;
166         payload = EdgePayload.fromJson(jsonString);
167         type = "tosca.relationships.invalidType";
168         try {
169             RelationshipSchemaValidator.validateIncomingAddPayload(version, type, payload);
170         } catch (CrudException e) {
171             assertEquals(400, e.getHttpStatus().getStatusCode());
172             assertThat(e.getMessage(), is("Invalid source/target/relationship type: vserver:pserver:" + type));
173         }
174     }
175
176     @Test
177     public void testValidateIncomingPatchPayloadMissingSource() throws CrudException {
178         String type = "tosca.relationships.HostedOn";
179         String version = "v11";
180         String jsonString;
181
182         String champJson = champEdge.replace("edgeType", type);
183         Edge edge = Edge.fromJson(champJson);
184
185         EdgePayload payload;
186
187         jsonString = edgePayload.replace("services/inventory/v12/vserver/50bdab41-ad1c-4d00-952c-a0aa5d827811", "");
188         payload = EdgePayload.fromJson(jsonString);
189
190         try {
191             RelationshipSchemaValidator.validateIncomingPatchPayload(edge, version, payload);
192         } catch (CrudException e) {
193             assertEquals(400, e.getHttpStatus().getStatusCode());
194             assertThat(e.getMessage(), is("Invalid Source/Target Urls"));
195         }
196     }
197
198     @Test
199     public void testValidateIncomingPatchPayloadInvalidSource() throws CrudException {
200         String type = "tosca.relationships.HostedOn";
201         String version = "v11";
202         String jsonString;
203
204         String champJson = champEdge.replace("edgeType", type);
205         Edge edge = Edge.fromJson(champJson);
206
207         EdgePayload payload;
208
209         jsonString = edgePayload.replace("50bdab41-ad1c-4d00-952c-a0aa5d827811", "invalidId");
210         payload = EdgePayload.fromJson(jsonString);
211         try {
212             RelationshipSchemaValidator.validateIncomingPatchPayload(edge, version, payload);
213         } catch (CrudException e) {
214             assertEquals(400, e.getHttpStatus().getStatusCode());
215             assertThat(e.getMessage(), is("Source can't be updated"));
216         }
217     }
218
219     @Test
220     public void testValidateIncomingPatchPayloadMissingTarget() throws CrudException {
221         String type = "tosca.relationships.HostedOn";
222         String version = "v11";
223         String jsonString;
224
225         String champJson = champEdge.replace("edgeType", type);
226         Edge edge = Edge.fromJson(champJson);
227
228         EdgePayload payload;
229
230         jsonString = edgePayload.replace("services/inventory/v12/pserver/1d326bc7-b985-492b-9604-0d5d1f06f908", "");
231         payload = EdgePayload.fromJson(jsonString);
232
233         try {
234             RelationshipSchemaValidator.validateIncomingPatchPayload(edge, version, payload);
235         } catch (CrudException e) {
236             assertEquals(400, e.getHttpStatus().getStatusCode());
237             assertThat(e.getMessage(), is("Invalid Source/Target Urls"));
238         }
239     }
240
241     @Test
242     public void testValidateIncomingPatchPayloadInvalidTarget() throws CrudException {
243         String type = "tosca.relationships.HostedOn";
244         String version = "v11";
245         String jsonString;
246
247         String champJson = champEdge.replace("edgeType", type);
248         Edge edge = Edge.fromJson(champJson);
249
250         EdgePayload payload;
251
252         jsonString = edgePayload.replace("1d326bc7-b985-492b-9604-0d5d1f06f908", "invalidId");
253         payload = EdgePayload.fromJson(jsonString);
254         try {
255             RelationshipSchemaValidator.validateIncomingPatchPayload(edge, version, payload);
256         } catch (CrudException e) {
257             assertEquals(400, e.getHttpStatus().getStatusCode());
258             assertThat(e.getMessage(), is("Target can't be updated"));
259         }
260     }
261
262     @Test
263     public void testValidateTypeExceptionHandling() {
264         String version = "v11";
265         String type = "tosca.relationships.invalidType";
266
267         try {
268             RelationshipSchemaValidator.validateType(version, type);
269         } catch (CrudException e) {
270             assertEquals(400, e.getHttpStatus().getStatusCode());
271             assertThat(e.getMessage(), is("Invalid " + RelationshipSchema.SCHEMA_RELATIONSHIP_TYPE + ": " + type));
272         }
273     }
274 }