Replaced all tabs with spaces in java and pom.xml
[so.git] / adapters / mso-adapter-utils / src / test / java / org / onap / so / BaseTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so;
24
25
26 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
27 import static com.github.tomakehurst.wiremock.client.WireMock.get;
28 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
29 import java.io.BufferedReader;
30 import java.io.FileReader;
31 import java.io.IOException;
32 import javax.ws.rs.core.MediaType;
33 import org.apache.http.HttpHeaders;
34 import org.apache.http.HttpStatus;
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.runner.RunWith;
38 import org.onap.so.db.catalog.beans.AuthenticationType;
39 import org.onap.so.db.catalog.beans.CloudIdentity;
40 import org.onap.so.db.catalog.beans.CloudSite;
41 import org.onap.so.db.catalog.beans.ServerType;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.beans.factory.annotation.Value;
44 import org.springframework.boot.test.context.SpringBootTest;
45 import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
46 import org.springframework.test.context.ActiveProfiles;
47 import org.springframework.test.context.junit4.SpringRunner;
48 import com.github.tomakehurst.wiremock.WireMockServer;
49 import com.github.tomakehurst.wiremock.client.WireMock;
50
51 @RunWith(SpringRunner.class)
52 @SpringBootTest(classes = TestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
53 @ActiveProfiles("test")
54 @AutoConfigureWireMock(port = 0)
55 public abstract class BaseTest extends TestDataSetup {
56
57     @Value("${wiremock.server.port}")
58     protected int wireMockPort;
59     @Autowired
60     protected WireMockServer wireMockServer;
61
62     @After
63     public void after() {
64         wireMockServer.resetAll();
65     }
66
67     protected static String getBody(String body, int port, String urlPath) throws IOException {
68         return body.replaceAll("port", "http://localhost:" + port + urlPath);
69     }
70
71     @Before
72     public void init() throws IOException {
73         CloudIdentity identity = getCloudIdentity();
74         CloudSite cloudSite = getCloudSite(identity);
75         mockCloud(identity, cloudSite);
76     }
77
78     private void mockCloud(CloudIdentity identity, CloudSite cloudSite) throws IOException {
79         wireMockServer.stubFor(get(urlPathEqualTo("/cloudSite/MTN13")).willReturn(aResponse()
80                 .withBody(getBody(mapper.writeValueAsString(cloudSite), wireMockPort, ""))
81                 .withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).withStatus(HttpStatus.SC_OK)));
82         wireMockServer.stubFor(get(urlPathEqualTo("/cloudSite/DEFAULT")).willReturn(aResponse()
83                 .withBody(getBody(mapper.writeValueAsString(cloudSite), wireMockPort, ""))
84                 .withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).withStatus(HttpStatus.SC_OK)));
85         wireMockServer.stubFor(get(urlPathEqualTo("/cloudIdentity/mtn13")).willReturn(aResponse()
86                 .withBody(getBody(mapper.writeValueAsString(identity), wireMockPort, ""))
87                 .withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).withStatus(HttpStatus.SC_OK)));
88     }
89
90     protected CloudIdentity getCloudIdentity() {
91         CloudIdentity identity = new CloudIdentity();
92         identity.setId("mtn13");
93         identity.setMsoId("m93945");
94         identity.setMsoPass("93937EA01B94A10A49279D4572B48369");
95         identity.setAdminTenant("admin");
96         identity.setMemberRole("admin");
97         identity.setTenantMetadata(false);
98         identity.setIdentityUrl("http://localhost:" + wireMockPort + "/v2.0");
99         identity.setIdentityAuthenticationType(AuthenticationType.USERNAME_PASSWORD);
100         identity.setIdentityServerType(ServerType.KEYSTONE);
101         return identity;
102     }
103
104     protected CloudSite getCloudSite(CloudIdentity identity) {
105         CloudSite cloudSite = new CloudSite();
106         cloudSite.setId("MTN13");
107         cloudSite.setCloudVersion("3.0");
108         cloudSite.setClli("MDT13");
109         cloudSite.setRegionId("mtn13");
110         cloudSite.setIdentityService(identity);
111         return cloudSite;
112     }
113
114     private static String readFile(String fileName) throws IOException {
115         try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
116             StringBuilder sb = new StringBuilder();
117             String line = br.readLine();
118
119             while (line != null) {
120                 sb.append(line);
121                 sb.append("\n");
122                 line = br.readLine();
123             }
124             return sb.toString();
125         }
126     }
127 }