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