Saved vnfOperationalEnvironmentId to be used later
[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 com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
29 import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
30 import static com.github.tomakehurst.wiremock.client.WireMock.containing;
31 import static org.junit.Assert.assertEquals;
32 import static org.junit.Assert.assertNotNull;
33
34 import java.util.ArrayList;
35 import java.util.List;
36
37 import com.fasterxml.jackson.core.JsonProcessingException;
38 import com.fasterxml.jackson.databind.ObjectMapper;
39 import org.apache.http.HttpStatus;
40 import org.json.JSONObject;
41 import org.junit.Before;
42 import org.junit.Rule;
43 import org.junit.Test;
44 import org.junit.rules.ExpectedException;
45 import org.onap.aai.domain.yang.OperationalEnvironment;
46 import org.onap.so.apihandlerinfra.BaseTest;
47 import org.onap.so.apihandlerinfra.exceptions.ApiException;
48 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
49 import org.onap.so.apihandlerinfra.tenantisolation.CloudOrchestrationRequest;
50 import org.onap.so.apihandlerinfra.tenantisolation.helpers.AAIClientHelper;
51 import org.onap.so.apihandlerinfra.tenantisolationbeans.RequestDetails;
52 import org.onap.so.apihandlerinfra.tenantisolationbeans.RequestParameters;
53 import org.onap.so.apihandlerinfra.tenantisolationbeans.ServiceModelList;
54 import org.onap.so.apihandlerinfra.tenantisolationbeans.RecoveryAction;
55 import org.onap.so.apihandlerinfra.tenantisolationbeans.Manifest;
56 import org.onap.so.client.aai.AAIVersion;
57 import org.onap.so.client.aai.entities.AAIResultWrapper;
58 import org.onap.so.db.request.beans.InfraActiveRequests;
59 import org.springframework.beans.factory.annotation.Autowired;
60
61 import javax.ws.rs.core.HttpHeaders;
62 import javax.ws.rs.core.MediaType;
63
64
65 public class ActivateVnfOperationalEnvironmentTest extends BaseTest{
66
67         @Rule
68         public ExpectedException thrown = ExpectedException.none();
69
70         @Autowired
71         private ActivateVnfOperationalEnvironment activateVnf;
72         @Autowired
73         private AAIClientHelper clientHelper;
74
75         private final String requestId = "TEST_requestId";
76         private final String operationalEnvironmentId = "1dfe7154-eae0-44f2-8e7a-8e5e7882e55d"; 
77         private final String vnfOperationalEnvironmentId = "1dfe7154-eae0-44f2-8e7a-8e5e7882e66d";
78         private final CloudOrchestrationRequest request = new CloudOrchestrationRequest();
79         private final String workloadContext = "PVT";
80         String recoveryActionRetry  = "RETRY";
81         private final String serviceModelVersionId = "TEST_serviceModelVersionId";      
82         int retryCount = 3;
83         private final String sdcDistributionId = "TEST_distributionId";
84         private final String statusSent = "SENT";
85         private final ObjectMapper mapper = new ObjectMapper();
86
87         @Before
88         public void init(){
89                 stubFor(post(urlPathEqualTo("/operationalEnvServiceModelStatus/"))
90                                 .withRequestBody(equalTo("{\"requestId\":\"TEST_requestId\",\"operationalEnvId\":\"1dfe7154-eae0-44f2-8e7a-8e5e7882e55d\",\"serviceModelVersionId\":\"TEST_serviceModelVersionId\",\"serviceModelVersionDistrStatus\":\"SENT\",\"recoveryAction\":\"RETRY\",\"retryCount\":3,\"workloadContext\":\"PVT\",\"createTime\":null,\"modifyTime\":null,\"vnfOperationalEnvId\":\"1dfe7154-eae0-44f2-8e7a-8e5e7882e66d\"}"))
91                                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
92                                                 .withStatus(HttpStatus.SC_OK)));
93                 stubFor(post(urlPathEqualTo("/operationalEnvDistributionStatus/"))
94                                 .withRequestBody(equalTo("{\"distributionId\":\"TEST_distributionId\",\"operationalEnvId\":\"1dfe7154-eae0-44f2-8e7a-8e5e7882e55d\",\"serviceModelVersionId\":\"TEST_serviceModelVersionId\",\"requestId\":\"TEST_requestId\",\"distributionIdStatus\":\"SENT\",\"distributionIdErrorReason\":\"\",\"createTime\":null,\"modifyTime\":null}"))
95                                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
96                                                 .withStatus(HttpStatus.SC_OK)));
97         }
98         
99
100         @Test
101         public void getAAIOperationalEnvironmentTest() {
102
103                 OperationalEnvironment aaiOpEnv;
104
105                 stubFor(get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
106                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("vnfoperenv/ecompOperationalEnvironmentWithRelationship.json").withStatus(HttpStatus.SC_ACCEPTED)));
107                 
108                 AAIResultWrapper wrapper = clientHelper.getAaiOperationalEnvironment("EMOE-001");
109                 aaiOpEnv = wrapper.asBean(OperationalEnvironment.class).get();
110                 assertEquals("EMOE-001", aaiOpEnv.getOperationalEnvironmentId());                       
111                 assertEquals("1dfe7154-eae0-44f2-8e7a-8e5e7882e55d", aaiOpEnv.getRelationshipList().getRelationship().get(0).getRelationshipData().get(0).getRelationshipValue());
112                 assertNotNull(activateVnf.getAAIOperationalEnvironment(operationalEnvironmentId));      
113                 assertEquals( "EMOE-001", activateVnf.getAAIOperationalEnvironment(operationalEnvironmentId).asBean(OperationalEnvironment.class).get().getOperationalEnvironmentId());         
114                 
115         }       
116                 
117         @Test
118         public void executionTest() throws Exception {
119
120                 List<ServiceModelList> serviceModelVersionIdList = new ArrayList<>();
121                 ServiceModelList serviceModelList1 = new ServiceModelList(); 
122                 serviceModelList1.setRecoveryAction(RecoveryAction.retry);
123                 serviceModelList1.setServiceModelVersionId(serviceModelVersionId);
124                 serviceModelVersionIdList.add(serviceModelList1);
125                 
126                 RequestDetails requestDetails = new RequestDetails();
127                 RequestParameters requestParameters = new RequestParameters();
128                 Manifest manifest = new Manifest();
129                 manifest.setServiceModelList(serviceModelVersionIdList);
130                 requestParameters.setManifest(manifest);
131                 requestParameters.setWorkloadContext(workloadContext);
132                 requestDetails.setRequestParameters(requestParameters);
133                 
134                 request.setOperationalEnvironmentId(vnfOperationalEnvironmentId);
135                 request.setRequestDetails(requestDetails);
136                 
137                 JSONObject jsonObject = new JSONObject();
138                 jsonObject.put("statusCode", "202");
139                 jsonObject.put("message", "Success");
140                 jsonObject.put("distributionId", sdcDistributionId);
141                 
142                 stubFor(get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
143                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("vnfoperenv/activateOperationalEnvironmentWithRelationship.json").withStatus(HttpStatus.SC_ACCEPTED)));
144                 stubFor(post(urlPathMatching("/sdc/v1/catalog/services/TEST_serviceModelVersionId/distr.*"))
145                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(jsonObject.toString()).withStatus(HttpStatus.SC_ACCEPTED)));
146                 activateVnf.execute(requestId, request);
147         }                       
148         
149         @Test
150         public void processActivateSDCRequestTest_202() throws Exception {
151
152                 String distributionId = "TEST_distributionId";
153                 
154                 JSONObject jsonObject = new JSONObject();
155                 jsonObject.put("statusCode", "202");
156                 jsonObject.put("message", "Success");
157                 jsonObject.put("distributionId", distributionId);
158                 
159                 // prepare request detail
160                 List<ServiceModelList> serviceModelVersionIdList = new ArrayList<>();
161                 ServiceModelList serviceModelList1 = new ServiceModelList(); 
162                 serviceModelList1.setRecoveryAction(RecoveryAction.retry);
163                 serviceModelList1.setServiceModelVersionId(serviceModelVersionId);
164                 serviceModelVersionIdList.add(serviceModelList1);
165                 
166                 stubFor(post(urlPathMatching("/sdc/v1/catalog/services/TEST_serviceModelVersionId/distr.*"))
167                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(jsonObject.toString()).withStatus(HttpStatus.SC_ACCEPTED)));
168                 
169                 activateVnf.processActivateSDCRequest(requestId, operationalEnvironmentId, serviceModelVersionIdList, workloadContext, vnfOperationalEnvironmentId);
170         }       
171         
172         @Test
173         public void processActivateSDCRequestTest_409() throws ApiException, JsonProcessingException {
174
175                 // ERROR in asdc
176                 JSONObject jsonMessages = new JSONObject();
177                 jsonMessages.put("message", "Failure");
178                 jsonMessages.put("messageId", "SVC4675");
179                 jsonMessages.put("text", "Error: Service state is invalid for this action.");
180                 JSONObject jsonServException = new JSONObject();
181                 jsonServException.put("policyException", jsonMessages);
182                 //jsonServException.put("serviceException", jsonMessages);
183                 JSONObject jsonErrorResponse = new JSONObject();
184                 jsonErrorResponse.put("requestError", jsonServException);
185                 
186                 // prepare request detail
187                 List<ServiceModelList> serviceModelVersionIdList = new ArrayList<>();
188                 ServiceModelList serviceModelList1 = new ServiceModelList(); 
189                 serviceModelList1.setRecoveryAction(RecoveryAction.retry);
190                 serviceModelList1.setServiceModelVersionId(serviceModelVersionId);
191                 serviceModelVersionIdList.add(serviceModelList1);
192                 
193                 InfraActiveRequests iar = new InfraActiveRequests();
194                 iar.setRequestId(requestId);
195                 iar.setRequestStatus("PENDING");
196                 stubFor(get(urlPathEqualTo("/infraActiveRequests/"+requestId))
197                                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
198                                                 .withBody(mapper.writeValueAsString(iar))
199                                                 .withStatus(HttpStatus.SC_OK)));
200                 stubFor(post(urlPathMatching("/sdc/v1/catalog/services/TEST_serviceModelVersionId/distr.*"))
201                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(jsonErrorResponse.toString()).withStatus(HttpStatus.SC_CONFLICT)));
202                 stubFor(post(urlPathEqualTo("/infraActiveRequests/"))
203                                 .withRequestBody(containing("operationalEnvId\":\"1dfe7154-eae0-44f2-8e7a-8e5e7882e55d\""))
204                                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
205                                                 .withStatus(HttpStatus.SC_OK)));
206
207                 thrown.expect(ValidateException.class);
208
209                 activateVnf.processActivateSDCRequest(requestId, operationalEnvironmentId, serviceModelVersionIdList, workloadContext, vnfOperationalEnvironmentId);
210         }               
211         
212 }