2 * Copyright (C) 2021 CTC, Inc. and others. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.usecaseui.server.service.intent.impl;
18 import java.io.IOException;
19 import java.io.Serializable;
20 import java.util.ArrayList;
21 import java.util.List;
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.IntentInstance;
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.core.classloader.annotations.PrepareForTest;
44 import org.powermock.modules.junit4.PowerMockRunner;
46 import static org.junit.Assert.*;
47 import static org.mockito.ArgumentMatchers.any;
48 import static org.mockito.ArgumentMatchers.anyString;
49 import static org.powermock.api.mockito.PowerMockito.doReturn;
50 import static org.powermock.api.mockito.PowerMockito.when;
52 import retrofit2.Call;
53 import retrofit2.Response;
55 @RunWith(PowerMockRunner.class)
56 @PrepareForTest({IntentInstanceServiceImpl.class})
57 public class IntentInstanceServiceImplTest {
59 public IntentInstanceServiceImplTest() {
63 private IntentInstanceServiceImpl intentInstanceService;
66 private IntentApiService intentApiService;
69 private SOService soService;
73 private SessionFactory sessionFactory;
76 private Session session;
79 public void before() throws Exception {
80 MemberModifier.field(IntentInstanceServiceImpl.class, "sessionFactory").set(intentInstanceService , sessionFactory);
81 doReturn(session).when(sessionFactory,"openSession");
85 public void queryIntentInstance() {
86 IntentInstance instance = new IntentInstance();
87 instance.setInstanceId("1");
88 instance.setJobId("1");
89 instance.setStatus("1");
91 Query query = Mockito.mock(Query.class);
92 when(session.createQuery(anyString())).thenReturn(query);
93 List<IntentModel> list = new ArrayList<>();
94 when(query.list()).thenReturn(list);
95 when(query.uniqueResult()).thenReturn(10);
96 assertTrue(intentInstanceService.queryIntentInstance(instance,1,2).getList().isEmpty());
99 public void createIntentInstance() throws IOException {
100 IntentInstance instance = new IntentInstance();
101 instance.setInstanceId("1");
102 instance.setJobId("1");
103 instance.setStatus("1");
105 Call mockCall = PowerMockito.mock(Call.class);
106 JSONObject body = JSONObject.parseObject("{\"jobId\":\"123\"}");
107 Response<JSONObject> response = Response.success(body);
108 Mockito.when(intentApiService.createIntentInstance(any())).thenReturn(mockCall);
109 Mockito.when(mockCall.execute()).thenReturn(response);
112 Transaction tx = Mockito.mock(Transaction.class);
113 Mockito.when(session.beginTransaction()).thenReturn(tx);
114 Serializable save = Mockito.mock(Serializable.class);
115 Mockito.when(session.save(any())).thenReturn(save);
116 Mockito.doNothing().when(tx).commit();
118 assertEquals(intentInstanceService.createIntentInstance(instance), 1);
121 public void getIntentInstanceProgress() throws IOException {
123 Query query1 = Mockito.mock(Query.class);
124 when(session.createQuery("from IntentInstance where deleteState = 0 and status = '0'")).thenReturn(query1);
125 List<IntentInstance> q = new ArrayList<>();
126 IntentInstance instance = new IntentInstance();
127 instance.setInstanceId("1");
129 when(query1.list()).thenReturn(q);
131 OperationProgressInformation operationProgressInformation = new OperationProgressInformation();
132 OperationProgress operationProgress = new OperationProgress();
133 operationProgress.setProgress(100);
134 operationProgressInformation.setOperationStatus(operationProgress);
135 Call mockCall = PowerMockito.mock(Call.class);
136 Response<OperationProgressInformation> response = Response.success(operationProgressInformation);
137 Mockito.when(soService.queryOperationProgress(any(),any())).thenReturn(mockCall);
138 Mockito.when(mockCall.execute()).thenReturn(response);
140 Transaction tx = Mockito.mock(Transaction.class);
141 Mockito.when(session.beginTransaction()).thenReturn(tx);
142 Serializable save = Mockito.mock(Serializable.class);
143 Mockito.when(session.save(any())).thenReturn(save);
144 Mockito.doNothing().when(tx).commit();
146 intentInstanceService.getIntentInstanceProgress();
150 public void getFinishedInstanceInfo() {
151 Query query = Mockito.mock(Query.class);
152 when(session.createQuery(anyString())).thenReturn(query);
153 when(query.list()).thenReturn(new ArrayList());
154 assertTrue(intentInstanceService.getFinishedInstanceInfo().isEmpty());
158 public void getIntentInstanceBandwidth() throws IOException {
159 Query query1 = Mockito.mock(Query.class);
160 when(session.createQuery("from IntentInstance where deleteState = 0 and status = '1'")).thenReturn(query1);
161 List<IntentInstance> q = new ArrayList<>();
162 IntentInstance instance = new IntentInstance();
163 instance.setInstanceId("1");
164 instance.setResourceInstanceId("1");
166 when(query1.list()).thenReturn(q);
168 Call mockCall = PowerMockito.mock(Call.class);
169 JSONObject jsonObject = JSONObject.parseObject("{\n" +
170 " \"service-instance-id\":\"cll-101\",\n" +
171 " \"service-instance-name\":\"cloud-leased-line-101\",\n" +
172 " \"service-type\":\"CLL\",\n" +
173 " \"service-role\":\"cll\",\n" +
174 " \"environment-context\":\"cll\",\n" +
175 " \"model-invariant-id\":\"6790ab0e-034f-11eb-adc1-0242ac120002\",\n" +
176 " \"model-version-id\":\"6790ab0e-034f-11eb-adc1-0242ac120002\",\n" +
177 " \"resource-version\":\"1628714665927\",\n" +
178 " \"orchestration-status\":\"created\",\n" +
179 " \"allotted-resources\":{\n" +
180 " \"allotted-resource\":[\n" +
182 " \"id\":\"cll-101-network-001\",\n" +
183 " \"resource-version\":\"1628714665798\",\n" +
184 " \"type\":\"TsciNetwork\",\n" +
185 " \"allotted-resource-name\":\"network_cll-101-network-001\",\n" +
186 " \"relationship-list\":{\n" +
187 " \"relationship\":[\n" +
189 " \"related-to\":\"logical-link\",\n" +
190 " \"relationship-label\":\"org.onap.relationships.inventory.ComposedOf\",\n" +
191 " \"related-link\":\"/aai/v24/network/logical-links/logical-link/tranportEp_UNI_ID_311_1\",\n" +
192 " \"relationship-data\":[\n" +
194 " \"relationship-key\":\"logical-link.link-name\",\n" +
195 " \"relationship-value\":\"tranportEp_UNI_ID_311_1\"\n" +
200 " \"related-to\":\"network-policy\",\n" +
201 " \"relationship-label\":\"org.onap.relationships.inventory.Uses\",\n" +
202 " \"related-link\":\"/aai/v24/network/network-policies/network-policy/de00a0a0-be2e-4d19-974a-80a2bca6bdf9\",\n" +
203 " \"relationship-data\":[\n" +
205 " \"relationship-key\":\"network-policy.network-policy-id\",\n" +
206 " \"relationship-value\":\"de00a0a0-be2e-4d19-974a-80a2bca6bdf9\"\n" +
209 " \"related-to-property\":[\n" +
211 " \"property-key\":\"network-policy.network-policy-fqdn\",\n" +
212 " \"property-value\":\"cll-101\"\n" +
222 Response<JSONObject> response = Response.success(jsonObject);
223 Mockito.when(intentApiService.getInstanceNetworkInfo(any())).thenReturn(mockCall);
224 Mockito.when(mockCall.execute()).thenReturn(response);
226 Call mockCall1 = PowerMockito.mock(Call.class);
227 JSONObject jsonObject1 = JSONObject.parseObject("{\n" +
228 " \"network-policy-id\":\"de00a0a0-be2e-4d19-974a-80a2bca6bdf9\",\n" +
229 " \"network-policy-fqdn\":\"cll-101\",\n" +
230 " \"resource-version\":\"1628714665619\",\n" +
231 " \"name\":\"TSCi policy\",\n" +
232 " \"type\":\"SLA\",\n" +
233 " \"latency\":2,\n" +
234 " \"max-bandwidth\":3000,\n" +
235 " \"relationship-list\":{\n" +
236 " \"relationship\":[\n" +
238 " \"related-to\":\"allotted-resource\",\n" +
239 " \"relationship-label\":\"org.onap.relationships.inventory.Uses\",\n" +
240 " \"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" +
241 " \"relationship-data\":[\n" +
243 " \"relationship-key\":\"customer.global-customer-id\",\n" +
244 " \"relationship-value\":\"IBNCustomer\"\n" +
247 " \"relationship-key\":\"service-subscription.service-type\",\n" +
248 " \"relationship-value\":\"IBN\"\n" +
251 " \"relationship-key\":\"service-instance.service-instance-id\",\n" +
252 " \"relationship-value\":\"cll-101\"\n" +
255 " \"relationship-key\":\"allotted-resource.id\",\n" +
256 " \"relationship-value\":\"cll-101-network-001\"\n" +
259 " \"related-to-property\":[\n" +
261 " \"property-key\":\"allotted-resource.description\"\n" +
264 " \"property-key\":\"allotted-resource.allotted-resource-name\",\n" +
265 " \"property-value\":\"network_cll-101-network-001\"\n" +
272 Response<JSONObject> response1 = Response.success(jsonObject1);
273 Mockito.when(intentApiService.getInstanceNetworkPolicyInfo(any())).thenReturn(mockCall1);
274 Mockito.when(mockCall1.execute()).thenReturn(response1);
276 Call mockCall2 = PowerMockito.mock(Call.class);
277 JSONObject jsonObject2 = JSONObject.parseObject("{\n" +
278 " \"metadatum\":[\n" +
280 " \"metaname\":\"ethernet-uni-id-1\",\n" +
281 " \"metaval\":\"1234\",\n" +
282 " \"resource-version\":\"1629409084707\"\n" +
285 " \"metaname\":\"ethernet-uni-id-2\",\n" +
286 " \"metaval\":\"5678\",\n" +
287 " \"resource-version\":\"1629409204904\"\n" +
291 Response<JSONObject> response2 = Response.success(jsonObject2);
292 Mockito.when(intentApiService.getInstanceBandwidth(any())).thenReturn(mockCall2);
293 Mockito.when(mockCall2.execute()).thenReturn(response2);
295 Transaction tx = Mockito.mock(Transaction.class);
296 Mockito.when(session.beginTransaction()).thenReturn(tx);
297 Serializable save = Mockito.mock(Serializable.class);
298 Mockito.when(session.save(any())).thenReturn(save);
299 Mockito.doNothing().when(tx).commit();
301 intentInstanceService.getIntentInstanceBandwidth();
305 public void deleteIntentInstance() throws IOException {
306 IntentInstance instance = new IntentInstance();
307 instance.setResourceInstanceId("1");
309 Query query = Mockito.mock(Query.class);
310 when(session.createQuery(anyString())).thenReturn(query);
311 when(query.setParameter(anyString(), anyString())).thenReturn(query);
312 when(query.uniqueResult()).thenReturn(instance);
314 Call mockCall = PowerMockito.mock(Call.class);
315 when(intentApiService.deleteIntentInstance(any())).thenReturn(mockCall);
316 Mockito.when(mockCall.execute()).thenReturn(null);
318 Transaction tx = Mockito.mock(Transaction.class);
319 Mockito.when(session.beginTransaction()).thenReturn(tx);
320 Serializable save = Mockito.mock(Serializable.class);
321 Mockito.doNothing().when(session).delete(any());
322 Mockito.doNothing().when(tx).commit();
324 intentInstanceService.deleteIntentInstance("1");
328 public void activeIntentInstance() throws IOException {
329 IntentInstance instance = new IntentInstance();
330 instance.setInstanceId("1");
331 instance.setJobId("1");
332 instance.setStatus("1");
334 Query query = Mockito.mock(Query.class);
335 when(session.createQuery(anyString())).thenReturn(query);
336 when(query.setParameter(anyString(), anyString())).thenReturn(query);
337 when(query.uniqueResult()).thenReturn(instance);
340 Call mockCall = PowerMockito.mock(Call.class);
341 JSONObject body = JSONObject.parseObject("{\"jobId\":\"123\"}");
342 Response<JSONObject> response = Response.success(body);
343 Mockito.when(intentApiService.createIntentInstance(any())).thenReturn(mockCall);
344 Mockito.when(mockCall.execute()).thenReturn(response);
346 Transaction tx = Mockito.mock(Transaction.class);
347 Mockito.when(session.beginTransaction()).thenReturn(tx);
348 Serializable save = Mockito.mock(Serializable.class);
349 Mockito.when(session.save(any())).thenReturn(save);
350 Mockito.doNothing().when(tx).commit();
352 intentInstanceService.activeIntentInstance("1");
357 public void queryAccessNodeInfo() throws IOException {
359 Call mockCall = PowerMockito.mock(Call.class);
360 JSONObject body = JSONObject.parseObject("{\"data\":[{\"type\":\"ROOT\",\"route-id\":\"route1\"},{\"type\":\"route\",\"route-id\":\"route2\"}]}");
361 Response<JSONObject> response = Response.success(body);
362 Mockito.when(intentApiService.queryNetworkRoute()).thenReturn(mockCall);
363 Mockito.when(mockCall.execute()).thenReturn(response);
364 Map<String, Object> result = (Map<String, Object>) intentInstanceService.queryAccessNodeInfo();
365 assertEquals(((List)result.get("accessNodeList")).size(), 1);