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