2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2018 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=========================================================
24 import com.github.tomakehurst.wiremock.client.WireMock;
25 import org.apache.http.HttpHeaders;
26 import org.apache.http.HttpStatus;
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.runner.RunWith;
30 import org.onap.so.db.catalog.beans.AuthenticationType;
31 import org.onap.so.db.catalog.beans.CloudIdentity;
32 import org.onap.so.db.catalog.beans.CloudSite;
33 import org.onap.so.db.catalog.beans.ServerType;
34 import org.springframework.beans.factory.annotation.Value;
35 import org.springframework.boot.test.context.SpringBootTest;
36 import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
37 import org.springframework.test.context.ActiveProfiles;
38 import org.springframework.test.context.junit4.SpringRunner;
40 import javax.ws.rs.core.MediaType;
41 import java.io.BufferedReader;
42 import java.io.FileReader;
43 import java.io.IOException;
45 import static com.github.tomakehurst.wiremock.client.WireMock.*;
46 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
47 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
49 @RunWith(SpringRunner.class)
50 @SpringBootTest(classes = TestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
51 @ActiveProfiles("test")
52 @AutoConfigureWireMock(port = 0)
53 public abstract class BaseTest extends TestDataSetup {
55 @Value("${wiremock.server.port}")
56 protected int wireMockPort;
63 protected static String getBody(String body, int port, String urlPath) throws IOException {
64 return body.replaceAll("port", "http://localhost:" + port + urlPath);
68 public void init() throws IOException {
69 CloudIdentity identity = getCloudIdentity();
70 CloudSite cloudSite = getCloudSite(identity);
71 mockCloud(identity, cloudSite);
74 private void mockCloud(CloudIdentity identity, CloudSite cloudSite) throws IOException {
75 stubFor(get(urlPathEqualTo("/cloudSite/MTN13")).willReturn(aResponse()
76 .withBody(getBody(mapper.writeValueAsString(cloudSite),wireMockPort, ""))
77 .withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
78 .withStatus(HttpStatus.SC_OK)));
79 stubFor(get(urlPathEqualTo("/cloudSite/default")).willReturn(aResponse()
80 .withBody(getBody(mapper.writeValueAsString(cloudSite),wireMockPort, ""))
81 .withHeader(HttpHeaders.CONTENT_TYPE,MediaType.APPLICATION_JSON)
82 .withStatus(HttpStatus.SC_OK)));
83 stubFor(get(urlPathEqualTo("/cloudIdentity/mtn13")).willReturn(aResponse()
84 .withBody(getBody(mapper.writeValueAsString(identity),wireMockPort, ""))
85 .withHeader(HttpHeaders.CONTENT_TYPE,MediaType.APPLICATION_JSON)
86 .withStatus(HttpStatus.SC_OK)));
89 private CloudIdentity getCloudIdentity() {
90 CloudIdentity identity = new CloudIdentity();
91 identity.setId("mtn13");
92 identity.setMsoId("m93945");
93 identity.setMsoPass("93937EA01B94A10A49279D4572B48369");
94 identity.setAdminTenant("admin");
95 identity.setMemberRole("admin");
96 identity.setTenantMetadata(false);
97 identity.setIdentityUrl("http://localhost:"+wireMockPort+"/v2.0");
98 identity.setIdentityAuthenticationType(AuthenticationType.USERNAME_PASSWORD);
99 identity.setIdentityServerType(ServerType.KEYSTONE);
103 private CloudSite getCloudSite(CloudIdentity identity) {
104 CloudSite cloudSite = new CloudSite();
105 cloudSite.setId("MTN13");
106 cloudSite.setCloudVersion("3.0");
107 cloudSite.setClli("MDT13");
108 cloudSite.setRegionId("mtn13");
109 cloudSite.setIdentityService(identity);
113 private static String readFile(String fileName) throws IOException {
114 try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
115 StringBuilder sb = new StringBuilder();
116 String line = br.readLine();
118 while (line != null) {
121 line = br.readLine();
123 return sb.toString();