2 * Copyright 2016-2017 ZTE Corporation.
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.lcm.impl;
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;
27 import java.util.List;
29 import javax.servlet.http.HttpServletRequest;
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;
41 import com.alibaba.fastjson.JSONObject;
43 import okhttp3.ResponseBody;
44 import retrofit2.Call;
46 public class DefaultCustomerServiceTest {
49 public void itCanRetrieveCustomersFromAAI() {
50 List<AAICustomer> customers = singletonList(new AAICustomer("1", "name", "type","version"));
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);
58 CustomerService customerService = new DefaultCustomerService(aaiService);
59 Assert.assertSame(customers, customerService.listCustomer());
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);
68 CustomerService customerService = new DefaultCustomerService(aaiService);
69 customerService.listCustomer();
73 public void itWillRetrieveEmptyListWhenNoCustomerInAAI() {
74 AAIService aaiService = mock(AAIService.class);
75 Call<AAICustomerRsp> call = emptyBodyCall();
76 when(aaiService.listCustomer()).thenReturn(call);
78 CustomerService customerService = new DefaultCustomerService(aaiService);
79 List<AAICustomer> customers = customerService.listCustomer();
81 Assert.assertTrue("customers should be empty list.", customers.isEmpty());
85 public void itCanRetrieveServiceSubscriptionsFromAAI() {
86 List<AAIServiceSubscription> serviceSubscriptions = singletonList(new AAIServiceSubscription("service type","resourceVersion"));
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);
94 CustomerService customerService = new DefaultCustomerService(aaiService);
95 Assert.assertSame(serviceSubscriptions, customerService.listServiceSubscriptions("1"));
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);
104 CustomerService customerService = new DefaultCustomerService(aaiService);
105 customerService.listServiceSubscriptions("1");
109 public void itWillRetrieveEmptyListWhenNoServiceSubscriptionsInAAI() {
110 DefaultCustomerService dc = new DefaultCustomerService();
111 AAIService aaiService = mock(AAIService.class);
112 Call<ServiceSubscriptionRsp> call = emptyBodyCall();
113 when(aaiService.listServiceSubscriptions("1")).thenReturn(call);
115 CustomerService customerService = new DefaultCustomerService(aaiService);
116 List<AAIServiceSubscription> serviceSubscriptions = customerService.listServiceSubscriptions("1");
118 Assert.assertTrue("service subscription should be empty list.", serviceSubscriptions.isEmpty());
122 public void itCanCreateOrUpdateCustomer(){
123 HttpServletRequest request = mock(HttpServletRequest.class);
124 AAIService aaiService = mock(AAIService.class);
125 CustomerService customerService = new DefaultCustomerService(aaiService);
126 String customerId="1";
127 ResponseBody result=null;
128 when(aaiService.createOrUpdateCustomer(eq(customerId),anyObject())).thenReturn(successfulCall(result));
129 customerService.createOrUpdateCustomer(request, customerId);
133 public void createOrUpdateCustomerWillThrowExceptionWhenVFCIsNotAvailable(){
134 HttpServletRequest request = mock(HttpServletRequest.class);
135 AAIService aaiService = mock(AAIService.class);
136 CustomerService customerService = new DefaultCustomerService(aaiService);
137 String customerId="1";
138 when(aaiService.createOrUpdateCustomer(eq(customerId),anyObject())).thenReturn(failedCall("VFC is not available!"));
139 customerService.createOrUpdateCustomer(request, customerId);
143 public void createOrUpdateCustomerWillThrowExceptionWhenVFCResponseError(){
144 HttpServletRequest request = mock(HttpServletRequest.class);
145 AAIService aaiService = mock(AAIService.class);
146 CustomerService customerService = new DefaultCustomerService(aaiService);
147 String customerId="1";
148 when(aaiService.createOrUpdateCustomer(eq(customerId),anyObject())).thenReturn(emptyBodyCall());
149 customerService.createOrUpdateCustomer(request, customerId);
153 public void itCanGetCustomerById(){
154 AAIService aaiService = mock(AAIService.class);
155 CustomerService customerService = new DefaultCustomerService(aaiService);
156 String customerId="1";
157 AAICustomer customer = new AAICustomer(customerId, customerId, customerId,customerId);
158 when(aaiService.getCustomerById(eq(customerId))).thenReturn(successfulCall(customer));
159 customerService.getCustomerById(customerId);
163 public void getCustomerByIdWillThrowExceptionWhenVFCIsNotAvailable(){
164 AAIService aaiService = mock(AAIService.class);
165 CustomerService customerService = new DefaultCustomerService(aaiService);
166 String customerId="1";
167 when(aaiService.getCustomerById(eq(customerId))).thenReturn(failedCall("VFC is not available!"));
168 customerService.getCustomerById(customerId);
172 public void getCustomerByIdWillThrowExceptionWhenVFCResponseError(){
173 AAIService aaiService = mock(AAIService.class);
174 CustomerService customerService = new DefaultCustomerService(aaiService);
175 String customerId="1";
176 when(aaiService.getCustomerById(eq(customerId))).thenReturn(emptyBodyCall());
177 customerService.getCustomerById(customerId);
181 public void itCanDeleteCustomerById(){
182 AAIService aaiService = mock(AAIService.class);
183 CustomerService customerService = new DefaultCustomerService(aaiService);
184 String customerId="1";
185 String resourceVersion="1412934";
186 ResponseBody result= null;
187 when(aaiService.deleteCustomer(eq(customerId),eq(resourceVersion))).thenReturn(successfulCall(result));
188 customerService.deleteCustomer(customerId,resourceVersion);
192 public void deleteCustomerByIdWillThrowExceptionWhenVFCIsNotAvailable(){
193 AAIService aaiService = mock(AAIService.class);
194 CustomerService customerService = new DefaultCustomerService(aaiService);
195 String resourceVersion="1412934";
196 String customerId="1";
197 when(aaiService.deleteCustomer(eq(customerId),eq(resourceVersion))).thenReturn(failedCall("VFC is not available!"));
198 customerService.deleteCustomer(customerId,resourceVersion);
202 public void deleteCustomerByIdWillThrowExceptionWhenVFCResponseError(){
203 AAIService aaiService = mock(AAIService.class);
204 CustomerService customerService = new DefaultCustomerService(aaiService);
205 String resourceVersion="1412934";
206 String customerId="1";
207 when(aaiService.deleteCustomer(eq(customerId),eq(resourceVersion))).thenReturn(emptyBodyCall());
208 customerService.deleteCustomer(customerId,resourceVersion);
212 public void itCreateOrUpdateServiceType(){
213 HttpServletRequest request2 = mock(HttpServletRequest.class);
214 AAIService aaiService = mock(AAIService.class);
215 CustomerService customerService = new DefaultCustomerService(aaiService);
216 String serviceType="1";
217 String customerId="demo";
218 ResponseBody result=null;
219 when(aaiService.createOrUpdateServiceType(eq(customerId),eq(serviceType),anyObject())).thenReturn(successfulCall(result));
220 customerService.createOrUpdateServiceType(request2, serviceType,customerId);
224 public void createOrUpdateServiceTypeWillThrowExceptionWhenVFCIsNotAvailable(){
225 HttpServletRequest request2 = mock(HttpServletRequest.class);
226 String serviceType="1";
227 String customerId="demo";
228 AAIService aaiService = mock(AAIService.class);
229 CustomerService customerService = new DefaultCustomerService(aaiService);
230 when(aaiService.createOrUpdateServiceType(eq(customerId),eq(serviceType),anyObject())).thenReturn(failedCall("VFC is not available!"));
231 customerService.createOrUpdateServiceType(request2, serviceType,customerId);
235 public void createOrUpdateServiceTypeWillThrowExceptionWhenVFCResponseError(){
236 HttpServletRequest request2 = mock(HttpServletRequest.class);
237 String serviceType="1";
238 String customerId="demo";
239 AAIService aaiService = mock(AAIService.class);
240 CustomerService customerService = new DefaultCustomerService(aaiService);
241 when(aaiService.createOrUpdateServiceType(eq(customerId),eq(serviceType),anyObject())).thenReturn(emptyBodyCall());
242 customerService.createOrUpdateServiceType(request2, serviceType,customerId);
246 public void itCanDeleteServiceType(){
247 AAIService aaiService = mock(AAIService.class);
248 CustomerService customerService = new DefaultCustomerService(aaiService);
249 String serviceTypeName="1";
250 String resourceVersion="214341235";
251 String coustomerId="1";
252 ResponseBody result=null;
253 when(aaiService.deleteServiceType(eq(coustomerId),eq(serviceTypeName),eq(resourceVersion))).thenReturn(successfulCall(result));
254 customerService.deleteServiceType(coustomerId,serviceTypeName,resourceVersion);
258 public void deleteServiceTypeWillThrowExceptionWhenVFCIsNotAvailable(){
259 AAIService aaiService = mock(AAIService.class);
260 CustomerService customerService = new DefaultCustomerService(aaiService);
261 String serviceTypeName="1";
262 String resourceVersion="214341235";
263 String coustomerId="1";
264 when(aaiService.deleteServiceType(eq(coustomerId),eq(serviceTypeName),eq(resourceVersion))).thenReturn(failedCall("VFC is not available!"));
265 customerService.deleteServiceType(coustomerId,serviceTypeName,resourceVersion);
269 public void deleteServiceTypeByIdWillThrowExceptionWhenVFCResponseError(){
270 AAIService aaiService = mock(AAIService.class);
271 CustomerService customerService = new DefaultCustomerService(aaiService);
272 String serviceTypeName="1";
273 String resourceVersion="214341235";
274 String coustomerId="1";
275 when(aaiService.deleteServiceType(eq(coustomerId),eq(serviceTypeName),eq(resourceVersion))).thenReturn(emptyBodyCall());
276 customerService.deleteServiceType(coustomerId,serviceTypeName,resourceVersion);
280 public void itCanGetServiceTypeById(){
281 AAIService aaiService = mock(AAIService.class);
282 CustomerService customerService = new DefaultCustomerService(aaiService);
283 String serviceTypeName="1";
284 String resourceVersion="214341235";
285 AAIServiceSubscription aaIServiceSubscription = new AAIServiceSubscription(serviceTypeName,resourceVersion);
286 when(aaiService.getServiceTypeById(eq(serviceTypeName),eq(resourceVersion))).thenReturn(successfulCall(aaIServiceSubscription));
287 customerService.getServiceTypeById(serviceTypeName,resourceVersion);
291 public void getServiceTypeByIdWillThrowExceptionWhenVFCIsNotAvailable(){
292 AAIService aaiService = mock(AAIService.class);
293 CustomerService customerService = new DefaultCustomerService(aaiService);
294 String serviceTypeName="1";
295 String resourceVersion="214341235";
296 when(aaiService.getServiceTypeById(eq(serviceTypeName),eq(resourceVersion))).thenReturn(failedCall("VFC is not available!"));
297 customerService.getServiceTypeById(serviceTypeName,resourceVersion);
301 public void getServiceTypeByIdWillThrowExceptionWhenVFCResponseError(){
302 AAIService aaiService = mock(AAIService.class);
303 CustomerService customerService = new DefaultCustomerService(aaiService);
304 String serviceTypeName="1";
305 String resourceVersion="214341235";
306 when(aaiService.getServiceTypeById(eq(serviceTypeName),eq(resourceVersion))).thenReturn(emptyBodyCall());
307 customerService.getServiceTypeById(serviceTypeName,resourceVersion);