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 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 OperationalEnvironment aaiOpEnv;
104 stubFor(get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
105 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("vnfoperenv/ecompOperationalEnvironmentWithRelationship.json").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).getRelationshipData().get(0).getRelationshipValue());
111 assertNotNull(activateVnf.getAAIOperationalEnvironment(operationalEnvironmentId));
112 assertEquals( "EMOE-001", activateVnf.getAAIOperationalEnvironment(operationalEnvironmentId).asBean(OperationalEnvironment.class).get().getOperationalEnvironmentId());
117 public void executionTest() throws Exception {
119 List<ServiceModelList> serviceModelVersionIdList = new ArrayList<>();
120 ServiceModelList serviceModelList1 = new ServiceModelList();
121 serviceModelList1.setRecoveryAction(RecoveryAction.retry);
122 serviceModelList1.setServiceModelVersionId(serviceModelVersionId);
123 serviceModelVersionIdList.add(serviceModelList1);
125 RequestDetails requestDetails = new RequestDetails();
126 RequestParameters requestParameters = new RequestParameters();
127 Manifest manifest = new Manifest();
128 manifest.setServiceModelList(serviceModelVersionIdList);
129 requestParameters.setManifest(manifest);
130 requestParameters.setWorkloadContext(workloadContext);
131 requestDetails.setRequestParameters(requestParameters);
133 request.setOperationalEnvironmentId(operationalEnvironmentId);
134 request.setRequestDetails(requestDetails);
136 JSONObject jsonObject = new JSONObject();
137 jsonObject.put("statusCode", "202");
138 jsonObject.put("message", "Success");
139 jsonObject.put("distributionId", sdcDistributionId);
141 stubFor(get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
142 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("vnfoperenv/ecompOperationalEnvironmentWithRelationship.json").withStatus(HttpStatus.SC_ACCEPTED)));
143 stubFor(post(urlPathMatching("/sdc/v1/catalog/services/TEST_serviceModelVersionId/distr.*"))
144 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(jsonObject.toString()).withStatus(HttpStatus.SC_ACCEPTED)));
145 activateVnf.execute(requestId, request);
149 public void processActivateSDCRequestTest_202() throws Exception {
151 String distributionId = "TEST_distributionId";
153 JSONObject jsonObject = new JSONObject();
154 jsonObject.put("statusCode", "202");
155 jsonObject.put("message", "Success");
156 jsonObject.put("distributionId", distributionId);
158 // prepare request detail
159 List<ServiceModelList> serviceModelVersionIdList = new ArrayList<>();
160 ServiceModelList serviceModelList1 = new ServiceModelList();
161 serviceModelList1.setRecoveryAction(RecoveryAction.retry);
162 serviceModelList1.setServiceModelVersionId(serviceModelVersionId);
163 serviceModelVersionIdList.add(serviceModelList1);
165 stubFor(post(urlPathMatching("/sdc/v1/catalog/services/TEST_serviceModelVersionId/distr.*"))
166 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(jsonObject.toString()).withStatus(HttpStatus.SC_ACCEPTED)));
168 activateVnf.processActivateSDCRequest(requestId, operationalEnvironmentId, serviceModelVersionIdList, workloadContext);
172 public void processActivateSDCRequestTest_409() throws ApiException, JsonProcessingException {
175 JSONObject jsonMessages = new JSONObject();
176 jsonMessages.put("message", "Failure");
177 jsonMessages.put("messageId", "SVC4675");
178 jsonMessages.put("text", "Error: Service state is invalid for this action.");
179 JSONObject jsonServException = new JSONObject();
180 jsonServException.put("policyException", jsonMessages);
181 //jsonServException.put("serviceException", jsonMessages);
182 JSONObject jsonErrorResponse = new JSONObject();
183 jsonErrorResponse.put("requestError", jsonServException);
185 // prepare request detail
186 List<ServiceModelList> serviceModelVersionIdList = new ArrayList<>();
187 ServiceModelList serviceModelList1 = new ServiceModelList();
188 serviceModelList1.setRecoveryAction(RecoveryAction.retry);
189 serviceModelList1.setServiceModelVersionId(serviceModelVersionId);
190 serviceModelVersionIdList.add(serviceModelList1);
192 InfraActiveRequests iar = new InfraActiveRequests();
193 iar.setRequestId(requestId);
194 iar.setRequestStatus("PENDING");
195 stubFor(get(urlPathEqualTo("/infraActiveRequests/"+requestId))
196 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
197 .withBody(mapper.writeValueAsString(iar))
198 .withStatus(HttpStatus.SC_OK)));
199 stubFor(post(urlPathMatching("/sdc/v1/catalog/services/TEST_serviceModelVersionId/distr.*"))
200 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(jsonErrorResponse.toString()).withStatus(HttpStatus.SC_CONFLICT)));
201 stubFor(post(urlPathEqualTo("/infraActiveRequests/"))
202 .withRequestBody(containing("operationalEnvId\":\"1dfe7154-eae0-44f2-8e7a-8e5e7882e55d\""))
203 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
204 .withStatus(HttpStatus.SC_OK)));
206 thrown.expect(ValidateException.class);
208 activateVnf.processActivateSDCRequest(requestId, operationalEnvironmentId, serviceModelVersionIdList, workloadContext);