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.crud.service;
\r
23 import static org.junit.Assert.assertTrue;
\r
24 import static org.junit.Assert.fail;
\r
26 import java.util.Map;
\r
28 import org.junit.Before;
\r
29 import org.junit.Test;
\r
30 import org.onap.aai.exceptions.AAIException;
\r
31 import org.onap.aai.serialization.db.EdgeProperty;
\r
32 import org.onap.aai.serialization.db.EdgeRule;
\r
33 import org.onap.aai.serialization.db.EdgeRules;
\r
34 import org.onap.aai.serialization.db.EdgeType;
\r
35 import org.onap.crud.exception.CrudException;
\r
36 import org.onap.crud.parser.EdgePayload;
\r
37 import com.google.gson.JsonElement;
\r
39 public class AaiResourceServiceTest {
\r
41 public AaiResourceService aaiResSvc = null;
\r
45 public void setup() {
\r
46 System.setProperty("AJSC_HOME", ".");
\r
47 System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
\r
49 aaiResSvc = new AaiResourceService();
\r
54 * This test validates that we can apply db edge rules against an edge request
\r
55 * payload and have the properties defined in the edge rules merged into the
\r
58 * @throws CrudException
\r
59 * @throws AAIException
\r
62 public void applyEdgeRulesToPayloadTest() throws CrudException, AAIException {
\r
64 String content = "{" +
\r
65 "\"source\": \"services/inventory/v8/l-interface/369553424\", " +
\r
66 "\"target\": \"services/inventory/v8/logical-link/573444128\"," +
\r
67 "\"properties\": {" +
\r
71 // Convert our simulated payload to an EdgePayload object.
\r
72 EdgePayload payload = EdgePayload.fromJson(content);
\r
74 // Now, apply the db edge rules against our edge payload.
\r
75 EdgePayload payloadAfterEdgeRules = aaiResSvc.applyEdgeRulesToPayload(payload);
\r
77 EdgeRules rules = EdgeRules.getInstance();
\r
78 EdgeRule rule = rules.getEdgeRule(EdgeType.COUSIN, "l-interface", "logical-link");
\r
79 Map<EdgeProperty, String> edgeProps = rule.getEdgeProperties();
\r
81 // Validate that the properties defined in the DB edge rules show up in our
\r
83 for(EdgeProperty key : edgeProps.keySet()) {
\r
84 assertTrue(payloadAfterEdgeRules.toString().contains(key.toString()));
\r
90 * This test validates that trying to apply edge rules where there is no
\r
91 * db edge rules entry for the supplied source and target vertex types
\r
92 * produces an exception.
\r
94 * @throws CrudException
\r
97 public void noRuleForEdgeTest() throws CrudException {
\r
99 String content = "{" +
\r
100 "\"source\": \"services/inventory/v8/commodore-64/12345\", " +
\r
101 "\"target\": \"services/inventory/v8/jumpman/67890\"," +
\r
102 "\"properties\": {" +
\r
106 // Convert our simulated payload to an EdgePayload object.
\r
107 EdgePayload payload = EdgePayload.fromJson(content);
\r
109 // Now, apply the db edge rules against our edge payload.
\r
111 aaiResSvc.applyEdgeRulesToPayload(payload);
\r
113 } catch (CrudException e) {
\r
115 // We expected an exception since there is no rule for our made up vertices..
\r
116 assertTrue(e.getMessage().contains("No edge rules for"));
\r
120 // If we're here then something unexpected happened...
\r
126 * This test validates that it is possible to merge client supplied and edge rule
\r
127 * supplied properties into one edge property list.
\r
129 * @throws Exception
\r
132 public void mergeEdgePropertiesTest() throws Exception {
\r
134 String content = "{" +
\r
135 "\"source\": \"services/inventory/v8/l-interface/369553424\", " +
\r
136 "\"target\": \"services/inventory/v8/logical-link/573444128\"," +
\r
137 "\"properties\": {" +
\r
138 "\"multiplicity\": \"many\"," +
\r
139 "\"is-parent\": true," +
\r
140 "\"uses-resource\": \"true\"," +
\r
141 "\"has-del-target\": \"true\"" +
\r
145 EdgePayload payload = EdgePayload.fromJson(content);
\r
146 EdgeRules rules = EdgeRules.getInstance();
\r
147 EdgeRule rule = rules.getEdgeRule(EdgeType.COUSIN, "l-interface", "logical-link");
\r
148 Map<EdgeProperty, String> edgeProps = rule.getEdgeProperties();
\r
150 // Merge the client supplied properties with the properties defined in the DB edge rules.
\r
151 JsonElement mergedProperties =
\r
152 aaiResSvc.mergeProperties(payload.getProperties(), rule.getEdgeProperties());
\r
154 // Now, validate that the resulting set of properties contains both the client and edge
\r
155 // rule supplied properties.
\r
156 String mergedPropertiesString = mergedProperties.toString();
\r
157 assertTrue("Client supplied property 'multiplicity' is missing from merged properties set",
\r
158 mergedPropertiesString.contains("multiplicity"));
\r
159 assertTrue("Client supplied property 'is-parent' is missing from merged properties set",
\r
160 mergedPropertiesString.contains("is-parent"));
\r
161 assertTrue("Client supplied property 'uses-resource' is missing from merged properties set",
\r
162 mergedPropertiesString.contains("uses-resource"));
\r
163 assertTrue("Client supplied property 'has-del-target' is missing from merged properties set",
\r
164 mergedPropertiesString.contains("has-del-target"));
\r
166 for(EdgeProperty key : edgeProps.keySet()) {
\r
167 assertTrue("Edge rule supplied property '" + key.toString() + "' is missing from merged properties set",
\r
168 mergedPropertiesString.contains(key.toString()));
\r
173 * This test validates that if we try to merge client supplied edge properties
\r
174 * with the properties defined in the db edge rules, and there is a conflict,
\r
175 * then the merge will fail.
\r
177 * @throws Exception
\r
180 public void mergeEdgePropertiesConflictTest() throws Exception {
\r
182 String content = "{" +
\r
183 "\"source\": \"services/inventory/v8/l-interface/369553424\", " +
\r
184 "\"target\": \"services/inventory/v8/logical-link/573444128\"," +
\r
185 "\"properties\": {" +
\r
186 "\"contains-other-v\": \"OUT\"" +
\r
190 EdgePayload payload = EdgePayload.fromJson(content);
\r
191 EdgeRules rules = EdgeRules.getInstance();
\r
192 EdgeRule rule = rules.getEdgeRule(EdgeType.COUSIN, "l-interface", "logical-link");
\r
196 // Try to merge our client supplied properties with the properties defined
\r
197 // in the db edge rules.
\r
198 aaiResSvc.mergeProperties(payload.getProperties(), rule.getEdgeProperties());
\r
200 } catch (CrudException e) {
\r
202 // We should have gotten an exception because we are trying to set a parameter which is
\r
203 // already defined in the db edge rules, so if we're here then we are good.
\r
207 // If we made it here then we were allowed to set a property that is already defined
\r
208 // in the db edge rules, which we should not have...
\r