Containerization feature of SO
[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  * 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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so;
22
23 import org.apache.http.HttpStatus;
24
25 import java.io.BufferedReader;
26 import java.io.FileReader;
27 import java.io.IOException;
28
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;
37
38 public class StubOpenStack {
39
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)));
44     }
45
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)));
49     }
50
51
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)));
56     }
57
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)));
61     }
62
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)));
66     }
67
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)));
71     }
72
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)));
76     }
77
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)));
82     }
83
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)));
88     }
89
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)));
93     }
94
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)));
99     }
100
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)));
106     }
107
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)));
113     }
114     
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)));
119     }
120
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)));
124     }
125
126     public static void mockOpenStackDeleteNeutronNetwork(String networkId) {
127         stubFor(delete(urlPathEqualTo("/mockPublicUrl/v2.0/networks/" + networkId))
128                 .willReturn(aResponse().withStatus(HttpStatus.SC_OK)));
129     }
130
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();
135
136             while (line != null) {
137                 sb.append(line);
138                 sb.append("\n");
139                 line = br.readLine();
140             }
141             return sb.toString();
142         }
143     }
144
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);
147     }
148 }