766fcff6208492bd4118b9eb0d0833a689d4ba98
[aai/gizmo.git] / src / test / java / org / onap / schema / AaiResourceServiceTest.java
1 package org.onap.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.Map;\r
7 \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
18 \r
19 import com.google.gson.JsonElement;\r
20 \r
21 public class AaiResourceServiceTest {\r
22 \r
23   public AaiResourceService aaiResSvc = null;\r
24   \r
25   \r
26   @Before\r
27   public void setup() {\r
28     System.setProperty("AJSC_HOME", ".");\r
29     System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");\r
30     \r
31     aaiResSvc = new AaiResourceService();\r
32   }\r
33   \r
34   \r
35   /**\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
38    * payload.\r
39    * \r
40    * @throws CrudException\r
41    * @throws AAIException\r
42    */\r
43   @Test\r
44   public void applyEdgeRulesToPayloadTest() throws CrudException, AAIException {\r
45     \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
50         "}" +\r
51      "}";\r
52     \r
53     // Convert our simulated payload to an EdgePayload object.\r
54     EdgePayload payload = EdgePayload.fromJson(content);\r
55     \r
56     // Now, apply the db edge rules against our edge payload.\r
57     EdgePayload payloadAfterEdgeRules = aaiResSvc.applyEdgeRulesToPayload(payload);\r
58     \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
62     \r
63     // Validate that the properties defined in the DB edge rules show up in our\r
64     // final payload.\r
65     for(EdgeProperty key : edgeProps.keySet()) {\r
66       assertTrue(payloadAfterEdgeRules.toString().contains(key.toString()));\r
67     }\r
68   }\r
69   \r
70   \r
71   /**\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
75    * \r
76    * @throws CrudException\r
77    */\r
78   @Test\r
79   public void noRuleForEdgeTest() throws CrudException {\r
80         \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
85         "}" +\r
86      "}";\r
87     \r
88     // Convert our simulated payload to an EdgePayload object.\r
89     EdgePayload payload = EdgePayload.fromJson(content);\r
90     \r
91     // Now, apply the db edge rules against our edge payload.\r
92     try {\r
93       aaiResSvc.applyEdgeRulesToPayload(payload);\r
94       \r
95     } catch (CrudException e) {\r
96       \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
99       return;\r
100     }\r
101     \r
102     // If we're here then something unexpected happened...\r
103     fail();\r
104   }\r
105   \r
106   \r
107   /**\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
110    * \r
111    * @throws Exception\r
112    */\r
113   @Test\r
114   public void mergeEdgePropertiesTest() throws Exception {\r
115         \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
124         "}" +\r
125      "}";\r
126     \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
131 \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
135     \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
147     \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
151     }\r
152   }\r
153   \r
154   /**\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
158    * \r
159    * @throws Exception\r
160    */\r
161   @Test\r
162   public void mergeEdgePropertiesConflictTest() throws Exception {\r
163         \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
169         "}" +\r
170      "}";\r
171     \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
175 \r
176     try {\r
177       \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
181     \r
182     } catch (CrudException e) {\r
183       \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
186       return;\r
187     }\r
188 \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
191     fail();\r
192   }\r
193   \r
194 \r
195 \r
196 \r
197 }\r