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