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