a9be10c3f4b454e4b4f74b7e682ba0056d622f64
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.apihandlerinfra.tenantisolation.process;
22
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;
34
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;
47
48 public class DeactivateVnfOperationalEnvironmentTest extends BaseTest{
49
50     @Rule
51     public ExpectedException thrown = ExpectedException.none();
52
53         @Autowired
54         private DeactivateVnfOperationalEnvironment deactivate;
55         @Autowired
56         private InfraActiveRequestsRepository infraActiveRequestsRepository;
57         
58         private CloudOrchestrationRequest request = new CloudOrchestrationRequest();
59         private String operationalEnvironmentId = "ff3514e3-5a33-55df-13ab-12abad84e7ff";
60         private String requestId = "ff3514e3-5a33-55df-13ab-12abad84e7fe";
61         
62         @Test
63         public void testDeactivateOperationalEnvironment() throws Exception {
64                 request.setOperationalEnvironmentId(operationalEnvironmentId);
65                 request.setRequestDetails(null);
66
67                 String json = "{\"operational-environment-status\" : \"ACTIVE\"}";
68                 
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)));
75                 
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);
83                 
84                 deactivate.execute(requestId, request);
85                 
86                 InfraActiveRequests infraActiveRequest = infraActiveRequestsRepository.findOne(requestId);
87                 assertNotNull(infraActiveRequest);
88                 assertTrue(infraActiveRequest.getStatusMessage().contains("SUCCESSFUL"));
89
90         }
91         
92         @Test
93         public void testDeactivateInvalidStatus() throws Exception {
94                 request.setOperationalEnvironmentId(operationalEnvironmentId);
95                 request.setRequestDetails(null);
96
97                 String json = "{\"operational-environment-status\" : \"SUCCESS\"}";
98                 
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)));
101
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)));
106         
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);
114                 
115         deactivate.execute(requestId, request);
116         }
117         
118         @Test
119         public void testDeactivateInactiveStatus() throws Exception {
120                 request.setOperationalEnvironmentId(operationalEnvironmentId);
121                 request.setRequestDetails(null);
122
123                 String json = "{\"operational-environment-status\" : \"INACTIVE\"}";
124                 
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)));
127                 
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);
135                 
136                 deactivate.execute(requestId, request);
137                 
138                 InfraActiveRequests infraActiveRequest = infraActiveRequestsRepository.findOne(requestId);
139                 assertNotNull(infraActiveRequest);
140                 assertTrue(infraActiveRequest.getStatusMessage().contains("SUCCESS"));
141         }
142         
143         @Test
144         public void testDeactivateNullStatus() throws Exception {
145                 request.setOperationalEnvironmentId(operationalEnvironmentId);
146                 request.setRequestDetails(null);
147
148                 String json = "{\"operational-environment-status\" : \"\"}";
149                 
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)));
152
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);
158         }
159 }
160