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;
34 import javax.ws.rs.core.HttpHeaders;
35 import javax.ws.rs.core.MediaType;
37 import org.apache.http.HttpStatus;
38 import org.junit.Before;
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.junit.rules.ExpectedException;
42 import org.onap.so.apihandler.common.ErrorNumbers;
43 import org.onap.so.apihandlerinfra.BaseTest;
44 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
45 import org.onap.so.apihandlerinfra.tenantisolation.CloudOrchestrationRequest;
46 import org.onap.so.client.aai.AAIVersion;
47 import org.onap.so.db.request.beans.InfraActiveRequests;
48 import org.springframework.beans.factory.annotation.Autowired;
50 import com.fasterxml.jackson.databind.ObjectMapper;
52 public class DeactivateVnfOperationalEnvironmentTest extends BaseTest{
55 public ExpectedException thrown = ExpectedException.none();
58 private DeactivateVnfOperationalEnvironment deactivate;
60 private CloudOrchestrationRequest request = new CloudOrchestrationRequest();
61 private String operationalEnvironmentId = "ff3514e3-5a33-55df-13ab-12abad84e7ff";
62 private String requestId = "ff3514e3-5a33-55df-13ab-12abad84e7fe";
64 private ObjectMapper mapper = new ObjectMapper();
68 wireMockServer.stubFor(post(urlPathEqualTo("/infraActiveRequests/"))
69 .withRequestBody(containing("{\"requestId\":\""+ requestId+"\",\"clientRequestId\":null,\"action\":null,\"requestStatus\":\"COMPLETE\",\"statusMessage\":\"SUCCESSFUL"))
70 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
71 .withStatus(HttpStatus.SC_OK)));
74 public void testDeactivateOperationalEnvironment() throws Exception {
75 request.setOperationalEnvironmentId(operationalEnvironmentId);
76 request.setRequestDetails(null);
78 String json = "{\"operational-environment-status\" : \"ACTIVE\"}";
80 wireMockServer.stubFor(get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
81 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(json).withStatus(HttpStatus.SC_ACCEPTED)));
82 wireMockServer.stubFor(put(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
83 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_ACCEPTED)));
84 wireMockServer.stubFor(post(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
85 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_ACCEPTED)));
87 InfraActiveRequests iar = new InfraActiveRequests();
88 iar.setRequestId(requestId);
89 iar.setOperationalEnvName("myOpEnv");
90 iar.setRequestScope("create");
91 iar.setRequestStatus("PENDING");
92 iar.setRequestAction("UNKNOWN");
93 wireMockServer.stubFor(get(urlPathEqualTo("/infraActiveRequests/"+requestId))
94 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
95 .withBody(mapper.writeValueAsString(iar))
96 .withStatus(HttpStatus.SC_OK)));
97 deactivate.execute(requestId, request);
101 public void testDeactivateInvalidStatus() throws Exception {
102 request.setOperationalEnvironmentId(operationalEnvironmentId);
103 request.setRequestDetails(null);
105 String json = "{\"operational-environment-status\" : \"SUCCESS\"}";
107 wireMockServer.stubFor(get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
108 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(json).withStatus(HttpStatus.SC_ACCEPTED)));
110 thrown.expect(ValidateException.class);
111 thrown.expectMessage(startsWith("Invalid OperationalEnvironmentStatus on OperationalEnvironmentId: "));
112 thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_BAD_REQUEST)));
113 thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_DETAILED_SERVICE_ERROR)));
115 InfraActiveRequests iar = new InfraActiveRequests();
116 iar.setRequestId(requestId);
117 iar.setOperationalEnvName("myOpEnv");
118 iar.setRequestScope("create");
119 iar.setRequestStatus("PENDING");
120 iar.setRequestAction("UNKNOWN");
121 wireMockServer.stubFor(get(urlPathEqualTo("/infraActiveRequests/"+requestId))
122 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
123 .withBody(mapper.writeValueAsString(iar))
124 .withStatus(HttpStatus.SC_OK)));
125 wireMockServer.stubFor(post(urlPathEqualTo("/infraActiveRequests/"))
126 .withRequestBody(containing("{\"requestId\":\""+ requestId+"\",\"clientRequestId\":null,\"action\":null,\"requestStatus\":\"FAILED\",\"statusMessage\":\"FAILURE"))
127 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
128 .withStatus(HttpStatus.SC_OK)));
130 deactivate.execute(requestId, request);
134 public void testDeactivateInactiveStatus() throws Exception {
135 request.setOperationalEnvironmentId(operationalEnvironmentId);
136 request.setRequestDetails(null);
138 String json = "{\"operational-environment-status\" : \"INACTIVE\"}";
140 wireMockServer.stubFor(get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
141 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(json).withStatus(HttpStatus.SC_ACCEPTED)));
143 InfraActiveRequests iar = new InfraActiveRequests();
144 iar.setRequestId(requestId);
145 iar.setOperationalEnvName("myOpEnv");
146 iar.setRequestScope("create");
147 iar.setRequestStatus("PENDING");
148 iar.setRequestAction("UNKNOWN");
149 wireMockServer.stubFor(get(urlPathEqualTo("/infraActiveRequests/"+requestId))
150 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
151 .withBody(mapper.writeValueAsString(iar))
152 .withStatus(HttpStatus.SC_OK)));
154 deactivate.execute(requestId, request);
158 public void testDeactivateNullStatus() throws Exception {
159 request.setOperationalEnvironmentId(operationalEnvironmentId);
160 request.setRequestDetails(null);
162 String json = "{\"operational-environment-status\" : \"\"}";
164 wireMockServer.stubFor(get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
165 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(json).withStatus(HttpStatus.SC_ACCEPTED)));
167 thrown.expect(ValidateException.class);
168 thrown.expectMessage(startsWith("OperationalEnvironmentStatus is null on OperationalEnvironmentId: "));
169 thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_BAD_REQUEST)));
170 thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_DETAILED_SERVICE_ERROR)));
171 deactivate.execute(requestId, request);