2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
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 import java.io.BufferedReader;
33 import java.io.FileReader;
34 import java.io.IOException;
35 import org.apache.http.HttpStatus;
36 import com.github.tomakehurst.wiremock.WireMockServer;
38 public class StubOpenStack {
40 public static void mockOpenStackResponseAccess(WireMockServer wireMockServer, int port) throws IOException {
41 wireMockServer.stubFor(post(urlPathEqualTo("/v2.0/tokens"))
42 .willReturn(aResponse().withHeader("Content-Type", "application/json")
43 .withBody(getBodyFromFile("OpenstackResponse_Access.json", port, "/mockPublicUrl"))
44 .withStatus(HttpStatus.SC_OK)));
47 public static void mockOpenStackResponseUnauthorized(WireMockServer wireMockServer, int port) throws IOException {
48 wireMockServer.stubFor(post(urlPathEqualTo("/v2.0/tokens"))
49 .willReturn(aResponse().withHeader("Content-Type", "application/json")
50 .withBody(getBodyFromFile("OpenstackResponse_Access.json", port, "/mockPublicUrl"))
51 .withStatus(HttpStatus.SC_UNAUTHORIZED)));
54 public static void mockOpenStackDelete(WireMockServer wireMockServer, String id) {
55 wireMockServer.stubFor(delete(urlMatching("/mockPublicUrl/stacks/" + id))
56 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_OK)));
59 public static void mockOpenStackGet(WireMockServer wireMockServer, String id) {
60 wireMockServer.stubFor(get(urlPathEqualTo("/mockPublicUrl/stacks/" + id))
61 .willReturn(aResponse().withHeader("Content-Type", "application/json")
62 .withBodyFile("OpenstackResponse_Stack_Created.json").withStatus(HttpStatus.SC_OK)));
66 public static void mockOpenStackPostStack_200(WireMockServer wireMockServer, String filename) {
67 wireMockServer.stubFor(post(urlPathEqualTo("/mockPublicUrl/stacks")).willReturn(aResponse()
68 .withHeader("Content-Type", "application/json").withBodyFile(filename).withStatus(HttpStatus.SC_OK)));
71 public static void mockOpenStackPostTenantWithBodyFile_200(WireMockServer wireMockServer) throws IOException {
72 wireMockServer.stubFor(post(urlPathEqualTo("/mockPublicUrl/tenants"))
73 .willReturn(aResponse().withBodyFile("OpenstackResponse_Tenant.json").withStatus(HttpStatus.SC_OK)));
76 public static void mockOpenStackGetTenantByName(WireMockServer wireMockServer, String tenantName)
78 wireMockServer.stubFor(get(urlMatching("/mockPublicUrl/tenants/[?]name=" + tenantName))
79 .willReturn(aResponse().withBodyFile("OpenstackResponse_Tenant.json").withStatus(HttpStatus.SC_OK)));
82 public static void mockOpenStackGetTenantById(WireMockServer wireMockServer, String tenantId) throws IOException {
83 wireMockServer.stubFor(get(urlPathEqualTo("/mockPublicUrl/tenants/tenantId"))
84 .willReturn(aResponse().withBodyFile("OpenstackResponse_Tenant.json").withStatus(HttpStatus.SC_OK)));
87 public static void mockOpenStackDeleteTenantById_200(WireMockServer wireMockServer, String tenantId) {
88 wireMockServer.stubFor(delete(urlPathEqualTo("/mockPublicUrl/tenants/" + tenantId))
89 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_OK)));
92 public static void mockOpenStackGetUserById(WireMockServer wireMockServer, String user) {
93 wireMockServer.stubFor(get(urlPathEqualTo("/mockPublicUrl/users/" + user))
94 .willReturn(aResponse().withHeader("Content-Type", "application/json")
95 .withBodyFile("OpenstackResponse_User.json").withStatus(HttpStatus.SC_OK)));
98 public static void mockOpenStackGetUserByName(WireMockServer wireMockServer, String userName) {
99 wireMockServer.stubFor(get(urlMatching("/mockPublicUrl/users/[?]name=" + userName))
100 .willReturn(aResponse().withHeader("Content-Type", "application/json")
101 .withBodyFile("OpenstackResponse_User.json").withStatus(HttpStatus.SC_OK)));
104 public static void mockOpenStackGetUserByName_500(WireMockServer wireMockServer, String userName) {
105 wireMockServer.stubFor(get(urlMatching("/mockPublicUrl/users/[?]name=" + userName))
106 .willReturn(aResponse().withStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR)));
109 public static void mockOpenStackGetRoles_200(WireMockServer wireMockServer, String roleFor) {
110 wireMockServer.stubFor(get(urlPathEqualTo("/mockPublicUrl/" + roleFor + "/roles"))
111 .willReturn(aResponse().withHeader("Content-Type", "application/json")
112 .withBodyFile("OpenstackResponse_Roles.json").withStatus(HttpStatus.SC_OK)));
115 public static void mockOpenstackPostNetwork(WireMockServer wireMockServer, String responseFile) {
116 wireMockServer.stubFor(post(urlPathEqualTo("/mockPublicUrl/v2.0/networks"))
117 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile(responseFile)
118 .withStatus(HttpStatus.SC_OK)));
121 public static void mockOpenstackPutNetwork(WireMockServer wireMockServer, String responseFile, String networkId) {
122 wireMockServer.stubFor(put(urlPathEqualTo("/mockPublicUrl/v2.0/networks/" + networkId))
123 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile(responseFile)
124 .withStatus(HttpStatus.SC_OK)));
127 public static void mockOpenStackGetNeutronNetwork(WireMockServer wireMockServer, String filename,
129 wireMockServer.stubFor(get(urlPathEqualTo("/mockPublicUrl/v2.0/networks/" + networkId)).willReturn(aResponse()
130 .withHeader("Content-Type", "application/json").withBodyFile(filename).withStatus(HttpStatus.SC_OK)));
133 public static void mockOpenStackGetNeutronNetwork_500(WireMockServer wireMockServer, String networkId) {
134 wireMockServer.stubFor(get(urlPathEqualTo("/mockPublicUrl/v2.0/networks/" + networkId))
135 .willReturn(aResponse().withStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR)));
138 public static void mockOpenStackDeleteNeutronNetwork(WireMockServer wireMockServer, String networkId) {
139 wireMockServer.stubFor(delete(urlPathEqualTo("/mockPublicUrl/v2.0/networks/" + networkId))
140 .willReturn(aResponse().withStatus(HttpStatus.SC_OK)));
143 private static String readFile(String fileName) throws IOException {
144 try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
145 StringBuilder sb = new StringBuilder();
146 String line = br.readLine();
148 while (line != null) {
151 line = br.readLine();
153 return sb.toString();
157 public static String getBodyFromFile(String fileName, int port, String urlPath) throws IOException {
158 return readFile("src/test/resources/__files/" + fileName).replaceAll("port",
159 "http://localhost:" + port + urlPath);