replace all fixed wiremock ports
[so.git] / adapters / mso-adapter-utils / src / test / java / org / onap / so / StubOpenStack.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 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
26 import static com.github.tomakehurst.wiremock.client.WireMock.delete;
27 import static com.github.tomakehurst.wiremock.client.WireMock.get;
28 import static com.github.tomakehurst.wiremock.client.WireMock.post;
29 import static com.github.tomakehurst.wiremock.client.WireMock.put;
30 import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
31 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
32
33 import java.io.BufferedReader;
34 import java.io.FileReader;
35 import java.io.IOException;
36
37 import org.apache.http.HttpStatus;
38
39 import com.github.tomakehurst.wiremock.WireMockServer;
40
41 public class StubOpenStack {
42
43     public static void mockOpenStackResponseAccess(WireMockServer wireMockServer, int port) throws IOException {
44         wireMockServer.stubFor(post(urlPathEqualTo("/v2.0/tokens")).willReturn(aResponse().withHeader("Content-Type", "application/json")
45                 .withBody(getBodyFromFile("OpenstackResponse_Access.json", port, "/mockPublicUrl"))
46                 .withStatus(HttpStatus.SC_OK)));
47     }
48
49     public static void mockOpenStackResponseUnauthorized(WireMockServer wireMockServer, int port) throws IOException {
50         wireMockServer.stubFor(
51             post(urlPathEqualTo("/v2.0/tokens"))
52                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
53                     .withBody(getBodyFromFile("OpenstackResponse_Access.json", port, "/mockPublicUrl"))
54                     .withStatus(HttpStatus.SC_UNAUTHORIZED)));
55     }
56
57     public static void mockOpenStackDelete(WireMockServer wireMockServer, String id) {
58         wireMockServer.stubFor(delete(urlMatching("/mockPublicUrl/stacks/" + id)).willReturn(aResponse()
59                 .withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_OK)));
60     }
61
62     public static void mockOpenStackGet(WireMockServer wireMockServer, String id) {
63         wireMockServer.stubFor(
64             get(urlPathEqualTo("/mockPublicUrl/stacks/" + id))
65                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
66                     .withBodyFile("OpenstackResponse_Stack_Created.json")
67                     .withStatus(HttpStatus.SC_OK)));
68     }
69
70
71     public static void mockOpenStackPostStack_200(WireMockServer wireMockServer, String filename) {
72         wireMockServer.stubFor(post(urlPathEqualTo("/mockPublicUrl/stacks")).willReturn(aResponse()
73                 .withHeader("Content-Type", "application/json")
74                 .withBodyFile(filename).withStatus(HttpStatus.SC_OK)));
75     }
76
77     public static void mockOpenStackPostTenantWithBodyFile_200(WireMockServer wireMockServer) throws IOException {
78         wireMockServer.stubFor(post(urlPathEqualTo("/mockPublicUrl/tenants"))
79                 .willReturn(aResponse().withBodyFile("OpenstackResponse_Tenant.json").withStatus(HttpStatus.SC_OK)));
80     }
81
82     public static void mockOpenStackGetTenantByName(WireMockServer wireMockServer, String tenantName) throws IOException {
83         wireMockServer.stubFor(get(urlMatching("/mockPublicUrl/tenants/[?]name=" + tenantName))
84                 .willReturn(aResponse().withBodyFile("OpenstackResponse_Tenant.json").withStatus(HttpStatus.SC_OK)));
85     }
86
87     public static void mockOpenStackGetTenantById(WireMockServer wireMockServer, String tenantId) throws IOException {
88         wireMockServer.stubFor(get(urlPathEqualTo("/mockPublicUrl/tenants/tenantId"))
89                 .willReturn(aResponse().withBodyFile("OpenstackResponse_Tenant.json").withStatus(HttpStatus.SC_OK)));
90     }
91
92     public static void mockOpenStackDeleteTenantById_200(WireMockServer wireMockServer, String tenantId) {
93         wireMockServer.stubFor(delete(urlPathEqualTo("/mockPublicUrl/tenants/" + tenantId)).willReturn(aResponse()
94                 .withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_OK)));
95     }
96
97     public static void mockOpenStackGetUserById(WireMockServer wireMockServer, String user) {
98         wireMockServer.stubFor(get(urlPathEqualTo("/mockPublicUrl/users/" + user)).willReturn(aResponse()
99                 .withHeader("Content-Type", "application/json")
100                 .withBodyFile("OpenstackResponse_User.json").withStatus(HttpStatus.SC_OK)));
101     }
102
103     public static void mockOpenStackGetUserByName(WireMockServer wireMockServer, String userName) {
104         wireMockServer.stubFor(get(urlMatching("/mockPublicUrl/users/[?]name=" + userName)).willReturn(aResponse()
105                 .withHeader("Content-Type", "application/json")
106                 .withBodyFile("OpenstackResponse_User.json").withStatus(HttpStatus.SC_OK)));
107     }
108
109     public static void mockOpenStackGetUserByName_500(WireMockServer wireMockServer, String userName) {
110         wireMockServer.stubFor(get(urlMatching("/mockPublicUrl/users/[?]name=" + userName)).willReturn(aResponse()
111                 .withStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR)));
112     }
113
114     public static void mockOpenStackGetRoles_200(WireMockServer wireMockServer, String roleFor) {
115         wireMockServer.stubFor(get(urlPathEqualTo("/mockPublicUrl/" + roleFor + "/roles")).willReturn(aResponse()
116                 .withHeader("Content-Type", "application/json")
117                 .withBodyFile("OpenstackResponse_Roles.json").withStatus(HttpStatus.SC_OK)));
118     }
119
120     public static void mockOpenstackPostNetwork(WireMockServer wireMockServer, String responseFile) {
121         wireMockServer.stubFor(post(urlPathEqualTo("/mockPublicUrl/v2.0/networks")).willReturn(aResponse()
122                 .withHeader("Content-Type", "application/json")
123                 .withBodyFile(responseFile)
124                 .withStatus(HttpStatus.SC_OK)));
125     }
126
127     public static void mockOpenstackPutNetwork(WireMockServer wireMockServer, String responseFile, String networkId) {
128         wireMockServer.stubFor(put(urlPathEqualTo("/mockPublicUrl/v2.0/networks/"+networkId)).willReturn(aResponse()
129                 .withHeader("Content-Type", "application/json")
130                 .withBodyFile(responseFile)
131                 .withStatus(HttpStatus.SC_OK)));
132     }
133     
134     public static void mockOpenStackGetNeutronNetwork(WireMockServer wireMockServer, String filename,String networkId) {
135         wireMockServer.stubFor(get(urlPathEqualTo("/mockPublicUrl/v2.0/networks/"+ networkId))
136                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
137                         .withBodyFile(filename).withStatus(HttpStatus.SC_OK)));
138     }
139
140     public static void mockOpenStackGetNeutronNetwork_500(WireMockServer wireMockServer, String networkId) {
141         wireMockServer.stubFor(get(urlPathEqualTo("/mockPublicUrl/v2.0/networks/"+ networkId))
142                 .willReturn(aResponse().withStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR)));
143     }
144
145     public static void mockOpenStackDeleteNeutronNetwork(WireMockServer wireMockServer, String networkId) {
146         wireMockServer.stubFor(delete(urlPathEqualTo("/mockPublicUrl/v2.0/networks/" + networkId))
147                 .willReturn(aResponse().withStatus(HttpStatus.SC_OK)));
148     }
149
150     private static String readFile(String fileName) throws IOException {
151         try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
152             StringBuilder sb = new StringBuilder();
153             String line = br.readLine();
154
155             while (line != null) {
156                 sb.append(line);
157                 sb.append("\n");
158                 line = br.readLine();
159             }
160             return sb.toString();
161         }
162     }
163
164     public static String getBodyFromFile(String fileName, int port, String urlPath) throws IOException {
165         return readFile("src/test/resources/__files/" + fileName).replaceAll("port", "http://localhost:" + port + urlPath);
166     }
167 }