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 import org.junit.runner.RunWith;
34 import org.mockito.junit.MockitoJUnitRunner;
35 import org.onap.crud.OXMModelLoaderSetup;
37 @RunWith(MockitoJUnitRunner.Silent.class)
38 public class RelationshipSchemaValidatorTest extends OXMModelLoaderSetup{
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\"," +
45 "\"prevent-delete\": \"NONE\" } }";
47 private final String champEdge = "{" +
48 "\"key\": \"test-uuid\"," +
49 "\"type\": \"edgeType\"," +
51 "\"prevent-delete\": \"NONE\" }," +
53 "\"key\": \"50bdab41-ad1c-4d00-952c-a0aa5d827811\", \"type\": \"vserver\"}," +
55 "\"key\": \"1d326bc7-b985-492b-9604-0d5d1f06f908\", \"type\": \"pserver\"}" +
60 public void init() throws Exception {
61 System.setProperty("CONFIG_HOME", "src/test/resources");
65 public void testValidateIncomingUpdatePayloadMissingSource() throws CrudException {
66 String type = "tosca.relationships.HostedOn";
67 String version = "v11";
71 String champJson = champEdge.replace("edgeType", type);
72 Edge edge = Edge.fromJson(champJson);
74 jsonString = edgePayload.replace("services/inventory/v12/vserver/50bdab41-ad1c-4d00-952c-a0aa5d827811", "");
75 payload = EdgePayload.fromJson(jsonString);
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"));
86 public void testValidateIncomingUpdatePayloadInvalidSource() throws CrudException {
87 String type = "tosca.relationships.HostedOn";
88 String version = "v11";
92 String champJson = champEdge.replace("edgeType", type);
93 Edge edge = Edge.fromJson(champJson);
95 jsonString = edgePayload.replace("1d326bc7-b985-492b-9604-0d5d1f06f908", "invalidId");
96 payload = EdgePayload.fromJson(jsonString);
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"));
107 public void testValidateIncomingUpdatePayloadMissingTarget() throws CrudException {
108 String type = "tosca.relationships.HostedOn";
109 String version = "v11";
113 String champJson = champEdge.replace("edgeType", type);
114 Edge edge = Edge.fromJson(champJson);
116 jsonString = edgePayload.replace("services/inventory/v12/pserver/1d326bc7-b985-492b-9604-0d5d1f06f908", "");
117 payload = EdgePayload.fromJson(jsonString);
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"));
128 public void testValidateIncomingUpdatePayloadInvalidTarget() throws CrudException {
129 String type = "tosca.relationships.HostedOn";
130 String version = "v11";
134 String champJson = champEdge.replace("edgeType", type);
135 Edge edge = Edge.fromJson(champJson);
137 jsonString = edgePayload.replace("50bdab41-ad1c-4d00-952c-a0aa5d827811", "invalidId");
138 payload = EdgePayload.fromJson(jsonString);
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"));
149 public void testValidateIncomingAddPayloadExceptionHandling() throws CrudException {
150 String type = "tosca.relationships.HostedOn";
151 String version = "v11";
155 jsonString = edgePayload.replace("services/inventory/v12/vserver/50bdab41-ad1c-4d00-952c-a0aa5d827811", "");
156 payload = EdgePayload.fromJson(jsonString);
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"));
165 jsonString = edgePayload;
166 payload = EdgePayload.fromJson(jsonString);
167 type = "tosca.relationships.invalidType";
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));
177 public void testValidateIncomingPatchPayloadMissingSource() throws CrudException {
178 String type = "tosca.relationships.HostedOn";
179 String version = "v11";
182 String champJson = champEdge.replace("edgeType", type);
183 Edge edge = Edge.fromJson(champJson);
187 jsonString = edgePayload.replace("services/inventory/v12/vserver/50bdab41-ad1c-4d00-952c-a0aa5d827811", "");
188 payload = EdgePayload.fromJson(jsonString);
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"));
199 public void testValidateIncomingPatchPayloadInvalidSource() throws CrudException {
200 String type = "tosca.relationships.HostedOn";
201 String version = "v11";
204 String champJson = champEdge.replace("edgeType", type);
205 Edge edge = Edge.fromJson(champJson);
209 jsonString = edgePayload.replace("50bdab41-ad1c-4d00-952c-a0aa5d827811", "invalidId");
210 payload = EdgePayload.fromJson(jsonString);
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"));
220 public void testValidateIncomingPatchPayloadMissingTarget() throws CrudException {
221 String type = "tosca.relationships.HostedOn";
222 String version = "v11";
225 String champJson = champEdge.replace("edgeType", type);
226 Edge edge = Edge.fromJson(champJson);
230 jsonString = edgePayload.replace("services/inventory/v12/pserver/1d326bc7-b985-492b-9604-0d5d1f06f908", "");
231 payload = EdgePayload.fromJson(jsonString);
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"));
242 public void testValidateIncomingPatchPayloadInvalidTarget() throws CrudException {
243 String type = "tosca.relationships.HostedOn";
244 String version = "v11";
247 String champJson = champEdge.replace("edgeType", type);
248 Edge edge = Edge.fromJson(champJson);
252 jsonString = edgePayload.replace("1d326bc7-b985-492b-9604-0d5d1f06f908", "invalidId");
253 payload = EdgePayload.fromJson(jsonString);
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"));
263 public void testValidateTypeExceptionHandling() {
264 String version = "v11";
265 String type = "tosca.relationships.invalidType";
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));