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.springframework.beans.factory.annotation.Autowired;
42 import javax.ws.rs.core.HttpHeaders;
43 import javax.ws.rs.core.MediaType;
45 public class DeactivateVnfOperationalEnvironmentTest extends BaseTest{
48 public ExpectedException thrown = ExpectedException.none();
51 private DeactivateVnfOperationalEnvironment deactivate;
53 private CloudOrchestrationRequest request = new CloudOrchestrationRequest();
54 private String operationalEnvironmentId = "ff3514e3-5a33-55df-13ab-12abad84e7ff";
55 private String requestId = "ff3514e3-5a33-55df-13ab-12abad84e7fe";
57 private ObjectMapper mapper = new ObjectMapper();
61 stubFor(post(urlPathEqualTo("/infraActiveRequests/"))
62 .withRequestBody(containing("{\"requestId\":\""+ requestId+"\",\"clientRequestId\":null,\"action\":null,\"requestStatus\":\"COMPLETE\",\"statusMessage\":\"SUCCESSFUL"))
63 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
64 .withStatus(HttpStatus.SC_OK)));
67 public void testDeactivateOperationalEnvironment() throws Exception {
68 request.setOperationalEnvironmentId(operationalEnvironmentId);
69 request.setRequestDetails(null);
71 String json = "{\"operational-environment-status\" : \"ACTIVE\"}";
73 stubFor(get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
74 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(json).withStatus(HttpStatus.SC_ACCEPTED)));
75 stubFor(put(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
76 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_ACCEPTED)));
77 stubFor(post(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
78 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_ACCEPTED)));
80 InfraActiveRequests iar = new InfraActiveRequests();
81 iar.setRequestId(requestId);
82 iar.setOperationalEnvName("myOpEnv");
83 iar.setRequestScope("create");
84 iar.setRequestStatus("PENDING");
85 iar.setRequestAction("UNKNOWN");
86 stubFor(get(urlPathEqualTo("/infraActiveRequests/"+requestId))
87 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
88 .withBody(mapper.writeValueAsString(iar))
89 .withStatus(HttpStatus.SC_OK)));
90 deactivate.execute(requestId, request);
94 public void testDeactivateInvalidStatus() throws Exception {
95 request.setOperationalEnvironmentId(operationalEnvironmentId);
96 request.setRequestDetails(null);
98 String json = "{\"operational-environment-status\" : \"SUCCESS\"}";
100 stubFor(get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
101 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(json).withStatus(HttpStatus.SC_ACCEPTED)));
103 thrown.expect(ValidateException.class);
104 thrown.expectMessage(startsWith("Invalid OperationalEnvironmentStatus on OperationalEnvironmentId: "));
105 thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_BAD_REQUEST)));
106 thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_DETAILED_SERVICE_ERROR)));
108 InfraActiveRequests iar = new InfraActiveRequests();
109 iar.setRequestId(requestId);
110 iar.setOperationalEnvName("myOpEnv");
111 iar.setRequestScope("create");
112 iar.setRequestStatus("PENDING");
113 iar.setRequestAction("UNKNOWN");
114 stubFor(get(urlPathEqualTo("/infraActiveRequests/"+requestId))
115 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
116 .withBody(mapper.writeValueAsString(iar))
117 .withStatus(HttpStatus.SC_OK)));
118 stubFor(post(urlPathEqualTo("/infraActiveRequests/"))
119 .withRequestBody(containing("{\"requestId\":\""+ requestId+"\",\"clientRequestId\":null,\"action\":null,\"requestStatus\":\"FAILED\",\"statusMessage\":\"FAILURE"))
120 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
121 .withStatus(HttpStatus.SC_OK)));
123 deactivate.execute(requestId, request);
127 public void testDeactivateInactiveStatus() throws Exception {
128 request.setOperationalEnvironmentId(operationalEnvironmentId);
129 request.setRequestDetails(null);
131 String json = "{\"operational-environment-status\" : \"INACTIVE\"}";
133 stubFor(get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
134 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(json).withStatus(HttpStatus.SC_ACCEPTED)));
136 InfraActiveRequests iar = new InfraActiveRequests();
137 iar.setRequestId(requestId);
138 iar.setOperationalEnvName("myOpEnv");
139 iar.setRequestScope("create");
140 iar.setRequestStatus("PENDING");
141 iar.setRequestAction("UNKNOWN");
142 stubFor(get(urlPathEqualTo("/infraActiveRequests/"+requestId))
143 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
144 .withBody(mapper.writeValueAsString(iar))
145 .withStatus(HttpStatus.SC_OK)));
147 deactivate.execute(requestId, request);
151 public void testDeactivateNullStatus() throws Exception {
152 request.setOperationalEnvironmentId(operationalEnvironmentId);
153 request.setRequestDetails(null);
155 String json = "{\"operational-environment-status\" : \"\"}";
157 stubFor(get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
158 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(json).withStatus(HttpStatus.SC_ACCEPTED)));
160 thrown.expect(ValidateException.class);
161 thrown.expectMessage(startsWith("OperationalEnvironmentStatus is null on OperationalEnvironmentId: "));
162 thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_BAD_REQUEST)));
163 thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_DETAILED_SERVICE_ERROR)));
164 deactivate.execute(requestId, request);