2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
23 import org.apache.http.HttpStatus;
25 import java.io.BufferedReader;
26 import java.io.FileReader;
27 import java.io.IOException;
29 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
30 import static com.github.tomakehurst.wiremock.client.WireMock.delete;
31 import static com.github.tomakehurst.wiremock.client.WireMock.get;
32 import static com.github.tomakehurst.wiremock.client.WireMock.post;
33 import static com.github.tomakehurst.wiremock.client.WireMock.put;
34 import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
35 import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
36 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
38 public class StubOpenStack {
40 public static void mockOpenStackResponseAccess(int port) throws IOException {
41 stubFor(post(urlPathEqualTo("/v2.0/tokens")).willReturn(aResponse().withHeader("Content-Type", "application/json")
42 .withBody(getBodyFromFile("OpenstackResponse_Access.json", port, "/mockPublicUrl"))
43 .withStatus(HttpStatus.SC_OK)));
46 public static void mockOpenStackDelete(String id) {
47 stubFor(delete(urlMatching("/mockPublicUrl/stacks/" + id)).willReturn(aResponse()
48 .withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_OK)));
52 public static void mockOpenStackPostStack_200(String filename) {
53 stubFor(post(urlPathEqualTo("/mockPublicUrl/stacks")).willReturn(aResponse()
54 .withHeader("Content-Type", "application/json")
55 .withBodyFile(filename).withStatus(HttpStatus.SC_OK)));
58 public static void mockOpenStackPostTenantWithBodyFile_200() throws IOException {
59 stubFor(post(urlPathEqualTo("/mockPublicUrl/tenants"))
60 .willReturn(aResponse().withBodyFile("OpenstackResponse_Tenant.json").withStatus(HttpStatus.SC_OK)));
63 public static void mockOpenStackGetTenantByName(String tenantName) throws IOException {
64 stubFor(get(urlMatching("/mockPublicUrl/tenants/[?]name=" + tenantName))
65 .willReturn(aResponse().withBodyFile("OpenstackResponse_Tenant.json").withStatus(HttpStatus.SC_OK)));
68 public static void mockOpenStackGetTenantById(String tenantId) throws IOException {
69 stubFor(get(urlPathEqualTo("/mockPublicUrl/tenants/tenantId"))
70 .willReturn(aResponse().withBodyFile("OpenstackResponse_Tenant.json").withStatus(HttpStatus.SC_OK)));
73 public static void mockOpenStackDeleteTenantById_200(String tenantId) {
74 stubFor(delete(urlPathEqualTo("/mockPublicUrl/tenants/" + tenantId)).willReturn(aResponse()
75 .withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_OK)));
78 public static void mockOpenStackGetUserById(String user) {
79 stubFor(get(urlPathEqualTo("/mockPublicUrl/users/" + user)).willReturn(aResponse()
80 .withHeader("Content-Type", "application/json")
81 .withBodyFile("OpenstackResponse_User.json").withStatus(HttpStatus.SC_OK)));
84 public static void mockOpenStackGetUserByName(String userName) {
85 stubFor(get(urlMatching("/mockPublicUrl/users/[?]name=" + userName)).willReturn(aResponse()
86 .withHeader("Content-Type", "application/json")
87 .withBodyFile("OpenstackResponse_User.json").withStatus(HttpStatus.SC_OK)));
90 public static void mockOpenStackGetUserByName_500(String userName) {
91 stubFor(get(urlMatching("/mockPublicUrl/users/[?]name=" + userName)).willReturn(aResponse()
92 .withStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR)));
95 public static void mockOpenStackGetRoles_200(String roleFor) {
96 stubFor(get(urlPathEqualTo("/mockPublicUrl/" + roleFor + "/roles")).willReturn(aResponse()
97 .withHeader("Content-Type", "application/json")
98 .withBodyFile("OpenstackResponse_Roles.json").withStatus(HttpStatus.SC_OK)));
101 public static void mockOpenstackPostNetwork(String responseFile) {
102 stubFor(post(urlPathEqualTo("/mockPublicUrl/v2.0/networks")).willReturn(aResponse()
103 .withHeader("Content-Type", "application/json")
104 .withBodyFile(responseFile)
105 .withStatus(HttpStatus.SC_OK)));
108 public static void mockOpenstackPutNetwork(String responseFile, String networkId) {
109 stubFor(put(urlPathEqualTo("/mockPublicUrl/v2.0/networks/"+networkId)).willReturn(aResponse()
110 .withHeader("Content-Type", "application/json")
111 .withBodyFile(responseFile)
112 .withStatus(HttpStatus.SC_OK)));
115 public static void mockOpenStackGetNeutronNetwork(String filename,String networkId) {
116 stubFor(get(urlPathEqualTo("/mockPublicUrl/v2.0/networks/"+ networkId))
117 .willReturn(aResponse().withHeader("Content-Type", "application/json")
118 .withBodyFile(filename).withStatus(HttpStatus.SC_OK)));
121 public static void mockOpenStackGetNeutronNetwork_500(String networkId) {
122 stubFor(get(urlPathEqualTo("/mockPublicUrl/v2.0/networks/"+ networkId))
123 .willReturn(aResponse().withStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR)));
126 public static void mockOpenStackDeleteNeutronNetwork(String networkId) {
127 stubFor(delete(urlPathEqualTo("/mockPublicUrl/v2.0/networks/" + networkId))
128 .willReturn(aResponse().withStatus(HttpStatus.SC_OK)));
131 private static String readFile(String fileName) throws IOException {
132 try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
133 StringBuilder sb = new StringBuilder();
134 String line = br.readLine();
136 while (line != null) {
139 line = br.readLine();
141 return sb.toString();
145 public static String getBodyFromFile(String fileName, int port, String urlPath) throws IOException {
146 return readFile("src/test/resources/__files/" + fileName).replaceAll("port", "http://localhost:" + port + urlPath);