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.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;
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 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";
83 private final String sdcDistributionId = "TEST_distributionId";
84 private final String statusSent = "SENT";
85 private final ObjectMapper mapper = new ObjectMapper();
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)));
101 public void getAAIOperationalEnvironmentTest() {
103 OperationalEnvironment aaiOpEnv;
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)));
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());
118 public void executionTest() throws Exception {
120 List<ServiceModelList> serviceModelVersionIdList = new ArrayList<>();
121 ServiceModelList serviceModelList1 = new ServiceModelList();
122 serviceModelList1.setRecoveryAction(RecoveryAction.retry);
123 serviceModelList1.setServiceModelVersionId(serviceModelVersionId);
124 serviceModelVersionIdList.add(serviceModelList1);
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);
134 request.setOperationalEnvironmentId(vnfOperationalEnvironmentId);
135 request.setRequestDetails(requestDetails);
137 JSONObject jsonObject = new JSONObject();
138 jsonObject.put("statusCode", "202");
139 jsonObject.put("message", "Success");
140 jsonObject.put("distributionId", sdcDistributionId);
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);
150 public void processActivateSDCRequestTest_202() throws Exception {
152 String distributionId = "TEST_distributionId";
154 JSONObject jsonObject = new JSONObject();
155 jsonObject.put("statusCode", "202");
156 jsonObject.put("message", "Success");
157 jsonObject.put("distributionId", distributionId);
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);
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)));
169 activateVnf.processActivateSDCRequest(requestId, operationalEnvironmentId, serviceModelVersionIdList, workloadContext, vnfOperationalEnvironmentId);
173 public void processActivateSDCRequestTest_409() throws ApiException, JsonProcessingException {
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);
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);
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)));
207 thrown.expect(ValidateException.class);
209 activateVnf.processActivateSDCRequest(requestId, operationalEnvironmentId, serviceModelVersionIdList, workloadContext, vnfOperationalEnvironmentId);