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