Springboot 2.0 upgrade
[so.git] / adapters / mso-requests-db-adapter / src / test / java / org / onap / so / adapters / requestsdb / adapters / MSORequestDBImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.adapters.requestsdb.adapters;
22
23 import static com.shazam.shazamcrest.MatcherAssert.assertThat;
24 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.fail;
28
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Map;
32 import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
33 import org.junit.Before;
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.rules.ExpectedException;
37 import org.junit.runner.RunWith;
38 import org.onap.so.adapters.requestsdb.application.TestAppender;
39 import org.onap.logging.ref.slf4j.ONAPLogConstants;
40 import org.onap.so.adapters.requestsdb.MsoRequestsDbAdapter;
41 import org.onap.so.adapters.requestsdb.RequestStatusType;
42 import org.onap.so.adapters.requestsdb.application.MSORequestDBApplication;
43 import org.onap.so.adapters.requestsdb.exceptions.MsoRequestsDbException;
44 import org.onap.so.db.request.beans.InfraActiveRequests;
45 import org.onap.so.db.request.beans.OperationStatus;
46 import org.onap.so.db.request.beans.ResourceOperationStatus;
47 import org.onap.so.db.request.data.repository.OperationStatusRepository;
48 import org.onap.so.db.request.data.repository.ResourceOperationStatusRepository;
49 import org.onap.so.logger.MsoLogger;
50 import org.onap.so.requestsdb.RequestsDbConstant;
51 import org.springframework.beans.factory.annotation.Autowired;
52 import org.springframework.beans.factory.annotation.Qualifier;
53 import org.springframework.boot.web.server.LocalServerPort;
54 import org.springframework.boot.test.context.SpringBootTest;
55 import org.springframework.context.annotation.Bean;
56 import org.springframework.test.context.ActiveProfiles;
57 import org.springframework.test.context.ContextConfiguration;
58 import org.springframework.test.context.junit4.SpringRunner;
59 import ch.qos.logback.classic.spi.ILoggingEvent;
60
61 @RunWith(SpringRunner.class)
62 @SpringBootTest(classes = MSORequestDBApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
63 @ActiveProfiles("test")
64 public class MSORequestDBImplTest {
65
66         @LocalServerPort
67         private int port;
68
69         private MsoRequestsDbAdapter dbAdapter;
70                 
71     @Autowired
72     private OperationStatusRepository operationStatusRepository;
73     
74     @Autowired
75     private ResourceOperationStatusRepository resourceOperationStatusRepo;
76
77     @Rule
78     public ExpectedException thrown = ExpectedException.none();
79     
80         public InfraActiveRequests setupTestEntities()   {      
81                 return buildTestRequest();
82         }       
83         
84         @Before
85         public void before(){
86         JaxWsProxyFactoryBean jaxWsProxyFactory = new JaxWsProxyFactoryBean();
87         jaxWsProxyFactory.setServiceClass(MsoRequestsDbAdapter.class);
88         jaxWsProxyFactory.setAddress("http://localhost:" + port + "/services/RequestsDbAdapter");
89         jaxWsProxyFactory.setUsername("bpel");
90         jaxWsProxyFactory.setPassword("mso-db-1507!");
91         dbAdapter = (MsoRequestsDbAdapter) jaxWsProxyFactory.create();
92         }
93
94         private InfraActiveRequests buildTestRequest() {        
95                 InfraActiveRequests testRequest= new InfraActiveRequests();
96                 testRequest.setRequestId("00032ab7-3fb3-42e5-965d-8ea592502017");       
97                 testRequest.setClientRequestId("00032ab7-3fb3-42e5-965d-8ea592502016");
98                 testRequest.setRequestStatus("COMPLETE");
99                 testRequest.setStatusMessage("Vf Module has been deleted successfully.");
100                 testRequest.setProgress((long) 100);
101                 testRequest.setSource("VID");           
102                 testRequest.setTenantId("6accefef3cb442ff9e644d589fb04107");
103                 testRequest.setServiceInstanceId("e3b5744d-2ad1-4cdd-8390-c999a38829bc");
104                 testRequest.setRequestAction("deleteInstance");
105                 testRequest.setRequestScope("vfModule");
106                 testRequest.setAction("deleteInstance");
107                 testRequest.setAicCloudRegion("mtn6");
108                 testRequest.setLastModifiedBy("BPMN");
109                 testRequest.setVfModuleId("c7d527b1-7a91-49fd-b97d-1c8c0f4a7992");
110                 testRequest.setVfModuleModelName("vSAMP10aDEV::base::module-0");
111                 testRequest.setVnfId("b92f60c8-8de3-46c1-8dc1-e4390ac2b005");
112                 
113                 return testRequest;
114         }
115
116         @Test
117         public void getByRequestId() throws MsoRequestsDbException  {
118             
119                 InfraActiveRequests testRequest = setupTestEntities();
120                 // Given
121                 String requestId = "00032ab7-3fb3-42e5-965d-8ea592502017";
122                 
123                 // When
124                 InfraActiveRequests infraRequest = dbAdapter.getInfraRequest(requestId);
125                 if(infraRequest ==null)
126                          fail("Null infraRequest");
127                 
128                 // Then
129                 assertThat(infraRequest, sameBeanAs(testRequest).ignoring("requestBody").ignoring("endTime").ignoring("startTime").ignoring("modifyTime"));             
130         }
131         
132         
133         @Test
134         public void getByInvalidRequestId() throws MsoRequestsDbException  {            
135                 // Given
136                 String requestId = "invalidRequestId";
137
138                 try {
139                         dbAdapter.getInfraRequest(requestId);
140                         fail("Expected MsoRequestsDbException to be thrown");
141                 } catch (Exception e) {
142                     assertEquals(e.getMessage(),"Error retrieving MSO Infra Requests DB for Request ID invalidRequestId");
143                 }               
144         }
145         
146         @Test
147         public void getByClientRequestId() throws MsoRequestsDbException  {
148                 InfraActiveRequests testRequest = setupTestEntities();
149                 // Given
150                 String clientRequestId = "00032ab7-3fb3-42e5-965d-8ea592502016";
151                 
152                 // When
153                 InfraActiveRequests infraRequest = dbAdapter.getInfraRequest(clientRequestId);
154                 if(infraRequest ==null)
155                          fail("Null infraRequest");
156                 
157                 // Then
158                 assertThat(infraRequest, sameBeanAs(testRequest).ignoring("requestBody").ignoring("endTime").ignoring("startTime").ignoring("modifyTime"));             
159         }
160         
161         
162         @Test
163         public void updateInfraRequest() throws MsoRequestsDbException  {
164                 InfraActiveRequests testRequest = setupTestEntities();
165                 // Given
166                 String clientRequestId = "00032ab7-3fb3-42e5-965d-8ea592502016";
167                         
168
169                 // When
170                 String lastModifiedBy = "UNIT TEST";
171                 String statusMessage = "TESTING THE UDPATES";
172                 String progress = "50";
173                 String vnfOutputs = "VNF OUTPUTS";              
174                 String networkId = "New NetworkID";
175                 String vnfId = "NEWVNFID";
176                 String volumeGroupId = "NewVolumeGroupId";
177                 String serviceInstanceName = "NewServiceInstanceName";
178                 String configurationId = "NewConfigurationId";
179                 String configurationName = "NewConfigurationName";
180                 String vfModuleName = "VFModuleName";
181                 RequestStatusType requestStatus = RequestStatusType.COMPLETE ;
182                 String responseBody = "NewResponseBody";
183                 String vfModuleId = "NEW VF MODULEID";  
184                 String serviceInstanceId = " new serv ind";
185                 
186                 
187                 testRequest.setVolumeGroupId(volumeGroupId);
188                 testRequest.setServiceInstanceName(serviceInstanceName);
189                 testRequest.setConfigurationId(configurationId);
190                 testRequest.setConfigurationName(configurationName);
191                 testRequest.setNetworkId(networkId);
192                 testRequest.setResponseBody(responseBody);
193                 testRequest.setStatusMessage(statusMessage);
194                 testRequest.setProgress((long) 50);
195                 testRequest.setServiceInstanceId(lastModifiedBy);
196                 testRequest.setLastModifiedBy(lastModifiedBy);
197                 testRequest.setVfModuleId(vfModuleId);
198                 testRequest.setVfModuleName(vfModuleName);
199                 testRequest.setVnfId(vnfId);
200                 testRequest.setServiceInstanceId(serviceInstanceId);
201                 testRequest.setVfModuleName(vfModuleName);
202                 testRequest.setVnfOutputs(vnfOutputs);
203                                 
204                 
205                  dbAdapter.updateInfraRequest ( testRequest.getRequestId(),
206                  lastModifiedBy,
207                  statusMessage,
208                  responseBody,
209                  requestStatus,
210                  progress,
211                  vnfOutputs,
212                  serviceInstanceId,
213                  networkId,
214                  vnfId,
215                  vfModuleId,
216                  volumeGroupId,
217                  serviceInstanceName,
218                  configurationId,
219                  configurationName,
220                  vfModuleName);
221                 InfraActiveRequests infraRequest = dbAdapter.getInfraRequest(clientRequestId);
222                 // Then
223                 assertThat(infraRequest, sameBeanAs(testRequest).ignoring("requestBody").ignoring("endTime").ignoring("startTime").ignoring("modifyTime"));             
224         }
225         
226         @Test
227         public void UpdateByInvalidRequestId() throws MsoRequestsDbException  {         
228                 // Given
229                 String requestId = "invalidRequestId";
230
231                 try {
232                         dbAdapter.updateInfraRequest ( requestId,
233                                         null,
234                                         null,
235                                         null,
236                                         null,
237                                         null,
238                                         null,
239                                         null,
240                                         null,
241                                         null,
242                                         null,
243                                         null,
244                                         null,
245                                         null,
246                                         null,
247                                         null);
248                         fail("Expected MsoRequestsDbException to be thrown");   
249                 } catch (Exception e) {
250                     assertEquals(e.getMessage(),"Error retrieving MSO Infra Requests DB for Request ID invalidRequestId");
251                 }               
252         }
253         
254         
255         @Test
256         public void updateInfraRequestNulls() throws MsoRequestsDbException  {
257                 InfraActiveRequests testRequest = setupTestEntities();
258                 // Given
259                 String clientRequestId = "00032ab7-3fb3-42e5-965d-8ea592502016";
260
261                 // When
262                 dbAdapter.updateInfraRequest ( testRequest.getRequestId(),
263                                 testRequest.getLastModifiedBy(),
264                                 null,
265                                 null,
266                                 null,
267                                 null,
268                                 null,
269                                 null,
270                                 null,
271                                 null,
272                                 null,
273                                 null,
274                                 null,
275                                 null,
276                                 null,
277                                 null);
278                 InfraActiveRequests infraRequest = dbAdapter.getInfraRequest(clientRequestId);
279                 // Then
280                 assertThat(infraRequest, sameBeanAs(testRequest).ignoring("requestBody").ignoring("endTime").ignoring("startTime").ignoring("modifyTime"));             
281         }
282         
283         @Test
284         public void getSiteStatusNotDisabled() throws MsoRequestsDbException  {
285                 setupTestEntities();
286                 // Given
287                 String siteName = "siteName";
288                 
289                 // When
290                 boolean siteDisabled = dbAdapter.getSiteStatus(siteName);
291                 
292                 // Then
293                 assertEquals(siteDisabled, true);               
294         }
295         
296         @Test
297         public void getSiteStatusDisabled() throws MsoRequestsDbException  {
298                 setupTestEntities();
299                 // Given
300                 String siteName = "testSite";
301                 
302                 // When
303                 boolean siteDisabled = dbAdapter.getSiteStatus(siteName);
304                 
305                 // Then
306                 assertEquals(siteDisabled, false);              
307         }
308         
309         @Test 
310         public void updateServiceOperation() throws MsoRequestsDbException{
311                 String serviceId = "serviceid";
312                 String operationId = "operationid";
313                 String serviceName = "servicename";
314                 String operation = "newOperationType";
315                 String userId = "NewUserId";
316                 String result = "NewResult";
317                 String operationContent = "newOperationContent";
318                 String progress = "Newprogress";
319                 String reason = "NewReason";            
320                 
321                 OperationStatus updatedOperationStatus = new OperationStatus();
322                 
323                 
324                 
325                 updatedOperationStatus.setServiceId(serviceId);
326                 updatedOperationStatus.setServiceName(serviceName);
327                 updatedOperationStatus.setOperationId(operationId);
328                 updatedOperationStatus.setOperation(operation);
329                 updatedOperationStatus.setUserId(userId);
330                 updatedOperationStatus.setResult(result);
331                 updatedOperationStatus.setProgress(progress);
332                 updatedOperationStatus.setReason(reason);
333                 updatedOperationStatus.setOperationContent(operationContent);
334                 
335                 dbAdapter.updateServiceOperationStatus(serviceId, operationId, operation,  userId,
336                      result, operationContent,  progress, reason);              
337                 OperationStatus dbOpStatus = operationStatusRepository.findOneByServiceIdAndOperationId(serviceId,operationId);         
338                 assertThat(dbOpStatus, sameBeanAs(updatedOperationStatus).ignoring("operateAt").ignoring("finishedAt"));        
339         }
340         
341         
342         @Test 
343         public void updateServiceOperation_Not_Found() throws MsoRequestsDbException{
344             TestAppender.events.clear();
345                 String serviceId = "badserviceId";
346                 String operationId = "operationid";
347                 String operation = "newOperationType";
348                 String userId = "NewUserId";
349                 String result = "NewResult";
350                 String operationContent = "newOperationContent";
351                 String progress = "Newprogress";
352                 String reason = "NewReason";            
353                 
354                 OperationStatus updatedOperationStatus = new OperationStatus();
355                 
356                 
357                 
358                 updatedOperationStatus.setServiceId(serviceId);
359                 updatedOperationStatus.setOperationId(operationId);
360                 updatedOperationStatus.setOperation(operation);
361                 updatedOperationStatus.setUserId(userId);
362                 updatedOperationStatus.setResult(result);
363                 updatedOperationStatus.setProgress(progress);
364                 updatedOperationStatus.setReason(reason);
365                 updatedOperationStatus.setOperationContent(operationContent);
366
367                 try {
368             dbAdapter.updateServiceOperationStatus(serviceId, operationId, operation,  userId,
369                      result, operationContent,  progress, reason);
370             fail("Expected MsoRequestsDbException to be thrown");   
371         } catch (Exception e) {
372             assertEquals("Entity not found. Unable to retrieve OperationStatus Object ServiceId: " + serviceId + " operationId: " + operationId,e.getMessage());
373             for(ILoggingEvent logEvent : TestAppender.events)
374                 if(logEvent.getLoggerName().equals("org.onap.so.logging.cxf.interceptor.SOAPLoggingInInterceptor") &&
375                         logEvent.getMarker().getName().equals("ENTRY")
376                         ){
377                     Map<String,String> mdc = logEvent.getMDCPropertyMap();
378                     assertNotNull(mdc.get(ONAPLogConstants.MDCs.INSTANCE_UUID));
379                     assertNotNull(mdc.get(ONAPLogConstants.MDCs.REQUEST_ID));
380                     assertNotNull(mdc.get(ONAPLogConstants.MDCs.INVOCATION_ID));
381                     assertEquals("",mdc.get(ONAPLogConstants.MDCs.PARTNER_NAME));
382                     assertEquals("/services/RequestsDbAdapter",mdc.get(ONAPLogConstants.MDCs.SERVICE_NAME));
383                     assertEquals("INPROGRESS",mdc.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
384                 }else if(logEvent.getLoggerName().equals("org.onap.so.logging.cxf.interceptor.SOAPLoggingOutInterceptor") &&
385                         logEvent.getMarker()!= null && logEvent.getMarker().getName().equals("EXIT")){
386                     Map<String,String> mdc = logEvent.getMDCPropertyMap();
387                     assertNotNull(mdc.get(ONAPLogConstants.MDCs.REQUEST_ID));
388                     assertNotNull(mdc.get(ONAPLogConstants.MDCs.INVOCATION_ID));
389                     assertEquals("500",mdc.get(ONAPLogConstants.MDCs.RESPONSE_CODE));
390                     assertEquals("",mdc.get(ONAPLogConstants.MDCs.PARTNER_NAME));
391                     assertEquals("/services/RequestsDbAdapter",mdc.get(ONAPLogConstants.MDCs.SERVICE_NAME));
392                     assertEquals("ERROR",mdc.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
393                 }
394         }               
395                 
396         }
397         
398         @Test 
399         public void initResourceOperationStatus() throws MsoRequestsDbException{
400                 String resourceTemplateUUIDs = "template1:template2:template3:";
401                 String serviceId = "serviceId";
402                 String operationId = "operationId";
403                 String operationType = "operationType";
404                 
405              ResourceOperationStatus resource1 = new ResourceOperationStatus();
406              resource1.setOperationId(operationId);
407              resource1.setServiceId(serviceId);
408              resource1.setResourceTemplateUUID("template1");
409              resource1.setOperType(operationType);
410              resource1.setStatus(RequestsDbConstant.Status.PROCESSING);
411              resource1.setStatusDescription("Waiting for start");
412          
413          ResourceOperationStatus resource2 = new ResourceOperationStatus();
414          resource2.setOperationId(operationId);
415          resource2.setServiceId(serviceId);
416          resource2.setResourceTemplateUUID("template2");
417          resource2.setOperType(operationType);
418          resource2.setStatus(RequestsDbConstant.Status.PROCESSING);
419          resource2.setStatusDescription("Waiting for start");
420          
421          ResourceOperationStatus resource3 = new ResourceOperationStatus();
422          resource3.setOperationId(operationId);
423          resource3.setServiceId(serviceId);
424          resource3.setResourceTemplateUUID("template3");
425          resource3.setOperType(operationType);
426          resource3.setStatus(RequestsDbConstant.Status.PROCESSING);
427          resource3.setStatusDescription("Waiting for start");
428          
429          List<ResourceOperationStatus> expectedResult = new ArrayList<ResourceOperationStatus>();
430          expectedResult.add(resource1);
431          expectedResult.add(resource2);
432          expectedResult.add(resource3);
433                 
434                 dbAdapter.initResourceOperationStatus(serviceId, operationId, operationType,resourceTemplateUUIDs);             
435                 List<ResourceOperationStatus> testList = resourceOperationStatusRepo.findByServiceIdAndOperationId(serviceId,operationId);              
436                 assertThat(testList, sameBeanAs(expectedResult));       
437         }
438
439         @Test
440         public void getResourceOperationStatus() throws MsoRequestsDbException{
441                 String resourceTemplateUUIDs = "template1";
442                 String serviceId = "serviceId";
443                 String operationId = "operationId";
444                 String operationType = "operationType";
445
446                 ResourceOperationStatus resource1 = new ResourceOperationStatus();
447                 resource1.setOperationId(operationId);
448                 resource1.setServiceId(serviceId);
449                 resource1.setResourceTemplateUUID("template1");
450                 resource1.setOperType(operationType);
451                 resource1.setStatus(RequestsDbConstant.Status.PROCESSING);
452                 resource1.setStatusDescription("Waiting for start");
453
454
455                 dbAdapter.initResourceOperationStatus(serviceId, operationId, operationType,resourceTemplateUUIDs);
456
457                 ResourceOperationStatus actualResource = dbAdapter.getResourceOperationStatus(serviceId, operationId,"template1");
458                 assertThat(actualResource, sameBeanAs(resource1));
459         }
460
461         @Test
462         public void updateResourceOperationStatus() throws MsoRequestsDbException{
463             TestAppender.events.clear();
464                 String resourceTemplateUUID = "template1";
465                 String serviceId = "serviceId";
466                 String operationId = "operationId";
467                 String operationType = "operationType";
468                 String resourceInstanceID = "resourceInstanceID";
469                 String jobId = "jobId";
470                 String status = RequestsDbConstant.Status.FINISHED;
471                 String progress = "50";
472                 String errorCode = "errorCode";
473                 String statusDescription = "statusDescription";
474
475
476                 ResourceOperationStatus expectedResource = new ResourceOperationStatus();
477                 expectedResource.setOperationId(operationId);
478                 expectedResource.setServiceId(serviceId);
479                 expectedResource.setResourceTemplateUUID(resourceTemplateUUID);
480                 expectedResource.setOperType(operationType);
481                 expectedResource.setJobId(jobId);
482                 expectedResource.setErrorCode(errorCode);
483                 expectedResource.setStatus(RequestsDbConstant.Status.FINISHED);
484                 expectedResource.setStatusDescription(statusDescription);
485                 expectedResource.setProgress(progress);
486                 expectedResource.setResourceInstanceID(resourceInstanceID);
487
488
489                 dbAdapter.updateResourceOperationStatus(serviceId, operationId, resourceTemplateUUID,
490                                 operationType, resourceInstanceID, jobId, status, progress,
491                                 errorCode, statusDescription);
492
493                 ResourceOperationStatus actualResource = dbAdapter.getResourceOperationStatus(serviceId, operationId,"template1");
494                 assertThat(actualResource, sameBeanAs(expectedResource));
495                 
496                 for(ILoggingEvent logEvent : TestAppender.events)
497             if(logEvent.getLoggerName().equals("org.onap.so.logging.cxf.interceptor.SOAPLoggingInInterceptor") &&
498                     logEvent.getMarker().getName().equals("ENTRY")
499                     ){
500                 Map<String,String> mdc = logEvent.getMDCPropertyMap();
501                 assertNotNull(mdc.get(ONAPLogConstants.MDCs.INSTANCE_UUID));
502                 assertNotNull(mdc.get(ONAPLogConstants.MDCs.REQUEST_ID));
503                 assertNotNull(mdc.get(ONAPLogConstants.MDCs.INVOCATION_ID));
504                 assertEquals("",mdc.get(ONAPLogConstants.MDCs.PARTNER_NAME));
505                 assertEquals("/services/RequestsDbAdapter",mdc.get(ONAPLogConstants.MDCs.SERVICE_NAME));
506                 assertEquals("INPROGRESS",mdc.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
507             }else if(logEvent.getLoggerName().equals("org.onap.so.logging.cxf.interceptor.SOAPLoggingOutInterceptor") &&
508                     logEvent.getMarker().getName().equals("EXIT")){
509                 Map<String,String> mdc = logEvent.getMDCPropertyMap();
510                 assertNotNull(mdc.get(ONAPLogConstants.MDCs.REQUEST_ID));
511                 assertNotNull(mdc.get(ONAPLogConstants.MDCs.INVOCATION_ID));
512                 assertEquals(null,mdc.get(ONAPLogConstants.MDCs.RESPONSE_CODE));
513                 assertEquals("",mdc.get(ONAPLogConstants.MDCs.PARTNER_NAME));
514                 assertEquals("/services/RequestsDbAdapter",mdc.get(ONAPLogConstants.MDCs.SERVICE_NAME));
515                 assertEquals("COMPLETED",mdc.get(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE));
516             }
517         }
518
519
520 }