8e591ea681bb3c9e634629f49912a39cf1065609
[usecase-ui/server.git] /
1 /*
2  * Copyright (C) 2021 CTC, Inc. and others. All rights reserved.
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 package org.onap.usecaseui.server.service.intent.impl;
17
18 import java.io.IOException;
19 import java.io.Serializable;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Map;
23
24 import com.alibaba.fastjson.JSONObject;
25 import org.hibernate.Session;
26 import org.hibernate.SessionFactory;
27 import org.hibernate.Transaction;
28 import org.hibernate.query.Query;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.InjectMocks;
33 import org.mockito.Mock;
34 import org.mockito.Mockito;
35 import org.onap.usecaseui.server.bean.intent.CCVPNInstance;
36 import org.onap.usecaseui.server.bean.intent.IntentModel;
37 import org.onap.usecaseui.server.service.intent.IntentApiService;
38 import org.onap.usecaseui.server.service.lcm.domain.so.SOService;
39 import org.onap.usecaseui.server.service.lcm.domain.so.bean.OperationProgress;
40 import org.onap.usecaseui.server.service.lcm.domain.so.bean.OperationProgressInformation;
41 import org.powermock.api.mockito.PowerMockito;
42 import org.powermock.api.support.membermodification.MemberModifier;
43 import org.powermock.modules.junit4.PowerMockRunner;
44
45 import static org.junit.Assert.*;
46 import static org.mockito.ArgumentMatchers.any;
47 import static org.mockito.ArgumentMatchers.anyString;
48 import static org.powermock.api.mockito.PowerMockito.doReturn;
49 import static org.powermock.api.mockito.PowerMockito.when;
50
51 import retrofit2.Call;
52 import retrofit2.Response;
53
54 @RunWith(PowerMockRunner.class)
55 public class IntentInstanceServiceImplTest {
56
57     public IntentInstanceServiceImplTest() {
58     }
59
60     @InjectMocks
61     private IntentInstanceServiceImpl intentInstanceService;
62
63     @Mock
64     private IntentApiService intentApiService;
65
66     @Mock
67     private SOService soService;
68
69
70     @Mock
71     private SessionFactory sessionFactory;
72
73     @Mock
74     private Session session;
75
76     @Before
77     public void before() throws Exception {
78         MemberModifier.field(IntentInstanceServiceImpl.class, "sessionFactory").set(intentInstanceService , sessionFactory);
79         doReturn(session).when(sessionFactory,"openSession");
80     }
81
82     @Test
83     public void queryIntentInstance() {
84         CCVPNInstance instance = new CCVPNInstance();
85         instance.setInstanceId("1");
86         instance.setJobId("1");
87         instance.setStatus("1");
88
89         Query query = Mockito.mock(Query.class);
90         when(session.createQuery(anyString())).thenReturn(query);
91         List<IntentModel> list = new ArrayList<>();
92         when(query.list()).thenReturn(list);
93         when(query.uniqueResult()).thenReturn(10);
94         assertTrue(intentInstanceService.queryIntentInstance(instance,1,2).getList().isEmpty());
95     }
96     @Test
97     public void createIntentInstance() throws IOException {
98         CCVPNInstance instance = new CCVPNInstance();
99         instance.setInstanceId("1");
100         instance.setJobId("1");
101         instance.setStatus("1");
102
103         Call mockCall = PowerMockito.mock(Call.class);
104         JSONObject body = JSONObject.parseObject("{\"jobId\":\"123\"}");
105         Response<JSONObject> response = Response.success(body);
106         Mockito.when(intentApiService.createIntentInstance(any())).thenReturn(mockCall);
107         Mockito.when(mockCall.execute()).thenReturn(response);
108
109
110         Transaction tx = Mockito.mock(Transaction.class);
111         Mockito.when(session.beginTransaction()).thenReturn(tx);
112         Serializable save = Mockito.mock(Serializable.class);
113         Mockito.when(session.save(any())).thenReturn(save);
114         Mockito.doNothing().when(tx).commit();
115
116         assertEquals(intentInstanceService.createIntentInstance(instance), 1);
117     }
118     @Test
119     public void getIntentInstanceProgress() throws IOException {
120
121         Query query1 = Mockito.mock(Query.class);
122         when(session.createQuery("from CCVPNInstance where deleteState = 0 and status = '0'")).thenReturn(query1);
123         List<CCVPNInstance> q = new ArrayList<>();
124         CCVPNInstance instance = new CCVPNInstance();
125         instance.setInstanceId("1");
126         q.add(instance);
127         when(query1.list()).thenReturn(q);
128
129         OperationProgressInformation operationProgressInformation = new OperationProgressInformation();
130         OperationProgress operationProgress = new OperationProgress();
131         operationProgress.setProgress(100);
132         operationProgressInformation.setOperationStatus(operationProgress);
133         Call mockCall = PowerMockito.mock(Call.class);
134         Response<OperationProgressInformation> response = Response.success(operationProgressInformation);
135         Mockito.when(soService.queryOperationProgress(any(),any())).thenReturn(mockCall);
136         Mockito.when(mockCall.execute()).thenReturn(response);
137
138         Transaction tx = Mockito.mock(Transaction.class);
139         Mockito.when(session.beginTransaction()).thenReturn(tx);
140         Serializable save = Mockito.mock(Serializable.class);
141         Mockito.when(session.save(any())).thenReturn(save);
142         Mockito.doNothing().when(tx).commit();
143
144         intentInstanceService.getIntentInstanceProgress();
145     }
146
147     @Test
148     public void getFinishedInstanceInfo() {
149         Query query = Mockito.mock(Query.class);
150         when(session.createQuery(anyString())).thenReturn(query);
151         when(query.list()).thenReturn(new ArrayList());
152         assertTrue(intentInstanceService.getFinishedInstanceInfo().isEmpty());
153     }
154
155     @Test
156     public void getIntentInstanceBandwidth() throws IOException {
157         Query query1 = Mockito.mock(Query.class);
158         when(session.createQuery("from CCVPNInstance where deleteState = 0 and status = '1'")).thenReturn(query1);
159         List<CCVPNInstance> q = new ArrayList<>();
160         CCVPNInstance instance = new CCVPNInstance();
161         instance.setInstanceId("1");
162         instance.setResourceInstanceId("1");
163         q.add(instance);
164         when(query1.list()).thenReturn(q);
165
166         Call mockCall = PowerMockito.mock(Call.class);
167         JSONObject jsonObject = JSONObject.parseObject("{\n" +
168                 "    \"service-instance-id\":\"cll-101\",\n" +
169                 "    \"service-instance-name\":\"cloud-leased-line-101\",\n" +
170                 "    \"service-type\":\"CLL\",\n" +
171                 "    \"service-role\":\"cll\",\n" +
172                 "    \"environment-context\":\"cll\",\n" +
173                 "    \"model-invariant-id\":\"6790ab0e-034f-11eb-adc1-0242ac120002\",\n" +
174                 "    \"model-version-id\":\"6790ab0e-034f-11eb-adc1-0242ac120002\",\n" +
175                 "    \"resource-version\":\"1628714665927\",\n" +
176                 "    \"orchestration-status\":\"created\",\n" +
177                 "    \"allotted-resources\":{\n" +
178                 "        \"allotted-resource\":[\n" +
179                 "            {\n" +
180                 "                \"id\":\"cll-101-network-001\",\n" +
181                 "                \"resource-version\":\"1628714665798\",\n" +
182                 "                \"type\":\"TsciNetwork\",\n" +
183                 "                \"allotted-resource-name\":\"network_cll-101-network-001\",\n" +
184                 "                \"relationship-list\":{\n" +
185                 "                    \"relationship\":[\n" +
186                 "                        {\n" +
187                 "                            \"related-to\":\"logical-link\",\n" +
188                 "                            \"relationship-label\":\"org.onap.relationships.inventory.ComposedOf\",\n" +
189                 "                            \"related-link\":\"/aai/v24/network/logical-links/logical-link/tranportEp_UNI_ID_311_1\",\n" +
190                 "                            \"relationship-data\":[\n" +
191                 "                                {\n" +
192                 "                                    \"relationship-key\":\"logical-link.link-name\",\n" +
193                 "                                    \"relationship-value\":\"tranportEp_UNI_ID_311_1\"\n" +
194                 "                                }\n" +
195                 "                            ]\n" +
196                 "                        },\n" +
197                 "                        {\n" +
198                 "                            \"related-to\":\"network-policy\",\n" +
199                 "                            \"relationship-label\":\"org.onap.relationships.inventory.Uses\",\n" +
200                 "                            \"related-link\":\"/aai/v24/network/network-policies/network-policy/de00a0a0-be2e-4d19-974a-80a2bca6bdf9\",\n" +
201                 "                            \"relationship-data\":[\n" +
202                 "                                {\n" +
203                 "                                    \"relationship-key\":\"network-policy.network-policy-id\",\n" +
204                 "                                    \"relationship-value\":\"de00a0a0-be2e-4d19-974a-80a2bca6bdf9\"\n" +
205                 "                                }\n" +
206                 "                            ],\n" +
207                 "                            \"related-to-property\":[\n" +
208                 "                                {\n" +
209                 "                                    \"property-key\":\"network-policy.network-policy-fqdn\",\n" +
210                 "                                    \"property-value\":\"cll-101\"\n" +
211                 "                                }\n" +
212                 "                            ]\n" +
213                 "                        }\n" +
214                 "                    ]\n" +
215                 "                }\n" +
216                 "            }\n" +
217                 "        ]\n" +
218                 "    }\n" +
219                 "}");
220         Response<JSONObject> response = Response.success(jsonObject);
221         Mockito.when(intentApiService.getInstanceNetworkInfo(any())).thenReturn(mockCall);
222         Mockito.when(mockCall.execute()).thenReturn(response);
223
224         Call mockCall1 = PowerMockito.mock(Call.class);
225         JSONObject jsonObject1 = JSONObject.parseObject("{\n" +
226                 "    \"network-policy-id\":\"de00a0a0-be2e-4d19-974a-80a2bca6bdf9\",\n" +
227                 "    \"network-policy-fqdn\":\"cll-101\",\n" +
228                 "    \"resource-version\":\"1628714665619\",\n" +
229                 "    \"name\":\"TSCi policy\",\n" +
230                 "    \"type\":\"SLA\",\n" +
231                 "    \"latency\":2,\n" +
232                 "    \"max-bandwidth\":3000,\n" +
233                 "    \"relationship-list\":{\n" +
234                 "        \"relationship\":[\n" +
235                 "            {\n" +
236                 "                \"related-to\":\"allotted-resource\",\n" +
237                 "                \"relationship-label\":\"org.onap.relationships.inventory.Uses\",\n" +
238                 "                \"related-link\":\"/aai/v24/business/customers/customer/IBNCustomer/service-subscriptions/service-subscription/IBN/service-instances/service-instance/cll-101/allotted-resources/allotted-resource/cll-101-network-001\",\n" +
239                 "                \"relationship-data\":[\n" +
240                 "                    {\n" +
241                 "                        \"relationship-key\":\"customer.global-customer-id\",\n" +
242                 "                        \"relationship-value\":\"IBNCustomer\"\n" +
243                 "                    },\n" +
244                 "                    {\n" +
245                 "                        \"relationship-key\":\"service-subscription.service-type\",\n" +
246                 "                        \"relationship-value\":\"IBN\"\n" +
247                 "                    },\n" +
248                 "                    {\n" +
249                 "                        \"relationship-key\":\"service-instance.service-instance-id\",\n" +
250                 "                        \"relationship-value\":\"cll-101\"\n" +
251                 "                    },\n" +
252                 "                    {\n" +
253                 "                        \"relationship-key\":\"allotted-resource.id\",\n" +
254                 "                        \"relationship-value\":\"cll-101-network-001\"\n" +
255                 "                    }\n" +
256                 "                ],\n" +
257                 "                \"related-to-property\":[\n" +
258                 "                    {\n" +
259                 "                        \"property-key\":\"allotted-resource.description\"\n" +
260                 "                    },\n" +
261                 "                    {\n" +
262                 "                        \"property-key\":\"allotted-resource.allotted-resource-name\",\n" +
263                 "                        \"property-value\":\"network_cll-101-network-001\"\n" +
264                 "                    }\n" +
265                 "                ]\n" +
266                 "            }\n" +
267                 "        ]\n" +
268                 "    }\n" +
269                 "}");
270         Response<JSONObject> response1 = Response.success(jsonObject1);
271         Mockito.when(intentApiService.getInstanceNetworkPolicyInfo(any())).thenReturn(mockCall1);
272         Mockito.when(mockCall1.execute()).thenReturn(response1);
273
274         Call mockCall2 = PowerMockito.mock(Call.class);
275         JSONObject jsonObject2 = JSONObject.parseObject("{\n" +
276                 "    \"metadatum\":[\n" +
277                 "        {\n" +
278                 "            \"metaname\":\"ethernet-uni-id-1\",\n" +
279                 "            \"metaval\":\"1234\",\n" +
280                 "            \"resource-version\":\"1629409084707\"\n" +
281                 "        },\n" +
282                 "        {\n" +
283                 "            \"metaname\":\"ethernet-uni-id-2\",\n" +
284                 "            \"metaval\":\"5678\",\n" +
285                 "            \"resource-version\":\"1629409204904\"\n" +
286                 "        }\n" +
287                 "    ]\n" +
288                 "}");
289         Response<JSONObject> response2 = Response.success(jsonObject2);
290         Mockito.when(intentApiService.getInstanceBandwidth(any())).thenReturn(mockCall2);
291         Mockito.when(mockCall2.execute()).thenReturn(response2);
292
293         Transaction tx = Mockito.mock(Transaction.class);
294         Mockito.when(session.beginTransaction()).thenReturn(tx);
295         Serializable save = Mockito.mock(Serializable.class);
296         Mockito.when(session.save(any())).thenReturn(save);
297         Mockito.doNothing().when(tx).commit();
298
299         intentInstanceService.getIntentInstanceBandwidth();
300     }
301
302     @Test
303     public void deleteIntentInstance() throws IOException {
304         CCVPNInstance instance = new CCVPNInstance();
305         instance.setResourceInstanceId("1");
306
307         Query query = Mockito.mock(Query.class);
308         when(session.createQuery(anyString())).thenReturn(query);
309         when(query.setParameter(anyString(), anyString())).thenReturn(query);
310         when(query.uniqueResult()).thenReturn(instance);
311
312         Call mockCall = PowerMockito.mock(Call.class);
313         when(intentApiService.deleteIntentInstance(any())).thenReturn(mockCall);
314         Mockito.when(mockCall.execute()).thenReturn(null);
315
316         Transaction tx = Mockito.mock(Transaction.class);
317         Mockito.when(session.beginTransaction()).thenReturn(tx);
318         Serializable save = Mockito.mock(Serializable.class);
319         Mockito.doNothing().when(session).delete(any());
320         Mockito.doNothing().when(tx).commit();
321
322         intentInstanceService.deleteIntentInstance("1");
323     }
324
325     @Test
326     public void activeIntentInstance() throws IOException {
327         CCVPNInstance instance = new CCVPNInstance();
328         instance.setInstanceId("1");
329         instance.setJobId("1");
330         instance.setStatus("1");
331
332         Query query = Mockito.mock(Query.class);
333         when(session.createQuery(anyString())).thenReturn(query);
334         when(query.setParameter(anyString(), anyString())).thenReturn(query);
335         when(query.uniqueResult()).thenReturn(instance);
336
337
338         Call mockCall = PowerMockito.mock(Call.class);
339         JSONObject body = JSONObject.parseObject("{\"jobId\":\"123\"}");
340         Response<JSONObject> response = Response.success(body);
341         Mockito.when(intentApiService.createIntentInstance(any())).thenReturn(mockCall);
342         Mockito.when(mockCall.execute()).thenReturn(response);
343
344         Transaction tx = Mockito.mock(Transaction.class);
345         Mockito.when(session.beginTransaction()).thenReturn(tx);
346         Serializable save = Mockito.mock(Serializable.class);
347         Mockito.when(session.save(any())).thenReturn(save);
348         Mockito.doNothing().when(tx).commit();
349
350         intentInstanceService.activeIntentInstance("1");
351
352     }
353
354     @Test
355     public void queryAccessNodeInfo() throws IOException {
356
357         Call mockCall = PowerMockito.mock(Call.class);
358         JSONObject body = JSONObject.parseObject("{\"data\":[{\"type\":\"ROOT\",\"route-id\":\"route1\"},{\"type\":\"route\",\"route-id\":\"route2\"}]}");
359         Response<JSONObject> response = Response.success(body);
360         Mockito.when(intentApiService.queryNetworkRoute()).thenReturn(mockCall);
361         Mockito.when(mockCall.execute()).thenReturn(response);
362         Map<String, Object> result = (Map<String, Object>) intentInstanceService.queryAccessNodeInfo();
363         assertEquals(((List)result.get("accessNodeList")).size(), 1);
364     }
365 }