2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.apihandlerinfra.tenantisolation.process;
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;
49 public class DeactivateVnfOperationalEnvironmentTest extends BaseTest {
52 public ExpectedException thrown = ExpectedException.none();
55 private DeactivateVnfOperationalEnvironment deactivate;
57 private CloudOrchestrationRequest request = new CloudOrchestrationRequest();
58 private String operationalEnvironmentId = "ff3514e3-5a33-55df-13ab-12abad84e7ff";
59 private String requestId = "ff3514e3-5a33-55df-13ab-12abad84e7fe";
61 private ObjectMapper mapper = new ObjectMapper();
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)));
73 public void testDeactivateOperationalEnvironment() throws Exception {
74 request.setOperationalEnvironmentId(operationalEnvironmentId);
75 request.setRequestDetails(null);
77 String json = "{\"operational-environment-status\" : \"ACTIVE\"}";
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)));
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);
105 public void testDeactivateInvalidStatus() throws Exception {
106 request.setOperationalEnvironmentId(operationalEnvironmentId);
107 request.setRequestDetails(null);
109 String json = "{\"operational-environment-status\" : \"SUCCESS\"}";
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)));
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)));
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)));
136 deactivate.execute(requestId, request);
140 public void testDeactivateInactiveStatus() throws Exception {
141 request.setOperationalEnvironmentId(operationalEnvironmentId);
142 request.setRequestDetails(null);
144 String json = "{\"operational-environment-status\" : \"INACTIVE\"}";
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)));
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)));
161 deactivate.execute(requestId, request);
165 public void testDeactivateNullStatus() throws Exception {
166 request.setOperationalEnvironmentId(operationalEnvironmentId);
167 request.setRequestDetails(null);
169 String json = "{\"operational-environment-status\" : \"\"}";
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)));
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);