2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
\r
6 * Copyright © 2017-2018 Amdocs
\r
7 * ================================================================================
\r
8 * Licensed under the Apache License, Version 2.0 (the "License");
\r
9 * you may not use this file except in compliance with the License.
\r
10 * You may obtain a copy of the License at
\r
12 * http://www.apache.org/licenses/LICENSE-2.0
\r
14 * Unless required by applicable law or agreed to in writing, software
\r
15 * distributed under the License is distributed on an "AS IS" BASIS,
\r
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
17 * See the License for the specific language governing permissions and
\r
18 * limitations under the License.
\r
19 * ============LICENSE_END=========================================================
\r
21 package org.onap.schema.validation;
\r
23 import java.util.ArrayList;
\r
24 import java.util.HashMap;
\r
25 import java.util.List;
\r
26 import java.util.Map;
\r
27 import org.junit.Assert;
\r
28 import org.junit.Before;
\r
29 import org.junit.Rule;
\r
30 import org.junit.Test;
\r
31 import org.junit.rules.ExpectedException;
\r
32 import org.onap.crud.entity.Edge;
\r
33 import org.onap.crud.entity.Vertex;
\r
34 import org.onap.crud.exception.CrudException;
\r
35 import org.onap.crud.parser.EdgePayload;
\r
36 import org.onap.schema.EdgeRulesLoader;
\r
37 import org.onap.schema.validation.MultiplicityValidator.MultiplicityType;
\r
39 public class MultiplicityValidatorTest {
\r
41 private final String postEdgePayload = "{" + "\"type\": \"tosca.relationships.HostedOn\","
\r
42 + "\"source\": \"services/inventory/v12/vserver/50bdab41-ad1c-4d00-952c-a0aa5d827811\","
\r
43 + "\"target\": \"services/inventory/v12/pserver/1d326bc7-b985-492b-9604-0d5d1f06f908\","
\r
44 + "\"properties\": {" + "\"prevent-delete\": \"NONE\" } }";
\r
47 public ExpectedException thrown = ExpectedException.none();
\r
50 public void init() {
\r
51 System.setProperty("CONFIG_HOME", "src/test/resources");
\r
52 EdgeRulesLoader.resetSchemaVersionContext();
\r
56 public void testValidPayloadForMultiplicityRule() throws CrudException {
\r
57 Map<String, List<Edge>> vertexMap = getEdgesForVertex(MultiplicityType.MANY2ONE, true);
\r
58 MultiplicityValidator.validatePayloadMultiplicity(EdgePayload.fromJson(postEdgePayload),
\r
59 vertexMap.get("source"), vertexMap.get("target"),
\r
60 "tosca.relationships.HostedOn", "v11");
\r
64 public void testInvalidPayloadForMultiplicityRule() throws CrudException {
\r
65 thrown.expect(CrudException.class);
\r
66 thrown.expectMessage("MANY2ONE multiplicity rule broken for Edge:vserver:pserver:tosca.relationships.HostedOn");
\r
68 Map<String, List<Edge>> vertexMap = getEdgesForVertex(MultiplicityType.MANY2ONE, false);
\r
69 MultiplicityValidator.validatePayloadMultiplicity(EdgePayload.fromJson(postEdgePayload),
\r
70 vertexMap.get("source"), vertexMap.get("target"),
\r
71 "tosca.relationships.HostedOn", "v11");
\r
75 public void testIsVertexValidForMultiplicityType() throws CrudException {
\r
77 Map<String, List<Edge>> vertexMap = getEdgesForVertex(MultiplicityType.MANY2MANY, true);
\r
78 Assert.assertTrue(MultiplicityValidator.isVertexValidForMultiplicityType(vertexMap.get("source"),
\r
79 vertexMap.get("target"), MultiplicityType.MANY2MANY));
\r
81 vertexMap = getEdgesForVertex(MultiplicityType.MANY2ONE, true);
\r
82 Assert.assertTrue(MultiplicityValidator.isVertexValidForMultiplicityType(
\r
83 vertexMap.get("source"), vertexMap.get("target"), MultiplicityType.MANY2ONE));
\r
85 vertexMap = getEdgesForVertex(MultiplicityType.ONE2MANY, true);
\r
86 Assert.assertTrue(MultiplicityValidator.isVertexValidForMultiplicityType(
\r
87 vertexMap.get("source"), vertexMap.get("target"), MultiplicityType.ONE2MANY));
\r
89 vertexMap = getEdgesForVertex(MultiplicityType.ONE2ONE, true);
\r
90 Assert.assertTrue(MultiplicityValidator.isVertexValidForMultiplicityType(
\r
91 vertexMap.get("source"), vertexMap.get("target"), MultiplicityType.ONE2ONE));
\r
93 vertexMap = getEdgesForVertex(MultiplicityType.ONE2MANY, false);
\r
94 Assert.assertFalse(MultiplicityValidator.isVertexValidForMultiplicityType(
\r
95 vertexMap.get("source"), vertexMap.get("target"), MultiplicityType.ONE2MANY));
\r
97 vertexMap = getEdgesForVertex(MultiplicityType.ONE2ONE, false);
\r
98 Assert.assertFalse(MultiplicityValidator.isVertexValidForMultiplicityType(
\r
99 vertexMap.get("source"), vertexMap.get("target"), MultiplicityType.ONE2ONE));
\r
102 private Map<String, List<Edge>> getEdgesForVertex(MultiplicityType multiplicityType, boolean pass) {
\r
104 Map<String, List<Edge>> vertexMap = new HashMap<String, List<Edge>>();
\r
105 List<Edge> edgesForSourceVertex = new ArrayList<>();
\r
106 List<Edge> edgesForTargetVertex = new ArrayList<>();
\r
108 switch (multiplicityType) {
\r
111 Edge edge = new Edge.Builder("type").source(new Vertex.Builder("source").build())
\r
112 .target(new Vertex.Builder("target").build()).build();
\r
113 edgesForSourceVertex.add(edge);
\r
114 edgesForTargetVertex.add(edge);
\r
119 Edge edge = new Edge.Builder("type").source(new Vertex.Builder("source").build())
\r
120 .target(new Vertex.Builder("target").build()).build();
\r
121 edgesForTargetVertex.add(edge);
\r
123 Edge edge = new Edge.Builder("type").source(new Vertex.Builder("source").build())
\r
124 .target(new Vertex.Builder("target").build()).build();
\r
125 edgesForSourceVertex.add(edge);
\r
126 edgesForTargetVertex.add(edge);
\r
131 Edge edge = new Edge.Builder("type").source(new Vertex.Builder("source").build())
\r
132 .target(new Vertex.Builder("target").build()).build();
\r
133 edgesForSourceVertex.add(edge);
\r
135 Edge edge = new Edge.Builder("type").source(new Vertex.Builder("source").build())
\r
136 .target(new Vertex.Builder("target").build()).build();
\r
137 edgesForSourceVertex.add(edge);
\r
138 edgesForTargetVertex.add(edge);
\r
143 Edge edge = new Edge.Builder("type").source(new Vertex.Builder("source").build())
\r
144 .target(new Vertex.Builder("target").build()).build();
\r
145 edgesForSourceVertex.add(edge);
\r
146 edgesForTargetVertex.add(edge);
\r
150 vertexMap.put("source", edgesForSourceVertex);
\r
151 vertexMap.put("target", edgesForTargetVertex);
\r