c33eca9d66e9d74baa23b4b30ec0b8fb59dc7aa0
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.so.apihandlerinfra.tenantisolation.process;
22
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.get;
26 import static com.github.tomakehurst.wiremock.client.WireMock.post;
27 import static com.github.tomakehurst.wiremock.client.WireMock.put;
28 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
29 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
30 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
31 import static org.hamcrest.MatcherAssert.assertThat;
32 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
33 import java.util.UUID;
34 import javax.ws.rs.core.HttpHeaders;
35 import javax.ws.rs.core.MediaType;
36 import org.apache.http.HttpStatus;
37 import org.junit.Test;
38 import org.onap.so.apihandler.common.ErrorNumbers;
39 import org.onap.so.apihandlerinfra.BaseTest;
40 import org.onap.so.apihandlerinfra.exceptions.ApiException;
41 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
42 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
43 import org.onap.so.apihandlerinfra.tenantisolation.CloudOrchestrationRequest;
44 import org.onap.so.apihandlerinfra.tenantisolationbeans.OperationalEnvironment;
45 import org.onap.so.apihandlerinfra.tenantisolationbeans.RequestDetails;
46 import org.onap.so.apihandlerinfra.tenantisolationbeans.RequestInfo;
47 import org.onap.so.apihandlerinfra.tenantisolationbeans.RequestParameters;
48 import org.onap.aaiclient.client.aai.AAIVersion;
49 import org.onap.so.db.request.beans.InfraActiveRequests;
50 import org.onap.logging.filter.base.ErrorCode;
51 import org.onap.so.logger.MessageEnum;
52 import org.springframework.beans.factory.annotation.Autowired;
53 import com.fasterxml.jackson.core.JsonProcessingException;
54 import com.fasterxml.jackson.databind.ObjectMapper;
55
56
57 public class CreateEcompOperationalEnvironmentTest extends BaseTest {
58
59     @Autowired
60     private CreateEcompOperationalEnvironment createEcompOpEn;
61     private final ObjectMapper mapper = new ObjectMapper();
62
63     public CloudOrchestrationRequest getCloudOrchestrationRequest() {
64         CloudOrchestrationRequest request = new CloudOrchestrationRequest();
65         RequestDetails reqDetails = new RequestDetails();
66         RequestInfo reqInfo = new RequestInfo();
67         RequestParameters reqParams = new RequestParameters();
68         reqParams.setTenantContext("TEST");
69         reqParams.setWorkloadContext("ECOMP_TEST");
70         reqParams.setOperationalEnvironmentType(OperationalEnvironment.ECOMP);
71         reqInfo.setInstanceName("TEST_ECOMP_ENVIRONMENT");
72         reqDetails.setRequestInfo(reqInfo);
73         reqDetails.setRequestParameters(reqParams);
74         request.setRequestDetails(reqDetails);
75         request.setOperationalEnvironmentId("operationalEnvId");
76
77         return request;
78     }
79
80     @Test
81     public void testProcess() throws ApiException, JsonProcessingException {
82         wireMockServer.stubFor(
83                 put(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
84                         .willReturn(aResponse().withHeader("Content-Type", "application/json")
85                                 .withStatus(HttpStatus.SC_ACCEPTED)));
86         wireMockServer.stubFor(post(urlPathMatching("/events/.*")).willReturn(
87                 aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_ACCEPTED)));
88
89         InfraActiveRequests iar = new InfraActiveRequests();
90         iar.setRequestId("123");
91         iar.setOperationalEnvName("myOpEnv");
92         iar.setRequestScope("create");
93         iar.setRequestStatus("PENDING");
94         iar.setRequestAction("UNKNOWN");
95         wireMockServer.stubFor(get(urlPathEqualTo("/infraActiveRequests/123"))
96                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
97                         .withBody(mapper.writeValueAsString(iar)).withStatus(HttpStatus.SC_OK)));
98         wireMockServer.stubFor(post(urlPathEqualTo("/infraActiveRequests/")).withRequestBody(containing(
99                 "{\"requestId\":\"123\",\"requestStatus\":\"COMPLETE\",\"statusMessage\":\"SUCCESSFUL, operationalEnvironmentId - operationalEnvId; Success Message: SUCCESSFULLY Created ECOMP OperationalEnvironment.\",\"progress\":100"))
100                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
101                         .withStatus(HttpStatus.SC_OK)));
102
103         assertDoesNotThrow(() -> createEcompOpEn.execute("123", getCloudOrchestrationRequest()));
104     }
105
106     @Test
107     public void testProcessException() throws JsonProcessingException {
108         wireMockServer.stubFor(
109                 put(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
110                         .willReturn(aResponse().withHeader("Content-Type", "application/json")
111                                 .withStatus(HttpStatus.SC_ACCEPTED)));
112         wireMockServer.stubFor(post(urlPathMatching("/events/.*")).willReturn(
113                 aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_NOT_FOUND)));
114         ErrorLoggerInfo errorLoggerInfo =
115                 new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.DataError).build();
116         ValidateException expectedException = new ValidateException.Builder("Could not publish DMaap",
117                 HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).errorInfo(errorLoggerInfo).build();
118
119         InfraActiveRequests iar = new InfraActiveRequests();
120         String uuid = UUID.randomUUID().toString();
121         iar.setRequestId(uuid);
122         iar.setOperationalEnvName("myOpEnv");
123         iar.setRequestScope("create");
124         iar.setRequestStatus("PENDING");
125         iar.setRequestAction("UNKNOWN");
126         wireMockServer.stubFor(get(urlPathEqualTo("/infraActiveRequests/" + uuid))
127                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
128                         .withBody(mapper.writeValueAsString(iar)).withStatus(HttpStatus.SC_OK)));
129         wireMockServer.stubFor(post(urlPathEqualTo("/infraActiveRequests/"))
130                 .withRequestBody(containing("{\"requestId\":\"" + uuid
131                         + "\",\"requestStatus\":\"FAILED\",\"statusMessage\":\"FAILURE, operationalEnvironmentId - operationalEnvId; Error message:"))
132                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
133                         .withStatus(HttpStatus.SC_OK)));
134
135         try {
136             createEcompOpEn.execute(uuid, getCloudOrchestrationRequest());
137         } catch (ApiException e) {
138             assertThat(e, sameBeanAs((ApiException) expectedException).ignoring("cause"));
139         }
140     }
141
142 }