fee98b928bf7e88a2aa6d27a8046e3cc30dac7a4
[holmes/common.git] / holmes-actions / src / test / java / org / onap / holmes / common / aai / AaiResponseUtilTest.java
1 /**
2  * Copyright 2017 ZTE Corporation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.holmes.common.aai;
18
19 import static org.hamcrest.CoreMatchers.equalTo;
20 import static org.hamcrest.MatcherAssert.assertThat;
21
22 import java.io.IOException;
23 import java.util.List;
24 import org.junit.Before;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28 import org.onap.holmes.common.aai.entity.VmEntity;
29 import org.onap.holmes.common.aai.entity.VmResourceLink;
30 import org.onap.holmes.common.aai.entity.VnfEntity;
31 import org.powermock.core.classloader.annotations.PrepareForTest;
32
33 @PrepareForTest(AaiResponseUtil.class)
34 public class AaiResponseUtilTest {
35
36     @Rule
37     public ExpectedException thrown = ExpectedException.none();
38
39     private AaiResponseUtil aaiResponseUtil;
40
41     @Before
42     public void setUp() {
43         aaiResponseUtil = new AaiResponseUtil();
44     }
45
46     @Test
47     public void testAaiResponseUtil_convert_resouce_link_success() throws IOException {
48         String json = "{"
49                 + "\"result-data\": ["
50                 + "{"
51                 + "\"resource-link\": \"/aai/example-vserver-id-val-2\","
52                 + "\"resource-type\": \"vserver\""
53                 + "},"
54                 + "{"
55                 + "\"resource-link\": \"/111aai/example-vserver-id-val-2\","
56                 + "\"resource-type\": \"111vserver\""
57                 + "}"
58                 + "]"
59                 + "}";
60
61         List<VmResourceLink> actual = aaiResponseUtil.convertJsonToVmResourceLink(json);
62
63         assertThat(actual.get(0).getResourceLink(), equalTo("/aai/example-vserver-id-val-2"));
64         assertThat(actual.get(0).getResourceType(), equalTo("vserver"));
65         assertThat(actual.get(1).getResourceLink(), equalTo("/111aai/example-vserver-id-val-2"));
66         assertThat(actual.get(1).getResourceType(), equalTo("111vserver"));
67     }
68
69     @Test
70     public void testAaiResponseUtil_convert_resource_link_input_empty_array() throws IOException {
71         String json = "{"
72                 + "\"result-data\": ["
73                 + "]"
74                 + "}";
75         List<VmResourceLink> actual = aaiResponseUtil.convertJsonToVmResourceLink(json);
76         assertThat(actual.isEmpty(), equalTo(true));
77     }
78
79     @Test
80     public void testAaiResponseUtil_convert_resource_link_input_empty() throws IOException {
81         String json = "{}";
82         List<VmResourceLink> actual = aaiResponseUtil.convertJsonToVmResourceLink(json);
83         assertThat(actual.isEmpty(), equalTo(true));
84     }
85
86     @Test
87     public void testAaiResponseUtil_convert_resource_link_input_error() throws IOException {
88         String json = "{"
89                 + "\"result-data\": ["
90                 + "{"
91                 + "\"resource-link1\": \"/aai/example-vserver-id-val-2\","
92                 + "\"resource-type\": \"vserver\""
93                 + "},"
94                 + "{"
95                 + "\"resource-link\": \"/111aai/example-vserver-id-val-2\","
96                 + "\"resource-type\": \"111vserver\""
97                 + "}"
98                 + "]"
99                 + "}";
100         List<VmResourceLink> actual = aaiResponseUtil.convertJsonToVmResourceLink(json);
101         assertThat(actual.size(), equalTo(1));
102         assertThat(actual.get(0).getResourceType(), equalTo("111vserver"));
103     }
104
105     @Test
106     public void testAaiResponseUtil_convert_resource_link_input_error1() throws IOException {
107         String json = "{"
108                 + "\"result-data1\": ["
109                 + "]"
110                 + "}";
111         List<VmResourceLink> actual = aaiResponseUtil.convertJsonToVmResourceLink(json);
112         assertThat(actual.isEmpty(), equalTo(true));
113     }
114
115     @Test
116     public void testAaiResponseUtil_convert_resource_link_throw_IOException() throws IOException {
117         thrown.expect(IOException.class);
118         String json = "{**}";
119         aaiResponseUtil.convertJsonToVmResourceLink(json);
120     }
121
122     @Test
123     public void testAaiResponseUtil_convert_VmEntity_success() throws IOException {
124         String json = "{"
125                 + "\"in-maint\": true,"
126                 + "\"is-closed-loop-disabled\": true,"
127                 + "\"prov-status\": \"example-prov-status-val-2\","
128                 + "\"resource-version\": \"1504912891060\","
129                 + "\"vserver-id\": \"example-vserver-id-val-2\","
130                 + "\"vserver-name\": \"example-vserver-name-val-2\","
131                 + "\"vserver-name2\": \"example-vserver-name2-val-2\","
132                 + "\"vserver-selflink\": \"example-vserver-selflink-val-2\""
133                 + "}";
134         VmEntity actual = aaiResponseUtil.convertJsonToVmEntity(json);
135         assertThat(actual.getInMaint(), equalTo(true));
136         assertThat(actual.getClosedLoopDisable(), equalTo(true));
137         assertThat(actual.getProvStatus(), equalTo("example-prov-status-val-2"));
138         assertThat(actual.getResourceVersion(), equalTo("1504912891060"));
139         assertThat(actual.getVserverId(), equalTo("example-vserver-id-val-2"));
140         assertThat(actual.getVserverName(), equalTo("example-vserver-name-val-2"));
141         assertThat(actual.getVserverName2(), equalTo("example-vserver-name2-val-2"));
142         assertThat(actual.getVserverSelflink(), equalTo("example-vserver-selflink-val-2"));
143     }
144
145     @Test
146     public void testAaiResponseUtil_convert_VmEntity_throw_IOException() throws IOException {
147         thrown.expect(IOException.class);
148         String json = "{**}";
149         aaiResponseUtil.convertJsonToVmEntity(json);
150     }
151
152     @Test
153     public void testAaiResponseUtil_convert_VmEntity_input_empty() throws IOException {
154         String json = "{}";
155         VmEntity actual = aaiResponseUtil.convertJsonToVmEntity(json);
156         assertThat(actual == null, equalTo(true));
157     }
158
159     @Test
160     public void testAaiResponseUtil_convert_VmEntity_input_error() throws IOException {
161         String json = "{"
162                 + "\"in-maint1\": true,"
163                 + "\"is-closed-loop-disabled\": true,"
164                 + "\"prov-status\": \"example-prov-status-val-2\","
165                 + "\"resource-version\": \"1504912891060\","
166                 + "\"vserver-id\": \"example-vserver-id-val-2\","
167                 + "\"vserver-name\": \"example-vserver-name-val-2\","
168                 + "\"vserver-name2\": \"example-vserver-name2-val-2\","
169                 + "\"vserver-selflink\": \"example-vserver-selflink-val-2\""
170                 + "}";
171         VmEntity actual = aaiResponseUtil.convertJsonToVmEntity(json);
172         assertThat(actual.getInMaint() == null, equalTo(true));
173         assertThat(actual.getClosedLoopDisable(), equalTo(true));
174         assertThat(actual.getProvStatus(), equalTo("example-prov-status-val-2"));
175         assertThat(actual.getResourceVersion(), equalTo("1504912891060"));
176         assertThat(actual.getVserverId(), equalTo("example-vserver-id-val-2"));
177         assertThat(actual.getVserverName(), equalTo("example-vserver-name-val-2"));
178         assertThat(actual.getVserverName2(), equalTo("example-vserver-name2-val-2"));
179         assertThat(actual.getVserverSelflink(), equalTo("example-vserver-selflink-val-2"));
180     }
181
182     @Test
183     public void testAaiResponseUtil_convert_success() throws IOException {
184         String json = "{"
185                 + "\"in-maint\":false,"
186                 + "\"relationship-list\":{"
187                 + "\"relationship\":["
188                 + "{"
189                 + "\"related-link\":\"/aai/v11/e8fe\","
190                 + "\"related-to\":\"service-instance\","
191                 + "\"related-to-property\":["
192                 + "{"
193                 + "\"property-key\":\"service-i\","
194                 + "\"property-value\":\"vCPEInfraSI13\""
195                 + "}"
196                 + "],"
197                 + "\"relationship-data\":["
198                 + "{"
199                 + "\"relationship-key\":\"custome\","
200                 + "\"relationship-value\":\"Demonstration3\""
201                 + "}"
202                 + "]"
203                 + "}"
204                 + "]"
205                 + "}"
206                 + "}";
207         VnfEntity actual = aaiResponseUtil.convertJsonToVnfEntity(json);
208
209         assertThat(actual.getInMaint(), equalTo(false));
210         assertThat(actual.getRelationshipList().getRelationships().get(0).getRelatedLink(),
211                 equalTo("/aai/v11/e8fe"));
212         assertThat(actual.getRelationshipList().getRelationships().get(0).getRelatedTo(),
213                 equalTo("service-instance"));
214         assertThat(actual.getRelationshipList().getRelationships().get(0).getRelatedToPropertyList()
215                 .get(0).getPropertyKey(), equalTo("service-i"));
216         assertThat(actual.getRelationshipList().getRelationships().get(0).getRelatedToPropertyList()
217                 .get(0).getPropertyValue(), equalTo("vCPEInfraSI13"));
218         assertThat(actual.getRelationshipList().getRelationships().get(0).getRelationshipDataList().get(0)
219                         .getRelationshipKey(), equalTo("custome"));
220         assertThat(actual.getRelationshipList().getRelationships().get(0).getRelationshipDataList().get(0)
221                         .getRelationshipValue(), equalTo("Demonstration3"));
222     }
223
224     @Test
225     public void testAaiResponseUtil_throw_IOException() throws IOException {
226         thrown.expect(IOException.class);
227         String json = "{**}";
228         aaiResponseUtil.convertJsonToVnfEntity(json);
229     }
230
231     @Test
232     public void testAaiResponseUtil_convert_VnfEntity_input_empty() throws IOException {
233         String json = "{}";
234         VnfEntity actual = aaiResponseUtil.convertJsonToVnfEntity(json);
235         assertThat(actual == null, equalTo(true));
236     }
237
238     @Test
239     public void testAaiResponseUtil_convert_input_not_include_relationship_list() throws IOException {
240         String json = "{"
241                 + "\"in-maint\":false,"
242                 + "\"vnf-type\":\"vCPEInfraService10/vCPEInfraService10 0\""
243                 + "}";
244         VnfEntity actual = aaiResponseUtil.convertJsonToVnfEntity(json);
245         assertThat(actual.getRelationshipList().getRelationships().isEmpty(), equalTo(true));
246     }
247
248     @Test
249     public void testAaiResponseUtil_convert_input_not_include_relationship() throws IOException {
250         String json = "{"
251                 + "\"in-maint\":false,"
252                 + "\"is-closed-loop-disabled\":false,"
253                 + "\"relationship-list\":{"
254                 + "},"
255                 + "\"service-id\":\"e8cb8968-5411-478b-906a-f28747de72cd\""
256                 + "}";
257         VnfEntity actual = aaiResponseUtil.convertJsonToVnfEntity(json);
258         assertThat(actual.getRelationshipList().getRelationships().isEmpty(), equalTo(true));
259     }
260
261     @Test
262     public void testAaiResponseUtil_convert_input_relationship_empty() throws IOException {
263         String json = "{"
264                 + "\"in-maint\":false,"
265                 + "\"relationship-list\":{"
266                 + "\"relationship\":["
267                 + "]"
268                 + "},"
269                 + "\"vnf-type\":\"vCPEInfraService10/vCPEInfraService10 0\""
270                 + "}";
271         VnfEntity actual = aaiResponseUtil.convertJsonToVnfEntity(json);
272         assertThat(actual.getRelationshipList().getRelationships().isEmpty(), equalTo(true));
273     }
274
275     @Test
276     public void testAaiResponseUtil_convert_input_not_include_related_to_property() throws IOException {
277         String json = "{"
278                 + "\"in-maint\":false,"
279                 + "\"relationship-list\":{"
280                 + "\"relationship\":["
281                 + "{"
282                 + "\"related-link\":\"/aai/6\","
283                 + "\"related-to\":\"service-instance\","
284                 + "\"relationship-data\":["
285                 + "{"
286                 + "\"relationship-key\":\"service-instance.service-instance-id\","
287                 + "\"relationship-value\":\"e8feceb6-28ae-480a-bfbc-1985ce333526\""
288                 + "}"
289                 + "]"
290                 + "}"
291                 + "]"
292                 + "},"
293                 + "\"vnf-type\":\"vCPEInfraSe0\""
294                 + "}";
295         VnfEntity actual = aaiResponseUtil.convertJsonToVnfEntity(json);
296         assertThat(actual.getRelationshipList().getRelationships().get(0).getRelatedToPropertyList()
297                 .isEmpty(), equalTo(true));
298     }
299
300     @Test
301     public void testAaiResponseUtil_convert_input_related_to_property_empty() throws IOException {
302         String json = "{"
303                 + "\"in-maint\":false,"
304                 + "\"relationship-list\":{"
305                 + "\"relationship\":["
306                 + "{"
307                 + "\"related-link\":\"/aai/3526\","
308                 + "\"related-to\":\"service-instance\","
309                 + "\"related-to-property\":["
310                 + "],"
311                 + "\"relationship-data\":["
312                 + "{"
313                 + "\"relationship-key\":\"servicnce-id\","
314                 + "\"relationship-value\":\"e8feceb6-28a6\""
315                 + "}"
316                 + "]"
317                 + "}"
318                 + "]"
319                 + "},"
320                 + "\"vnf-type\":\"vCPEce10\""
321                 + "}";
322         VnfEntity actual = aaiResponseUtil.convertJsonToVnfEntity(json);
323         assertThat(actual.getRelationshipList().getRelationships().get(0).getRelatedToPropertyList()
324                 .isEmpty(), equalTo(true));
325     }
326 }