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