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.service.EdgePayload;
\r
38 import com.google.gson.JsonElement;
\r
40 public class AaiResourceServiceTest {
\r
42 public AaiResourceService aaiResSvc = null;
\r
46 public void setup() {
\r
47 System.setProperty("AJSC_HOME", ".");
\r
48 System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
\r
50 aaiResSvc = new AaiResourceService();
\r
55 * This test validates that we can apply db edge rules against an edge request
\r
56 * payload and have the properties defined in the edge rules merged into the
\r
59 * @throws CrudException
\r
60 * @throws AAIException
\r
63 public void applyEdgeRulesToPayloadTest() throws CrudException, AAIException {
\r
65 String content = "{" +
\r
66 "\"source\": \"services/inventory/v8/l-interface/369553424\", " +
\r
67 "\"target\": \"services/inventory/v8/logical-link/573444128\"," +
\r
68 "\"properties\": {" +
\r
72 // Convert our simulated payload to an EdgePayload object.
\r
73 EdgePayload payload = EdgePayload.fromJson(content);
\r
75 // Now, apply the db edge rules against our edge payload.
\r
76 EdgePayload payloadAfterEdgeRules = aaiResSvc.applyEdgeRulesToPayload(payload);
\r
78 EdgeRules rules = EdgeRules.getInstance();
\r
79 EdgeRule rule = rules.getEdgeRule(EdgeType.COUSIN, "l-interface", "logical-link");
\r
80 Map<EdgeProperty, String> edgeProps = rule.getEdgeProperties();
\r
82 // Validate that the properties defined in the DB edge rules show up in our
\r
84 for(EdgeProperty key : edgeProps.keySet()) {
\r
85 assertTrue(payloadAfterEdgeRules.toString().contains(key.toString()));
\r
91 * This test validates that trying to apply edge rules where there is no
\r
92 * db edge rules entry for the supplied source and target vertex types
\r
93 * produces an exception.
\r
95 * @throws CrudException
\r
98 public void noRuleForEdgeTest() throws CrudException {
\r
100 String content = "{" +
\r
101 "\"source\": \"services/inventory/v8/commodore-64/12345\", " +
\r
102 "\"target\": \"services/inventory/v8/jumpman/67890\"," +
\r
103 "\"properties\": {" +
\r
107 // Convert our simulated payload to an EdgePayload object.
\r
108 EdgePayload payload = EdgePayload.fromJson(content);
\r
110 // Now, apply the db edge rules against our edge payload.
\r
112 aaiResSvc.applyEdgeRulesToPayload(payload);
\r
114 } catch (CrudException e) {
\r
116 // We expected an exception since there is no rule for our made up vertices..
\r
117 assertTrue(e.getMessage().contains("No edge rules for"));
\r
121 // If we're here then something unexpected happened...
\r
127 * This test validates that it is possible to merge client supplied and edge rule
\r
128 * supplied properties into one edge property list.
\r
130 * @throws Exception
\r
133 public void mergeEdgePropertiesTest() throws Exception {
\r
135 String content = "{" +
\r
136 "\"source\": \"services/inventory/v8/l-interface/369553424\", " +
\r
137 "\"target\": \"services/inventory/v8/logical-link/573444128\"," +
\r
138 "\"properties\": {" +
\r
139 "\"multiplicity\": \"many\"," +
\r
140 "\"is-parent\": true," +
\r
141 "\"uses-resource\": \"true\"," +
\r
142 "\"has-del-target\": \"true\"" +
\r
146 EdgePayload payload = EdgePayload.fromJson(content);
\r
147 EdgeRules rules = EdgeRules.getInstance();
\r
148 EdgeRule rule = rules.getEdgeRule(EdgeType.COUSIN, "l-interface", "logical-link");
\r
149 Map<EdgeProperty, String> edgeProps = rule.getEdgeProperties();
\r
151 // Merge the client supplied properties with the properties defined in the DB edge rules.
\r
152 JsonElement mergedProperties =
\r
153 aaiResSvc.mergeProperties(payload.getProperties(), rule.getEdgeProperties());
\r
155 // Now, validate that the resulting set of properties contains both the client and edge
\r
156 // rule supplied properties.
\r
157 String mergedPropertiesString = mergedProperties.toString();
\r
158 assertTrue("Client supplied property 'multiplicity' is missing from merged properties set",
\r
159 mergedPropertiesString.contains("multiplicity"));
\r
160 assertTrue("Client supplied property 'is-parent' is missing from merged properties set",
\r
161 mergedPropertiesString.contains("is-parent"));
\r
162 assertTrue("Client supplied property 'uses-resource' is missing from merged properties set",
\r
163 mergedPropertiesString.contains("uses-resource"));
\r
164 assertTrue("Client supplied property 'has-del-target' is missing from merged properties set",
\r
165 mergedPropertiesString.contains("has-del-target"));
\r
167 for(EdgeProperty key : edgeProps.keySet()) {
\r
168 assertTrue("Edge rule supplied property '" + key.toString() + "' is missing from merged properties set",
\r
169 mergedPropertiesString.contains(key.toString()));
\r
174 * This test validates that if we try to merge client supplied edge properties
\r
175 * with the properties defined in the db edge rules, and there is a conflict,
\r
176 * then the merge will fail.
\r
178 * @throws Exception
\r
181 public void mergeEdgePropertiesConflictTest() throws Exception {
\r
183 String content = "{" +
\r
184 "\"source\": \"services/inventory/v8/l-interface/369553424\", " +
\r
185 "\"target\": \"services/inventory/v8/logical-link/573444128\"," +
\r
186 "\"properties\": {" +
\r
187 "\"contains-other-v\": \"OUT\"" +
\r
191 EdgePayload payload = EdgePayload.fromJson(content);
\r
192 EdgeRules rules = EdgeRules.getInstance();
\r
193 EdgeRule rule = rules.getEdgeRule(EdgeType.COUSIN, "l-interface", "logical-link");
\r
197 // Try to merge our client supplied properties with the properties defined
\r
198 // in the db edge rules.
\r
199 aaiResSvc.mergeProperties(payload.getProperties(), rule.getEdgeProperties());
\r
201 } catch (CrudException e) {
\r
203 // We should have gotten an exception because we are trying to set a parameter which is
\r
204 // already defined in the db edge rules, so if we're here then we are good.
\r
208 // If we made it here then we were allowed to set a property that is already defined
\r
209 // in the db edge rules, which we should not have...
\r