Test Replace Jackson with GSON
[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
20 import static org.hamcrest.core.IsEqual.equalTo;
21 import static org.junit.Assert.assertThat;
22
23 import java.io.IOException;
24 import java.util.List;
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.ExpectedException;
29 import org.onap.holmes.common.aai.entity.VmEntity;
30 import org.onap.holmes.common.aai.entity.VmResourceLink;
31 import org.onap.holmes.common.aai.entity.VnfEntity;
32 import org.powermock.core.classloader.annotations.PrepareForTest;
33
34 @PrepareForTest(AaiResponseUtil.class)
35 public class AaiResponseUtilTest {
36
37     @Rule
38     public ExpectedException thrown = ExpectedException.none();
39
40     private AaiResponseUtil aaiResponseUtil;
41
42     @Before
43     public void setUp() {
44         aaiResponseUtil = new AaiResponseUtil();
45     }
46
47     @Test
48     public void testAaiResponseUtil_convert_resouce_link_success() throws IOException {
49         String json = "{"
50                 + "\"result-data\": ["
51                 + "{"
52                 + "\"resource-link\": \"/aai/example-vserver-id-val-2\","
53                 + "\"resource-type\": \"vserver\""
54                 + "},"
55                 + "{"
56                 + "\"resource-link\": \"/111aai/example-vserver-id-val-2\","
57                 + "\"resource-type\": \"111vserver\""
58                 + "}"
59                 + "]"
60                 + "}";
61
62         List<VmResourceLink> actual = aaiResponseUtil.convertJsonToVmResourceLink(json);
63
64         assertThat(actual.get(0).getResourceLink(), equalTo("/aai/example-vserver-id-val-2"));
65         assertThat(actual.get(0).getResourceType(), equalTo("vserver"));
66         assertThat(actual.get(1).getResourceLink(), equalTo("/111aai/example-vserver-id-val-2"));
67         assertThat(actual.get(1).getResourceType(), equalTo("111vserver"));
68     }
69
70     @Test
71     public void testAaiResponseUtil_convert_resource_link_input_empty_array() throws IOException {
72         String json = "{"
73                 + "\"result-data\": ["
74                 + "]"
75                 + "}";
76         List<VmResourceLink> actual = aaiResponseUtil.convertJsonToVmResourceLink(json);
77         assertThat(actual.isEmpty(), equalTo(true));
78     }
79
80     @Test
81     public void testAaiResponseUtil_convert_resource_link_input_empty() throws IOException {
82         String json = "{}";
83         List<VmResourceLink> actual = aaiResponseUtil.convertJsonToVmResourceLink(json);
84         assertThat(actual.isEmpty(), equalTo(true));
85     }
86
87     @Test
88     public void testAaiResponseUtil_convert_resource_link_input_error() throws IOException {
89         String json = "{"
90                 + "\"result-data\": ["
91                 + "{"
92                 + "\"resource-link1\": \"/aai/example-vserver-id-val-2\","
93                 + "\"resource-type\": \"vserver\""
94                 + "},"
95                 + "{"
96                 + "\"resource-link\": \"/111aai/example-vserver-id-val-2\","
97                 + "\"resource-type\": \"111vserver\""
98                 + "}"
99                 + "]"
100                 + "}";
101         List<VmResourceLink> actual = aaiResponseUtil.convertJsonToVmResourceLink(json);
102         assertThat(actual.size(), equalTo(1));
103         assertThat(actual.get(0).getResourceType(), equalTo("111vserver"));
104     }
105
106     @Test
107     public void testAaiResponseUtil_convert_resource_link_input_error1() throws IOException {
108         String json = "{"
109                 + "\"result-data1\": ["
110                 + "]"
111                 + "}";
112         List<VmResourceLink> actual = aaiResponseUtil.convertJsonToVmResourceLink(json);
113         assertThat(actual.isEmpty(), equalTo(true));
114     }
115
116
117     @Test
118     public void testAaiResponseUtil_convert_VmEntity_success() throws IOException {
119         String json = "{"
120                 + "\"in-maint\": true,"
121                 + "\"is-closed-loop-disabled\": true,"
122                 + "\"prov-status\": \"example-prov-status-val-2\","
123                 + "\"resource-version\": \"1504912891060\","
124                 + "\"vserver-id\": \"example-vserver-id-val-2\","
125                 + "\"vserver-name\": \"example-vserver-name-val-2\","
126                 + "\"vserver-name2\": \"example-vserver-name2-val-2\","
127                 + "\"vserver-selflink\": \"example-vserver-selflink-val-2\""
128                 + "}";
129         VmEntity actual = aaiResponseUtil.convertJsonToVmEntity(json);
130         assertThat(actual.getInMaint(), equalTo(true));
131         assertThat(actual.getClosedLoopDisable(), equalTo(true));
132         assertThat(actual.getProvStatus(), equalTo("example-prov-status-val-2"));
133         assertThat(actual.getResourceVersion(), equalTo("1504912891060"));
134         assertThat(actual.getVserverId(), equalTo("example-vserver-id-val-2"));
135         assertThat(actual.getVserverName(), equalTo("example-vserver-name-val-2"));
136         assertThat(actual.getVserverName2(), equalTo("example-vserver-name2-val-2"));
137         assertThat(actual.getVserverSelflink(), equalTo("example-vserver-selflink-val-2"));
138     }
139
140
141     @Test
142     public void testAaiResponseUtil_convert_VmEntity_input_empty() throws IOException {
143         String json = "{}";
144         VmEntity actual = aaiResponseUtil.convertJsonToVmEntity(json);
145         assertThat(actual == null, equalTo(true));
146     }
147
148     @Test
149     public void testAaiResponseUtil_convert_VmEntity_input_error() throws IOException {
150         String json = "{"
151                 + "\"in-maint1\": true,"
152                 + "\"is-closed-loop-disabled\": true,"
153                 + "\"prov-status\": \"example-prov-status-val-2\","
154                 + "\"resource-version\": \"1504912891060\","
155                 + "\"vserver-id\": \"example-vserver-id-val-2\","
156                 + "\"vserver-name\": \"example-vserver-name-val-2\","
157                 + "\"vserver-name2\": \"example-vserver-name2-val-2\","
158                 + "\"vserver-selflink\": \"example-vserver-selflink-val-2\""
159                 + "}";
160         VmEntity actual = aaiResponseUtil.convertJsonToVmEntity(json);
161         assertThat(actual.getInMaint() == null, equalTo(true));
162         assertThat(actual.getClosedLoopDisable(), equalTo(true));
163         assertThat(actual.getProvStatus(), equalTo("example-prov-status-val-2"));
164         assertThat(actual.getResourceVersion(), equalTo("1504912891060"));
165         assertThat(actual.getVserverId(), equalTo("example-vserver-id-val-2"));
166         assertThat(actual.getVserverName(), equalTo("example-vserver-name-val-2"));
167         assertThat(actual.getVserverName2(), equalTo("example-vserver-name2-val-2"));
168         assertThat(actual.getVserverSelflink(), equalTo("example-vserver-selflink-val-2"));
169     }
170
171     @Test
172     public void testAaiResponseUtil_convert_success() throws IOException {
173         String json = "{"
174                 + "\"in-maint\":false,"
175                 + "\"relationship-list\":{"
176                 + "\"relationship\":["
177                 + "{"
178                 + "\"related-link\":\"/aai/v11/e8fe\","
179                 + "\"related-to\":\"service-instance\","
180                 + "\"related-to-property\":["
181                 + "{"
182                 + "\"property-key\":\"service-i\","
183                 + "\"property-value\":\"vCPEInfraSI13\""
184                 + "}"
185                 + "],"
186                 + "\"relationship-data\":["
187                 + "{"
188                 + "\"relationship-key\":\"custome\","
189                 + "\"relationship-value\":\"Demonstration3\""
190                 + "}"
191                 + "]"
192                 + "}"
193                 + "]"
194                 + "}"
195                 + "}";
196         VnfEntity actual = aaiResponseUtil.convertJsonToVnfEntity(json);
197
198         assertThat(actual.getInMaint(), equalTo(false));
199         assertThat(actual.getRelationshipList().getRelationships().get(0).getRelatedLink(),
200                 equalTo("/aai/v11/e8fe"));
201         assertThat(actual.getRelationshipList().getRelationships().get(0).getRelatedTo(),
202                 equalTo("service-instance"));
203         assertThat(actual.getRelationshipList().getRelationships().get(0).getRelatedToPropertyList()
204                 .get(0).getPropertyKey(), equalTo("service-i"));
205         assertThat(actual.getRelationshipList().getRelationships().get(0).getRelatedToPropertyList()
206                 .get(0).getPropertyValue(), equalTo("vCPEInfraSI13"));
207         assertThat(actual.getRelationshipList().getRelationships().get(0).getRelationshipDataList().get(0)
208                         .getRelationshipKey(), equalTo("custome"));
209         assertThat(actual.getRelationshipList().getRelationships().get(0).getRelationshipDataList().get(0)
210                         .getRelationshipValue(), equalTo("Demonstration3"));
211     }
212
213     @Test
214     public void testAaiResponseUtil_convert_VnfEntity_input_empty() throws IOException {
215         String json = "{}";
216         VnfEntity actual = aaiResponseUtil.convertJsonToVnfEntity(json);
217         assertThat(actual == null, equalTo(true));
218     }
219
220     @Test
221     public void testAaiResponseUtil_convert_input_not_include_relationship_list() throws IOException {
222         String json = "{"
223                 + "\"in-maint\":false,"
224                 + "\"vnf-type\":\"vCPEInfraService10/vCPEInfraService10 0\""
225                 + "}";
226         VnfEntity actual = aaiResponseUtil.convertJsonToVnfEntity(json);
227         assertThat(actual.getRelationshipList().getRelationships().isEmpty(), equalTo(true));
228     }
229
230     @Test
231     public void testAaiResponseUtil_convert_input_not_include_relationship() throws IOException {
232         String json = "{"
233                 + "\"in-maint\":false,"
234                 + "\"is-closed-loop-disabled\":false,"
235                 + "\"relationship-list\":{"
236                 + "},"
237                 + "\"service-id\":\"e8cb8968-5411-478b-906a-f28747de72cd\""
238                 + "}";
239         VnfEntity actual = aaiResponseUtil.convertJsonToVnfEntity(json);
240         assertThat(actual.getRelationshipList().getRelationships().isEmpty(), equalTo(true));
241     }
242
243     @Test
244     public void testAaiResponseUtil_convert_input_relationship_empty() throws IOException {
245         String json = "{"
246                 + "\"in-maint\":false,"
247                 + "\"relationship-list\":{"
248                 + "\"relationship\":["
249                 + "]"
250                 + "},"
251                 + "\"vnf-type\":\"vCPEInfraService10/vCPEInfraService10 0\""
252                 + "}";
253         VnfEntity actual = aaiResponseUtil.convertJsonToVnfEntity(json);
254         assertThat(actual.getRelationshipList().getRelationships().isEmpty(), equalTo(true));
255     }
256
257     @Test
258     public void testAaiResponseUtil_convert_input_not_include_related_to_property() throws IOException {
259         String json = "{"
260                 + "\"in-maint\":false,"
261                 + "\"relationship-list\":{"
262                 + "\"relationship\":["
263                 + "{"
264                 + "\"related-link\":\"/aai/6\","
265                 + "\"related-to\":\"service-instance\","
266                 + "\"relationship-data\":["
267                 + "{"
268                 + "\"relationship-key\":\"service-instance.service-instance-id\","
269                 + "\"relationship-value\":\"e8feceb6-28ae-480a-bfbc-1985ce333526\""
270                 + "}"
271                 + "]"
272                 + "}"
273                 + "]"
274                 + "},"
275                 + "\"vnf-type\":\"vCPEInfraSe0\""
276                 + "}";
277         VnfEntity actual = aaiResponseUtil.convertJsonToVnfEntity(json);
278         assertThat(actual.getRelationshipList().getRelationships().get(0).getRelatedToPropertyList()
279                 .isEmpty(), equalTo(true));
280     }
281
282     @Test
283     public void testAaiResponseUtil_convert_input_related_to_property_empty() throws IOException {
284         String json = "{"
285                 + "\"in-maint\":false,"
286                 + "\"relationship-list\":{"
287                 + "\"relationship\":["
288                 + "{"
289                 + "\"related-link\":\"/aai/3526\","
290                 + "\"related-to\":\"service-instance\","
291                 + "\"related-to-property\":["
292                 + "],"
293                 + "\"relationship-data\":["
294                 + "{"
295                 + "\"relationship-key\":\"servicnce-id\","
296                 + "\"relationship-value\":\"e8feceb6-28a6\""
297                 + "}"
298                 + "]"
299                 + "}"
300                 + "]"
301                 + "},"
302                 + "\"vnf-type\":\"vCPEce10\""
303                 + "}";
304         VnfEntity actual = aaiResponseUtil.convertJsonToVnfEntity(json);
305         assertThat(actual.getRelationshipList().getRelationships().get(0).getRelatedToPropertyList()
306                 .isEmpty(), equalTo(true));
307     }
308 }