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.*;
24 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
25 import static org.junit.Assert.assertThat;
27 import com.fasterxml.jackson.core.JsonProcessingException;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29 import org.apache.http.HttpStatus;
30 import org.junit.Test;
31 import org.onap.so.apihandler.common.ErrorNumbers;
32 import org.onap.so.apihandlerinfra.BaseTest;
33 import org.onap.so.apihandlerinfra.exceptions.ApiException;
34 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
35 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
36 import org.onap.so.apihandlerinfra.tenantisolation.CloudOrchestrationRequest;
37 import org.onap.so.apihandlerinfra.tenantisolationbeans.OperationalEnvironment;
38 import org.onap.so.apihandlerinfra.tenantisolationbeans.RequestDetails;
39 import org.onap.so.apihandlerinfra.tenantisolationbeans.RequestInfo;
40 import org.onap.so.apihandlerinfra.tenantisolationbeans.RequestParameters;
41 import org.onap.so.client.aai.AAIVersion;
42 import org.onap.so.db.request.beans.InfraActiveRequests;
43 import org.onap.so.logger.MessageEnum;
44 import org.onap.so.logger.MsoLogger;
45 import org.springframework.beans.factory.annotation.Autowired;
47 import javax.ws.rs.core.HttpHeaders;
48 import javax.ws.rs.core.MediaType;
51 public class CreateEcompOperationalEnvironmentTest extends BaseTest{
54 private CreateEcompOperationalEnvironment createEcompOpEn;
55 private final ObjectMapper mapper = new ObjectMapper();
57 public CloudOrchestrationRequest getCloudOrchestrationRequest() {
58 CloudOrchestrationRequest request = new CloudOrchestrationRequest();
59 RequestDetails reqDetails = new RequestDetails();
60 RequestInfo reqInfo = new RequestInfo();
61 RequestParameters reqParams = new RequestParameters();
62 reqParams.setTenantContext("TEST");
63 reqParams.setWorkloadContext("ECOMP_TEST");
64 reqParams.setOperationalEnvironmentType(OperationalEnvironment.ECOMP);
65 reqInfo.setInstanceName("TEST_ECOMP_ENVIRONMENT");
66 reqDetails.setRequestInfo(reqInfo);
67 reqDetails.setRequestParameters(reqParams);
68 request.setRequestDetails(reqDetails);
69 request.setOperationalEnvironmentId("operationalEnvId");
75 public void testProcess() throws ApiException, JsonProcessingException {
76 stubFor(put(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
77 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_ACCEPTED)));
78 stubFor(post(urlPathMatching("/events/.*"))
79 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_ACCEPTED)));
81 InfraActiveRequests iar = new InfraActiveRequests();
82 iar.setRequestId("123");
83 iar.setOperationalEnvName("myOpEnv");
84 iar.setRequestScope("create");
85 iar.setRequestStatus("PENDING");
86 iar.setRequestAction("UNKNOWN");
87 stubFor(get(urlPathEqualTo("/infraActiveRequests/123"))
88 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
89 .withBody(mapper.writeValueAsString(iar))
90 .withStatus(HttpStatus.SC_OK)));
91 stubFor(post(urlPathEqualTo("/infraActiveRequests/"))
92 .withRequestBody(containing("{\"requestId\":\"123\",\"clientRequestId\":null,\"action\":null,\"requestStatus\":\"COMPLETE\",\"statusMessage\":\"SUCCESSFUL, operationalEnvironmentId - operationalEnvId; Success Message: SUCCESSFULLY Created ECOMP OperationalEnvironment.\",\"progress\":100"))
93 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
94 .withStatus(HttpStatus.SC_OK)));
96 createEcompOpEn.execute("123", getCloudOrchestrationRequest());
100 public void testProcessException() throws JsonProcessingException {
101 stubFor(put(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
102 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_ACCEPTED)));
103 stubFor(post(urlPathMatching("/events/.*"))
104 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_NOT_FOUND)));
105 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, MsoLogger.ErrorCode.DataError).build();
106 ValidateException expectedException = new ValidateException.Builder("Could not publish DMaap", HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER)
107 .errorInfo(errorLoggerInfo).build();
109 InfraActiveRequests iar = new InfraActiveRequests();
110 iar.setRequestId("123");
111 iar.setOperationalEnvName("myOpEnv");
112 iar.setRequestScope("create");
113 iar.setRequestStatus("PENDING");
114 iar.setRequestAction("UNKNOWN");
115 stubFor(get(urlPathEqualTo("/infraActiveRequests/123"))
116 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
117 .withBody(mapper.writeValueAsString(iar))
118 .withStatus(HttpStatus.SC_OK)));
119 stubFor(post(urlPathEqualTo("/infraActiveRequests/"))
120 .withRequestBody(containing("{\"requestId\":\"123\",\"clientRequestId\":null,\"action\":null,\"requestStatus\":\"FAILED\",\"statusMessage\":\"FAILURE, operationalEnvironmentId - operationalEnvId; Error message: empty"))
121 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
122 .withStatus(HttpStatus.SC_OK)));
125 createEcompOpEn.execute("123", getCloudOrchestrationRequest());
126 }catch(ApiException e){
127 assertThat(e, sameBeanAs((ApiException) expectedException).ignoring("cause"));