Containerization feature of SO
[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.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.junit.Assert.assertEquals;
30 import static org.junit.Assert.assertNotNull;
31 import static org.junit.Assert.assertTrue;
32
33 import java.io.File;
34 import java.nio.file.Files;
35 import java.util.List;
36 import java.util.UUID;
37
38 import org.apache.http.HttpStatus;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.onap.so.apihandlerinfra.BaseTest;
42 import org.onap.so.apihandlerinfra.exceptions.ApiException;
43 import org.onap.so.apihandlerinfra.tenantisolation.CloudOrchestrationRequest;
44 import org.onap.so.client.aai.AAIVersion;
45 import org.onap.so.client.aai.objects.AAIOperationalEnvironment;
46 import org.onap.so.client.grm.beans.Property;
47 import org.onap.so.client.grm.beans.ServiceEndPointList;
48 import org.onap.so.db.request.beans.InfraActiveRequests;
49 import org.onap.so.db.request.data.repository.InfraActiveRequestsRepository;
50 import org.springframework.beans.factory.annotation.Autowired;
51
52 import com.fasterxml.jackson.databind.ObjectMapper;
53
54
55 public class CreateVnfOperationalEnvironmentTest extends BaseTest{
56         
57         private CloudOrchestrationRequest request;
58         private ServiceEndPointList serviceEndpoints;
59         
60         @Autowired
61         private CreateVnfOperationalEnvironment createVnfOpEnv;
62         @Autowired
63         private InfraActiveRequestsRepository infraActiveRequestsRepository;
64         
65         @Before
66         public void testSetUp() throws Exception {
67                 ObjectMapper mapper = new ObjectMapper();
68                 String jsonRequest = getFileContentsAsString("__files/vnfoperenv/createVnfOperationalEnvironmentRequest.json");
69                 request = mapper.readValue(jsonRequest, CloudOrchestrationRequest.class);
70                 String jsonServiceEndpoints = getFileContentsAsString("__files/vnfoperenv/endpoints.json");
71                 serviceEndpoints = mapper.readValue(jsonServiceEndpoints, ServiceEndPointList.class);
72         }
73         
74         @Test
75         public void testGetEcompManagingEnvironmentId() throws Exception { 
76                 createVnfOpEnv.setRequest(request);
77                 assertEquals("ff305d54-75b4-431b-adb2-eb6b9e5ff000", createVnfOpEnv.getEcompManagingEnvironmentId());
78         }
79         
80         @Test
81         public void testGetTenantContext() throws Exception { 
82                 createVnfOpEnv.setRequest(request);
83                 assertEquals("Test", createVnfOpEnv.getTenantContext());
84         }
85         
86         @Test
87         public void testGetEnvironmentName() throws Exception {
88                 createVnfOpEnv.setRequest(request);
89                 List<Property> props = serviceEndpoints.getServiceEndPointList().get(0).getProperties();
90                 assertEquals("DEV", createVnfOpEnv.getEnvironmentName(props));
91         }
92         
93         @Test 
94         public void testBuildServiceNameForVnf() throws Exception {
95                 createVnfOpEnv.setRequest(request);
96                 assertEquals("Test.VNF_E2E-IST.Inventory", createVnfOpEnv.buildServiceNameForVnf("TEST.ECOMP_PSL.Inventory"));
97         }
98         
99         @Test
100         public void testGetSearchKey() {
101                 createVnfOpEnv.setRequest(request);
102                 AAIOperationalEnvironment ecompEnv = new AAIOperationalEnvironment();
103                 ecompEnv.setTenantContext("Test");
104                 ecompEnv.setWorkloadContext("ECOMPL_PSL");
105                 assertEquals("Test.ECOMPL_PSL.*", createVnfOpEnv.getSearchKey(ecompEnv));
106         }
107         
108         public String getFileContentsAsString(String fileName) {
109                 String content = "";
110                 try {
111                         ClassLoader classLoader = this.getClass().getClassLoader();
112                         File file = new File(classLoader.getResource(fileName).getFile());
113                         content = new String(Files.readAllBytes(file.toPath()));
114                 }
115                 catch(Exception e) {
116                         e.printStackTrace();
117                         System.out.println("Exception encountered reading " + fileName + ". Error: " + e.getMessage());
118                 }
119                 return content;
120         }
121         
122         @Test
123         public void testExecute() throws ApiException{
124                 stubFor(get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
125                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("vnfoperenv/ecompOperationalEnvironment.json").withStatus(HttpStatus.SC_ACCEPTED)));
126                 stubFor(post(urlPathMatching("/GRMLWPService/v1/serviceEndPoint/findRunning"))
127                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("vnfoperenv/endpoints.json").withStatus(HttpStatus.SC_ACCEPTED)));
128                 stubFor(post(urlPathMatching("/GRMLWPService/v1/serviceEndPoint/add"))
129                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_ACCEPTED)));
130                 stubFor(put(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
131                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("vnfoperenv/ecompOperationalEnvironment.json").withStatus(HttpStatus.SC_ACCEPTED)));
132                 String requestId = UUID.randomUUID().toString();
133                 InfraActiveRequests iar = new InfraActiveRequests();
134                 iar.setRequestId(requestId);
135                 iar.setOperationalEnvName("myOpEnv");
136                 iar.setRequestScope("create");
137                 iar.setRequestStatus("PENDING");
138                 iar.setRequestAction("UNKNOWN");
139                 infraActiveRequestsRepository.saveAndFlush(iar);
140                 createVnfOpEnv.execute(requestId, request);
141                 
142                 InfraActiveRequests infraActiveRequest = infraActiveRequestsRepository.findOne(requestId);
143                 assertNotNull(infraActiveRequest);
144                 assertTrue(infraActiveRequest.getStatusMessage().contains("SUCCESS"));
145         }
146 }