2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.apihandlerinfra.tenantisolation.process;
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;
34 import java.util.ArrayList;
35 import java.util.List;
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.so.apihandlerinfra.BaseTest;
46 import org.onap.so.apihandlerinfra.exceptions.ApiException;
47 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
48 import org.onap.so.apihandlerinfra.tenantisolation.CloudOrchestrationRequest;
49 import org.onap.so.apihandlerinfra.tenantisolation.helpers.AAIClientHelper;
50 import org.onap.so.apihandlerinfra.tenantisolationbeans.RequestDetails;
51 import org.onap.so.apihandlerinfra.tenantisolationbeans.RequestParameters;
52 import org.onap.so.apihandlerinfra.tenantisolationbeans.ServiceModelList;
53 import org.onap.so.apihandlerinfra.tenantisolationbeans.RecoveryAction;
54 import org.onap.so.apihandlerinfra.tenantisolationbeans.Manifest;
55 import org.onap.so.client.aai.AAIVersion;
56 import org.onap.so.client.aai.entities.AAIResultWrapper;
57 import org.onap.so.client.aai.objects.AAIOperationalEnvironment;
58 import org.onap.so.db.request.beans.InfraActiveRequests;
59 import org.springframework.beans.factory.annotation.Autowired;
61 import javax.ws.rs.core.HttpHeaders;
62 import javax.ws.rs.core.MediaType;
65 public class ActivateVnfOperationalEnvironmentTest extends BaseTest{
68 public ExpectedException thrown = ExpectedException.none();
71 private ActivateVnfOperationalEnvironment activateVnf;
73 private AAIClientHelper clientHelper;
75 private final String requestId = "TEST_requestId";
76 private final String operationalEnvironmentId = "1dfe7154-eae0-44f2-8e7a-8e5e7882e55d";
77 private final CloudOrchestrationRequest request = new CloudOrchestrationRequest();
78 private final String workloadContext = "PVT";
79 String recoveryActionRetry = "RETRY";
80 private final String serviceModelVersionId = "TEST_serviceModelVersionId";
82 private final String sdcDistributionId = "TEST_distributionId";
83 private final String statusSent = "SENT";
84 private final ObjectMapper mapper = new ObjectMapper();
88 stubFor(post(urlPathEqualTo("/operationalEnvServiceModelStatus/"))
89 .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}"))
90 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
91 .withStatus(HttpStatus.SC_OK)));
92 stubFor(post(urlPathEqualTo("/operationalEnvDistributionStatus/"))
93 .withRequestBody(equalTo("{\"distributionId\":\"TEST_distributionId\",\"operationalEnvId\":\"1dfe7154-eae0-44f2-8e7a-8e5e7882e55d\",\"serviceModelVersionId\":\"TEST_serviceModelVersionId\",\"requestId\":\"TEST_requestId\",\"distributionIdStatus\":\"SENT\",\"distributionIdErrorReason\":\"\",\"createTime\":null,\"modifyTime\":null}"))
94 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
95 .withStatus(HttpStatus.SC_OK)));
100 public void getAAIOperationalEnvironmentTest() {
102 AAIOperationalEnvironment aaiOpEnv;
104 stubFor(get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
105 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("vnfoperenv/ecompOperationalEnvironment.json").withStatus(HttpStatus.SC_ACCEPTED)));
107 AAIResultWrapper wrapper = clientHelper.getAaiOperationalEnvironment("EMOE-001");
108 aaiOpEnv = wrapper.asBean(AAIOperationalEnvironment.class).get();
109 assertEquals("EMOE-001", aaiOpEnv.getOperationalEnvironmentId());
110 assertNotNull(activateVnf.getAAIOperationalEnvironment(operationalEnvironmentId));
111 assertEquals( "EMOE-001", activateVnf.getAAIOperationalEnvironment(operationalEnvironmentId).getOperationalEnvironmentId());
116 public void executionTest() throws Exception {
118 List<ServiceModelList> serviceModelVersionIdList = new ArrayList<>();
119 ServiceModelList serviceModelList1 = new ServiceModelList();
120 serviceModelList1.setRecoveryAction(RecoveryAction.retry);
121 serviceModelList1.setServiceModelVersionId(serviceModelVersionId);
122 serviceModelVersionIdList.add(serviceModelList1);
124 RequestDetails requestDetails = new RequestDetails();
125 RequestParameters requestParameters = new RequestParameters();
126 Manifest manifest = new Manifest();
127 manifest.setServiceModelList(serviceModelVersionIdList);
128 requestParameters.setManifest(manifest);
129 requestParameters.setWorkloadContext(workloadContext);
130 requestDetails.setRequestParameters(requestParameters);
132 request.setOperationalEnvironmentId(operationalEnvironmentId);
133 request.setRequestDetails(requestDetails);
135 JSONObject jsonObject = new JSONObject();
136 jsonObject.put("statusCode", "202");
137 jsonObject.put("message", "Success");
138 jsonObject.put("distributionId", sdcDistributionId);
140 stubFor(get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
141 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("vnfoperenv/ecompOperationalEnvironment.json").withStatus(HttpStatus.SC_ACCEPTED)));
142 stubFor(post(urlPathMatching("/sdc/v1/catalog/services/TEST_serviceModelVersionId/distr.*"))
143 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(jsonObject.toString()).withStatus(HttpStatus.SC_ACCEPTED)));
144 activateVnf.execute(requestId, request);
148 public void processActivateSDCRequestTest_202() throws Exception {
150 String distributionId = "TEST_distributionId";
152 JSONObject jsonObject = new JSONObject();
153 jsonObject.put("statusCode", "202");
154 jsonObject.put("message", "Success");
155 jsonObject.put("distributionId", distributionId);
157 // prepare request detail
158 List<ServiceModelList> serviceModelVersionIdList = new ArrayList<>();
159 ServiceModelList serviceModelList1 = new ServiceModelList();
160 serviceModelList1.setRecoveryAction(RecoveryAction.retry);
161 serviceModelList1.setServiceModelVersionId(serviceModelVersionId);
162 serviceModelVersionIdList.add(serviceModelList1);
164 stubFor(post(urlPathMatching("/sdc/v1/catalog/services/TEST_serviceModelVersionId/distr.*"))
165 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(jsonObject.toString()).withStatus(HttpStatus.SC_ACCEPTED)));
167 activateVnf.processActivateSDCRequest(requestId, operationalEnvironmentId, serviceModelVersionIdList, workloadContext);
171 public void processActivateSDCRequestTest_409() throws ApiException, JsonProcessingException {
174 JSONObject jsonMessages = new JSONObject();
175 jsonMessages.put("message", "Failure");
176 jsonMessages.put("messageId", "SVC4675");
177 jsonMessages.put("text", "Error: Service state is invalid for this action.");
178 JSONObject jsonServException = new JSONObject();
179 jsonServException.put("policyException", jsonMessages);
180 //jsonServException.put("serviceException", jsonMessages);
181 JSONObject jsonErrorResponse = new JSONObject();
182 jsonErrorResponse.put("requestError", jsonServException);
184 // prepare request detail
185 List<ServiceModelList> serviceModelVersionIdList = new ArrayList<>();
186 ServiceModelList serviceModelList1 = new ServiceModelList();
187 serviceModelList1.setRecoveryAction(RecoveryAction.retry);
188 serviceModelList1.setServiceModelVersionId(serviceModelVersionId);
189 serviceModelVersionIdList.add(serviceModelList1);
191 InfraActiveRequests iar = new InfraActiveRequests();
192 iar.setRequestId(requestId);
193 iar.setRequestStatus("PENDING");
194 stubFor(get(urlPathEqualTo("/infraActiveRequests/"+requestId))
195 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
196 .withBody(mapper.writeValueAsString(iar))
197 .withStatus(HttpStatus.SC_OK)));
198 stubFor(post(urlPathMatching("/sdc/v1/catalog/services/TEST_serviceModelVersionId/distr.*"))
199 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(jsonErrorResponse.toString()).withStatus(HttpStatus.SC_CONFLICT)));
200 stubFor(post(urlPathEqualTo("/infraActiveRequests/"))
201 .withRequestBody(containing("operationalEnvId\":\"1dfe7154-eae0-44f2-8e7a-8e5e7882e55d\""))
202 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
203 .withStatus(HttpStatus.SC_OK)));
205 thrown.expect(ValidateException.class);
207 activateVnf.processActivateSDCRequest(requestId, operationalEnvironmentId, serviceModelVersionIdList, workloadContext);