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.*;
24 import static org.hamcrest.Matchers.hasProperty;
25 import static org.hamcrest.Matchers.is;
26 import static org.hamcrest.Matchers.startsWith;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29 import org.apache.http.HttpStatus;
30 import org.junit.Before;
31 import org.junit.Rule;
32 import org.junit.Test;
33 import org.junit.rules.ExpectedException;
34 import org.onap.so.apihandler.common.ErrorNumbers;
35 import org.onap.so.apihandlerinfra.BaseTest;
36 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
37 import org.onap.so.apihandlerinfra.tenantisolation.CloudOrchestrationRequest;
38 import org.onap.so.client.aai.AAIVersion;
39 import org.onap.so.db.request.beans.InfraActiveRequests;
40 import org.onap.so.db.request.data.repository.InfraActiveRequestsRepository;
41 import org.springframework.beans.factory.annotation.Autowired;
43 import javax.ws.rs.core.HttpHeaders;
44 import javax.ws.rs.core.MediaType;
46 public class DeactivateVnfOperationalEnvironmentTest extends BaseTest{
49 public ExpectedException thrown = ExpectedException.none();
52 private DeactivateVnfOperationalEnvironment deactivate;
54 private CloudOrchestrationRequest request = new CloudOrchestrationRequest();
55 private String operationalEnvironmentId = "ff3514e3-5a33-55df-13ab-12abad84e7ff";
56 private String requestId = "ff3514e3-5a33-55df-13ab-12abad84e7fe";
58 private ObjectMapper mapper = new ObjectMapper();
62 stubFor(post(urlPathEqualTo("/infraActiveRequests/"))
63 .withRequestBody(containing("{\"requestId\":\""+ requestId+"\",\"clientRequestId\":null,\"action\":null,\"requestStatus\":\"COMPLETE\",\"statusMessage\":\"SUCCESSFUL"))
64 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
65 .withStatus(HttpStatus.SC_OK)));
68 public void testDeactivateOperationalEnvironment() throws Exception {
69 request.setOperationalEnvironmentId(operationalEnvironmentId);
70 request.setRequestDetails(null);
72 String json = "{\"operational-environment-status\" : \"ACTIVE\"}";
74 stubFor(get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
75 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(json).withStatus(HttpStatus.SC_ACCEPTED)));
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("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
79 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_ACCEPTED)));
81 InfraActiveRequests iar = new InfraActiveRequests();
82 iar.setRequestId(requestId);
83 iar.setOperationalEnvName("myOpEnv");
84 iar.setRequestScope("create");
85 iar.setRequestStatus("PENDING");
86 iar.setRequestAction("UNKNOWN");
87 stubFor(get(urlPathEqualTo("/infraActiveRequests/"+requestId))
88 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
89 .withBody(mapper.writeValueAsString(iar))
90 .withStatus(HttpStatus.SC_OK)));
91 deactivate.execute(requestId, request);
95 public void testDeactivateInvalidStatus() throws Exception {
96 request.setOperationalEnvironmentId(operationalEnvironmentId);
97 request.setRequestDetails(null);
99 String json = "{\"operational-environment-status\" : \"SUCCESS\"}";
101 stubFor(get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
102 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(json).withStatus(HttpStatus.SC_ACCEPTED)));
104 thrown.expect(ValidateException.class);
105 thrown.expectMessage(startsWith("Invalid OperationalEnvironmentStatus on OperationalEnvironmentId: "));
106 thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_BAD_REQUEST)));
107 thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_DETAILED_SERVICE_ERROR)));
109 InfraActiveRequests iar = new InfraActiveRequests();
110 iar.setRequestId(requestId);
111 iar.setOperationalEnvName("myOpEnv");
112 iar.setRequestScope("create");
113 iar.setRequestStatus("PENDING");
114 iar.setRequestAction("UNKNOWN");
115 stubFor(get(urlPathEqualTo("/infraActiveRequests/"+requestId))
116 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
117 .withBody(mapper.writeValueAsString(iar))
118 .withStatus(HttpStatus.SC_OK)));
119 stubFor(post(urlPathEqualTo("/infraActiveRequests/"))
120 .withRequestBody(containing("{\"requestId\":\""+ requestId+"\",\"clientRequestId\":null,\"action\":null,\"requestStatus\":\"FAILED\",\"statusMessage\":\"FAILURE"))
121 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
122 .withStatus(HttpStatus.SC_OK)));
124 deactivate.execute(requestId, request);
128 public void testDeactivateInactiveStatus() throws Exception {
129 request.setOperationalEnvironmentId(operationalEnvironmentId);
130 request.setRequestDetails(null);
132 String json = "{\"operational-environment-status\" : \"INACTIVE\"}";
134 stubFor(get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
135 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(json).withStatus(HttpStatus.SC_ACCEPTED)));
137 InfraActiveRequests iar = new InfraActiveRequests();
138 iar.setRequestId(requestId);
139 iar.setOperationalEnvName("myOpEnv");
140 iar.setRequestScope("create");
141 iar.setRequestStatus("PENDING");
142 iar.setRequestAction("UNKNOWN");
143 stubFor(get(urlPathEqualTo("/infraActiveRequests/"+requestId))
144 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
145 .withBody(mapper.writeValueAsString(iar))
146 .withStatus(HttpStatus.SC_OK)));
148 deactivate.execute(requestId, request);
152 public void testDeactivateNullStatus() throws Exception {
153 request.setOperationalEnvironmentId(operationalEnvironmentId);
154 request.setRequestDetails(null);
156 String json = "{\"operational-environment-status\" : \"\"}";
158 stubFor(get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
159 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(json).withStatus(HttpStatus.SC_ACCEPTED)));
161 thrown.expect(ValidateException.class);
162 thrown.expectMessage(startsWith("OperationalEnvironmentStatus is null on OperationalEnvironmentId: "));
163 thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_BAD_REQUEST)));
164 thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_DETAILED_SERVICE_ERROR)));
165 deactivate.execute(requestId, request);