2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.schema;
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;
33 public class RelationshipSchemaValidatorTest {
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\"," +
40 "\"prevent-delete\": \"NONE\" } }";
42 private final String champEdge = "{" +
43 "\"key\": \"test-uuid\"," +
44 "\"type\": \"edgeType\"," +
46 "\"prevent-delete\": \"NONE\" }," +
48 "\"key\": \"50bdab41-ad1c-4d00-952c-a0aa5d827811\", \"type\": \"vserver\"}," +
50 "\"key\": \"1d326bc7-b985-492b-9604-0d5d1f06f908\", \"type\": \"pserver\"}" +
55 public void init() throws Exception {
56 System.setProperty("CONFIG_HOME", "src/test/resources");
60 public void testValidateIncomingUpdatePayloadMissingSource() throws CrudException {
61 String type = "tosca.relationships.HostedOn";
62 String version = "v11";
66 String champJson = champEdge.replace("edgeType", type);
67 Edge edge = Edge.fromJson(champJson);
69 jsonString = edgePayload.replace("services/inventory/v12/vserver/50bdab41-ad1c-4d00-952c-a0aa5d827811", "");
70 payload = EdgePayload.fromJson(jsonString);
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"));
81 public void testValidateIncomingUpdatePayloadInvalidSource() throws CrudException {
82 String type = "tosca.relationships.HostedOn";
83 String version = "v11";
87 String champJson = champEdge.replace("edgeType", type);
88 Edge edge = Edge.fromJson(champJson);
90 jsonString = edgePayload.replace("1d326bc7-b985-492b-9604-0d5d1f06f908", "invalidId");
91 payload = EdgePayload.fromJson(jsonString);
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"));
102 public void testValidateIncomingUpdatePayloadMissingTarget() throws CrudException {
103 String type = "tosca.relationships.HostedOn";
104 String version = "v11";
108 String champJson = champEdge.replace("edgeType", type);
109 Edge edge = Edge.fromJson(champJson);
111 jsonString = edgePayload.replace("services/inventory/v12/pserver/1d326bc7-b985-492b-9604-0d5d1f06f908", "");
112 payload = EdgePayload.fromJson(jsonString);
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"));
123 public void testValidateIncomingUpdatePayloadInvalidTarget() throws CrudException {
124 String type = "tosca.relationships.HostedOn";
125 String version = "v11";
129 String champJson = champEdge.replace("edgeType", type);
130 Edge edge = Edge.fromJson(champJson);
132 jsonString = edgePayload.replace("50bdab41-ad1c-4d00-952c-a0aa5d827811", "invalidId");
133 payload = EdgePayload.fromJson(jsonString);
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"));
144 public void testValidateIncomingAddPayloadExceptionHandling() throws CrudException {
145 String type = "tosca.relationships.HostedOn";
146 String version = "v11";
150 jsonString = edgePayload.replace("services/inventory/v12/vserver/50bdab41-ad1c-4d00-952c-a0aa5d827811", "");
151 payload = EdgePayload.fromJson(jsonString);
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"));
160 jsonString = edgePayload;
161 payload = EdgePayload.fromJson(jsonString);
162 type = "tosca.relationships.invalidType";
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));
172 public void testValidateIncomingPatchPayloadMissingSource() throws CrudException {
173 String type = "tosca.relationships.HostedOn";
174 String version = "v11";
177 String champJson = champEdge.replace("edgeType", type);
178 Edge edge = Edge.fromJson(champJson);
182 jsonString = edgePayload.replace("services/inventory/v12/vserver/50bdab41-ad1c-4d00-952c-a0aa5d827811", "");
183 payload = EdgePayload.fromJson(jsonString);
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"));
194 public void testValidateIncomingPatchPayloadInvalidSource() throws CrudException {
195 String type = "tosca.relationships.HostedOn";
196 String version = "v11";
199 String champJson = champEdge.replace("edgeType", type);
200 Edge edge = Edge.fromJson(champJson);
204 jsonString = edgePayload.replace("50bdab41-ad1c-4d00-952c-a0aa5d827811", "invalidId");
205 payload = EdgePayload.fromJson(jsonString);
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"));
215 public void testValidateIncomingPatchPayloadMissingTarget() throws CrudException {
216 String type = "tosca.relationships.HostedOn";
217 String version = "v11";
220 String champJson = champEdge.replace("edgeType", type);
221 Edge edge = Edge.fromJson(champJson);
225 jsonString = edgePayload.replace("services/inventory/v12/pserver/1d326bc7-b985-492b-9604-0d5d1f06f908", "");
226 payload = EdgePayload.fromJson(jsonString);
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"));
237 public void testValidateIncomingPatchPayloadInvalidTarget() throws CrudException {
238 String type = "tosca.relationships.HostedOn";
239 String version = "v11";
242 String champJson = champEdge.replace("edgeType", type);
243 Edge edge = Edge.fromJson(champJson);
247 jsonString = edgePayload.replace("1d326bc7-b985-492b-9604-0d5d1f06f908", "invalidId");
248 payload = EdgePayload.fromJson(jsonString);
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"));
258 public void testValidateTypeExceptionHandling() {
259 String version = "v11";
260 String type = "tosca.relationships.invalidType";
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));