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;
22 import com.alibaba.fastjson.JSONArray;
23 import com.alibaba.fastjson.JSONObject;
24 import okhttp3.MediaType;
25 import okhttp3.ResponseBody;
26 import okio.BufferedSource;
27 import org.hibernate.Session;
28 import org.hibernate.SessionFactory;
29 import org.hibernate.Transaction;
30 import org.hibernate.query.Query;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.InjectMocks;
35 import org.mockito.Mock;
36 import org.mockito.Mockito;
37 import org.onap.usecaseui.server.bean.intent.CCVPNInstance;
38 import org.onap.usecaseui.server.bean.intent.InstancePerformance;
39 import org.onap.usecaseui.server.bean.intent.IntentModel;
40 import org.onap.usecaseui.server.service.intent.IntentApiService;
41 import org.onap.usecaseui.server.service.lcm.domain.so.SOService;
42 import org.onap.usecaseui.server.service.lcm.domain.so.bean.OperationProgress;
43 import org.onap.usecaseui.server.service.lcm.domain.so.bean.OperationProgressInformation;
44 import org.powermock.api.mockito.PowerMockito;
45 import org.powermock.api.support.membermodification.MemberModifier;
46 import org.powermock.modules.junit4.PowerMockRunner;
48 import static org.junit.Assert.*;
49 import static org.mockito.ArgumentMatchers.*;
50 import static org.powermock.api.mockito.PowerMockito.*;
52 import retrofit2.Call;
53 import retrofit2.Response;
55 import javax.annotation.Nullable;
57 @RunWith(PowerMockRunner.class)
58 public class IntentInstanceServiceImplTest {
60 public IntentInstanceServiceImplTest() {
64 private IntentInstanceServiceImpl intentInstanceService;
67 private IntentApiService intentApiService;
70 private SOService soService;
74 private SessionFactory sessionFactory;
77 private Session session;
80 public void before() throws Exception {
81 MemberModifier.field(IntentInstanceServiceImpl.class, "sessionFactory").set(intentInstanceService , sessionFactory);
82 doReturn(session).when(sessionFactory,"openSession");
86 public void queryIntentInstanceTest() {
87 CCVPNInstance instance = new CCVPNInstance();
88 instance.setInstanceId("1");
89 instance.setJobId("1");
90 instance.setStatus("1");
92 Query query = Mockito.mock(Query.class);
93 when(session.createQuery(anyString())).thenReturn(query);
94 List<IntentModel> list = new ArrayList<>();
95 when(query.list()).thenReturn(list);
96 when(query.uniqueResult()).thenReturn(10L);
97 assertTrue(intentInstanceService.queryIntentInstance(instance,1,2).getList().isEmpty());
101 public void queryIntentInstanceGetCountErrorTest() {
102 CCVPNInstance instance = new CCVPNInstance();
103 instance.setInstanceId("1");
104 instance.setJobId("1");
105 instance.setStatus("1");
107 Query query = Mockito.mock(Query.class);
108 when(session.createQuery(anyString())).thenReturn(query);
109 List<IntentModel> list = new ArrayList<>();
110 when(query.list()).thenReturn(list);
111 when(query.uniqueResult()).thenReturn(10);
112 assertTrue(intentInstanceService.queryIntentInstance(instance,1,2).getList().isEmpty());
116 public void queryIntentInstanceThrowErrorTest() {
117 CCVPNInstance instance = new CCVPNInstance();
118 instance.setInstanceId("1");
119 instance.setJobId("1");
120 instance.setStatus("1");
122 when(session.createQuery(anyString())).thenThrow(new RuntimeException());
124 assertEquals(intentInstanceService.queryIntentInstance(instance,1,2), null);
127 public void createIntentInstanceTest() throws IOException {
128 CCVPNInstance instance = new CCVPNInstance();
129 instance.setInstanceId("1");
130 instance.setJobId("1");
131 instance.setStatus("1");
133 Call mockCall = PowerMockito.mock(Call.class);
134 JSONObject body = JSONObject.parseObject("{\"jobId\":\"123\"}");
135 Response<JSONObject> response = Response.success(body);
136 Mockito.when(intentApiService.createIntentInstance(any())).thenReturn(mockCall);
137 Mockito.when(mockCall.execute()).thenReturn(response);
139 IntentInstanceServiceImpl spy = PowerMockito.spy(intentInstanceService);
140 doNothing().when(spy).saveIntentInstanceToAAI(isNull(),any(CCVPNInstance.class));
142 Transaction tx = Mockito.mock(Transaction.class);
143 Mockito.when(session.beginTransaction()).thenReturn(tx);
144 Serializable save = Mockito.mock(Serializable.class);
145 Mockito.when(session.save(any())).thenReturn(save);
146 Mockito.doNothing().when(tx).commit();
148 assertEquals(spy.createIntentInstance(instance), 1);
152 public void createIntentInstanceThrowErrorTest() throws IOException {
153 CCVPNInstance instance = new CCVPNInstance();
154 instance.setInstanceId("1");
155 instance.setJobId("1");
156 instance.setStatus("1");
158 Call mockCall = PowerMockito.mock(Call.class);
159 JSONObject body = JSONObject.parseObject("{\"jobId\":\"123\"}");
160 Response<JSONObject> response = Response.success(body);
161 Mockito.when(intentApiService.createIntentInstance(any())).thenReturn(mockCall);
162 Mockito.when(mockCall.execute()).thenReturn(response);
164 IntentInstanceServiceImpl spy = PowerMockito.spy(intentInstanceService);
165 doThrow(new RuntimeException()).when(spy).saveIntentInstanceToAAI(isNull(),any(CCVPNInstance.class));
167 Transaction tx = Mockito.mock(Transaction.class);
168 Mockito.when(session.beginTransaction()).thenReturn(tx);
169 Serializable save = Mockito.mock(Serializable.class);
170 Mockito.when(session.save(any())).thenReturn(save);
171 Mockito.doNothing().when(tx).commit();
173 assertEquals(spy.createIntentInstance(instance), 0);
177 public void createIntentInstanceInstanceIsNullTest() throws IOException {
178 assertEquals(intentInstanceService.createIntentInstance(null), 0);
181 public void createIntentInstanceInstanceJobIdIsNullTest() throws IOException {
182 CCVPNInstance instance = new CCVPNInstance();
183 instance.setInstanceId("1");
184 instance.setStatus("1");
185 assertEquals(intentInstanceService.createIntentInstance(instance), 0);
189 public void getIntentInstanceProgressTest() throws IOException {
191 Query query1 = Mockito.mock(Query.class);
192 when(session.createQuery("from CCVPNInstance where deleteState = 0 and status = '0'")).thenReturn(query1);
193 List<CCVPNInstance> q = new ArrayList<>();
194 CCVPNInstance instance = new CCVPNInstance();
195 instance.setInstanceId("1");
196 instance.setResourceInstanceId("1");
197 instance.setJobId("1");
199 when(query1.list()).thenReturn(q);
201 OperationProgressInformation operationProgressInformation = new OperationProgressInformation();
202 OperationProgress operationProgress = new OperationProgress();
203 operationProgress.setProgress(100);
204 operationProgressInformation.setOperationStatus(operationProgress);
206 JSONObject jsonObject = new JSONObject();
207 JSONObject operation = new JSONObject();
208 operation.put("progress", 100);
209 jsonObject.put("operation", operation);
210 Call mockCall = PowerMockito.mock(Call.class);
211 Response<JSONObject> response = Response.success(jsonObject);
212 Mockito.when(intentApiService.queryOperationProgress(anyString(),anyString())).thenReturn(mockCall);
213 Mockito.when(mockCall.execute()).thenReturn(response);
215 IntentInstanceServiceImpl spy = PowerMockito.spy(intentInstanceService);
216 doNothing().when(spy).saveIntentInstanceToAAI(anyString(),any(CCVPNInstance.class));
218 Transaction tx = Mockito.mock(Transaction.class);
219 Mockito.when(session.beginTransaction()).thenReturn(tx);
220 Serializable save = Mockito.mock(Serializable.class);
221 Mockito.when(session.save(any())).thenReturn(save);
222 Mockito.doNothing().when(tx).commit();
224 spy.getIntentInstanceProgress();
225 assertEquals(operation.getString("progress"),"100");
228 public void getIntentInstanceCreateStatusTest() throws IOException {
230 Query query1 = Mockito.mock(Query.class);
231 when(session.createQuery("from CCVPNInstance where deleteState = 0 and status = '0'")).thenReturn(query1);
232 List<CCVPNInstance> q = new ArrayList<>();
233 CCVPNInstance instance = new CCVPNInstance();
234 instance.setInstanceId("1");
235 instance.setResourceInstanceId("1");
236 instance.setJobId("1");
238 when(query1.list()).thenReturn(q);
240 OperationProgressInformation operationProgressInformation = new OperationProgressInformation();
241 OperationProgress operationProgress = new OperationProgress();
242 operationProgress.setProgress(100);
243 operationProgressInformation.setOperationStatus(operationProgress);
245 JSONObject jsonObject = new JSONObject();
246 jsonObject.put("orchestration-status", "created");
247 Call mockCall = PowerMockito.mock(Call.class);
248 Response<JSONObject> response = Response.success(jsonObject);
249 Mockito.when(intentApiService.getInstanceInfo(anyString())).thenReturn(mockCall);
250 Mockito.when(mockCall.execute()).thenReturn(response);
252 IntentInstanceServiceImpl spy = PowerMockito.spy(intentInstanceService);
253 doNothing().when(spy).saveIntentInstanceToAAI(anyString(),any(CCVPNInstance.class));
255 Transaction tx = Mockito.mock(Transaction.class);
256 Mockito.when(session.beginTransaction()).thenReturn(tx);
257 Serializable save = Mockito.mock(Serializable.class);
258 Mockito.when(session.save(any())).thenReturn(save);
259 Mockito.doNothing().when(tx).commit();
261 spy.getIntentInstanceCreateStatus();
262 assertEquals(jsonObject.getString("orchestration-status"),"created");
266 public void getFinishedInstanceInfo() {
267 Query query = Mockito.mock(Query.class);
268 when(session.createQuery(anyString())).thenReturn(query);
269 when(query.list()).thenReturn(new ArrayList());
270 assertTrue(intentInstanceService.getFinishedInstanceInfo().isEmpty());
274 public void getIntentInstanceBandwidth() throws IOException {
275 Query query1 = Mockito.mock(Query.class);
276 when(session.createQuery("from CCVPNInstance where deleteState = 0 and status = '1'")).thenReturn(query1);
277 List<CCVPNInstance> q = new ArrayList<>();
278 CCVPNInstance instance = new CCVPNInstance();
279 instance.setInstanceId("1");
280 instance.setResourceInstanceId("1");
282 when(query1.list()).thenReturn(q);
284 Call mockCall = PowerMockito.mock(Call.class);
285 JSONObject jsonObject = JSONObject.parseObject("{\n" +
286 " \"service-instance-id\":\"cll-101\",\n" +
287 " \"service-instance-name\":\"cloud-leased-line-101\",\n" +
288 " \"service-type\":\"CLL\",\n" +
289 " \"service-role\":\"cll\",\n" +
290 " \"environment-context\":\"cll\",\n" +
291 " \"model-invariant-id\":\"6790ab0e-034f-11eb-adc1-0242ac120002\",\n" +
292 " \"model-version-id\":\"6790ab0e-034f-11eb-adc1-0242ac120002\",\n" +
293 " \"resource-version\":\"1628714665927\",\n" +
294 " \"orchestration-status\":\"created\",\n" +
295 " \"allotted-resources\":{\n" +
296 " \"allotted-resource\":[\n" +
298 " \"id\":\"cll-101-network-001\",\n" +
299 " \"resource-version\":\"1628714665798\",\n" +
300 " \"type\":\"TsciNetwork\",\n" +
301 " \"allotted-resource-name\":\"network_cll-101-network-001\",\n" +
302 " \"relationship-list\":{\n" +
303 " \"relationship\":[\n" +
305 " \"related-to\":\"logical-link\",\n" +
306 " \"relationship-label\":\"org.onap.relationships.inventory.ComposedOf\",\n" +
307 " \"related-link\":\"/aai/v24/network/logical-links/logical-link/tranportEp_UNI_ID_311_1\",\n" +
308 " \"relationship-data\":[\n" +
310 " \"relationship-key\":\"logical-link.link-name\",\n" +
311 " \"relationship-value\":\"tranportEp_UNI_ID_311_1\"\n" +
316 " \"related-to\":\"network-policy\",\n" +
317 " \"relationship-label\":\"org.onap.relationships.inventory.Uses\",\n" +
318 " \"related-link\":\"/aai/v24/network/network-policies/network-policy/de00a0a0-be2e-4d19-974a-80a2bca6bdf9\",\n" +
319 " \"relationship-data\":[\n" +
321 " \"relationship-key\":\"network-policy.network-policy-id\",\n" +
322 " \"relationship-value\":\"de00a0a0-be2e-4d19-974a-80a2bca6bdf9\"\n" +
325 " \"related-to-property\":[\n" +
327 " \"property-key\":\"network-policy.network-policy-fqdn\",\n" +
328 " \"property-value\":\"cll-101\"\n" +
338 Response<JSONObject> response = Response.success(jsonObject);
339 Mockito.when(intentApiService.getInstanceNetworkInfo(any())).thenReturn(mockCall);
340 Mockito.when(mockCall.execute()).thenReturn(response);
342 Call mockCall1 = PowerMockito.mock(Call.class);
343 JSONObject jsonObject1 = JSONObject.parseObject("{\n" +
344 " \"network-policy-id\":\"de00a0a0-be2e-4d19-974a-80a2bca6bdf9\",\n" +
345 " \"network-policy-fqdn\":\"cll-101\",\n" +
346 " \"resource-version\":\"1628714665619\",\n" +
347 " \"name\":\"TSCi policy\",\n" +
348 " \"type\":\"SLA\",\n" +
349 " \"latency\":2,\n" +
350 " \"max-bandwidth\":3000,\n" +
351 " \"relationship-list\":{\n" +
352 " \"relationship\":[\n" +
354 " \"related-to\":\"allotted-resource\",\n" +
355 " \"relationship-label\":\"org.onap.relationships.inventory.Uses\",\n" +
356 " \"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" +
357 " \"relationship-data\":[\n" +
359 " \"relationship-key\":\"customer.global-customer-id\",\n" +
360 " \"relationship-value\":\"IBNCustomer\"\n" +
363 " \"relationship-key\":\"service-subscription.service-type\",\n" +
364 " \"relationship-value\":\"IBN\"\n" +
367 " \"relationship-key\":\"service-instance.service-instance-id\",\n" +
368 " \"relationship-value\":\"cll-101\"\n" +
371 " \"relationship-key\":\"allotted-resource.id\",\n" +
372 " \"relationship-value\":\"cll-101-network-001\"\n" +
375 " \"related-to-property\":[\n" +
377 " \"property-key\":\"allotted-resource.description\"\n" +
380 " \"property-key\":\"allotted-resource.allotted-resource-name\",\n" +
381 " \"property-value\":\"network_cll-101-network-001\"\n" +
388 Response<JSONObject> response1 = Response.success(jsonObject1);
389 Mockito.when(intentApiService.getInstanceNetworkPolicyInfo(any())).thenReturn(mockCall1);
390 Mockito.when(mockCall1.execute()).thenReturn(response1);
392 Call mockCall2 = PowerMockito.mock(Call.class);
393 JSONObject jsonObject2 = JSONObject.parseObject("{\n" +
394 " \"metadatum\":[\n" +
396 " \"metaname\":\"ethernet-uni-id-1\",\n" +
397 " \"metaval\":\"1234\",\n" +
398 " \"resource-version\":\"1629409084707\"\n" +
401 " \"metaname\":\"ethernet-uni-id-2\",\n" +
402 " \"metaval\":\"5678\",\n" +
403 " \"resource-version\":\"1629409204904\"\n" +
407 Response<JSONObject> response2 = Response.success(jsonObject2);
408 Mockito.when(intentApiService.getInstanceBandwidth(any())).thenReturn(mockCall2);
409 Mockito.when(mockCall2.execute()).thenReturn(response2);
411 Transaction tx = Mockito.mock(Transaction.class);
412 Mockito.when(session.beginTransaction()).thenReturn(tx);
413 Serializable save = Mockito.mock(Serializable.class);
414 Mockito.when(session.save(any())).thenReturn(save);
415 Mockito.doNothing().when(tx).commit();
417 intentInstanceService.getIntentInstanceBandwidth();
421 public void deleteIntentInstance() throws IOException {
422 CCVPNInstance instance = new CCVPNInstance();
423 instance.setResourceInstanceId("1");
425 Query query = Mockito.mock(Query.class);
426 when(session.createQuery(anyString())).thenReturn(query);
427 when(query.setParameter(anyString(), anyString())).thenReturn(query);
428 when(query.uniqueResult()).thenReturn(instance);
430 Call mockCall = PowerMockito.mock(Call.class);
431 when(intentApiService.deleteIntentInstance(any())).thenReturn(mockCall);
432 when(mockCall.execute()).thenReturn(null);
434 Transaction tx = PowerMockito.mock(Transaction.class);
435 when(session.beginTransaction()).thenReturn(tx);
436 Serializable save = PowerMockito.mock(Serializable.class);
437 doNothing().when(session).delete(any());
438 doNothing().when(tx).commit();
440 IntentInstanceServiceImpl spy = spy(intentInstanceService);
441 doNothing().when(spy).deleteIntentInstanceToAAI(anyString());
443 spy.deleteIntentInstance("1");
448 public void invalidIntentInstanceTest() throws IOException {
449 CCVPNInstance instance = new CCVPNInstance();
450 instance.setResourceInstanceId("1");
452 Query query = Mockito.mock(Query.class);
453 when(session.createQuery(anyString())).thenReturn(query);
454 when(query.setParameter(anyString(), anyString())).thenReturn(query);
455 when(query.uniqueResult()).thenReturn(instance);
457 Call mockCall = PowerMockito.mock(Call.class);
458 when(intentApiService.deleteIntentInstance(any())).thenReturn(mockCall);
459 when(mockCall.execute()).thenReturn(null);
461 Transaction tx = PowerMockito.mock(Transaction.class);
462 when(session.beginTransaction()).thenReturn(tx);
463 Serializable save = PowerMockito.mock(Serializable.class);
464 doNothing().when(session).delete(any());
465 doNothing().when(tx).commit();
467 intentInstanceService.invalidIntentInstance("1");
470 public void queryInstancePerformanceDataTest() throws IOException {
471 CCVPNInstance instance = new CCVPNInstance();
472 instance.setResourceInstanceId("1");
474 InstancePerformance instancePerformance = new InstancePerformance();
475 instancePerformance.setBandwidth(2000);
476 instancePerformance.setMaxBandwidth(20000);
477 instancePerformance.setDate(new Date());
478 Object[] o = {null,instancePerformance};
479 List<Object[]> queryResult= new ArrayList<>();
482 Query query = Mockito.mock(Query.class);
483 when(session.createQuery(anyString())).thenReturn(query);
484 when(query.setParameter(anyString(), anyString())).thenReturn(query);
485 when(query.list()).thenReturn(queryResult);
487 intentInstanceService.queryInstancePerformanceData("1");
493 Call mockCall = PowerMockito.mock(Call.class);
494 when(intentApiService.deleteIntentInstance(any())).thenReturn(mockCall);
495 when(mockCall.execute()).thenReturn(null);
497 Transaction tx = PowerMockito.mock(Transaction.class);
498 when(session.beginTransaction()).thenReturn(tx);
499 Serializable save = PowerMockito.mock(Serializable.class);
500 doNothing().when(session).delete(any());
501 doNothing().when(tx).commit();
503 intentInstanceService.invalidIntentInstance("1");
507 public void activeIntentInstance() throws IOException {
508 CCVPNInstance instance = new CCVPNInstance();
509 instance.setInstanceId("1");
510 instance.setJobId("1");
511 instance.setStatus("1");
513 Query query = Mockito.mock(Query.class);
514 when(session.createQuery(anyString())).thenReturn(query);
515 when(query.setParameter(anyString(), anyString())).thenReturn(query);
516 when(query.uniqueResult()).thenReturn(instance);
519 Call mockCall = PowerMockito.mock(Call.class);
520 JSONObject body = JSONObject.parseObject("{\"jobId\":\"123\"}");
521 Response<JSONObject> response = Response.success(body);
522 Mockito.when(intentApiService.createIntentInstance(any())).thenReturn(mockCall);
523 Mockito.when(mockCall.execute()).thenReturn(response);
525 Transaction tx = Mockito.mock(Transaction.class);
526 Mockito.when(session.beginTransaction()).thenReturn(tx);
527 Serializable save = Mockito.mock(Serializable.class);
528 Mockito.when(session.save(any())).thenReturn(save);
529 Mockito.doNothing().when(tx).commit();
531 intentInstanceService.activeIntentInstance("1");
536 public void queryAccessNodeInfo() throws IOException {
538 Call mockCall = PowerMockito.mock(Call.class);
539 JSONObject body = JSONObject.parseObject("{\n" +
540 " \"network-route\": [\n" +
542 " \"route-id\": \"tranportEp_src_ID_111_1\",\n" +
543 " \"type\": \"LEAF\",\n" +
544 " \"role\": \"3gppTransportEP\",\n" +
545 " \"function\": \"3gppTransportEP\",\n" +
546 " \"ip-address\": \"10.2.3.4\",\n" +
547 " \"prefix-length\": 24,\n" +
548 " \"next-hop\": \"networkId-providerId-10-clientId-0-topologyId-2-nodeId-10.1.1.1-ltpId-1000\",\n" +
549 " \"address-family\": \"ipv4\",\n" +
550 " \"resource-version\": \"1634198223345\"\n" +
553 " \"route-id\": \"tranportEp_src_ID_113_1\",\n" +
554 " \"type\": \"LEAF\",\n" +
555 " \"role\": \"3gppTransportEP\",\n" +
556 " \"function\": \"3gppTransportEP\",\n" +
557 " \"ip-address\": \"10.2.3.4\",\n" +
558 " \"prefix-length\": 24,\n" +
559 " \"next-hop\": \"networkId-providerId-10-clientId-0-topologyId-2-nodeId-10.1.1.3-ltpId-1000\",\n" +
560 " \"address-family\": \"ipv4\",\n" +
561 " \"resource-version\": \"1634198260496\"\n" +
564 " \"route-id\": \"tranportEp_src_ID_111_2\",\n" +
565 " \"type\": \"LEAF\",\n" +
566 " \"role\": \"3gppTransportEP\",\n" +
567 " \"function\": \"3gppTransportEP\",\n" +
568 " \"ip-address\": \"10.2.3.4\",\n" +
569 " \"prefix-length\": 24,\n" +
570 " \"next-hop\": \"networkId-providerId-10-clientId-0-topologyId-2-nodeId-10.1.1.1-ltpId-2000\",\n" +
571 " \"address-family\": \"ipv4\",\n" +
572 " \"resource-version\": \"1634198251534\"\n" +
575 " \"route-id\": \"tranportEp_dst_ID_212_1\",\n" +
576 " \"type\": \"ROOT\",\n" +
577 " \"role\": \"3gppTransportEP\",\n" +
578 " \"function\": \"3gppTransportEP\",\n" +
579 " \"ip-address\": \"10.2.3.4\",\n" +
580 " \"prefix-length\": 24,\n" +
581 " \"next-hop\": \"networkId-providerId-20-clientId-0-topologyId-2-nodeId-10.2.1.2-ltpId-512\",\n" +
582 " \"address-family\": \"ipv4\",\n" +
583 " \"resource-version\": \"1634198274852\"\n" +
587 Response<JSONObject> response = Response.success(body);
588 Mockito.when(intentApiService.queryNetworkRoute()).thenReturn(mockCall);
589 Mockito.when(mockCall.execute()).thenReturn(response);
590 Map<String, Object> result = (Map<String, Object>) intentInstanceService.queryAccessNodeInfo();
591 assertEquals(((List)result.get("accessNodeList")).size(), 3);
595 public void getInstanceStatusTest() {
596 List<CCVPNInstance> queryResult = new ArrayList<>();
597 CCVPNInstance instance = new CCVPNInstance();
598 instance.setInstanceId("id1");
599 instance.setStatus("1");
600 queryResult.add(instance);
602 Query query = Mockito.mock(Query.class);
603 when(session.createQuery(anyString())).thenReturn(query);
604 when(query.setParameter(anyString(), any())).thenReturn(query);
605 when(query.list()).thenReturn(queryResult);
608 JSONObject instanceStatus = intentInstanceService.getInstanceStatus(new JSONArray());
609 assertEquals(instanceStatus.getJSONArray("IntentInstances").getJSONObject(0).getString("id"), "id1");
612 public void formatBandwidthTest() {
614 String bandwidth = intentInstanceService.formatBandwidth("2Gbps");
615 assertEquals(bandwidth, "2000");
618 public void formatCloudPointTest() {
620 String bandwidth = intentInstanceService.formatCloudPoint("Cloud one");
621 assertEquals(bandwidth, "tranportEp_dst_ID_212_1");
624 public void formatAccessPointOneTest() {
625 String bandwidth = intentInstanceService.formatAccessPoint("Access one");
626 assertEquals(bandwidth, "tranportEp_src_ID_111_1");
629 public void formatAccessPointTwoTest() {
630 String bandwidth = intentInstanceService.formatAccessPoint("Access two");
631 assertEquals(bandwidth, "tranportEp_src_ID_111_2");
634 public void formatAccessPointThreeTest() {
635 String bandwidth = intentInstanceService.formatAccessPoint("Access three");
636 assertEquals(bandwidth, "tranportEp_src_ID_113_1");
640 public void addCustomerTest() throws IOException {
642 Call mockCall = PowerMockito.mock(Call.class);
643 Response<Object> response = Response.error(404, new ResponseBody() {
646 public MediaType contentType() {
651 public long contentLength() {
656 public BufferedSource source() {
660 when(intentApiService.queryCustomer(anyString())).thenReturn(mockCall);
661 when(mockCall.execute()).thenReturn(response);
663 Properties properties = new Properties();
664 properties.put("ccvpn.globalCustomerId", "IBNCustomer");
665 properties.put("ccvpn.subscriberName", "IBNCustomer");
666 properties.put("ccvpn.subscriberType", "INFRA");
667 properties.put("ccvpn.serviceType", "IBN");
668 IntentInstanceServiceImpl spy = spy(intentInstanceService);
669 doReturn(properties).when(spy).getProperties();
671 Call mockCall2 = PowerMockito.mock(Call.class);
672 when(intentApiService.addCustomer(anyString(),any())).thenReturn(mockCall2);
675 Mockito.verify(intentApiService,Mockito.times(1)).addCustomer(anyString(),any());
680 public void addSubscriptionTest() throws IOException {
682 Call mockCall = PowerMockito.mock(Call.class);
683 Response<Object> response = Response.error(404, new ResponseBody() {
686 public MediaType contentType() {
691 public long contentLength() {
696 public BufferedSource source() {
700 when(intentApiService.querySubscription(anyString(),anyString())).thenReturn(mockCall);
701 when(mockCall.execute()).thenReturn(response);
703 Properties properties = new Properties();
704 properties.put("ccvpn.globalCustomerId", "IBNCustomer");
705 properties.put("ccvpn.subscriberName", "IBNCustomer");
706 properties.put("ccvpn.subscriberType", "INFRA");
707 properties.put("ccvpn.serviceType", "IBN");
708 IntentInstanceServiceImpl spy = spy(intentInstanceService);
709 doReturn(properties).when(spy).getProperties();
711 Call mockCall2 = PowerMockito.mock(Call.class);
712 when(intentApiService.addSubscription(anyString(),anyString(),any())).thenReturn(mockCall2);
714 spy.addSubscription();
715 Mockito.verify(intentApiService,Mockito.times(1)).addSubscription(anyString(),anyString(),any());
719 public void saveIntentInstanceToAAITest() throws IOException {
720 IntentInstanceServiceImpl spy = spy(intentInstanceService);
721 doNothing().when(spy).addCustomer();
722 doNothing().when(spy).addSubscription();
724 Properties properties = new Properties();
725 properties.put("ccvpn.globalCustomerId", "IBNCustomer");
726 properties.put("ccvpn.subscriberName", "IBNCustomer");
727 properties.put("ccvpn.subscriberType", "INFRA");
728 properties.put("ccvpn.serviceType", "IBN");
729 doReturn(properties).when(spy).getProperties();
731 JSONObject body = new JSONObject();
732 body.put("resource-version",123);
733 Call mockCall = PowerMockito.mock(Call.class);
734 Response<JSONObject> response = Response.success(body);
735 when(intentApiService.queryServiceInstance(anyString(),anyString(),anyString())).thenReturn(mockCall);
736 when(mockCall.execute()).thenReturn(response);
738 CCVPNInstance instance = new CCVPNInstance();
739 instance.setName("name");
740 instance.setInstanceId("id");
742 Call mockCall2 = PowerMockito.mock(Call.class);
743 Response<JSONObject> response2 = Response.success(body);
744 when(intentApiService.saveServiceInstance(anyString(),anyString(),anyString(),any())).thenReturn(mockCall2);
745 when(mockCall2.execute()).thenReturn(response2);
747 spy.saveIntentInstanceToAAI("CCVPN-id",instance);
748 Mockito.verify(intentApiService, Mockito.times(1)).saveServiceInstance(anyString(),anyString(),anyString(),any());
752 public void deleteIntentInstanceToAAITest() throws IOException {
753 IntentInstanceServiceImpl spy = spy(intentInstanceService);
754 doNothing().when(spy).addCustomer();
755 doNothing().when(spy).addSubscription();
757 Properties properties = new Properties();
758 properties.put("ccvpn.globalCustomerId", "IBNCustomer");
759 properties.put("ccvpn.subscriberName", "IBNCustomer");
760 properties.put("ccvpn.subscriberType", "INFRA");
761 properties.put("ccvpn.serviceType", "IBN");
762 doReturn(properties).when(spy).getProperties();
764 JSONObject body = new JSONObject();
765 body.put("resource-version",123);
766 Call mockCall = PowerMockito.mock(Call.class);
767 Response<JSONObject> response = Response.success(body);
768 when(intentApiService.queryServiceInstance(anyString(),anyString(),anyString())).thenReturn(mockCall);
769 when(mockCall.execute()).thenReturn(response);
771 Call mockCall2 = PowerMockito.mock(Call.class);
772 Response<JSONObject> response2 = Response.success(body);
773 when(intentApiService.deleteServiceInstance(anyString(),anyString(),anyString(),anyString())).thenReturn(mockCall2);
774 when(mockCall2.execute()).thenReturn(response2);
776 spy.deleteIntentInstanceToAAI("CCVPN-id");
777 Mockito.verify(intentApiService, Mockito.times(1)).deleteServiceInstance(anyString(),anyString(),anyString(),any());