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