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.service.EdgePayload;
31 import org.junit.runner.RunWith;
32 import org.mockito.junit.MockitoJUnitRunner;
33 import org.onap.crud.OXMModelLoaderSetup;
35 @RunWith(MockitoJUnitRunner.Silent.class)
36 public class RelationshipSchemaValidatorTest extends OXMModelLoaderSetup{
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\"," +
43 "\"prevent-delete\": \"NONE\" } }";
45 private final String champEdge = "{" +
46 "\"key\": \"test-uuid\"," +
47 "\"type\": \"edgeType\"," +
49 "\"prevent-delete\": \"NONE\" }," +
51 "\"key\": \"50bdab41-ad1c-4d00-952c-a0aa5d827811\", \"type\": \"vserver\"}," +
53 "\"key\": \"1d326bc7-b985-492b-9604-0d5d1f06f908\", \"type\": \"pserver\"}" +
58 public void init() throws Exception {
59 System.setProperty("CONFIG_HOME", "src/test/resources");
63 public void testValidateIncomingUpdatePayloadMissingSource() throws CrudException {
64 String type = "tosca.relationships.HostedOn";
65 String version = "v11";
69 String champJson = champEdge.replace("edgeType", type);
70 Edge edge = Edge.fromJson(champJson);
72 jsonString = edgePayload.replace("services/inventory/v12/vserver/50bdab41-ad1c-4d00-952c-a0aa5d827811", "");
73 payload = EdgePayload.fromJson(jsonString);
76 RelationshipSchemaValidator.validateIncomingUpdatePayload(edge, version, payload);
77 } catch (CrudException e) {
78 assertEquals(400, e.getHttpStatus().getStatusCode());
79 assertThat(e.getMessage(), is("Invalid Source Urls"));
84 public void testValidateIncomingUpdatePayloadInvalidSource() throws CrudException {
85 String type = "tosca.relationships.HostedOn";
86 String version = "v11";
90 String champJson = champEdge.replace("edgeType", type);
91 Edge edge = Edge.fromJson(champJson);
93 jsonString = edgePayload.replace("1d326bc7-b985-492b-9604-0d5d1f06f908", "invalidId");
94 payload = EdgePayload.fromJson(jsonString);
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"));
105 public void testValidateIncomingUpdatePayloadMissingTarget() throws CrudException {
106 String type = "tosca.relationships.HostedOn";
107 String version = "v11";
111 String champJson = champEdge.replace("edgeType", type);
112 Edge edge = Edge.fromJson(champJson);
114 jsonString = edgePayload.replace("services/inventory/v12/pserver/1d326bc7-b985-492b-9604-0d5d1f06f908", "");
115 payload = EdgePayload.fromJson(jsonString);
118 RelationshipSchemaValidator.validateIncomingUpdatePayload(edge, version, payload);
119 } catch (CrudException e) {
120 assertEquals(400, e.getHttpStatus().getStatusCode());
121 assertThat(e.getMessage(), is("Invalid Target Urls"));
126 public void testValidateIncomingUpdatePayloadInvalidTarget() throws CrudException {
127 String type = "tosca.relationships.HostedOn";
128 String version = "v11";
132 String champJson = champEdge.replace("edgeType", type);
133 Edge edge = Edge.fromJson(champJson);
135 jsonString = edgePayload.replace("50bdab41-ad1c-4d00-952c-a0aa5d827811", "invalidId");
136 payload = EdgePayload.fromJson(jsonString);
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"));
147 public void testValidateIncomingAddPayloadExceptionHandling() throws CrudException {
148 String type = "tosca.relationships.HostedOn";
149 String version = "v11";
153 jsonString = edgePayload.replace("services/inventory/v12/vserver/50bdab41-ad1c-4d00-952c-a0aa5d827811", "");
154 payload = EdgePayload.fromJson(jsonString);
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"));
163 jsonString = edgePayload;
164 payload = EdgePayload.fromJson(jsonString);
165 type = "tosca.relationships.invalidType";
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));
175 public void testValidateIncomingPatchPayloadMissingSource() throws CrudException {
176 String type = "tosca.relationships.HostedOn";
177 String version = "v11";
180 String champJson = champEdge.replace("edgeType", type);
181 Edge edge = Edge.fromJson(champJson);
185 jsonString = edgePayload.replace("services/inventory/v12/vserver/50bdab41-ad1c-4d00-952c-a0aa5d827811", "");
186 payload = EdgePayload.fromJson(jsonString);
189 RelationshipSchemaValidator.validateIncomingPatchPayload(edge, version, payload);
190 } catch (CrudException e) {
191 assertEquals(400, e.getHttpStatus().getStatusCode());
192 assertThat(e.getMessage(), is("Invalid Source Urls"));
197 public void testValidateIncomingPatchPayloadInvalidSource() throws CrudException {
198 String type = "tosca.relationships.HostedOn";
199 String version = "v11";
202 String champJson = champEdge.replace("edgeType", type);
203 Edge edge = Edge.fromJson(champJson);
207 jsonString = edgePayload.replace("50bdab41-ad1c-4d00-952c-a0aa5d827811", "invalidId");
208 payload = EdgePayload.fromJson(jsonString);
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"));
218 public void testValidateIncomingPatchPayloadMissingTarget() throws CrudException {
219 String type = "tosca.relationships.HostedOn";
220 String version = "v11";
223 String champJson = champEdge.replace("edgeType", type);
224 Edge edge = Edge.fromJson(champJson);
228 jsonString = edgePayload.replace("services/inventory/v12/pserver/1d326bc7-b985-492b-9604-0d5d1f06f908", "");
229 payload = EdgePayload.fromJson(jsonString);
232 RelationshipSchemaValidator.validateIncomingPatchPayload(edge, version, payload);
233 } catch (CrudException e) {
234 assertEquals(400, e.getHttpStatus().getStatusCode());
235 assertThat(e.getMessage(), is("Invalid Target Urls"));
240 public void testValidateIncomingPatchPayloadInvalidTarget() throws CrudException {
241 String type = "tosca.relationships.HostedOn";
242 String version = "v11";
245 String champJson = champEdge.replace("edgeType", type);
246 Edge edge = Edge.fromJson(champJson);
250 jsonString = edgePayload.replace("1d326bc7-b985-492b-9604-0d5d1f06f908", "invalidId");
251 payload = EdgePayload.fromJson(jsonString);
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"));
261 public void testValidateTypeExceptionHandling() {
262 String version = "v11";
263 String type = "tosca.relationships.invalidType";
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));