65ec5fade63285551841fed94fad939bbf8a0b21
[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.*;
24 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
25 import static org.junit.Assert.assertThat;
26 import java.util.UUID;
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.ErrorCode;
44 import org.onap.so.logger.MessageEnum;
45 import org.springframework.beans.factory.annotation.Autowired;
46
47 import javax.ws.rs.core.HttpHeaders;
48 import javax.ws.rs.core.MediaType;
49
50
51 public class CreateEcompOperationalEnvironmentTest extends BaseTest{
52         
53         @Autowired
54         private CreateEcompOperationalEnvironment createEcompOpEn;
55         private final ObjectMapper mapper = new ObjectMapper();
56
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");
70                 
71                 return request;
72         }
73         
74         @Test
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)));
80                 
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.\",\"rollbackStatusMessage\":null,\"flowStatus\":null,\"retryStatusMessage\":null,\"progress\":100"))
93                                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
94                                                 .withStatus(HttpStatus.SC_OK)));
95                 
96                 createEcompOpEn.execute("123", getCloudOrchestrationRequest());
97         }
98         
99         @Test
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, 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();
108         
109         InfraActiveRequests iar = new InfraActiveRequests();
110         String uuid = UUID.randomUUID().toString();
111                 iar.setRequestId(uuid);
112                 iar.setOperationalEnvName("myOpEnv");
113                 iar.setRequestScope("create");
114                 iar.setRequestStatus("PENDING");
115                 iar.setRequestAction("UNKNOWN");
116                 stubFor(get(urlPathEqualTo("/infraActiveRequests/"+uuid))
117                                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
118                                                 .withBody(mapper.writeValueAsString(iar))
119                                                 .withStatus(HttpStatus.SC_OK)));
120                 stubFor(post(urlPathEqualTo("/infraActiveRequests/"))
121                                 .withRequestBody(containing("{\"requestId\":\""+uuid+ "\",\"clientRequestId\":null,\"action\":null,\"requestStatus\":\"FAILED\",\"statusMessage\":\"FAILURE, operationalEnvironmentId - operationalEnvId; Error message:"))
122                                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
123                                                 .withStatus(HttpStatus.SC_OK)));
124
125                 try {
126             createEcompOpEn.execute(uuid, getCloudOrchestrationRequest());
127         }catch(ApiException e){
128             assertThat(e, sameBeanAs((ApiException) expectedException).ignoring("cause"));
129         }
130         }
131
132 }