1 package org.onap.schema;
\r
3 import static org.junit.Assert.assertTrue;
\r
4 import static org.junit.Assert.fail;
\r
6 import java.util.Map;
\r
8 import org.junit.Before;
\r
9 import org.junit.Test;
\r
10 import org.onap.aai.exceptions.AAIException;
\r
11 import org.onap.aai.serialization.db.EdgeProperty;
\r
12 import org.onap.aai.serialization.db.EdgeRule;
\r
13 import org.onap.aai.serialization.db.EdgeRules;
\r
14 import org.onap.aai.serialization.db.EdgeType;
\r
15 import org.onap.crud.exception.CrudException;
\r
16 import org.onap.crud.service.AaiResourceService;
\r
17 import org.onap.crud.service.EdgePayload;
\r
19 import com.google.gson.JsonElement;
\r
21 public class AaiResourceServiceTest {
\r
23 public AaiResourceService aaiResSvc = null;
\r
27 public void setup() {
\r
28 System.setProperty("AJSC_HOME", ".");
\r
29 System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
\r
31 aaiResSvc = new AaiResourceService();
\r
36 * This test validates that we can apply db edge rules against an edge request
\r
37 * payload and have the properties defined in the edge rules merged into the
\r
40 * @throws CrudException
\r
41 * @throws AAIException
\r
44 public void applyEdgeRulesToPayloadTest() throws CrudException, AAIException {
\r
46 String content = "{" +
\r
47 "\"source\": \"services/inventory/v8/l-interface/369553424\", " +
\r
48 "\"target\": \"services/inventory/v8/logical-link/573444128\"," +
\r
49 "\"properties\": {" +
\r
53 // Convert our simulated payload to an EdgePayload object.
\r
54 EdgePayload payload = EdgePayload.fromJson(content);
\r
56 // Now, apply the db edge rules against our edge payload.
\r
57 EdgePayload payloadAfterEdgeRules = aaiResSvc.applyEdgeRulesToPayload(payload);
\r
59 EdgeRules rules = EdgeRules.getInstance();
\r
60 EdgeRule rule = rules.getEdgeRule(EdgeType.COUSIN, "l-interface", "logical-link");
\r
61 Map<EdgeProperty, String> edgeProps = rule.getEdgeProperties();
\r
63 // Validate that the properties defined in the DB edge rules show up in our
\r
65 for(EdgeProperty key : edgeProps.keySet()) {
\r
66 assertTrue(payloadAfterEdgeRules.toString().contains(key.toString()));
\r
72 * This test validates that trying to apply edge rules where there is no
\r
73 * db edge rules entry for the supplied source and target vertex types
\r
74 * produces an exception.
\r
76 * @throws CrudException
\r
79 public void noRuleForEdgeTest() throws CrudException {
\r
81 String content = "{" +
\r
82 "\"source\": \"services/inventory/v8/commodore-64/12345\", " +
\r
83 "\"target\": \"services/inventory/v8/jumpman/67890\"," +
\r
84 "\"properties\": {" +
\r
88 // Convert our simulated payload to an EdgePayload object.
\r
89 EdgePayload payload = EdgePayload.fromJson(content);
\r
91 // Now, apply the db edge rules against our edge payload.
\r
93 aaiResSvc.applyEdgeRulesToPayload(payload);
\r
95 } catch (CrudException e) {
\r
97 // We expected an exception since there is no rule for our made up vertices..
\r
98 assertTrue(e.getMessage().contains("No edge rules for"));
\r
102 // If we're here then something unexpected happened...
\r
108 * This test validates that it is possible to merge client supplied and edge rule
\r
109 * supplied properties into one edge property list.
\r
111 * @throws Exception
\r
114 public void mergeEdgePropertiesTest() throws Exception {
\r
116 String content = "{" +
\r
117 "\"source\": \"services/inventory/v8/l-interface/369553424\", " +
\r
118 "\"target\": \"services/inventory/v8/logical-link/573444128\"," +
\r
119 "\"properties\": {" +
\r
120 "\"multiplicity\": \"many\"," +
\r
121 "\"is-parent\": true," +
\r
122 "\"uses-resource\": \"true\"," +
\r
123 "\"has-del-target\": \"true\"" +
\r
127 EdgePayload payload = EdgePayload.fromJson(content);
\r
128 EdgeRules rules = EdgeRules.getInstance();
\r
129 EdgeRule rule = rules.getEdgeRule(EdgeType.COUSIN, "l-interface", "logical-link");
\r
130 Map<EdgeProperty, String> edgeProps = rule.getEdgeProperties();
\r
132 // Merge the client supplied properties with the properties defined in the DB edge rules.
\r
133 JsonElement mergedProperties =
\r
134 aaiResSvc.mergeProperties(payload.getProperties(), rule.getEdgeProperties());
\r
136 // Now, validate that the resulting set of properties contains both the client and edge
\r
137 // rule supplied properties.
\r
138 String mergedPropertiesString = mergedProperties.toString();
\r
139 assertTrue("Client supplied property 'multiplicity' is missing from merged properties set",
\r
140 mergedPropertiesString.contains("multiplicity"));
\r
141 assertTrue("Client supplied property 'is-parent' is missing from merged properties set",
\r
142 mergedPropertiesString.contains("is-parent"));
\r
143 assertTrue("Client supplied property 'uses-resource' is missing from merged properties set",
\r
144 mergedPropertiesString.contains("uses-resource"));
\r
145 assertTrue("Client supplied property 'has-del-target' is missing from merged properties set",
\r
146 mergedPropertiesString.contains("has-del-target"));
\r
148 for(EdgeProperty key : edgeProps.keySet()) {
\r
149 assertTrue("Edge rule supplied property '" + key.toString() + "' is missing from merged properties set",
\r
150 mergedPropertiesString.contains(key.toString()));
\r
155 * This test validates that if we try to merge client supplied edge properties
\r
156 * with the properties defined in the db edge rules, and there is a conflict,
\r
157 * then the merge will fail.
\r
159 * @throws Exception
\r
162 public void mergeEdgePropertiesConflictTest() throws Exception {
\r
164 String content = "{" +
\r
165 "\"source\": \"services/inventory/v8/l-interface/369553424\", " +
\r
166 "\"target\": \"services/inventory/v8/logical-link/573444128\"," +
\r
167 "\"properties\": {" +
\r
168 "\"contains-other-v\": \"OUT\"" +
\r
172 EdgePayload payload = EdgePayload.fromJson(content);
\r
173 EdgeRules rules = EdgeRules.getInstance();
\r
174 EdgeRule rule = rules.getEdgeRule(EdgeType.COUSIN, "l-interface", "logical-link");
\r
178 // Try to merge our client supplied properties with the properties defined
\r
179 // in the db edge rules.
\r
180 aaiResSvc.mergeProperties(payload.getProperties(), rule.getEdgeProperties());
\r
182 } catch (CrudException e) {
\r
184 // We should have gotten an exception because we are trying to set a parameter which is
\r
185 // already defined in the db edge rules, so if we're here then we are good.
\r
189 // If we made it here then we were allowed to set a property that is already defined
\r
190 // in the db edge rules, which we should not have...
\r