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.get;
25 import static com.github.tomakehurst.wiremock.client.WireMock.post;
26 import static com.github.tomakehurst.wiremock.client.WireMock.put;
27 import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
28 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
29 import static org.hamcrest.Matchers.hasProperty;
30 import static org.hamcrest.Matchers.is;
31 import static org.hamcrest.Matchers.startsWith;
32 import static org.junit.Assert.assertNotNull;
33 import static org.junit.Assert.assertTrue;
35 import org.apache.http.HttpStatus;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.rules.ExpectedException;
39 import org.onap.so.apihandler.common.ErrorNumbers;
40 import org.onap.so.apihandlerinfra.BaseTest;
41 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
42 import org.onap.so.apihandlerinfra.tenantisolation.CloudOrchestrationRequest;
43 import org.onap.so.client.aai.AAIVersion;
44 import org.onap.so.db.request.beans.InfraActiveRequests;
45 import org.onap.so.db.request.data.repository.InfraActiveRequestsRepository;
46 import org.springframework.beans.factory.annotation.Autowired;
48 public class DeactivateVnfOperationalEnvironmentTest extends BaseTest{
51 public ExpectedException thrown = ExpectedException.none();
54 private DeactivateVnfOperationalEnvironment deactivate;
56 private InfraActiveRequestsRepository infraActiveRequestsRepository;
58 private CloudOrchestrationRequest request = new CloudOrchestrationRequest();
59 private String operationalEnvironmentId = "ff3514e3-5a33-55df-13ab-12abad84e7ff";
60 private String requestId = "ff3514e3-5a33-55df-13ab-12abad84e7fe";
63 public void testDeactivateOperationalEnvironment() throws Exception {
64 request.setOperationalEnvironmentId(operationalEnvironmentId);
65 request.setRequestDetails(null);
67 String json = "{\"operational-environment-status\" : \"ACTIVE\"}";
69 stubFor(get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
70 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(json).withStatus(HttpStatus.SC_ACCEPTED)));
71 stubFor(put(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
72 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_ACCEPTED)));
73 stubFor(post(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
74 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_ACCEPTED)));
76 InfraActiveRequests iar = new InfraActiveRequests();
77 iar.setRequestId(requestId);
78 iar.setOperationalEnvName("myOpEnv");
79 iar.setRequestScope("create");
80 iar.setRequestStatus("PENDING");
81 iar.setRequestAction("UNKNOWN");
82 infraActiveRequestsRepository.saveAndFlush(iar);
84 deactivate.execute(requestId, request);
86 InfraActiveRequests infraActiveRequest = infraActiveRequestsRepository.findOne(requestId);
87 assertNotNull(infraActiveRequest);
88 assertTrue(infraActiveRequest.getStatusMessage().contains("SUCCESSFUL"));
93 public void testDeactivateInvalidStatus() throws Exception {
94 request.setOperationalEnvironmentId(operationalEnvironmentId);
95 request.setRequestDetails(null);
97 String json = "{\"operational-environment-status\" : \"SUCCESS\"}";
99 stubFor(get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
100 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(json).withStatus(HttpStatus.SC_ACCEPTED)));
102 thrown.expect(ValidateException.class);
103 thrown.expectMessage(startsWith("Invalid OperationalEnvironmentStatus on OperationalEnvironmentId: "));
104 thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_BAD_REQUEST)));
105 thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_DETAILED_SERVICE_ERROR)));
107 InfraActiveRequests iar = new InfraActiveRequests();
108 iar.setRequestId(requestId);
109 iar.setOperationalEnvName("myOpEnv");
110 iar.setRequestScope("create");
111 iar.setRequestStatus("PENDING");
112 iar.setRequestAction("UNKNOWN");
113 infraActiveRequestsRepository.saveAndFlush(iar);
115 deactivate.execute(requestId, request);
119 public void testDeactivateInactiveStatus() throws Exception {
120 request.setOperationalEnvironmentId(operationalEnvironmentId);
121 request.setRequestDetails(null);
123 String json = "{\"operational-environment-status\" : \"INACTIVE\"}";
125 stubFor(get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
126 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(json).withStatus(HttpStatus.SC_ACCEPTED)));
128 InfraActiveRequests iar = new InfraActiveRequests();
129 iar.setRequestId(requestId);
130 iar.setOperationalEnvName("myOpEnv");
131 iar.setRequestScope("create");
132 iar.setRequestStatus("PENDING");
133 iar.setRequestAction("UNKNOWN");
134 infraActiveRequestsRepository.saveAndFlush(iar);
136 deactivate.execute(requestId, request);
138 InfraActiveRequests infraActiveRequest = infraActiveRequestsRepository.findOne(requestId);
139 assertNotNull(infraActiveRequest);
140 assertTrue(infraActiveRequest.getStatusMessage().contains("SUCCESS"));
144 public void testDeactivateNullStatus() throws Exception {
145 request.setOperationalEnvironmentId(operationalEnvironmentId);
146 request.setRequestDetails(null);
148 String json = "{\"operational-environment-status\" : \"\"}";
150 stubFor(get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
151 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(json).withStatus(HttpStatus.SC_ACCEPTED)));
153 thrown.expect(ValidateException.class);
154 thrown.expectMessage(startsWith("OperationalEnvironmentStatus is null on OperationalEnvironmentId: "));
155 thrown.expect(hasProperty("httpResponseCode", is(HttpStatus.SC_BAD_REQUEST)));
156 thrown.expect(hasProperty("messageID", is(ErrorNumbers.SVC_DETAILED_SERVICE_ERROR)));
157 deactivate.execute(requestId, request);