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