New endpoints to auto populate edge properties.
[aai/gizmo.git] / src / test / java / org / openecomp / schema / AaiResourceServiceTest.java
1 package org.openecomp.schema;\r
2 \r
3 import static org.junit.Assert.assertTrue;\r
4 import static org.junit.Assert.fail;\r
5 \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
11 \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
24 \r
25 import com.google.gson.Gson;\r
26 import com.google.gson.JsonElement;\r
27 import com.google.gson.JsonPrimitive;\r
28 \r
29 public class AaiResourceServiceTest {\r
30 \r
31   public AaiResourceService aaiResSvc = null;\r
32   \r
33   \r
34   @Before\r
35   public void setup() {\r
36     System.setProperty("AJSC_HOME", ".");\r
37     System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");\r
38     \r
39     aaiResSvc = new AaiResourceService();\r
40   }\r
41   \r
42   \r
43   /**\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
46    * payload.\r
47    * \r
48    * @throws CrudException\r
49    * @throws AAIException\r
50    */\r
51   @Test\r
52   public void applyEdgeRulesToPayloadTest() throws CrudException, AAIException {\r
53     \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
58         "}" +\r
59      "}";\r
60     \r
61     // Convert our simulated payload to an EdgePayload object.\r
62     EdgePayload payload = EdgePayload.fromJson(content);\r
63     \r
64     // Now, apply the db edge rules against our edge payload.\r
65     EdgePayload payloadAfterEdgeRules = aaiResSvc.applyEdgeRulesToPayload(payload);\r
66     \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
70     \r
71     // Validate that the properties defined in the DB edge rules show up in our\r
72     // final payload.\r
73     for(EdgeProperty key : edgeProps.keySet()) {\r
74       assertTrue(payloadAfterEdgeRules.toString().contains(key.toString()));\r
75     }\r
76   }\r
77   \r
78   \r
79   /**\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
83    * \r
84    * @throws CrudException\r
85    */\r
86   @Test\r
87   public void noRuleForEdgeTest() throws CrudException {\r
88         \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
93         "}" +\r
94      "}";\r
95     \r
96     // Convert our simulated payload to an EdgePayload object.\r
97     EdgePayload payload = EdgePayload.fromJson(content);\r
98     \r
99     // Now, apply the db edge rules against our edge payload.\r
100     try {\r
101       aaiResSvc.applyEdgeRulesToPayload(payload);\r
102       \r
103     } catch (CrudException e) {\r
104       \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
107       return;\r
108     }\r
109     \r
110     // If we're here then something unexpected happened...\r
111     fail();\r
112   }\r
113   \r
114   \r
115   /**\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
118    * \r
119    * @throws Exception\r
120    */\r
121   @Test\r
122   public void mergeEdgePropertiesTest() throws Exception {\r
123         \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
132         "}" +\r
133      "}";\r
134     \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
139 \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
143     \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
155     \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
159     }\r
160   }\r
161   \r
162   /**\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
166    * \r
167    * @throws Exception\r
168    */\r
169   @Test\r
170   public void mergeEdgePropertiesConflictTest() throws Exception {\r
171         \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
177         "}" +\r
178      "}";\r
179     \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
183 \r
184     try {\r
185       \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
189     \r
190     } catch (CrudException e) {\r
191       \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
194       return;\r
195     }\r
196 \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
199     fail();\r
200   }\r
201   \r
202 \r
203 \r
204 \r
205 }\r