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.io.File;
\r
24 import java.util.ArrayList;
\r
25 import java.util.HashMap;
\r
26 import java.util.List;
\r
27 import java.util.Map;
\r
28 import org.junit.Assert;
\r
29 import org.junit.Before;
\r
30 import org.junit.Rule;
\r
31 import org.junit.Test;
\r
32 import org.junit.rules.ExpectedException;
\r
33 import org.onap.crud.entity.Edge;
\r
34 import org.onap.crud.entity.Vertex;
\r
35 import org.onap.crud.exception.CrudException;
\r
36 import org.onap.crud.parser.EdgePayload;
\r
37 import org.onap.schema.EdgeRulesLoader;
\r
38 import org.onap.schema.validation.MultiplicityValidator.MultiplicityType;
\r
40 public class MultiplicityValidatorTest {
\r
42 private final String postEdgePayload = "{" + "\"type\": \"tosca.relationships.HostedOn\","
\r
43 + "\"source\": \"services/inventory/v12/vserver/50bdab41-ad1c-4d00-952c-a0aa5d827811\","
\r
44 + "\"target\": \"services/inventory/v12/pserver/1d326bc7-b985-492b-9604-0d5d1f06f908\","
\r
45 + "\"properties\": {" + "\"prevent-delete\": \"NONE\" } }";
\r
48 public ExpectedException thrown = ExpectedException.none();
\r
51 public void init() {
\r
52 ClassLoader classLoader = getClass().getClassLoader();
\r
53 File dir = new File(classLoader.getResource("rules").getFile());
\r
54 System.setProperty("CONFIG_HOME", dir.getParent());
\r
55 EdgeRulesLoader.resetSchemaVersionContext();
\r
59 public void testValidPayloadForMultiplicityRule() throws CrudException {
\r
60 Map<String, List<Edge>> vertexMap = getEdgesForVertex(MultiplicityType.MANY2ONE, true);
\r
61 MultiplicityValidator.validatePayloadMultiplicity(EdgePayload.fromJson(postEdgePayload),
\r
62 vertexMap.get("source"), vertexMap.get("target"),
\r
63 "tosca.relationships.HostedOn", "v11");
\r
67 public void testInvalidPayloadForMultiplicityRule() throws CrudException {
\r
68 thrown.expect(CrudException.class);
\r
69 thrown.expectMessage("MANY2ONE multiplicity rule broken for Edge:vserver:pserver:tosca.relationships.HostedOn");
\r
71 Map<String, List<Edge>> vertexMap = getEdgesForVertex(MultiplicityType.MANY2ONE, false);
\r
72 MultiplicityValidator.validatePayloadMultiplicity(EdgePayload.fromJson(postEdgePayload),
\r
73 vertexMap.get("source"), vertexMap.get("target"),
\r
74 "tosca.relationships.HostedOn", "v11");
\r
78 public void testIsVertexValidForMultiplicityType() throws CrudException {
\r
80 Map<String, List<Edge>> vertexMap = getEdgesForVertex(MultiplicityType.MANY2MANY, true);
\r
81 Assert.assertTrue(MultiplicityValidator.isVertexValidForMultiplicityType(vertexMap.get("source"),
\r
82 vertexMap.get("target"), MultiplicityType.MANY2MANY));
\r
84 vertexMap = getEdgesForVertex(MultiplicityType.MANY2ONE, true);
\r
85 Assert.assertTrue(MultiplicityValidator.isVertexValidForMultiplicityType(
\r
86 vertexMap.get("source"), vertexMap.get("target"), MultiplicityType.MANY2ONE));
\r
88 vertexMap = getEdgesForVertex(MultiplicityType.ONE2MANY, true);
\r
89 Assert.assertTrue(MultiplicityValidator.isVertexValidForMultiplicityType(
\r
90 vertexMap.get("source"), vertexMap.get("target"), MultiplicityType.ONE2MANY));
\r
92 vertexMap = getEdgesForVertex(MultiplicityType.ONE2ONE, true);
\r
93 Assert.assertTrue(MultiplicityValidator.isVertexValidForMultiplicityType(
\r
94 vertexMap.get("source"), vertexMap.get("target"), MultiplicityType.ONE2ONE));
\r
96 vertexMap = getEdgesForVertex(MultiplicityType.ONE2MANY, false);
\r
97 Assert.assertFalse(MultiplicityValidator.isVertexValidForMultiplicityType(
\r
98 vertexMap.get("source"), vertexMap.get("target"), MultiplicityType.ONE2MANY));
\r
100 vertexMap = getEdgesForVertex(MultiplicityType.ONE2ONE, false);
\r
101 Assert.assertFalse(MultiplicityValidator.isVertexValidForMultiplicityType(
\r
102 vertexMap.get("source"), vertexMap.get("target"), MultiplicityType.ONE2ONE));
\r
105 private Map<String, List<Edge>> getEdgesForVertex(MultiplicityType multiplicityType, boolean pass) {
\r
107 Map<String, List<Edge>> vertexMap = new HashMap<String, List<Edge>>();
\r
108 List<Edge> edgesForSourceVertex = new ArrayList<>();
\r
109 List<Edge> edgesForTargetVertex = new ArrayList<>();
\r
111 switch (multiplicityType) {
\r
114 Edge edge = new Edge.Builder("type").source(new Vertex.Builder("source").build())
\r
115 .target(new Vertex.Builder("target").build()).build();
\r
116 edgesForSourceVertex.add(edge);
\r
117 edgesForTargetVertex.add(edge);
\r
122 Edge edge = new Edge.Builder("type").source(new Vertex.Builder("source").build())
\r
123 .target(new Vertex.Builder("target").build()).build();
\r
124 edgesForTargetVertex.add(edge);
\r
126 Edge edge = new Edge.Builder("type").source(new Vertex.Builder("source").build())
\r
127 .target(new Vertex.Builder("target").build()).build();
\r
128 edgesForSourceVertex.add(edge);
\r
129 edgesForTargetVertex.add(edge);
\r
134 Edge edge = new Edge.Builder("type").source(new Vertex.Builder("source").build())
\r
135 .target(new Vertex.Builder("target").build()).build();
\r
136 edgesForSourceVertex.add(edge);
\r
138 Edge edge = new Edge.Builder("type").source(new Vertex.Builder("source").build())
\r
139 .target(new Vertex.Builder("target").build()).build();
\r
140 edgesForSourceVertex.add(edge);
\r
141 edgesForTargetVertex.add(edge);
\r
146 Edge edge = new Edge.Builder("type").source(new Vertex.Builder("source").build())
\r
147 .target(new Vertex.Builder("target").build()).build();
\r
148 edgesForSourceVertex.add(edge);
\r
149 edgesForTargetVertex.add(edge);
\r
153 vertexMap.put("source", edgesForSourceVertex);
\r
154 vertexMap.put("target", edgesForTargetVertex);
\r