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