19ca5dbbb8b9364138be600cb71ca45dac5efcfe
[usecase-ui/server.git] /
1 /*
2  * Copyright (C) 2019 CMCC, 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.nsmf.impl;
17
18 import static org.mockito.Mockito.mock;
19 import static org.mockito.Mockito.when;
20 import static org.onap.usecaseui.server.util.CallStub.failedCall;
21 import static org.onap.usecaseui.server.util.CallStub.successfulCall;
22
23 import com.alibaba.fastjson.JSONObject;
24 import java.util.ArrayList;
25 import java.util.List;
26 import okhttp3.RequestBody;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.onap.usecaseui.server.bean.nsmf.monitor.ServiceInfo;
30 import org.onap.usecaseui.server.bean.nsmf.monitor.ServiceList;
31 import org.onap.usecaseui.server.constant.nsmf.NsmfParamConstant;
32 import org.onap.usecaseui.server.service.slicingdomain.aai.AAISliceService;
33 import org.onap.usecaseui.server.service.slicingdomain.kpi.KpiSliceService;
34 import org.onap.usecaseui.server.service.slicingdomain.kpi.bean.KpiTotalBandwidth;
35 import org.onap.usecaseui.server.service.slicingdomain.kpi.bean.KpiTotalTraffic;
36 import org.onap.usecaseui.server.service.slicingdomain.kpi.bean.KpiUserNumber;
37
38 public class ResourceMonitorServiceImplTest {
39
40     ResourceMonitorServiceImpl resourceMonitorService = null;
41     KpiSliceService kpiSliceService = null;
42
43     @Before
44     public void before() throws Exception {
45         kpiSliceService = mock(KpiSliceService.class);
46         resourceMonitorService = new ResourceMonitorServiceImpl(kpiSliceService);
47     }
48
49     @Test
50     public void itCanInitConfig() {
51         resourceMonitorService.initConfig();
52     }
53
54     @Test
55     public void itCanQuerySlicingUsageTraffic() {
56         ServiceList serviceList = new ServiceList();
57         List<ServiceInfo> serviceInfoList = new ArrayList<>();
58         ServiceInfo serviceInfo = new ServiceInfo();
59         serviceInfo.setServiceId("123e-456t-567t-yui8");
60         serviceInfoList.add(serviceInfo);
61         serviceList.setServiceInfoList(serviceInfoList);
62         String queryTimestamp = "1577071879000";
63
64         KpiTotalTraffic kpiTotalTraffic = new KpiTotalTraffic();
65         RequestBody body = null;
66         when(kpiSliceService.listTotalTraffic(body)).thenReturn(successfulCall(kpiTotalTraffic));
67         resourceMonitorService.querySlicingUsageTraffic(queryTimestamp, serviceList);
68     }
69
70     @Test
71     public void querySlicingUsageTrafficWithThrowsException() {
72         ServiceList serviceList = new ServiceList();
73         List<ServiceInfo> serviceInfoList = new ArrayList<>();
74         ServiceInfo serviceInfo = new ServiceInfo();
75         serviceInfo.setServiceId("123e-456t-567t-yui8");
76         serviceInfoList.add(serviceInfo);
77         serviceList.setServiceInfoList(serviceInfoList);
78         String queryTimestamp = "1577071879000";
79
80         RequestBody body = null;
81         when(kpiSliceService.listTotalTraffic(body)).thenReturn(failedCall("kpi is not exist!"));
82         resourceMonitorService.querySlicingUsageTraffic(queryTimestamp, serviceList);
83     }
84
85     @Test
86     public void itCanQuerySlicingOnlineUserNumber() {
87         ServiceList serviceList = new ServiceList();
88         List<ServiceInfo> serviceInfoList = new ArrayList<>();
89         ServiceInfo serviceInfo = new ServiceInfo();
90         serviceInfo.setServiceId("123e-456t-567t-yui8");
91         serviceInfoList.add(serviceInfo);
92         serviceList.setServiceInfoList(serviceInfoList);
93         String queryTimestamp = "1577071879000";
94
95         KpiUserNumber kpiUserNumber = new KpiUserNumber();
96         RequestBody body = null;
97         when(kpiSliceService.listUserNumber(body)).thenReturn(successfulCall(kpiUserNumber));
98         resourceMonitorService.querySlicingOnlineUserNumber(queryTimestamp, serviceList);
99     }
100
101     @Test
102     public void querySlicingOnlineUserNumberWithThrowsException() {
103         ServiceList serviceList = new ServiceList();
104         List<ServiceInfo> serviceInfoList = new ArrayList<>();
105         ServiceInfo serviceInfo = new ServiceInfo();
106         serviceInfo.setServiceId("123e-456t-567t-yui8");
107         serviceInfoList.add(serviceInfo);
108         serviceList.setServiceInfoList(serviceInfoList);
109         String queryTimestamp = "1577071879000";
110
111         RequestBody body = null;
112         when(kpiSliceService.listUserNumber(body)).thenReturn(failedCall("kpi is not exist!"));
113         resourceMonitorService.querySlicingOnlineUserNumber(queryTimestamp, serviceList);
114     }
115
116     @Test
117     public void itCanQuerySlicingTotalBandwidth() {
118         ServiceList serviceList = new ServiceList();
119         List<ServiceInfo> serviceInfoList = new ArrayList<>();
120         ServiceInfo serviceInfo = new ServiceInfo();
121         serviceInfo.setServiceId("123e-456t-567t-yui8");
122         serviceInfoList.add(serviceInfo);
123         serviceList.setServiceInfoList(serviceInfoList);
124         String queryTimestamp = "1577071879000";
125
126         KpiTotalBandwidth kpiTotalBandwidth = new KpiTotalBandwidth();
127         RequestBody body = null;
128         when(kpiSliceService.listTotalBandwidth(body)).thenReturn(successfulCall(kpiTotalBandwidth));
129         resourceMonitorService.querySlicingTotalBandwidth(queryTimestamp, serviceList);
130     }
131
132     @Test
133     public void querySlicingTotalBandwidthWithThrowsException() {
134         ServiceList serviceList = new ServiceList();
135         List<ServiceInfo> serviceInfoList = new ArrayList<>();
136         ServiceInfo serviceInfo = new ServiceInfo();
137         serviceInfo.setServiceId("123e-456t-567t-yui8");
138         serviceInfoList.add(serviceInfo);
139         serviceList.setServiceInfoList(serviceInfoList);
140         String queryTimestamp = "1577071879000";
141
142         RequestBody body = null;
143         when(kpiSliceService.listTotalBandwidth(body)).thenReturn(failedCall("kpi is not exist!"));
144         resourceMonitorService.querySlicingTotalBandwidth(queryTimestamp, serviceList);
145     }
146 }