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;
33 import java.io.BufferedReader;
34 import java.io.FileReader;
35 import java.io.IOException;
37 import org.apache.http.HttpStatus;
39 import com.github.tomakehurst.wiremock.WireMockServer;
41 public class StubOpenStack {
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)));
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)));
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)));
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)));
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)));
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)));
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)));
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)));
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)));
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)));
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)));
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)));
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)));
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)));
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)));
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)));
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)));
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)));
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();
155 while (line != null) {
158 line = br.readLine();
160 return sb.toString();
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);