1 package org.openecomp.schema;
\r
3 import static org.junit.Assert.assertTrue;
\r
4 import static org.junit.Assert.fail;
\r
6 import java.util.AbstractMap;
\r
7 import java.util.AbstractSet;
\r
8 import java.util.HashSet;
\r
9 import java.util.Map;
\r
10 import java.util.Set;
\r
12 import org.junit.Before;
\r
13 import org.junit.Test;
\r
14 import org.openecomp.aai.exceptions.AAIException;
\r
15 import org.openecomp.aai.serialization.db.EdgeProperty;
\r
16 import org.openecomp.aai.serialization.db.EdgePropertyMap;
\r
17 import org.openecomp.aai.serialization.db.EdgeRule;
\r
18 import org.openecomp.aai.serialization.db.EdgeRules;
\r
19 import org.openecomp.aai.serialization.db.EdgeType;
\r
20 import org.openecomp.aai.util.AAIConstants;
\r
21 import org.openecomp.crud.exception.CrudException;
\r
22 import org.openecomp.crud.service.AaiResourceService;
\r
23 import org.openecomp.crud.service.EdgePayload;
\r
25 import com.google.gson.Gson;
\r
26 import com.google.gson.JsonElement;
\r
27 import com.google.gson.JsonPrimitive;
\r
29 public class AaiResourceServiceTest {
\r
31 public AaiResourceService aaiResSvc = null;
\r
35 public void setup() {
\r
36 System.setProperty("AJSC_HOME", ".");
\r
37 System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
\r
39 aaiResSvc = new AaiResourceService();
\r
44 * This test validates that we can apply db edge rules against an edge request
\r
45 * payload and have the properties defined in the edge rules merged into the
\r
48 * @throws CrudException
\r
49 * @throws AAIException
\r
52 public void applyEdgeRulesToPayloadTest() throws CrudException, AAIException {
\r
54 String content = "{" +
\r
55 "\"source\": \"services/inventory/v8/l-interface/369553424\", " +
\r
56 "\"target\": \"services/inventory/v8/logical-link/573444128\"," +
\r
57 "\"properties\": {" +
\r
61 // Convert our simulated payload to an EdgePayload object.
\r
62 EdgePayload payload = EdgePayload.fromJson(content);
\r
64 // Now, apply the db edge rules against our edge payload.
\r
65 EdgePayload payloadAfterEdgeRules = aaiResSvc.applyEdgeRulesToPayload(payload);
\r
67 EdgeRules rules = EdgeRules.getInstance();
\r
68 EdgeRule rule = rules.getEdgeRule(EdgeType.COUSIN, "l-interface", "logical-link");
\r
69 Map<EdgeProperty, String> edgeProps = rule.getEdgeProperties();
\r
71 // Validate that the properties defined in the DB edge rules show up in our
\r
73 for(EdgeProperty key : edgeProps.keySet()) {
\r
74 assertTrue(payloadAfterEdgeRules.toString().contains(key.toString()));
\r
80 * This test validates that trying to apply edge rules where there is no
\r
81 * db edge rules entry for the supplied source and target vertex types
\r
82 * produces an exception.
\r
84 * @throws CrudException
\r
87 public void noRuleForEdgeTest() throws CrudException {
\r
89 String content = "{" +
\r
90 "\"source\": \"services/inventory/v8/commodore-64/12345\", " +
\r
91 "\"target\": \"services/inventory/v8/jumpman/67890\"," +
\r
92 "\"properties\": {" +
\r
96 // Convert our simulated payload to an EdgePayload object.
\r
97 EdgePayload payload = EdgePayload.fromJson(content);
\r
99 // Now, apply the db edge rules against our edge payload.
\r
101 aaiResSvc.applyEdgeRulesToPayload(payload);
\r
103 } catch (CrudException e) {
\r
105 // We expected an exception since there is no rule for our made up vertices..
\r
106 assertTrue(e.getMessage().contains("No edge rules for"));
\r
110 // If we're here then something unexpected happened...
\r
116 * This test validates that it is possible to merge client supplied and edge rule
\r
117 * supplied properties into one edge property list.
\r
119 * @throws Exception
\r
122 public void mergeEdgePropertiesTest() throws Exception {
\r
124 String content = "{" +
\r
125 "\"source\": \"services/inventory/v8/l-interface/369553424\", " +
\r
126 "\"target\": \"services/inventory/v8/logical-link/573444128\"," +
\r
127 "\"properties\": {" +
\r
128 "\"multiplicity\": \"many\"," +
\r
129 "\"is-parent\": true," +
\r
130 "\"uses-resource\": \"true\"," +
\r
131 "\"has-del-target\": \"true\"" +
\r
135 EdgePayload payload = EdgePayload.fromJson(content);
\r
136 EdgeRules rules = EdgeRules.getInstance();
\r
137 EdgeRule rule = rules.getEdgeRule(EdgeType.COUSIN, "l-interface", "logical-link");
\r
138 Map<EdgeProperty, String> edgeProps = rule.getEdgeProperties();
\r
140 // Merge the client supplied properties with the properties defined in the DB edge rules.
\r
141 JsonElement mergedProperties =
\r
142 aaiResSvc.mergeProperties(payload.getProperties(), rule.getEdgeProperties());
\r
144 // Now, validate that the resulting set of properties contains both the client and edge
\r
145 // rule supplied properties.
\r
146 String mergedPropertiesString = mergedProperties.toString();
\r
147 assertTrue("Client supplied property 'multiplicity' is missing from merged properties set",
\r
148 mergedPropertiesString.contains("multiplicity"));
\r
149 assertTrue("Client supplied property 'is-parent' is missing from merged properties set",
\r
150 mergedPropertiesString.contains("is-parent"));
\r
151 assertTrue("Client supplied property 'uses-resource' is missing from merged properties set",
\r
152 mergedPropertiesString.contains("uses-resource"));
\r
153 assertTrue("Client supplied property 'has-del-target' is missing from merged properties set",
\r
154 mergedPropertiesString.contains("has-del-target"));
\r
156 for(EdgeProperty key : edgeProps.keySet()) {
\r
157 assertTrue("Edge rule supplied property '" + key.toString() + "' is missing from merged properties set",
\r
158 mergedPropertiesString.contains(key.toString()));
\r
163 * This test validates that if we try to merge client supplied edge properties
\r
164 * with the properties defined in the db edge rules, and there is a conflict,
\r
165 * then the merge will fail.
\r
167 * @throws Exception
\r
170 public void mergeEdgePropertiesConflictTest() throws Exception {
\r
172 String content = "{" +
\r
173 "\"source\": \"services/inventory/v8/l-interface/369553424\", " +
\r
174 "\"target\": \"services/inventory/v8/logical-link/573444128\"," +
\r
175 "\"properties\": {" +
\r
176 "\"contains-other-v\": \"OUT\"" +
\r
180 EdgePayload payload = EdgePayload.fromJson(content);
\r
181 EdgeRules rules = EdgeRules.getInstance();
\r
182 EdgeRule rule = rules.getEdgeRule(EdgeType.COUSIN, "l-interface", "logical-link");
\r
186 // Try to merge our client supplied properties with the properties defined
\r
187 // in the db edge rules.
\r
188 aaiResSvc.mergeProperties(payload.getProperties(), rule.getEdgeProperties());
\r
190 } catch (CrudException e) {
\r
192 // We should have gotten an exception because we are trying to set a parameter which is
\r
193 // already defined in the db edge rules, so if we're here then we are good.
\r
197 // If we made it here then we were allowed to set a property that is already defined
\r
198 // in the db edge rules, which we should not have...
\r