aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/test/java/org/onap/usecaseui/server/service/lcm/impl/DefaultCustomerServiceTest.java
blob: 7c6b101de4270341c840a8ed36eb9b65901ccf28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/**
 * Copyright 2016-2017 ZTE Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onap.usecaseui.server.service.lcm.impl;

import static java.util.Collections.singletonList;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.onap.usecaseui.server.util.CallStub.emptyBodyCall;
import static org.onap.usecaseui.server.util.CallStub.failedCall;
import static org.onap.usecaseui.server.util.CallStub.successfulCall;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.junit.Assert;
import org.junit.Test;
import org.onap.usecaseui.server.service.lcm.CustomerService;
import org.onap.usecaseui.server.service.lcm.domain.aai.AAIService;
import org.onap.usecaseui.server.service.lcm.domain.aai.bean.AAICustomer;
import org.onap.usecaseui.server.service.lcm.domain.aai.bean.AAICustomerRsp;
import org.onap.usecaseui.server.service.lcm.domain.aai.bean.AAIServiceSubscription;
import org.onap.usecaseui.server.service.lcm.domain.aai.bean.ServiceSubscriptionRsp;
import org.onap.usecaseui.server.service.lcm.domain.aai.exceptions.AAIException;

import com.alibaba.fastjson.JSONObject;

import okhttp3.ResponseBody;
import retrofit2.Call;

public class DefaultCustomerServiceTest {
	
    @Test
    public void itCanRetrieveCustomersFromAAI() {
        List<AAICustomer> customers = singletonList(new AAICustomer("1", "name", "type","version"));

        AAIService aaiService = mock(AAIService.class);
        AAICustomerRsp rsp = new AAICustomerRsp();
        rsp.setCustomer(customers);
        Call<AAICustomerRsp> call = successfulCall(rsp);
        when(aaiService.listCustomer()).thenReturn(call);

        CustomerService customerService = new DefaultCustomerService(aaiService);
        Assert.assertSame(customers, customerService.listCustomer());
    }

    @Test(expected = AAIException.class)
    public void itWillThrowExceptionWhenAAIIsNotAvailable() {
        AAIService aaiService = mock(AAIService.class);
        Call<AAICustomerRsp> call = failedCall("AAI is not available!");
        when(aaiService.listCustomer()).thenReturn(call);

        CustomerService customerService = new DefaultCustomerService(aaiService);
        customerService.listCustomer();
    }

    @Test
    public void itWillRetrieveEmptyListWhenNoCustomerInAAI() {
        AAIService aaiService = mock(AAIService.class);
        Call<AAICustomerRsp> call = emptyBodyCall();
        when(aaiService.listCustomer()).thenReturn(call);

        CustomerService customerService = new DefaultCustomerService(aaiService);
        List<AAICustomer> customers = customerService.listCustomer();

        Assert.assertTrue("customers should be empty list.", customers.isEmpty());
    }

    @Test
    public void itCanRetrieveServiceSubscriptionsFromAAI() {
        List<AAIServiceSubscription> serviceSubscriptions = singletonList(new AAIServiceSubscription("service type","resourceVersion"));

        AAIService aaiService = mock(AAIService.class);
        ServiceSubscriptionRsp rsp = new ServiceSubscriptionRsp();
        rsp.setServiceSubscriptions(serviceSubscriptions);
        Call<ServiceSubscriptionRsp> call = successfulCall(rsp);
        when(aaiService.listServiceSubscriptions("1")).thenReturn(call);

        CustomerService customerService = new DefaultCustomerService(aaiService);
        Assert.assertSame(serviceSubscriptions, customerService.listServiceSubscriptions("1"));
    }

    @Test(expected = AAIException.class)
    public void listServiceSubscriptionsWillThrowExceptionWhenAAIIsNotAvailable() {
        AAIService aaiService = mock(AAIService.class);
        Call<ServiceSubscriptionRsp> call = failedCall("AAI is not available!");
        when(aaiService.listServiceSubscriptions("1")).thenReturn(call);

        CustomerService customerService = new DefaultCustomerService(aaiService);
        customerService.listServiceSubscriptions("1");
    }

    @Test
    public void itWillRetrieveEmptyListWhenNoServiceSubscriptionsInAAI() {
        AAIService aaiService = mock(AAIService.class);
        Call<ServiceSubscriptionRsp> call = emptyBodyCall();
        when(aaiService.listServiceSubscriptions("1")).thenReturn(call);

        CustomerService customerService = new DefaultCustomerService(aaiService);
        List<AAIServiceSubscription> serviceSubscriptions = customerService.listServiceSubscriptions("1");

        Assert.assertTrue("service subscription should be empty list.", serviceSubscriptions.isEmpty());
    }
    
    @Test
    public void itCanCreateOrUpdateCustomer(){
    	HttpServletRequest request = mock(HttpServletRequest.class);
        AAIService aaiService = mock(AAIService.class);
        CustomerService customerService = new DefaultCustomerService(aaiService);
        String customerId="1";
        ResponseBody result=null;
        when(aaiService.createOrUpdateCustomer(eq(customerId),anyObject())).thenReturn(successfulCall(result));
        customerService.createOrUpdateCustomer(request, customerId);
    }
    
    @Test
    public void createOrUpdateCustomerWillThrowExceptionWhenVFCIsNotAvailable(){
    	HttpServletRequest request = mock(HttpServletRequest.class);
        AAIService aaiService = mock(AAIService.class);
        CustomerService customerService = new DefaultCustomerService(aaiService);
        String customerId="1";
        when(aaiService.createOrUpdateCustomer(eq(customerId),anyObject())).thenReturn(failedCall("VFC is not available!"));
        customerService.createOrUpdateCustomer(request, customerId);
    }
    
    @Test
    public void createOrUpdateCustomerWillThrowExceptionWhenVFCResponseError(){
    	HttpServletRequest request = mock(HttpServletRequest.class);
        AAIService aaiService = mock(AAIService.class);
        CustomerService customerService = new DefaultCustomerService(aaiService);
        String customerId="1";
        when(aaiService.createOrUpdateCustomer(eq(customerId),anyObject())).thenReturn(emptyBodyCall());
        customerService.createOrUpdateCustomer(request, customerId);
    }
    
    @Test
    public void itCanGetCustomerById(){
        AAIService aaiService = mock(AAIService.class);
        CustomerService customerService = new DefaultCustomerService(aaiService);
        String customerId="1";
        AAICustomer customer = new AAICustomer(customerId, customerId, customerId,customerId);
        when(aaiService.getCustomerById(eq(customerId))).thenReturn(successfulCall(customer));
        customerService.getCustomerById(customerId);
    }
    
    @Test
    public void getCustomerByIdWillThrowExceptionWhenVFCIsNotAvailable(){
        AAIService aaiService = mock(AAIService.class);
        CustomerService customerService = new DefaultCustomerService(aaiService);
        String customerId="1";
        when(aaiService.getCustomerById(eq(customerId))).thenReturn(failedCall("VFC is not available!"));
        customerService.getCustomerById(customerId);
    }
    
    @Test
    public void getCustomerByIdWillThrowExceptionWhenVFCResponseError(){
        AAIService aaiService = mock(AAIService.class);
        CustomerService customerService = new DefaultCustomerService(aaiService);
        String customerId="1";
        when(aaiService.getCustomerById(eq(customerId))).thenReturn(emptyBodyCall());
        customerService.getCustomerById(customerId);
    }
    
    @Test
    public void itCanDeleteCustomerById(){
        AAIService aaiService = mock(AAIService.class);
        CustomerService customerService = new DefaultCustomerService(aaiService);
        String customerId="1";
        String resourceVersion="1412934";
        ResponseBody result= null;
        when(aaiService.deleteCustomer(eq(customerId),eq(resourceVersion))).thenReturn(successfulCall(result));
        customerService.deleteCustomer(customerId,resourceVersion);
    }
    
    @Test
    public void deleteCustomerByIdWillThrowExceptionWhenVFCIsNotAvailable(){
        AAIService aaiService = mock(AAIService.class);
        CustomerService customerService = new DefaultCustomerService(aaiService);
    	 String resourceVersion="1412934";
        String customerId="1";
        when(aaiService.deleteCustomer(eq(customerId),eq(resourceVersion))).thenReturn(failedCall("VFC is not available!"));
        customerService.deleteCustomer(customerId,resourceVersion);
    }
    
    @Test
    public void deleteCustomerByIdWillThrowExceptionWhenVFCResponseError(){
        AAIService aaiService = mock(AAIService.class);
        CustomerService customerService = new DefaultCustomerService(aaiService);
    	 String resourceVersion="1412934";
        String customerId="1";
        when(aaiService.deleteCustomer(eq(customerId),eq(resourceVersion))).thenReturn(emptyBodyCall());
        customerService.deleteCustomer(customerId,resourceVersion);
    }
    
    @Test
    public void itCreateOrUpdateServiceType(){
    	HttpServletRequest request2 = mock(HttpServletRequest.class);
        AAIService aaiService = mock(AAIService.class);
        CustomerService customerService = new DefaultCustomerService(aaiService);
        String serviceType="1";
        String customerId="demo";
        ResponseBody result=null;
        when(aaiService.createOrUpdateServiceType(eq(customerId),eq(serviceType),anyObject())).thenReturn(successfulCall(result));
        customerService.createOrUpdateServiceType(request2, serviceType,customerId);
    }
    
    @Test
    public void createOrUpdateServiceTypeWillThrowExceptionWhenVFCIsNotAvailable(){
    	HttpServletRequest request2 = mock(HttpServletRequest.class);
        String serviceType="1";
        String customerId="demo";
        AAIService aaiService = mock(AAIService.class);
        CustomerService customerService = new DefaultCustomerService(aaiService);
        when(aaiService.createOrUpdateServiceType(eq(customerId),eq(serviceType),anyObject())).thenReturn(failedCall("VFC is not available!"));
        customerService.createOrUpdateServiceType(request2, serviceType,customerId);
    }
    
    @Test
    public void createOrUpdateServiceTypeWillThrowExceptionWhenVFCResponseError(){
    	HttpServletRequest request2 = mock(HttpServletRequest.class);
        String serviceType="1";
        String customerId="demo";
        AAIService aaiService = mock(AAIService.class);
        CustomerService customerService = new DefaultCustomerService(aaiService);
        when(aaiService.createOrUpdateServiceType(eq(customerId),eq(serviceType),anyObject())).thenReturn(emptyBodyCall());
        customerService.createOrUpdateServiceType(request2, serviceType,customerId);
    }
    
    @Test
    public void itCanDeleteServiceType(){
        AAIService aaiService = mock(AAIService.class);
        CustomerService customerService = new DefaultCustomerService(aaiService);
        String serviceTypeName="1";
        String resourceVersion="214341235";
        String coustomerId="1";
        ResponseBody result=null;
        when(aaiService.deleteServiceType(eq(coustomerId),eq(serviceTypeName),eq(resourceVersion))).thenReturn(successfulCall(result));
        customerService.deleteServiceType(coustomerId,serviceTypeName,resourceVersion);
    }
    
    @Test
    public void deleteServiceTypeWillThrowExceptionWhenVFCIsNotAvailable(){
        AAIService aaiService = mock(AAIService.class);
        CustomerService customerService = new DefaultCustomerService(aaiService);
        String serviceTypeName="1";
        String resourceVersion="214341235";
        String coustomerId="1";
        when(aaiService.deleteServiceType(eq(coustomerId),eq(serviceTypeName),eq(resourceVersion))).thenReturn(failedCall("VFC is not available!"));
        customerService.deleteServiceType(coustomerId,serviceTypeName,resourceVersion);
    }
    
    @Test
    public void deleteServiceTypeByIdWillThrowExceptionWhenVFCResponseError(){
        AAIService aaiService = mock(AAIService.class);
        CustomerService customerService = new DefaultCustomerService(aaiService);
        String serviceTypeName="1";
        String resourceVersion="214341235";
        String coustomerId="1";
        when(aaiService.deleteServiceType(eq(coustomerId),eq(serviceTypeName),eq(resourceVersion))).thenReturn(emptyBodyCall());
        customerService.deleteServiceType(coustomerId,serviceTypeName,resourceVersion);
    }
    
    @Test
    public void itCanGetServiceTypeById(){
        AAIService aaiService = mock(AAIService.class);
        CustomerService customerService = new DefaultCustomerService(aaiService);
        String serviceTypeName="1";
        String resourceVersion="214341235";
        AAIServiceSubscription aaIServiceSubscription = new AAIServiceSubscription(serviceTypeName,resourceVersion);
        when(aaiService.getServiceTypeById(eq(serviceTypeName),eq(resourceVersion))).thenReturn(successfulCall(aaIServiceSubscription));
        customerService.getServiceTypeById(serviceTypeName,resourceVersion);
    }
    
    @Test
    public void getServiceTypeByIdWillThrowExceptionWhenVFCIsNotAvailable(){
        AAIService aaiService = mock(AAIService.class);
        CustomerService customerService = new DefaultCustomerService(aaiService);
        String serviceTypeName="1";
        String resourceVersion="214341235";
        when(aaiService.getServiceTypeById(eq(serviceTypeName),eq(resourceVersion))).thenReturn(failedCall("VFC is not available!"));
        customerService.getServiceTypeById(serviceTypeName,resourceVersion);
    }
    
    @Test
    public void getServiceTypeByIdWillThrowExceptionWhenVFCResponseError(){
        AAIService aaiService = mock(AAIService.class);
        CustomerService customerService = new DefaultCustomerService(aaiService);
        String serviceTypeName="1";
        String resourceVersion="214341235";
        when(aaiService.getServiceTypeById(eq(serviceTypeName),eq(resourceVersion))).thenReturn(emptyBodyCall());
        customerService.getServiceTypeById(serviceTypeName,resourceVersion);
    }
}