Containerization feature of SO
[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.post;
25 import static com.github.tomakehurst.wiremock.client.WireMock.put;
26 import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
27 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
28 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
29 import static org.junit.Assert.assertNotNull;
30 import static org.junit.Assert.assertThat;
31 import static org.junit.Assert.assertTrue;
32
33 import org.apache.http.HttpStatus;
34 import org.junit.Test;
35 import org.onap.so.apihandler.common.ErrorNumbers;
36 import org.onap.so.apihandlerinfra.BaseTest;
37 import org.onap.so.apihandlerinfra.exceptions.ApiException;
38 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
39 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
40 import org.onap.so.apihandlerinfra.tenantisolation.CloudOrchestrationRequest;
41 import org.onap.so.apihandlerinfra.tenantisolationbeans.OperationalEnvironment;
42 import org.onap.so.apihandlerinfra.tenantisolationbeans.RequestDetails;
43 import org.onap.so.apihandlerinfra.tenantisolationbeans.RequestInfo;
44 import org.onap.so.apihandlerinfra.tenantisolationbeans.RequestParameters;
45 import org.onap.so.client.aai.AAIVersion;
46 import org.onap.so.db.request.beans.InfraActiveRequests;
47 import org.onap.so.db.request.data.repository.InfraActiveRequestsRepository;
48 import org.onap.so.logger.MessageEnum;
49 import org.onap.so.logger.MsoLogger;
50 import org.springframework.beans.factory.annotation.Autowired;
51
52
53 public class CreateEcompOperationalEnvironmentTest extends BaseTest{
54         
55         @Autowired
56         private CreateEcompOperationalEnvironment createEcompOpEn;
57         @Autowired
58         private InfraActiveRequestsRepository infraActiveRequestsRepository;
59         
60         public CloudOrchestrationRequest getCloudOrchestrationRequest() {
61                 CloudOrchestrationRequest request = new CloudOrchestrationRequest();
62                 RequestDetails reqDetails = new RequestDetails();
63                 RequestInfo reqInfo = new RequestInfo();
64                 RequestParameters reqParams = new RequestParameters();
65                 reqParams.setTenantContext("TEST");
66                 reqParams.setWorkloadContext("ECOMP_TEST");
67                 reqParams.setOperationalEnvironmentType(OperationalEnvironment.ECOMP);
68                 reqInfo.setInstanceName("TEST_ECOMP_ENVIRONMENT");
69                 reqDetails.setRequestInfo(reqInfo);
70                 reqDetails.setRequestParameters(reqParams);
71                 request.setRequestDetails(reqDetails);
72                 request.setOperationalEnvironmentId("operationalEnvId");
73                 
74                 return request;
75         }
76         
77         @Test
78         public void testProcess() throws ApiException {
79                 stubFor(put(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
80                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_ACCEPTED)));
81                 stubFor(post(urlPathMatching("/events/.*"))
82                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_ACCEPTED)));
83                 
84                 InfraActiveRequests iar = new InfraActiveRequests();
85                 iar.setRequestId("123");
86                 iar.setOperationalEnvName("myOpEnv");
87                 iar.setRequestScope("create");
88                 iar.setRequestStatus("PENDING");
89                 iar.setRequestAction("UNKNOWN");
90                 infraActiveRequestsRepository.saveAndFlush(iar);
91                 
92                 createEcompOpEn.execute("123", getCloudOrchestrationRequest());
93                 
94                 InfraActiveRequests infraActiveRequest = infraActiveRequestsRepository.findOneByRequestId("123");
95                 assertNotNull(infraActiveRequest);
96                 assertTrue(infraActiveRequest.getStatusMessage().contains("SUCCESS"));  
97                 assertTrue(infraActiveRequest.getRequestStatus().equals("COMPLETE"));
98         }
99         
100         @Test
101         public void testProcessException() {
102                 stubFor(put(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
103                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_ACCEPTED)));
104                 stubFor(post(urlPathMatching("/events/.*"))
105                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_NOT_FOUND)));
106         ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, MsoLogger.ErrorCode.DataError).build();
107         ValidateException expectedException = new ValidateException.Builder("Could not publish DMaap", HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER)
108                 .errorInfo(errorLoggerInfo).build();
109         
110         InfraActiveRequests iar = new InfraActiveRequests();
111                 iar.setRequestId("123");
112                 iar.setOperationalEnvName("myOpEnv");
113                 iar.setRequestScope("create");
114                 iar.setRequestStatus("PENDING");
115                 iar.setRequestAction("UNKNOWN");
116                 infraActiveRequestsRepository.saveAndFlush(iar);
117         
118                 try {
119             createEcompOpEn.execute("123", getCloudOrchestrationRequest());
120         }catch(ApiException e){
121             assertThat(e, sameBeanAs((ApiException) expectedException).ignoring("cause"));
122         }
123
124                 InfraActiveRequests infraActiveRequest = infraActiveRequestsRepository.findOneByRequestId("123");
125                 assertNotNull(infraActiveRequest);
126                 assertTrue(infraActiveRequest.getStatusMessage().contains("FAILURE"));  
127                 assertTrue(infraActiveRequest.getRequestStatus().equals("FAILED"));
128         }
129
130 }