Containerization feature of SO
[so.git] / mso-api-handlers / mso-api-handler-infra / src / test / java / org / onap / so / apihandlerinfra / tenantisolation / process / ActivateVnfOperationalEnvironmentTest.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.apihandlerinfra.tenantisolation.process;
22
23 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
24 import static com.github.tomakehurst.wiremock.client.WireMock.get;
25 import static com.github.tomakehurst.wiremock.client.WireMock.post;
26 import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
27 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertNotNull;
30 import static org.junit.Assert.assertTrue;
31
32 import java.util.ArrayList;
33 import java.util.List;
34
35 import org.apache.http.HttpStatus;
36 import org.json.JSONObject;
37 import org.junit.After;
38 import org.junit.Test;
39 import org.onap.so.apihandlerinfra.BaseTest;
40 import org.onap.so.apihandlerinfra.tenantisolation.CloudOrchestrationRequest;
41 import org.onap.so.apihandlerinfra.tenantisolation.helpers.AAIClientHelper;
42 import org.onap.so.apihandlerinfra.tenantisolationbeans.Manifest;
43 import org.onap.so.apihandlerinfra.tenantisolationbeans.RecoveryAction;
44 import org.onap.so.apihandlerinfra.tenantisolationbeans.RequestDetails;
45 import org.onap.so.apihandlerinfra.tenantisolationbeans.RequestParameters;
46 import org.onap.so.apihandlerinfra.tenantisolationbeans.ServiceModelList;
47 import org.onap.so.client.aai.AAIVersion;
48 import org.onap.so.client.aai.entities.AAIResultWrapper;
49 import org.onap.so.client.aai.objects.AAIOperationalEnvironment;
50 import org.onap.so.db.request.beans.InfraActiveRequests;
51 import org.onap.so.db.request.beans.OperationalEnvDistributionStatus;
52 import org.onap.so.db.request.beans.OperationalEnvServiceModelStatus;
53 import org.onap.so.db.request.data.repository.InfraActiveRequestsRepository;
54 import org.onap.so.db.request.data.repository.OperationalEnvDistributionStatusRepository;
55 import org.onap.so.db.request.data.repository.OperationalEnvServiceModelStatusRepository;
56 import org.springframework.beans.factory.annotation.Autowired;
57
58
59 public class ActivateVnfOperationalEnvironmentTest extends BaseTest{
60
61         @Autowired
62         private OperationalEnvDistributionStatusRepository distributionDbRepository;
63         @Autowired
64         private OperationalEnvServiceModelStatusRepository serviceModelDbRepository;
65         @Autowired
66         private ActivateVnfOperationalEnvironment activateVnf;
67         @Autowired
68         private InfraActiveRequestsRepository infraActiveRequestsRepository;    
69         @Autowired
70         private AAIClientHelper clientHelper;
71
72         String requestId = "TEST_requestId";
73         String operationalEnvironmentId = "EMOE-001";   
74         CloudOrchestrationRequest request = new CloudOrchestrationRequest();
75         String workloadContext = "PVT";
76         String recoveryActionRetry  = "RETRY";
77         String serviceModelVersionId = "TEST_serviceModelVersionId";    
78         int retryCount = 3;
79         String sdcDistributionId = "TEST_distributionId";
80         String statusSent = "SENT";
81
82         @After
83         public void after() throws Exception {
84                 distributionDbRepository.deleteAll();
85                 serviceModelDbRepository.deleteAll();           
86         }
87
88         @Test
89         public void getAAIOperationalEnvironmentTest() throws Exception {
90
91                 AAIOperationalEnvironment aaiOpEnv = null;
92
93                 stubFor(get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
94                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("vnfoperenv/ecompOperationalEnvironment.json").withStatus(HttpStatus.SC_ACCEPTED)));
95                 
96                 AAIResultWrapper wrapper = clientHelper.getAaiOperationalEnvironment("EMOE-001");
97                 aaiOpEnv = wrapper.asBean(AAIOperationalEnvironment.class).get();
98                 assertEquals("EMOE-001", aaiOpEnv.getOperationalEnvironmentId());                       
99                 assertNotNull(activateVnf.getAAIOperationalEnvironment(operationalEnvironmentId));      
100                 assertEquals( "EMOE-001", activateVnf.getAAIOperationalEnvironment(operationalEnvironmentId).getOperationalEnvironmentId());            
101                 
102         }       
103                 
104         @Test
105         public void executionTest() throws Exception {
106
107                 List<ServiceModelList> serviceModelVersionIdList = new ArrayList<ServiceModelList>();
108                 ServiceModelList serviceModelList1 = new ServiceModelList(); 
109                 serviceModelList1.setRecoveryAction(RecoveryAction.retry);
110                 serviceModelList1.setServiceModelVersionId(serviceModelVersionId);
111                 serviceModelVersionIdList.add(serviceModelList1);
112                 
113                 RequestDetails requestDetails = new RequestDetails();
114                 RequestParameters requestParameters = new RequestParameters();
115                 Manifest manifest = new Manifest();
116                 manifest.setServiceModelList(serviceModelVersionIdList);
117                 requestParameters.setManifest(manifest);
118                 requestParameters.setWorkloadContext(workloadContext);
119                 requestDetails.setRequestParameters(requestParameters);
120                 
121                 request.setOperationalEnvironmentId(operationalEnvironmentId);
122                 request.setRequestDetails(requestDetails);
123                 
124                 JSONObject jsonObject = new JSONObject();
125                 jsonObject.put("statusCode", "202");
126                 jsonObject.put("message", "Success");
127                 jsonObject.put("distributionId", sdcDistributionId);
128                 
129                 stubFor(get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
130                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("vnfoperenv/ecompOperationalEnvironment.json").withStatus(HttpStatus.SC_ACCEPTED)));
131                 stubFor(post(urlPathMatching("/sdc/v1/catalog/services/TEST_serviceModelVersionId/distr.*"))
132                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(jsonObject.toString()).withStatus(HttpStatus.SC_ACCEPTED)));
133                 
134                 
135                 activateVnf.execute(requestId, request, distributionDbRepository, serviceModelDbRepository);
136                 
137                 // insert record, status sent
138                 OperationalEnvDistributionStatus distStatus = distributionDbRepository.findOne(sdcDistributionId);
139                 assertNotNull(distStatus);
140                 assertEquals(operationalEnvironmentId, distStatus.getOperationalEnvId());
141                 assertEquals(statusSent, distStatus.getDistributionIdStatus());
142                 
143                 // insert record, status sent           
144                 OperationalEnvServiceModelStatus servStatus = serviceModelDbRepository.findOneByOperationalEnvIdAndServiceModelVersionId(operationalEnvironmentId, serviceModelVersionId);
145                 assertNotNull(servStatus);
146                 assertEquals(statusSent, servStatus.getServiceModelVersionDistrStatus());
147                 assertEquals(operationalEnvironmentId, servStatus.getOperationalEnvId());
148
149         }                       
150         
151         @Test
152         public void processActivateSDCRequestTest_202() throws Exception {
153
154                 String distributionId = "TEST_distributionId";
155                 
156                 JSONObject jsonObject = new JSONObject();
157                 jsonObject.put("statusCode", "202");
158                 jsonObject.put("message", "Success");
159                 jsonObject.put("distributionId", distributionId);
160                 
161                 // prepare request detail
162                 List<ServiceModelList> serviceModelVersionIdList = new ArrayList<ServiceModelList>();
163                 ServiceModelList serviceModelList1 = new ServiceModelList(); 
164                 serviceModelList1.setRecoveryAction(RecoveryAction.retry);
165                 serviceModelList1.setServiceModelVersionId(serviceModelVersionId);
166                 serviceModelVersionIdList.add(serviceModelList1);
167                 
168                 stubFor(post(urlPathMatching("/sdc/v1/catalog/services/TEST_serviceModelVersionId/distr.*"))
169                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(jsonObject.toString()).withStatus(HttpStatus.SC_ACCEPTED)));
170                 
171                 activateVnf.processActivateSDCRequest(requestId, operationalEnvironmentId, serviceModelVersionIdList, workloadContext, 
172                                                                                                         distributionDbRepository, serviceModelDbRepository);
173                 
174                 // insert record, status sent           
175                 OperationalEnvDistributionStatus distStatus = distributionDbRepository.findOne(sdcDistributionId);
176                 assertNotNull(distStatus);
177                 assertEquals(operationalEnvironmentId, distStatus.getOperationalEnvId());
178                 assertEquals(statusSent, distStatus.getDistributionIdStatus());         
179                 
180                 // insert record, status sent           
181                 OperationalEnvServiceModelStatus servStatus = serviceModelDbRepository.findOneByOperationalEnvIdAndServiceModelVersionId(operationalEnvironmentId, serviceModelVersionId);
182                 assertNotNull(servStatus);
183                 assertEquals(statusSent, servStatus.getServiceModelVersionDistrStatus());
184                 assertEquals(operationalEnvironmentId, servStatus.getOperationalEnvId());
185                 
186         }       
187         
188         @Test 
189         public void processActivateSDCRequestTest_409() throws Exception {
190
191                 // ERROR in asdc
192                 JSONObject jsonMessages = new JSONObject();
193                 jsonMessages.put("message", "Failure");
194                 jsonMessages.put("messageId", "SVC4675");
195                 jsonMessages.put("text", "Error: Service state is invalid for this action.");
196                 JSONObject jsonServException = new JSONObject();
197                 jsonServException.put("policyException", jsonMessages);
198                 //jsonServException.put("serviceException", jsonMessages);
199                 JSONObject jsonErrorResponse = new JSONObject();
200                 jsonErrorResponse.put("requestError", jsonServException);
201                 
202                 // prepare request detail
203                 List<ServiceModelList> serviceModelVersionIdList = new ArrayList<ServiceModelList>();
204                 ServiceModelList serviceModelList1 = new ServiceModelList(); 
205                 serviceModelList1.setRecoveryAction(RecoveryAction.retry);
206                 serviceModelList1.setServiceModelVersionId(serviceModelVersionId);
207                 serviceModelVersionIdList.add(serviceModelList1);
208                 
209                 InfraActiveRequests iar = new InfraActiveRequests();
210                 iar.setRequestId(requestId);
211                 iar.setRequestStatus("PENDING");
212                 infraActiveRequestsRepository.saveAndFlush(iar);
213                 
214                 stubFor(post(urlPathMatching("/sdc/v1/catalog/services/TEST_serviceModelVersionId/distr.*"))
215                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(jsonErrorResponse.toString()).withStatus(HttpStatus.SC_CONFLICT)));
216                 
217                 try {
218                         activateVnf.processActivateSDCRequest(requestId, operationalEnvironmentId, serviceModelVersionIdList, workloadContext, 
219                                                                                                         distributionDbRepository, serviceModelDbRepository);
220                         
221                 } catch (Exception  ex) {
222                         
223                         // insert record, status sent
224                         OperationalEnvServiceModelStatus servStatus = serviceModelDbRepository.findOneByOperationalEnvIdAndServiceModelVersionId(operationalEnvironmentId, serviceModelVersionId);
225                         assertNotNull(servStatus);
226                         assertEquals(statusSent, servStatus.getServiceModelVersionDistrStatus());
227                         
228                         InfraActiveRequests infraActiveRequest = infraActiveRequestsRepository.findOne(requestId);
229                         assertNotNull(infraActiveRequest);
230                         assertTrue(infraActiveRequest.getStatusMessage().contains("FAILURE"));
231                         assertTrue(infraActiveRequest.getRequestStatus().contains("FAILED"));                   
232                         
233                 }  
234                 
235                 infraActiveRequestsRepository.delete(requestId);
236         }               
237         
238 }