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 import org.junit.runner.RunWith;
\r
40 import org.mockito.junit.MockitoJUnitRunner;
\r
41 import org.onap.crud.OXMModelLoaderSetup;
\r
43 @RunWith(MockitoJUnitRunner.Silent.class)
\r
44 public class MultiplicityValidatorTest extends OXMModelLoaderSetup{
\r
46 private final String postEdgePayload = "{" + "\"type\": \"tosca.relationships.HostedOn\","
\r
47 + "\"source\": \"services/inventory/v12/vserver/50bdab41-ad1c-4d00-952c-a0aa5d827811\","
\r
48 + "\"target\": \"services/inventory/v12/pserver/1d326bc7-b985-492b-9604-0d5d1f06f908\","
\r
49 + "\"properties\": {" + "\"prevent-delete\": \"NONE\" } }";
\r
52 public ExpectedException thrown = ExpectedException.none();
\r
55 public void init() {
\r
56 System.setProperty("CONFIG_HOME", "src/test/resources");
\r
57 EdgeRulesLoader.resetSchemaVersionContext();
\r
61 public void testValidPayloadForMultiplicityRule() throws CrudException {
\r
62 Map<String, List<Edge>> vertexMap = getEdgesForVertex(MultiplicityType.MANY2ONE, true);
\r
63 MultiplicityValidator.validatePayloadMultiplicity(EdgePayload.fromJson(postEdgePayload),
\r
64 vertexMap.get("source"), vertexMap.get("target"),
\r
65 "tosca.relationships.HostedOn", "v11");
\r
69 public void testInvalidPayloadForMultiplicityRule() throws CrudException {
\r
70 thrown.expect(CrudException.class);
\r
71 thrown.expectMessage("MANY2ONE multiplicity rule broken for Edge:vserver:pserver:tosca.relationships.HostedOn");
\r
73 Map<String, List<Edge>> vertexMap = getEdgesForVertex(MultiplicityType.MANY2ONE, false);
\r
74 MultiplicityValidator.validatePayloadMultiplicity(EdgePayload.fromJson(postEdgePayload),
\r
75 vertexMap.get("source"), vertexMap.get("target"),
\r
76 "tosca.relationships.HostedOn", "v11");
\r
80 public void testIsVertexValidForMultiplicityType() throws CrudException {
\r
82 Map<String, List<Edge>> vertexMap = getEdgesForVertex(MultiplicityType.MANY2MANY, true);
\r
83 Assert.assertTrue(MultiplicityValidator.isVertexValidForMultiplicityType(vertexMap.get("source"),
\r
84 vertexMap.get("target"), MultiplicityType.MANY2MANY));
\r
86 vertexMap = getEdgesForVertex(MultiplicityType.MANY2ONE, true);
\r
87 Assert.assertTrue(MultiplicityValidator.isVertexValidForMultiplicityType(
\r
88 vertexMap.get("source"), vertexMap.get("target"), MultiplicityType.MANY2ONE));
\r
90 vertexMap = getEdgesForVertex(MultiplicityType.ONE2MANY, true);
\r
91 Assert.assertTrue(MultiplicityValidator.isVertexValidForMultiplicityType(
\r
92 vertexMap.get("source"), vertexMap.get("target"), MultiplicityType.ONE2MANY));
\r
94 vertexMap = getEdgesForVertex(MultiplicityType.ONE2ONE, true);
\r
95 Assert.assertTrue(MultiplicityValidator.isVertexValidForMultiplicityType(
\r
96 vertexMap.get("source"), vertexMap.get("target"), MultiplicityType.ONE2ONE));
\r
98 vertexMap = getEdgesForVertex(MultiplicityType.ONE2MANY, false);
\r
99 Assert.assertFalse(MultiplicityValidator.isVertexValidForMultiplicityType(
\r
100 vertexMap.get("source"), vertexMap.get("target"), MultiplicityType.ONE2MANY));
\r
102 vertexMap = getEdgesForVertex(MultiplicityType.ONE2ONE, false);
\r
103 Assert.assertFalse(MultiplicityValidator.isVertexValidForMultiplicityType(
\r
104 vertexMap.get("source"), vertexMap.get("target"), MultiplicityType.ONE2ONE));
\r
107 private Map<String, List<Edge>> getEdgesForVertex(MultiplicityType multiplicityType, boolean pass) {
\r
109 Map<String, List<Edge>> vertexMap = new HashMap<String, List<Edge>>();
\r
110 List<Edge> edgesForSourceVertex = new ArrayList<>();
\r
111 List<Edge> edgesForTargetVertex = new ArrayList<>();
\r
113 switch (multiplicityType) {
\r
116 Edge edge = new Edge.Builder("type").source(new Vertex.Builder("source").build())
\r
117 .target(new Vertex.Builder("target").build()).build();
\r
118 edgesForSourceVertex.add(edge);
\r
119 edgesForTargetVertex.add(edge);
\r
124 Edge edge = new Edge.Builder("type").source(new Vertex.Builder("source").build())
\r
125 .target(new Vertex.Builder("target").build()).build();
\r
126 edgesForTargetVertex.add(edge);
\r
128 Edge edge = new Edge.Builder("type").source(new Vertex.Builder("source").build())
\r
129 .target(new Vertex.Builder("target").build()).build();
\r
130 edgesForSourceVertex.add(edge);
\r
131 edgesForTargetVertex.add(edge);
\r
136 Edge edge = new Edge.Builder("type").source(new Vertex.Builder("source").build())
\r
137 .target(new Vertex.Builder("target").build()).build();
\r
138 edgesForSourceVertex.add(edge);
\r
140 Edge edge = new Edge.Builder("type").source(new Vertex.Builder("source").build())
\r
141 .target(new Vertex.Builder("target").build()).build();
\r
142 edgesForSourceVertex.add(edge);
\r
143 edgesForTargetVertex.add(edge);
\r
148 Edge edge = new Edge.Builder("type").source(new Vertex.Builder("source").build())
\r
149 .target(new Vertex.Builder("target").build()).build();
\r
150 edgesForSourceVertex.add(edge);
\r
151 edgesForTargetVertex.add(edge);
\r
155 vertexMap.put("source", edgesForSourceVertex);
\r
156 vertexMap.put("target", edgesForTargetVertex);
\r