9cfdf5309462fe45a38e9ed91e2b2ba295cabcde
[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 com.github.tomakehurst.wiremock.client.WireMock;
27 import org.apache.http.HttpHeaders;
28 import org.apache.http.HttpStatus;
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.runner.RunWith;
32 import org.onap.so.db.catalog.beans.AuthenticationType;
33 import org.onap.so.db.catalog.beans.CloudIdentity;
34 import org.onap.so.db.catalog.beans.CloudSite;
35 import org.onap.so.db.catalog.beans.ServerType;
36 import org.springframework.beans.factory.annotation.Value;
37 import org.springframework.boot.test.context.SpringBootTest;
38 import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
39 import org.springframework.test.context.ActiveProfiles;
40 import org.springframework.test.context.junit4.SpringRunner;
41
42 import javax.ws.rs.core.MediaType;
43 import java.io.BufferedReader;
44 import java.io.FileReader;
45 import java.io.IOException;
46
47 import static com.github.tomakehurst.wiremock.client.WireMock.*;
48 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
49 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
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         
60         @After
61         public void after() {
62                 WireMock.reset();
63         }
64
65         protected static String getBody(String body, int port, String urlPath) throws IOException {
66                 return body.replaceAll("port", "http://localhost:" + port + urlPath);
67         }
68
69         @Before
70         public void init() throws IOException {
71                 CloudIdentity identity = getCloudIdentity();
72                 CloudSite cloudSite = getCloudSite(identity);
73                 mockCloud(identity, cloudSite);
74         }
75
76         private void mockCloud(CloudIdentity identity, CloudSite cloudSite) throws IOException {
77                 stubFor(get(urlPathEqualTo("/cloudSite/MTN13")).willReturn(aResponse()
78                                 .withBody(getBody(mapper.writeValueAsString(cloudSite),wireMockPort, ""))
79                                 .withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
80                                 .withStatus(HttpStatus.SC_OK)));
81                 stubFor(get(urlPathEqualTo("/cloudSite/DEFAULT")).willReturn(aResponse()
82                                 .withBody(getBody(mapper.writeValueAsString(cloudSite),wireMockPort, ""))
83                                 .withHeader(HttpHeaders.CONTENT_TYPE,MediaType.APPLICATION_JSON)
84                                 .withStatus(HttpStatus.SC_OK)));
85                 stubFor(get(urlPathEqualTo("/cloudIdentity/mtn13")).willReturn(aResponse()
86                                 .withBody(getBody(mapper.writeValueAsString(identity),wireMockPort, ""))
87                                 .withHeader(HttpHeaders.CONTENT_TYPE,MediaType.APPLICATION_JSON)
88                                 .withStatus(HttpStatus.SC_OK)));
89         }
90
91         protected CloudIdentity getCloudIdentity() {
92                 CloudIdentity identity = new CloudIdentity();
93                 identity.setId("mtn13");
94                 identity.setMsoId("m93945");
95                 identity.setMsoPass("93937EA01B94A10A49279D4572B48369");
96                 identity.setAdminTenant("admin");
97                 identity.setMemberRole("admin");
98                 identity.setTenantMetadata(false);
99                 identity.setIdentityUrl("http://localhost:"+wireMockPort+"/v2.0");
100                 identity.setIdentityAuthenticationType(AuthenticationType.USERNAME_PASSWORD);
101                 identity.setIdentityServerType(ServerType.KEYSTONE);
102                 return identity;
103         }
104
105         protected CloudSite getCloudSite(CloudIdentity identity) {
106                 CloudSite cloudSite = new CloudSite();
107                 cloudSite.setId("MTN13");
108                 cloudSite.setCloudVersion("3.0");
109                 cloudSite.setClli("MDT13");
110                 cloudSite.setRegionId("mtn13");
111                 cloudSite.setIdentityService(identity);
112                 return cloudSite;
113         }
114
115         private static String readFile(String fileName) throws IOException {
116                 try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
117                         StringBuilder sb = new StringBuilder();
118                         String line = br.readLine();
119
120                         while (line != null) {
121                                 sb.append(line);
122                                 sb.append("\n");
123                                 line = br.readLine();
124                         }
125                         return sb.toString();
126                 }
127         }
128 }