forked from GoogleCloudPlatform/java-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnippetsTests.java
More file actions
208 lines (166 loc) · 6.6 KB
/
Copy pathSnippetsTests.java
File metadata and controls
208 lines (166 loc) · 6.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Base64;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.EnvironmentVariables;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class SnippetsTests {
private HttpServletRequest request;
private HttpServletResponse response;
private ByteArrayOutputStream stdOut;
private StringWriter responseOut;
@Rule
public final EnvironmentVariables environmentVariables
= new EnvironmentVariables();
@Before
public void beforeTest() throws Exception {
// Use a new mock for each test
request = mock(HttpServletRequest.class);
response = mock(HttpServletResponse.class);
responseOut = new StringWriter();
PrintWriter writer = new PrintWriter(responseOut);
when(response.getWriter()).thenReturn(writer);
// Capture std out
stdOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(stdOut));
}
@After
public void afterTest() {
request = null;
response = null;
responseOut = null;
stdOut = null;
System.setOut(null);
}
@Test
public void helloWorldTest() throws IOException {
new HelloWorld().helloGet(request, response);
assertThat(responseOut.toString(), containsString("Hello World!"));
}
@Test
public void logHelloWorldTest() throws IOException {
new LogHelloWorld().logHelloWorld(request, response);
assertThat(responseOut.toString(), containsString("Messages successfully logged!"));
}
@Test
public void sendHttpRequestTest() throws IOException, InterruptedException {
new SendHttpRequest().sendHttpRequest(request, response);
assertThat(responseOut.toString(), containsString("Received code "));
}
@Test
public void corsEnabledTest() throws IOException {
when(request.getMethod()).thenReturn("GET");
new CorsEnabled().corsEnabled(request, response);
assertThat(responseOut.toString(), containsString("CORS headers set successfully!"));
}
@Test
public void parseContentTypeTest_json() throws IOException {
// Send a request with JSON data
when(request.getContentType()).thenReturn("application/json");
BufferedReader bodyReader = new BufferedReader(new StringReader("{\"name\":\"John\"}"));
when(request.getReader()).thenReturn(bodyReader);
new ParseContentType().parseContentType(request, response);
assertThat(responseOut.toString(), containsString("Hello John!"));
}
@Test
public void parseContentTypeTest_base64() throws IOException {
// Send a request with octet-stream
when(request.getContentType()).thenReturn("application/octet-stream");
// Create mock input stream to return the data
byte[] b64Body = Base64.getEncoder().encode("John".getBytes());
ServletInputStream bodyInputStream = mock(ServletInputStream.class);
when(bodyInputStream.readAllBytes()).thenReturn(b64Body);
// Return the input stream when the request calls it
when(request.getInputStream()).thenReturn(bodyInputStream);
new ParseContentType().parseContentType(request, response);
assertThat(responseOut.toString(), containsString("Hello John!"));
}
@Test
public void parseContentTypeTest_text() throws IOException {
// Send a request with plain text
when(request.getContentType()).thenReturn("text/plain");
BufferedReader bodyReader = new BufferedReader(new StringReader("John"));
when(request.getReader()).thenReturn(bodyReader);
new ParseContentType().parseContentType(request, response);
assertThat(responseOut.toString(), containsString("Hello John!"));
}
@Test
public void parseContentTypeTest_form() throws IOException {
// Send a request with plain text
when(request.getContentType()).thenReturn("application/x-www-form-urlencoded");
when(request.getParameter("name")).thenReturn("John");
new ParseContentType().parseContentType(request, response);
assertThat(responseOut.toString(), containsString("Hello John!"));
}
@Test
public void scopesTest() throws IOException {
new Scopes().scopeDemo(request, response);
assertThat(responseOut.toString(), containsString("Instance:"));
}
@Test
public void lazyTest() throws IOException {
new Lazy().lazyGlobal(request, response);
assertThat(responseOut.toString(), containsString("Lazy global:"));
}
@Test
public void retrieveLogsTest() throws IOException {
new RetrieveLogs().retrieveLogs(request, response);
assertThat(responseOut.toString(), containsString("Logs retrieved successfully."));
}
@Test
public void helloBackgroundTest() throws IOException {
when(request.getContentType()).thenReturn("application/json");
BufferedReader bodyReader = new BufferedReader(new StringReader("{\"name\":\"John\"}"));
when(request.getReader()).thenReturn(bodyReader);
new HelloBackground().helloBackground(request, response);
assertThat(responseOut.toString(), containsString("Hello John!"));
}
@Test
public void filesTest() throws IOException {
new FileSystem().listFiles(request, response);
assertThat(responseOut.toString(), containsString("Files:"));
}
@Test
public void envTest() throws IOException {
environmentVariables.set("FOO", "BAR");
new EnvVars().envVar(request, response);
assertThat(responseOut.toString(), containsString("BAR"));
}
@Test
public void helloExecutionCount() throws IOException {
new Concepts().executionCount(request, response);
assertThat(responseOut.toString(), containsString("Instance execution count: 1"));
}
}