Springboot 2.0 upgrade
[so.git] / adapters / mso-openstack-adapters / src / test / java / org / onap / so / adapters / vnf / BaseRestTestUtils.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.adapters.vnf;
22
23 import com.fasterxml.jackson.core.JsonParseException;
24 import com.fasterxml.jackson.databind.JsonMappingException;
25 import com.fasterxml.jackson.databind.JsonNode;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 import org.apache.http.HttpStatus;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.onap.so.adapters.openstack.MsoOpenstackAdaptersApplication;
32 import org.onap.so.cloud.CloudConfig;
33 import org.onap.so.db.catalog.beans.AuthenticationType;
34 import org.onap.so.db.catalog.beans.CloudIdentity;
35 import org.onap.so.db.catalog.beans.CloudSite;
36 import org.onap.so.db.catalog.beans.ServerType;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.beans.factory.annotation.Qualifier;
39 import org.springframework.beans.factory.annotation.Value;
40 import org.springframework.boot.web.server.LocalServerPort;
41 import org.springframework.boot.test.context.SpringBootTest;
42 import org.springframework.boot.test.web.client.TestRestTemplate;
43 import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
44 import org.springframework.http.HttpHeaders;
45 import org.springframework.test.context.ActiveProfiles;
46 import org.springframework.test.context.junit4.SpringRunner;
47
48 import javax.ws.rs.core.MediaType;
49 import java.io.BufferedReader;
50 import java.io.File;
51 import java.io.FileReader;
52 import java.io.IOException;
53
54 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
55 import static com.github.tomakehurst.wiremock.client.WireMock.get;
56 import static com.github.tomakehurst.wiremock.client.WireMock.reset;
57 import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
58 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
59
60 @RunWith(SpringRunner.class)
61 @SpringBootTest(classes = MsoOpenstackAdaptersApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
62 @ActiveProfiles("test")
63 @AutoConfigureWireMock(port = 0)
64 public abstract class BaseRestTestUtils {
65         @Value("${wiremock.server.port}")
66     protected int wireMockPort;
67         @Autowired
68         CloudConfig cloudConfig;
69
70         @Autowired
71         @Qualifier("JettisonStyle")
72         protected TestRestTemplate restTemplate;
73
74         protected HttpHeaders headers = new HttpHeaders();      
75         
76         @LocalServerPort
77         private int port;
78
79         public ObjectMapper mapper;
80
81         public String orchestrator = "orchestrator";
82         public String cloudEndpoint = "/v2.0";
83
84         
85         protected String readJsonFileAsString(String fileLocation) throws JsonParseException, JsonMappingException, IOException{
86                 ObjectMapper mapper = new ObjectMapper();
87                 JsonNode jsonNode = mapper.readTree(new File(fileLocation));
88                 return jsonNode.asText();
89         }
90         
91         protected String createURLWithPort(String uri) {
92                 return "http://localhost:" + port + uri;
93         }
94         
95         protected String readFile(String fileName) throws IOException {
96                 try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
97                         StringBuilder sb = new StringBuilder();
98                         String line = br.readLine();
99
100                         while (line != null) {
101                                 sb.append(line);
102                                 sb.append("\n");
103                                 line = br.readLine();
104                         }
105                         return sb.toString();
106                 }
107         }
108
109         /***
110          * Before each test execution, updating IdentityUrl port value to the ramdom wireMockPort
111          * Since URL will be used as a rest call and required to be mocked in unit tests
112          */
113         @Before
114         public void setUp() throws Exception {
115                 reset();
116                 mapper = new ObjectMapper();
117                 CloudIdentity identity = new CloudIdentity();
118                 identity.setId("MTN13");
119                 identity.setMsoId("m93945");
120                 identity.setMsoPass("93937EA01B94A10A49279D4572B48369");
121                 identity.setAdminTenant("admin");
122                 identity.setMemberRole("admin");
123                 identity.setTenantMetadata(new Boolean(true));
124                 identity.setIdentityUrl("http://localhost:" + wireMockPort + cloudEndpoint);
125
126                 identity.setIdentityAuthenticationType(AuthenticationType.USERNAME_PASSWORD);
127
128                 CloudSite cloudSite = new CloudSite();
129                 cloudSite.setId("MTN13");
130                 cloudSite.setCloudVersion("3.0");
131                 cloudSite.setClli("MDT13");
132                 cloudSite.setRegionId("mtn13");
133                 cloudSite.setOrchestrator(orchestrator);
134                 identity.setIdentityServerType(ServerType.KEYSTONE);
135                 cloudSite.setIdentityService(identity);
136
137                 stubFor(get(urlPathEqualTo("/cloudSite/MTN13")).willReturn(aResponse()
138                                 .withBody(getBody(mapper.writeValueAsString(cloudSite),wireMockPort, ""))
139                                 .withHeader(org.apache.http.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
140                                 .withStatus(HttpStatus.SC_OK)));
141                 stubFor(get(urlPathEqualTo("/cloudSite/DEFAULT")).willReturn(aResponse()
142                                 .withBody(getBody(mapper.writeValueAsString(cloudSite),wireMockPort, ""))
143                                 .withHeader(org.apache.http.HttpHeaders.CONTENT_TYPE,MediaType.APPLICATION_JSON)
144                                 .withStatus(HttpStatus.SC_OK)));
145                 stubFor(get(urlPathEqualTo("/cloudIdentity/MTN13")).willReturn(aResponse()
146                                 .withBody(getBody(mapper.writeValueAsString(identity),wireMockPort, ""))
147                                 .withHeader(org.apache.http.HttpHeaders.CONTENT_TYPE,MediaType.APPLICATION_JSON)
148                                 .withStatus(HttpStatus.SC_OK)));
149                         cloudConfig.getCloudSite("MTN13").get().getIdentityService().setIdentityUrl("http://localhost:" + wireMockPort + cloudEndpoint);
150         }
151
152         protected static String getBody(String body, int port, String urlPath) throws IOException {
153                 return body.replaceAll("port", "http://localhost:" + port + urlPath);
154         }
155         
156         @Test
157         public void testNothing(){
158                 
159         }
160
161 }