Replaced all tabs with spaces in java and pom.xml
[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 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;
37
38 public class StubOpenStack {
39
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)));
45     }
46
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)));
52     }
53
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)));
57     }
58
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)));
63     }
64
65
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)));
69     }
70
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)));
74     }
75
76     public static void mockOpenStackGetTenantByName(WireMockServer wireMockServer, String tenantName)
77             throws IOException {
78         wireMockServer.stubFor(get(urlMatching("/mockPublicUrl/tenants/[?]name=" + tenantName))
79                 .willReturn(aResponse().withBodyFile("OpenstackResponse_Tenant.json").withStatus(HttpStatus.SC_OK)));
80     }
81
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)));
85     }
86
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)));
90     }
91
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)));
96     }
97
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)));
102     }
103
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)));
107     }
108
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)));
113     }
114
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)));
119     }
120
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)));
125     }
126
127     public static void mockOpenStackGetNeutronNetwork(WireMockServer wireMockServer, String filename,
128             String networkId) {
129         wireMockServer.stubFor(get(urlPathEqualTo("/mockPublicUrl/v2.0/networks/" + networkId)).willReturn(aResponse()
130                 .withHeader("Content-Type", "application/json").withBodyFile(filename).withStatus(HttpStatus.SC_OK)));
131     }
132
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)));
136     }
137
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)));
141     }
142
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();
147
148             while (line != null) {
149                 sb.append(line);
150                 sb.append("\n");
151                 line = br.readLine();
152             }
153             return sb.toString();
154         }
155     }
156
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);
160     }
161 }