ac274dc8dc0c55eee65dc4103d077f7b63753c3d
[so.git] / mso-api-handlers / mso-api-handler-infra / src / test / java / org / onap / so / apihandlerinfra / tenantisolation / process / CreateVnfOperationalEnvironmentTest.java
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.*;
24 import static org.junit.Assert.assertEquals;
25
26 import java.io.File;
27 import java.nio.file.Files;
28 import java.util.List;
29 import java.util.UUID;
30
31 import com.fasterxml.jackson.core.JsonProcessingException;
32 import org.apache.http.HttpStatus;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.aai.domain.yang.OperationalEnvironment;
36 import org.onap.so.apihandlerinfra.BaseTest;
37 import org.onap.so.apihandlerinfra.exceptions.ApiException;
38 import org.onap.so.apihandlerinfra.tenantisolation.CloudOrchestrationRequest;
39 import org.onap.so.client.aai.AAIVersion;
40 import org.onap.so.client.grm.beans.Property;
41 import org.onap.so.client.grm.beans.ServiceEndPointList;
42 import org.onap.so.db.request.beans.InfraActiveRequests;
43 import org.springframework.beans.factory.annotation.Autowired;
44
45 import com.fasterxml.jackson.databind.ObjectMapper;
46
47 import javax.ws.rs.core.HttpHeaders;
48 import javax.ws.rs.core.MediaType;
49
50
51 public class CreateVnfOperationalEnvironmentTest extends BaseTest{
52         
53         private CloudOrchestrationRequest request;
54         private ServiceEndPointList serviceEndpoints;
55         private ServiceEndPointList serviceEndpoints2;
56         
57         @Autowired
58         private CreateVnfOperationalEnvironment createVnfOpEnv;
59
60         @Before
61         public void testSetUp() throws Exception {
62                 ObjectMapper mapper = new ObjectMapper();
63                 String jsonRequest = getFileContentsAsString("__files/vnfoperenv/createVnfOperationalEnvironmentRequest.json");
64                 request = mapper.readValue(jsonRequest, CloudOrchestrationRequest.class);
65                 String jsonServiceEndpoints = getFileContentsAsString("__files/vnfoperenv/endpoints.json");
66                 serviceEndpoints = mapper.readValue(jsonServiceEndpoints, ServiceEndPointList.class);
67                 String jsonServiceEndpoints2 = getFileContentsAsString("__files/vnfoperenv/endpoints2.json");
68                 serviceEndpoints2 = mapper.readValue(jsonServiceEndpoints2, ServiceEndPointList.class);
69         }
70         @Test
71         public void testGetEcompManagingEnvironmentId() throws Exception { 
72                 createVnfOpEnv.setRequest(request);
73                 assertEquals("ff305d54-75b4-431b-adb2-eb6b9e5ff000", createVnfOpEnv.getEcompManagingEnvironmentId());
74         }
75         
76         @Test
77         public void testGetTenantContext() throws Exception { 
78                 createVnfOpEnv.setRequest(request);
79                 assertEquals("Test", createVnfOpEnv.getTenantContext());
80         }
81         
82         @Test
83         public void testGetEnvironmentName() {
84                 createVnfOpEnv.setRequest(request);
85                 List<Property> props = serviceEndpoints.getServiceEndPointList().get(0).getProperties();
86                 assertEquals("DEV", createVnfOpEnv.getEnvironmentName(props));
87         }
88         
89         @Test
90         public void testGetEnvironmentNameEndpointListBeginWithUpperCase() {
91                 createVnfOpEnv.setRequest(request);
92                 List<Property> props = serviceEndpoints2.getServiceEndPointList().get(0).getProperties();
93                 assertEquals("DEV", createVnfOpEnv.getEnvironmentName(props));
94         }
95         
96         @Test 
97         public void testBuildServiceNameForVnf() throws Exception {
98                 createVnfOpEnv.setRequest(request);
99                 assertEquals("Test.VNF_E2E-IST.Inventory", createVnfOpEnv.buildServiceNameForVnf("TEST.ECOMP_PSL.Inventory"));
100         }
101         
102         @Test
103         public void testGetSearchKey() {
104                 createVnfOpEnv.setRequest(request);
105                 OperationalEnvironment ecompEnv = new OperationalEnvironment();
106                 ecompEnv.setTenantContext("Test");
107                 ecompEnv.setWorkloadContext("ECOMPL_PSL");
108                 assertEquals("Test.ECOMPL_PSL.*", createVnfOpEnv.getSearchKey(ecompEnv));
109         }
110         
111         public String getFileContentsAsString(String fileName) {
112                 String content = "";
113                 try {
114                         ClassLoader classLoader = this.getClass().getClassLoader();
115                         File file = new File(classLoader.getResource(fileName).getFile());
116                         content = new String(Files.readAllBytes(file.toPath()));
117                 }
118                 catch(Exception e) {
119                         e.printStackTrace();
120                         System.out.println("Exception encountered reading " + fileName + ". Error: " + e.getMessage());
121                 }
122                 return content;
123         }
124         
125         @Test
126         public void testExecute() throws ApiException, JsonProcessingException {
127                 stubFor(get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
128                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("vnfoperenv/ecompOperationalEnvironment.json").withStatus(HttpStatus.SC_ACCEPTED)));
129                 stubFor(post(urlPathMatching("/GRMLWPService/v1/serviceEndPoint/findRunning"))
130                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("vnfoperenv/endpoints.json").withStatus(HttpStatus.SC_ACCEPTED)));
131                 stubFor(post(urlPathMatching("/GRMLWPService/v1/serviceEndPoint/add"))
132                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_ACCEPTED)));
133                 stubFor(put(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
134                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("vnfoperenv/ecompOperationalEnvironment.json").withStatus(HttpStatus.SC_ACCEPTED)));
135                 String requestId = UUID.randomUUID().toString();
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                 ObjectMapper mapper = new ObjectMapper();
143                 stubFor(post(urlPathEqualTo("/infraActiveRequests/"))
144                                 .withRequestBody(containing("{\"requestId\":\""+ requestId+"\",\"clientRequestId\":null,\"action\":null,\"requestStatus\":\"COMPLETE\",\"statusMessage\":\"SUCCESSFUL"))
145                                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
146                                                 .withStatus(HttpStatus.SC_OK)));
147
148                 stubFor(get(urlPathEqualTo("/infraActiveRequests/"+requestId))
149                                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
150                                                 .withBody(mapper.writeValueAsString(iar))
151                                                 .withStatus(HttpStatus.SC_OK)));
152                 createVnfOpEnv.execute(requestId, request);
153         }
154         
155         @Test
156         public void testExecuteEndpointsListBeginWithUppercase() throws ApiException, JsonProcessingException {
157                 stubFor(get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
158                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("vnfoperenv/ecompOperationalEnvironment.json").withStatus(HttpStatus.SC_ACCEPTED)));
159                 stubFor(post(urlPathMatching("/GRMLWPService/v1/serviceEndPoint/findRunning"))
160                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("vnfoperenv/endpoints2.json").withStatus(HttpStatus.SC_ACCEPTED)));
161                 stubFor(post(urlPathMatching("/GRMLWPService/v1/serviceEndPoint/add"))
162                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_ACCEPTED)));
163                 stubFor(put(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
164                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("vnfoperenv/ecompOperationalEnvironment.json").withStatus(HttpStatus.SC_ACCEPTED)));
165                 String requestId = UUID.randomUUID().toString();
166                 InfraActiveRequests iar = new InfraActiveRequests();
167                 iar.setRequestId(requestId);
168                 iar.setOperationalEnvName("myOpEnv");
169                 iar.setRequestScope("create");
170                 iar.setRequestStatus("PENDING");
171                 iar.setRequestAction("UNKNOWN");
172                 ObjectMapper mapper = new ObjectMapper();
173                 stubFor(post(urlPathEqualTo("/infraActiveRequests/"))
174                                 .withRequestBody(containing("{\"requestId\":\""+ requestId+"\",\"clientRequestId\":null,\"action\":null,\"requestStatus\":\"COMPLETE\",\"statusMessage\":\"SUCCESSFUL"))
175                                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
176                                                 .withStatus(HttpStatus.SC_OK)));
177
178                 stubFor(get(urlPathEqualTo("/infraActiveRequests/"+requestId))
179                                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
180                                                 .withBody(mapper.writeValueAsString(iar))
181                                                 .withStatus(HttpStatus.SC_OK)));
182                 createVnfOpEnv.execute(requestId, request);
183         }
184 }