Add some test code to improve code coverage
[usecase-ui/server.git] / server / src / test / java / org / onap / usecaseui / server / service / lcm / impl / DefaultCustomerServiceTest.java
1 /**
2  * Copyright 2016-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 package org.onap.usecaseui.server.service.lcm.impl;
17
18 import static java.util.Collections.singletonList;
19 import static org.mockito.Matchers.anyObject;
20 import static org.mockito.Matchers.eq;
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.when;
23 import static org.onap.usecaseui.server.util.CallStub.emptyBodyCall;
24 import static org.onap.usecaseui.server.util.CallStub.failedCall;
25 import static org.onap.usecaseui.server.util.CallStub.successfulCall;
26
27 import java.util.List;
28
29 import javax.servlet.http.HttpServletRequest;
30
31 import org.junit.Assert;
32 import org.junit.Test;
33 import org.onap.usecaseui.server.service.lcm.CustomerService;
34 import org.onap.usecaseui.server.service.lcm.domain.aai.AAIService;
35 import org.onap.usecaseui.server.service.lcm.domain.aai.bean.AAICustomer;
36 import org.onap.usecaseui.server.service.lcm.domain.aai.bean.AAICustomerRsp;
37 import org.onap.usecaseui.server.service.lcm.domain.aai.bean.AAIServiceSubscription;
38 import org.onap.usecaseui.server.service.lcm.domain.aai.bean.ServiceSubscriptionRsp;
39 import org.onap.usecaseui.server.service.lcm.domain.aai.exceptions.AAIException;
40
41 import com.alibaba.fastjson.JSONObject;
42
43 import okhttp3.ResponseBody;
44 import retrofit2.Call;
45
46 public class DefaultCustomerServiceTest {
47         
48     @Test
49     public void itCanRetrieveCustomersFromAAI() {
50         List<AAICustomer> customers = singletonList(new AAICustomer("1", "name", "type","version"));
51
52         AAIService aaiService = mock(AAIService.class);
53         AAICustomerRsp rsp = new AAICustomerRsp();
54         rsp.setCustomer(customers);
55         Call<AAICustomerRsp> call = successfulCall(rsp);
56         when(aaiService.listCustomer()).thenReturn(call);
57
58         CustomerService customerService = new DefaultCustomerService(aaiService);
59         Assert.assertSame(customers, customerService.listCustomer());
60     }
61
62     @Test(expected = AAIException.class)
63     public void itWillThrowExceptionWhenAAIIsNotAvailable() {
64         AAIService aaiService = mock(AAIService.class);
65         Call<AAICustomerRsp> call = failedCall("AAI is not available!");
66         when(aaiService.listCustomer()).thenReturn(call);
67
68         CustomerService customerService = new DefaultCustomerService(aaiService);
69         customerService.listCustomer();
70     }
71
72     @Test
73     public void itWillRetrieveEmptyListWhenNoCustomerInAAI() {
74         AAIService aaiService = mock(AAIService.class);
75         Call<AAICustomerRsp> call = emptyBodyCall();
76         when(aaiService.listCustomer()).thenReturn(call);
77
78         CustomerService customerService = new DefaultCustomerService(aaiService);
79         List<AAICustomer> customers = customerService.listCustomer();
80
81         Assert.assertTrue("customers should be empty list.", customers.isEmpty());
82     }
83
84     @Test
85     public void itCanRetrieveServiceSubscriptionsFromAAI() {
86         List<AAIServiceSubscription> serviceSubscriptions = singletonList(new AAIServiceSubscription("service type","resourceVersion"));
87
88         AAIService aaiService = mock(AAIService.class);
89         ServiceSubscriptionRsp rsp = new ServiceSubscriptionRsp();
90         rsp.setServiceSubscriptions(serviceSubscriptions);
91         Call<ServiceSubscriptionRsp> call = successfulCall(rsp);
92         when(aaiService.listServiceSubscriptions("1")).thenReturn(call);
93
94         CustomerService customerService = new DefaultCustomerService(aaiService);
95         Assert.assertSame(serviceSubscriptions, customerService.listServiceSubscriptions("1"));
96     }
97
98     @Test(expected = AAIException.class)
99     public void listServiceSubscriptionsWillThrowExceptionWhenAAIIsNotAvailable() {
100         AAIService aaiService = mock(AAIService.class);
101         Call<ServiceSubscriptionRsp> call = failedCall("AAI is not available!");
102         when(aaiService.listServiceSubscriptions("1")).thenReturn(call);
103
104         CustomerService customerService = new DefaultCustomerService(aaiService);
105         customerService.listServiceSubscriptions("1");
106     }
107
108     @Test
109     public void itWillRetrieveEmptyListWhenNoServiceSubscriptionsInAAI() {
110         AAIService aaiService = mock(AAIService.class);
111         Call<ServiceSubscriptionRsp> call = emptyBodyCall();
112         when(aaiService.listServiceSubscriptions("1")).thenReturn(call);
113
114         CustomerService customerService = new DefaultCustomerService(aaiService);
115         List<AAIServiceSubscription> serviceSubscriptions = customerService.listServiceSubscriptions("1");
116
117         Assert.assertTrue("service subscription should be empty list.", serviceSubscriptions.isEmpty());
118     }
119     
120     @Test
121     public void itCanCreateOrUpdateCustomer(){
122         HttpServletRequest request = mock(HttpServletRequest.class);
123         AAIService aaiService = mock(AAIService.class);
124         CustomerService customerService = new DefaultCustomerService(aaiService);
125         String customerId="1";
126         ResponseBody result=null;
127         when(aaiService.createOrUpdateCustomer(eq(customerId),anyObject())).thenReturn(successfulCall(result));
128         customerService.createOrUpdateCustomer(request, customerId);
129     }
130     
131     @Test
132     public void createOrUpdateCustomerWillThrowExceptionWhenVFCIsNotAvailable(){
133         HttpServletRequest request = mock(HttpServletRequest.class);
134         AAIService aaiService = mock(AAIService.class);
135         CustomerService customerService = new DefaultCustomerService(aaiService);
136         String customerId="1";
137         when(aaiService.createOrUpdateCustomer(eq(customerId),anyObject())).thenReturn(failedCall("VFC is not available!"));
138         customerService.createOrUpdateCustomer(request, customerId);
139     }
140     
141     @Test
142     public void createOrUpdateCustomerWillThrowExceptionWhenVFCResponseError(){
143         HttpServletRequest request = mock(HttpServletRequest.class);
144         AAIService aaiService = mock(AAIService.class);
145         CustomerService customerService = new DefaultCustomerService(aaiService);
146         String customerId="1";
147         when(aaiService.createOrUpdateCustomer(eq(customerId),anyObject())).thenReturn(emptyBodyCall());
148         customerService.createOrUpdateCustomer(request, customerId);
149     }
150     
151     @Test
152     public void itCanGetCustomerById(){
153         AAIService aaiService = mock(AAIService.class);
154         CustomerService customerService = new DefaultCustomerService(aaiService);
155         String customerId="1";
156         AAICustomer customer = new AAICustomer(customerId, customerId, customerId,customerId);
157         when(aaiService.getCustomerById(eq(customerId))).thenReturn(successfulCall(customer));
158         customerService.getCustomerById(customerId);
159     }
160     
161     @Test
162     public void getCustomerByIdWillThrowExceptionWhenVFCIsNotAvailable(){
163         AAIService aaiService = mock(AAIService.class);
164         CustomerService customerService = new DefaultCustomerService(aaiService);
165         String customerId="1";
166         when(aaiService.getCustomerById(eq(customerId))).thenReturn(failedCall("VFC is not available!"));
167         customerService.getCustomerById(customerId);
168     }
169     
170     @Test
171     public void getCustomerByIdWillThrowExceptionWhenVFCResponseError(){
172         AAIService aaiService = mock(AAIService.class);
173         CustomerService customerService = new DefaultCustomerService(aaiService);
174         String customerId="1";
175         when(aaiService.getCustomerById(eq(customerId))).thenReturn(emptyBodyCall());
176         customerService.getCustomerById(customerId);
177     }
178     
179     @Test
180     public void itCanDeleteCustomerById(){
181         AAIService aaiService = mock(AAIService.class);
182         CustomerService customerService = new DefaultCustomerService(aaiService);
183         String customerId="1";
184         String resourceVersion="1412934";
185         ResponseBody result= null;
186         when(aaiService.deleteCustomer(eq(customerId),eq(resourceVersion))).thenReturn(successfulCall(result));
187         customerService.deleteCustomer(customerId,resourceVersion);
188     }
189     
190     @Test
191     public void deleteCustomerByIdWillThrowExceptionWhenVFCIsNotAvailable(){
192         AAIService aaiService = mock(AAIService.class);
193         CustomerService customerService = new DefaultCustomerService(aaiService);
194          String resourceVersion="1412934";
195         String customerId="1";
196         when(aaiService.deleteCustomer(eq(customerId),eq(resourceVersion))).thenReturn(failedCall("VFC is not available!"));
197         customerService.deleteCustomer(customerId,resourceVersion);
198     }
199     
200     @Test
201     public void deleteCustomerByIdWillThrowExceptionWhenVFCResponseError(){
202         AAIService aaiService = mock(AAIService.class);
203         CustomerService customerService = new DefaultCustomerService(aaiService);
204          String resourceVersion="1412934";
205         String customerId="1";
206         when(aaiService.deleteCustomer(eq(customerId),eq(resourceVersion))).thenReturn(emptyBodyCall());
207         customerService.deleteCustomer(customerId,resourceVersion);
208     }
209     
210     @Test
211     public void itCreateOrUpdateServiceType(){
212         HttpServletRequest request2 = mock(HttpServletRequest.class);
213         AAIService aaiService = mock(AAIService.class);
214         CustomerService customerService = new DefaultCustomerService(aaiService);
215         String serviceType="1";
216         String customerId="demo";
217         ResponseBody result=null;
218         when(aaiService.createOrUpdateServiceType(eq(customerId),eq(serviceType),anyObject())).thenReturn(successfulCall(result));
219         customerService.createOrUpdateServiceType(request2, serviceType,customerId);
220     }
221     
222     @Test
223     public void createOrUpdateServiceTypeWillThrowExceptionWhenVFCIsNotAvailable(){
224         HttpServletRequest request2 = mock(HttpServletRequest.class);
225         String serviceType="1";
226         String customerId="demo";
227         AAIService aaiService = mock(AAIService.class);
228         CustomerService customerService = new DefaultCustomerService(aaiService);
229         when(aaiService.createOrUpdateServiceType(eq(customerId),eq(serviceType),anyObject())).thenReturn(failedCall("VFC is not available!"));
230         customerService.createOrUpdateServiceType(request2, serviceType,customerId);
231     }
232     
233     @Test
234     public void createOrUpdateServiceTypeWillThrowExceptionWhenVFCResponseError(){
235         HttpServletRequest request2 = mock(HttpServletRequest.class);
236         String serviceType="1";
237         String customerId="demo";
238         AAIService aaiService = mock(AAIService.class);
239         CustomerService customerService = new DefaultCustomerService(aaiService);
240         when(aaiService.createOrUpdateServiceType(eq(customerId),eq(serviceType),anyObject())).thenReturn(emptyBodyCall());
241         customerService.createOrUpdateServiceType(request2, serviceType,customerId);
242     }
243     
244     @Test
245     public void itCanDeleteServiceType(){
246         AAIService aaiService = mock(AAIService.class);
247         CustomerService customerService = new DefaultCustomerService(aaiService);
248         String serviceTypeName="1";
249         String resourceVersion="214341235";
250         String coustomerId="1";
251         ResponseBody result=null;
252         when(aaiService.deleteServiceType(eq(coustomerId),eq(serviceTypeName),eq(resourceVersion))).thenReturn(successfulCall(result));
253         customerService.deleteServiceType(coustomerId,serviceTypeName,resourceVersion);
254     }
255     
256     @Test
257     public void deleteServiceTypeWillThrowExceptionWhenVFCIsNotAvailable(){
258         AAIService aaiService = mock(AAIService.class);
259         CustomerService customerService = new DefaultCustomerService(aaiService);
260         String serviceTypeName="1";
261         String resourceVersion="214341235";
262         String coustomerId="1";
263         when(aaiService.deleteServiceType(eq(coustomerId),eq(serviceTypeName),eq(resourceVersion))).thenReturn(failedCall("VFC is not available!"));
264         customerService.deleteServiceType(coustomerId,serviceTypeName,resourceVersion);
265     }
266     
267     @Test
268     public void deleteServiceTypeByIdWillThrowExceptionWhenVFCResponseError(){
269         AAIService aaiService = mock(AAIService.class);
270         CustomerService customerService = new DefaultCustomerService(aaiService);
271         String serviceTypeName="1";
272         String resourceVersion="214341235";
273         String coustomerId="1";
274         when(aaiService.deleteServiceType(eq(coustomerId),eq(serviceTypeName),eq(resourceVersion))).thenReturn(emptyBodyCall());
275         customerService.deleteServiceType(coustomerId,serviceTypeName,resourceVersion);
276     }
277     
278     @Test
279     public void itCanGetServiceTypeById(){
280         AAIService aaiService = mock(AAIService.class);
281         CustomerService customerService = new DefaultCustomerService(aaiService);
282         String serviceTypeName="1";
283         String resourceVersion="214341235";
284         AAIServiceSubscription aaIServiceSubscription = new AAIServiceSubscription(serviceTypeName,resourceVersion);
285         when(aaiService.getServiceTypeById(eq(serviceTypeName),eq(resourceVersion))).thenReturn(successfulCall(aaIServiceSubscription));
286         customerService.getServiceTypeById(serviceTypeName,resourceVersion);
287     }
288     
289     @Test
290     public void getServiceTypeByIdWillThrowExceptionWhenVFCIsNotAvailable(){
291         AAIService aaiService = mock(AAIService.class);
292         CustomerService customerService = new DefaultCustomerService(aaiService);
293         String serviceTypeName="1";
294         String resourceVersion="214341235";
295         when(aaiService.getServiceTypeById(eq(serviceTypeName),eq(resourceVersion))).thenReturn(failedCall("VFC is not available!"));
296         customerService.getServiceTypeById(serviceTypeName,resourceVersion);
297     }
298     
299     @Test
300     public void getServiceTypeByIdWillThrowExceptionWhenVFCResponseError(){
301         AAIService aaiService = mock(AAIService.class);
302         CustomerService customerService = new DefaultCustomerService(aaiService);
303         String serviceTypeName="1";
304         String resourceVersion="214341235";
305         when(aaiService.getServiceTypeById(eq(serviceTypeName),eq(resourceVersion))).thenReturn(emptyBodyCall());
306         customerService.getServiceTypeById(serviceTypeName,resourceVersion);
307     }
308 }