9fc14460984f5d97f23e923100590e38d2907972
[aai/gizmo.git] / src / test / java / org / onap / schema / AaiResourceServiceTest.java
1 /**\r
2  * ============LICENSE_START=======================================================\r
3  * org.onap.aai\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
11  *\r
12  *       http://www.apache.org/licenses/LICENSE-2.0\r
13  *\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
20  */\r
21 package org.onap.schema;\r
22 \r
23 import static org.junit.Assert.assertTrue;\r
24 import static org.junit.Assert.fail;\r
25 \r
26 import java.util.Map;\r
27 \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.AaiResourceService;\r
37 import org.onap.crud.service.EdgePayload;\r
38 \r
39 import com.google.gson.JsonElement;\r
40 \r
41 public class AaiResourceServiceTest {\r
42 \r
43   public AaiResourceService aaiResSvc = null;\r
44   \r
45   \r
46   @Before\r
47   public void setup() {\r
48     System.setProperty("AJSC_HOME", ".");\r
49     System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");\r
50     \r
51     aaiResSvc = new AaiResourceService();\r
52   }\r
53   \r
54   \r
55   /**\r
56    * This test validates that we can apply db edge rules against an edge request\r
57    * payload and have the properties defined in the edge rules merged into the\r
58    * payload.\r
59    * \r
60    * @throws CrudException\r
61    * @throws AAIException\r
62    */\r
63   @Test\r
64   public void applyEdgeRulesToPayloadTest() throws CrudException, AAIException {\r
65     \r
66     String content = "{" +\r
67         "\"source\": \"services/inventory/v8/l-interface/369553424\", " +\r
68         "\"target\": \"services/inventory/v8/logical-link/573444128\"," +\r
69         "\"properties\": {" +\r
70         "}" +\r
71      "}";\r
72     \r
73     // Convert our simulated payload to an EdgePayload object.\r
74     EdgePayload payload = EdgePayload.fromJson(content);\r
75     \r
76     // Now, apply the db edge rules against our edge payload.\r
77     EdgePayload payloadAfterEdgeRules = aaiResSvc.applyEdgeRulesToPayload(payload);\r
78     \r
79     EdgeRules rules = EdgeRules.getInstance();\r
80     EdgeRule rule = rules.getEdgeRule(EdgeType.COUSIN, "l-interface", "logical-link");\r
81     Map<EdgeProperty, String> edgeProps = rule.getEdgeProperties();\r
82     \r
83     // Validate that the properties defined in the DB edge rules show up in our\r
84     // final payload.\r
85     for(EdgeProperty key : edgeProps.keySet()) {\r
86       assertTrue(payloadAfterEdgeRules.toString().contains(key.toString()));\r
87     }\r
88   }\r
89   \r
90   \r
91   /**\r
92    * This test validates that trying to apply edge rules where there is no\r
93    * db edge rules entry for the supplied source and target vertex types\r
94    * produces an exception.\r
95    * \r
96    * @throws CrudException\r
97    */\r
98   @Test\r
99   public void noRuleForEdgeTest() throws CrudException {\r
100         \r
101     String content = "{" +\r
102         "\"source\": \"services/inventory/v8/commodore-64/12345\", " +\r
103         "\"target\": \"services/inventory/v8/jumpman/67890\"," +\r
104         "\"properties\": {" +\r
105         "}" +\r
106      "}";\r
107     \r
108     // Convert our simulated payload to an EdgePayload object.\r
109     EdgePayload payload = EdgePayload.fromJson(content);\r
110     \r
111     // Now, apply the db edge rules against our edge payload.\r
112     try {\r
113       aaiResSvc.applyEdgeRulesToPayload(payload);\r
114       \r
115     } catch (CrudException e) {\r
116       \r
117       // We expected an exception since there is no rule for our made up vertices..\r
118       assertTrue(e.getMessage().contains("No edge rules for"));\r
119       return;\r
120     }\r
121     \r
122     // If we're here then something unexpected happened...\r
123     fail();\r
124   }\r
125   \r
126   \r
127   /**\r
128    * This test validates that it is possible to merge client supplied and edge rule\r
129    * supplied properties into one edge property list.\r
130    * \r
131    * @throws Exception\r
132    */\r
133   @Test\r
134   public void mergeEdgePropertiesTest() throws Exception {\r
135         \r
136     String content = "{" +\r
137         "\"source\": \"services/inventory/v8/l-interface/369553424\", " +\r
138         "\"target\": \"services/inventory/v8/logical-link/573444128\"," +\r
139         "\"properties\": {" +\r
140           "\"multiplicity\": \"many\"," +\r
141           "\"is-parent\": true," +\r
142           "\"uses-resource\": \"true\"," +\r
143           "\"has-del-target\": \"true\"" +\r
144         "}" +\r
145      "}";\r
146     \r
147     EdgePayload payload = EdgePayload.fromJson(content);\r
148     EdgeRules rules = EdgeRules.getInstance();\r
149     EdgeRule rule = rules.getEdgeRule(EdgeType.COUSIN, "l-interface", "logical-link");\r
150     Map<EdgeProperty, String> edgeProps = rule.getEdgeProperties();\r
151 \r
152     // Merge the client supplied properties with the properties defined in the DB edge rules.\r
153     JsonElement mergedProperties = \r
154         aaiResSvc.mergeProperties(payload.getProperties(), rule.getEdgeProperties());\r
155     \r
156     // Now, validate that the resulting set of properties contains both the client and edge\r
157     // rule supplied properties.\r
158     String mergedPropertiesString = mergedProperties.toString();\r
159     assertTrue("Client supplied property 'multiplicity' is missing from merged properties set",\r
160                mergedPropertiesString.contains("multiplicity"));\r
161     assertTrue("Client supplied property 'is-parent' is missing from merged properties set",\r
162                mergedPropertiesString.contains("is-parent"));\r
163     assertTrue("Client supplied property 'uses-resource' is missing from merged properties set",\r
164                mergedPropertiesString.contains("uses-resource"));\r
165     assertTrue("Client supplied property 'has-del-target' is missing from merged properties set",\r
166                mergedPropertiesString.contains("has-del-target"));\r
167     \r
168     for(EdgeProperty key : edgeProps.keySet()) {\r
169       assertTrue("Edge rule supplied property '" + key.toString() + "' is missing from merged properties set",\r
170                  mergedPropertiesString.contains(key.toString()));\r
171     }\r
172   }\r
173   \r
174   /**\r
175    * This test validates that if we try to merge client supplied edge properties\r
176    * with the properties defined in the db edge rules, and there is a conflict,\r
177    * then the merge will fail.\r
178    * \r
179    * @throws Exception\r
180    */\r
181   @Test\r
182   public void mergeEdgePropertiesConflictTest() throws Exception {\r
183         \r
184     String content = "{" +\r
185         "\"source\": \"services/inventory/v8/l-interface/369553424\", " +\r
186         "\"target\": \"services/inventory/v8/logical-link/573444128\"," +\r
187         "\"properties\": {" +\r
188           "\"contains-other-v\": \"OUT\"" +\r
189         "}" +\r
190      "}";\r
191     \r
192     EdgePayload payload = EdgePayload.fromJson(content);\r
193     EdgeRules rules = EdgeRules.getInstance();\r
194     EdgeRule rule = rules.getEdgeRule(EdgeType.COUSIN, "l-interface", "logical-link");\r
195 \r
196     try {\r
197       \r
198       // Try to merge our client supplied properties with the properties defined\r
199       // in the db edge rules.\r
200       aaiResSvc.mergeProperties(payload.getProperties(), rule.getEdgeProperties());\r
201     \r
202     } catch (CrudException e) {\r
203       \r
204       // We should have gotten an exception because we are trying to set a parameter which is\r
205       // already defined in the db edge rules, so if we're here then we are good.\r
206       return;\r
207     }\r
208 \r
209     // If we made it here then we were allowed to set a property that is already defined\r
210     // in the db edge rules, which we should not have...\r
211     fail();\r
212   }\r
213   \r
214 \r
215 \r
216 \r
217 }\r