rename package for external use
[so.git] / mso-api-handlers / mso-api-handler-infra / src / test / java / org / onap / so / apihandlerinfra / tenantisolation / process / DeactivateVnfOperationalEnvironmentTest.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 org.hamcrest.Matchers.hasProperty;
31 import static org.hamcrest.Matchers.is;
32 import static org.hamcrest.Matchers.startsWith;
33 import javax.ws.rs.core.HttpHeaders;
34 import javax.ws.rs.core.MediaType;
35 import org.apache.http.HttpStatus;
36 import org.junit.Before;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.rules.ExpectedException;
40 import org.onap.so.apihandler.common.ErrorNumbers;
41 import org.onap.so.apihandlerinfra.BaseTest;
42 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
43 import org.onap.so.apihandlerinfra.tenantisolation.CloudOrchestrationRequest;
44 import org.onap.aaiclient.client.aai.AAIVersion;
45 import org.onap.so.db.request.beans.InfraActiveRequests;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import com.fasterxml.jackson.databind.ObjectMapper;
48
49 public class DeactivateVnfOperationalEnvironmentTest extends BaseTest {
50
51     @Rule
52     public ExpectedException thrown = ExpectedException.none();
53
54     @Autowired
55     private DeactivateVnfOperationalEnvironment deactivate;
56
57     private CloudOrchestrationRequest request = new CloudOrchestrationRequest();
58     private String operationalEnvironmentId = "ff3514e3-5a33-55df-13ab-12abad84e7ff";
59     private String requestId = "ff3514e3-5a33-55df-13ab-12abad84e7fe";
60
61     private ObjectMapper mapper = new ObjectMapper();
62
63     @Before
64     public void init() {
65         wireMockServer.stubFor(post(urlPathEqualTo("/infraActiveRequests/"))
66                 .withRequestBody(containing("{\"requestId\":\"" + requestId
67                         + "\",\"requestStatus\":\"COMPLETE\",\"statusMessage\":\"SUCCESSFUL"))
68                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
69                         .withStatus(HttpStatus.SC_OK)));
70     }
71
72     @Test
73     public void testDeactivateOperationalEnvironment() throws Exception {
74         request.setOperationalEnvironmentId(operationalEnvironmentId);
75         request.setRequestDetails(null);
76
77         String json = "{\"operational-environment-status\" : \"ACTIVE\"}";
78
79         wireMockServer.stubFor(
80                 get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
81                         .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(json)
82                                 .withStatus(HttpStatus.SC_ACCEPTED)));
83         wireMockServer.stubFor(
84                 put(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
85                         .willReturn(aResponse().withHeader("Content-Type", "application/json")
86                                 .withStatus(HttpStatus.SC_ACCEPTED)));
87         wireMockServer.stubFor(
88                 post(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
89                         .willReturn(aResponse().withHeader("Content-Type", "application/json")
90                                 .withStatus(HttpStatus.SC_ACCEPTED)));
91
92         InfraActiveRequests iar = new InfraActiveRequests();
93         iar.setRequestId(requestId);
94         iar.setOperationalEnvName("myOpEnv");
95         iar.setRequestScope("create");
96         iar.setRequestStatus("PENDING");
97         iar.setRequestAction("UNKNOWN");
98         wireMockServer.stubFor(get(urlPathEqualTo("/infraActiveRequests/" + requestId))
99                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
100                         .withBody(mapper.writeValueAsString(iar)).withStatus(HttpStatus.SC_OK)));
101         deactivate.execute(requestId, request);
102     }
103
104     @Test
105     public void testDeactivateInvalidStatus() throws Exception {
106         request.setOperationalEnvironmentId(operationalEnvironmentId);
107         request.setRequestDetails(null);
108
109         String json = "{\"operational-environment-status\" : \"SUCCESS\"}";
110
111         wireMockServer.stubFor(
112                 get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
113                         .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(json)
114                                 .withStatus(HttpStatus.SC_ACCEPTED)));
115
116         thrown.expect(ValidateException.class);
117         thrown.expectMessage(startsWith("Invalid OperationalEnvironmentStatus on OperationalEnvironmentId: "));
118         thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_BAD_REQUEST)));
119         thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_DETAILED_SERVICE_ERROR)));
120
121         InfraActiveRequests iar = new InfraActiveRequests();
122         iar.setRequestId(requestId);
123         iar.setOperationalEnvName("myOpEnv");
124         iar.setRequestScope("create");
125         iar.setRequestStatus("PENDING");
126         iar.setRequestAction("UNKNOWN");
127         wireMockServer.stubFor(get(urlPathEqualTo("/infraActiveRequests/" + requestId))
128                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
129                         .withBody(mapper.writeValueAsString(iar)).withStatus(HttpStatus.SC_OK)));
130         wireMockServer.stubFor(post(urlPathEqualTo("/infraActiveRequests/"))
131                 .withRequestBody(containing("{\"requestId\":\"" + requestId
132                         + "\",\"requestStatus\":\"FAILED\",\"statusMessage\":\"FAILURE"))
133                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
134                         .withStatus(HttpStatus.SC_OK)));
135
136         deactivate.execute(requestId, request);
137     }
138
139     @Test
140     public void testDeactivateInactiveStatus() throws Exception {
141         request.setOperationalEnvironmentId(operationalEnvironmentId);
142         request.setRequestDetails(null);
143
144         String json = "{\"operational-environment-status\" : \"INACTIVE\"}";
145
146         wireMockServer.stubFor(
147                 get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
148                         .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(json)
149                                 .withStatus(HttpStatus.SC_ACCEPTED)));
150
151         InfraActiveRequests iar = new InfraActiveRequests();
152         iar.setRequestId(requestId);
153         iar.setOperationalEnvName("myOpEnv");
154         iar.setRequestScope("create");
155         iar.setRequestStatus("PENDING");
156         iar.setRequestAction("UNKNOWN");
157         wireMockServer.stubFor(get(urlPathEqualTo("/infraActiveRequests/" + requestId))
158                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
159                         .withBody(mapper.writeValueAsString(iar)).withStatus(HttpStatus.SC_OK)));
160
161         deactivate.execute(requestId, request);
162     }
163
164     @Test
165     public void testDeactivateNullStatus() throws Exception {
166         request.setOperationalEnvironmentId(operationalEnvironmentId);
167         request.setRequestDetails(null);
168
169         String json = "{\"operational-environment-status\" : \"\"}";
170
171         wireMockServer.stubFor(
172                 get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
173                         .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(json)
174                                 .withStatus(HttpStatus.SC_ACCEPTED)));
175
176         thrown.expect(ValidateException.class);
177         thrown.expectMessage(startsWith("OperationalEnvironmentStatus is null on OperationalEnvironmentId: "));
178         thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_BAD_REQUEST)));
179         thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_DETAILED_SERVICE_ERROR)));
180         deactivate.execute(requestId, request);
181     }
182 }
183