Add some test code to improve code coverage
[usecase-ui/server.git] / server / src / test / java / org / onap / usecaseui / server / service / lcm / impl / DefaultServiceLcmServiceTest.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 mockit.Mock;
19 import mockit.MockUp;
20 import org.hibernate.Query;
21 import org.hibernate.Session;
22 import org.hibernate.SessionFactory;
23 import org.hibernate.Transaction;
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.onap.usecaseui.server.bean.ServiceBean;
28 import org.onap.usecaseui.server.service.lcm.ServiceLcmService;
29 import org.onap.usecaseui.server.service.lcm.domain.so.SOService;
30 import org.onap.usecaseui.server.service.lcm.domain.so.bean.DeleteOperationRsp;
31 import org.onap.usecaseui.server.service.lcm.domain.so.bean.Operation;
32 import org.onap.usecaseui.server.service.lcm.domain.so.bean.OperationProgressInformation;
33 import org.onap.usecaseui.server.service.lcm.domain.so.bean.SaveOrUpdateOperationRsp;
34 import org.onap.usecaseui.server.service.lcm.domain.so.bean.ServiceOperation;
35 import org.onap.usecaseui.server.service.lcm.domain.so.exceptions.SOException;
36
37 import javax.servlet.ReadListener;
38 import javax.servlet.ServletInputStream;
39 import javax.servlet.http.HttpServletRequest;
40 import java.io.IOException;
41 import java.io.Serializable;
42 import java.util.Arrays;
43 import java.util.Date;
44 import java.util.List;
45
46 import static org.mockito.Matchers.anyObject;
47 import static org.mockito.Matchers.eq;
48 import static org.mockito.Mockito.mock;
49 import static org.mockito.Mockito.when;
50 import static org.onap.usecaseui.server.util.CallStub.emptyBodyCall;
51 import static org.onap.usecaseui.server.util.CallStub.failedCall;
52 import static org.onap.usecaseui.server.util.CallStub.successfulCall;
53
54 public class DefaultServiceLcmServiceTest {
55         
56         private static final long serialVersionUID = 1L;
57         
58          ServiceLcmService service = null;
59          
60         @Before
61         public void before() throws Exception {
62                 SOService soService = mock(SOService.class);
63                 service = new DefaultServiceLcmService(soService);
64
65                 MockUp<Transaction> mockUpTransaction = new MockUp<Transaction>() {
66                         @Mock
67                         public void commit() {
68                         }
69                 };
70                 MockUp<Query> mockUpQuery = new MockUp<Query>() {
71                 };
72                 new MockUp<Query>() {
73                         @Mock
74                         public Query setString(String name, String value) {
75                                 return mockUpQuery.getMockInstance();
76                         }
77                         @Mock
78                         public Query setDate(String name, Date value) {
79                                 return mockUpQuery.getMockInstance();
80                         }
81                         @Mock
82                         public Query setInteger(String name, int value) {
83                                 return mockUpQuery.getMockInstance();
84                         }
85                         @Mock
86                         public int executeUpdate() {
87                                 return 0;
88                         }
89                         @Mock
90                         public Query setMaxResults(int value) {
91                                 return mockUpQuery.getMockInstance();
92                         }
93                         @Mock
94                         public Query setFirstResult(int firstResult) {
95                                 return mockUpQuery.getMockInstance();
96                         }
97                         @Mock
98                         public Query setParameterList(String name, Object[] values) {
99                                 return mockUpQuery.getMockInstance();
100                         }
101                         @Mock
102                         public List<ServiceBean> list() {
103                 ServiceBean ah = new ServiceBean();
104                                 return Arrays.asList(ah);
105                         }
106                         @Mock
107                         public Object uniqueResult() {
108                                 return "0";
109                         }
110                 };
111                 MockUp<Session> mockedSession = new MockUp<Session>() {
112                         @Mock
113                         public Query createQuery(String sql) {
114                                 return mockUpQuery.getMockInstance();
115                         }
116                         @Mock
117                         public Transaction beginTransaction() {
118                                 return mockUpTransaction.getMockInstance();
119                         }
120                         @Mock
121                         public Transaction getTransaction() {
122                                 return mockUpTransaction.getMockInstance();
123                         }
124                         @Mock
125                         public Serializable save(Object object) {
126                                 return (Serializable) serialVersionUID;
127                         }
128                         @Mock
129                         public void flush() {
130                         }
131                         @Mock
132                         public void update(Object object) {
133                         }
134                 };
135                 new MockUp<SessionFactory>() {
136                         @Mock
137                         public Session openSession() {
138                                 return mockedSession.getMockInstance();
139                         }
140                 };
141                 new MockUp<DefaultServiceLcmService>() {
142                         @Mock
143                         private Session getSession() {
144                                 return mockedSession.getMockInstance();
145                         }
146                 };
147         }
148         
149     @Test
150     public void itCanInstantiateService() throws IOException {
151         SOService soService = mock(SOService.class);
152         Operation op = new Operation();
153         op.setOperationId("1");
154         op.setServiceId("1");
155         ServiceOperation operation = new ServiceOperation();
156         operation.setService(op);
157         when(soService.instantiateService(anyObject())).thenReturn(successfulCall(operation));
158
159         HttpServletRequest request = mockRequest();
160
161         ServiceLcmService service = new DefaultServiceLcmService(soService);
162
163         Assert.assertSame(operation, service.instantiateService(request));
164     }
165
166     private HttpServletRequest mockRequest() throws IOException {
167         HttpServletRequest request = mock(HttpServletRequest.class);
168         when(request.getContentLength()).thenReturn(0);
169         ServletInputStream inStream = new ServletInputStream() {
170             @Override
171             public boolean isFinished() {
172                 return false;
173             }
174
175             @Override
176             public boolean isReady() {
177                 return false;
178             }
179
180             @Override
181             public void setReadListener(ReadListener readListener) {
182
183             }
184
185             @Override
186             public int read() throws IOException {
187                 return 0;
188             }
189         };
190         when(request.getInputStream()).thenReturn(inStream);
191         return request;
192     }
193
194     @Test(expected = SOException.class)
195     public void instantiateServiceWillThrowExceptionWhenSOIsNotAvailable() throws IOException {
196         SOService soService = mock(SOService.class);
197         when(soService.instantiateService(anyObject())).thenReturn(failedCall("SO is not available!"));
198         HttpServletRequest request = mockRequest();
199
200         ServiceLcmService service = new DefaultServiceLcmService(soService);
201
202         service.instantiateService(request);
203     }
204
205     @Test(expected = SOException.class)
206     public void instantiateServiceWillThrowExceptionWhenSOResponseError() throws IOException {
207         SOService soService = mock(SOService.class);
208         when(soService.instantiateService(anyObject())).thenReturn(emptyBodyCall());
209         HttpServletRequest request = mockRequest();
210
211         ServiceLcmService service = new DefaultServiceLcmService(soService);
212
213         service.instantiateService(request);
214     }
215
216     @Test
217     public void itCanTerminateService() throws IOException {
218         SOService soService = mock(SOService.class);
219         String serviceId = "1";
220         DeleteOperationRsp rsp = new DeleteOperationRsp();
221         rsp.setOperationId("1");
222         when(soService.terminateService(eq(serviceId), anyObject())).thenReturn(successfulCall(rsp));
223         HttpServletRequest request = mockRequest();
224
225         ServiceLcmService service = new DefaultServiceLcmService(soService);
226
227         Assert.assertSame(rsp, service.terminateService(serviceId, request));
228     }
229
230     @Test(expected = SOException.class)
231     public void terminateServiceWillThrowExceptionWhenSOIsNotAvailable() throws IOException {
232         SOService soService = mock(SOService.class);
233         String serviceId = "1";
234         when(soService.terminateService(eq(serviceId), anyObject())).thenReturn(failedCall("SO is not available!"));
235         HttpServletRequest request = mockRequest();
236
237         ServiceLcmService service = new DefaultServiceLcmService(soService);
238
239         service.terminateService(serviceId, request);
240     }
241
242     @Test(expected = SOException.class)
243     public void terminateServiceWillThrowExceptionWhenSOResponseError() throws IOException {
244         SOService soService = mock(SOService.class);
245         String serviceId = "1";
246         when(soService.terminateService(eq(serviceId), anyObject())).thenReturn(emptyBodyCall());
247         HttpServletRequest request = mockRequest();
248
249         ServiceLcmService service = new DefaultServiceLcmService(soService);
250
251         service.terminateService(serviceId, request);
252     }
253
254     @Test
255     public void itCanQueryOperationProgress() {
256         SOService soService = mock(SOService.class);
257         String serviceId = "1";
258         String operationId = "1";
259         OperationProgressInformation progress = new OperationProgressInformation();
260         when(soService.queryOperationProgress(serviceId, operationId)).thenReturn(successfulCall(progress));
261
262         ServiceLcmService service = new DefaultServiceLcmService(soService);
263
264         Assert.assertSame(progress, service.queryOperationProgress(serviceId, operationId));
265     }
266
267     @Test(expected = SOException.class)
268     public void queryOperationProgressWillThrowExceptionWhenSOIsNotAvailable() {
269         SOService soService = mock(SOService.class);
270         String serviceId = "1";
271         String operationId = "1";
272         when(soService.queryOperationProgress(serviceId, operationId)).thenReturn(failedCall("SO is not available!"));
273
274         ServiceLcmService service = new DefaultServiceLcmService(soService);
275
276         service.queryOperationProgress(serviceId, operationId);
277     }
278
279     @Test(expected = SOException.class)
280     public void queryOperationProgressWillThrowExceptionWhenSOResponseError() {
281         SOService soService = mock(SOService.class);
282         String serviceId = "1";
283         String operationId = "1";
284         when(soService.queryOperationProgress(serviceId, operationId)).thenReturn(emptyBodyCall());
285
286         ServiceLcmService service = new DefaultServiceLcmService(soService);
287
288         service.queryOperationProgress(serviceId, operationId);
289     }
290     
291     @Test(expected = SOException.class)
292     public void scaleServiceWillThrowExceptionWhenSOIsNotAvailable() throws IOException {
293         SOService soService = mock(SOService.class);
294         String serviceId = "1";
295         when(soService.scaleService(eq(serviceId), anyObject())).thenReturn(failedCall("SO is not available!"));
296         HttpServletRequest request = mockRequest();
297
298         ServiceLcmService service = new DefaultServiceLcmService(soService);
299
300         service.scaleService(serviceId, request);
301     }
302
303     @Test(expected = SOException.class)
304     public void scaleServiceWillThrowExceptionWhenSOResponseError() throws IOException {
305         SOService soService = mock(SOService.class);
306         String serviceId = "1";
307         when(soService.scaleService(eq(serviceId), anyObject())).thenReturn(emptyBodyCall());
308         HttpServletRequest request = mockRequest();
309
310         ServiceLcmService service = new DefaultServiceLcmService(soService);
311
312         service.scaleService(serviceId, request);
313     }
314     
315     @Test
316     public void itCanScaleService() throws IOException {
317         SOService soService = mock(SOService.class);
318         String serviceId = "1";
319         SaveOrUpdateOperationRsp rsp = new SaveOrUpdateOperationRsp();
320         rsp.setOperationId("1");
321         when(soService.scaleService(eq(serviceId), anyObject())).thenReturn(successfulCall(rsp));
322         HttpServletRequest request = mockRequest();
323
324         ServiceLcmService service = new DefaultServiceLcmService(soService);
325
326         Assert.assertSame(rsp, service.scaleService(serviceId, request));
327     }
328     
329     @Test(expected = SOException.class)
330     public void updateServiceWillThrowExceptionWhenSOIsNotAvailable() throws IOException {
331         SOService soService = mock(SOService.class);
332         String serviceId = "1";
333         when(soService.updateService(eq(serviceId), anyObject())).thenReturn(failedCall("SO is not available!"));
334         HttpServletRequest request = mockRequest();
335
336         ServiceLcmService service = new DefaultServiceLcmService(soService);
337
338         service.updateService(serviceId, request);
339     }
340
341     @Test(expected = SOException.class)
342     public void updateServiceWillThrowExceptionWhenSOResponseError() throws IOException {
343         SOService soService = mock(SOService.class);
344         String serviceId = "1";
345         when(soService.updateService(eq(serviceId), anyObject())).thenReturn(emptyBodyCall());
346         HttpServletRequest request = mockRequest();
347
348         ServiceLcmService service = new DefaultServiceLcmService(soService);
349
350         service.updateService(serviceId, request);
351     }
352     
353     @Test
354     public void itCanUpdateService() throws IOException {
355         SOService soService = mock(SOService.class);
356         String serviceId = "1";
357         SaveOrUpdateOperationRsp rsp = new SaveOrUpdateOperationRsp();
358         rsp.setOperationId("1");
359         when(soService.updateService(eq(serviceId), anyObject())).thenReturn(successfulCall(rsp));
360         HttpServletRequest request = mockRequest();
361
362         ServiceLcmService service = new DefaultServiceLcmService(soService);
363
364         Assert.assertSame(rsp, service.updateService(serviceId, request));
365     }
366     
367     @Test
368     public void itCanGetServiceInstanceIdByParentId() throws IOException {
369         
370         String parentServiceInstanceId = "1";
371         
372         service.getServiceInstanceIdByParentId(parentServiceInstanceId);
373         
374         service.getServiceInstanceIdByParentId(null);
375     }
376     
377     @Test
378     public void itCanGetServiceBeanByServiceInStanceId() throws IOException {
379         
380         String serviceInstanceId = "1";
381         
382         service.getServiceBeanByServiceInStanceId(serviceInstanceId);
383         
384         service.getServiceBeanByServiceInStanceId(null);
385     }
386     
387     @Test
388     public void itCanUpdateServiceInstanceStatusById() throws IOException {
389         
390         String serviceInstanceId = "1";
391         
392         String status="active";
393         
394         service.updateServiceInstanceStatusById(status,serviceInstanceId);
395         
396         service.updateServiceInstanceStatusById(null,null);
397     }
398     
399     @Test
400     public void itCanSaveOrUpdateServiceBean() throws IOException {
401         
402         ServiceBean serviceBean = new ServiceBean();
403         
404         service.saveOrUpdateServiceBean(serviceBean);
405         
406         service.saveOrUpdateServiceBean(null);
407     }
408 }