/
HelloHttpTest.java
96 lines (78 loc) · 2.88 KB
/
HelloHttpTest.java
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
/*
* Copyright 2020 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.
*/
package functions;
// [START functions_http_unit_test]
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
import com.google.cloud.functions.HttpRequest;
import com.google.cloud.functions.HttpResponse;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Map;
import java.util.Optional;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
@RunWith(JUnit4.class)
public class HelloHttpTest {
@Mock private HttpRequest request;
@Mock private HttpResponse response;
private BufferedWriter writerOut;
private StringWriter responseOut;
private static final Gson gson = new Gson();
@Before
public void beforeTest() throws IOException {
MockitoAnnotations.openMocks(this);
// use an empty string as the default request content
BufferedReader reader = new BufferedReader(new StringReader(""));
when(request.getReader()).thenReturn(reader);
responseOut = new StringWriter();
writerOut = new BufferedWriter(responseOut);
when(response.getWriter()).thenReturn(writerOut);
}
@Test
public void helloHttp_noParamsGet() throws IOException {
new HelloHttp().service(request, response);
writerOut.flush();
assertThat(responseOut.toString()).isEqualTo("Hello world!");
}
// [END functions_http_unit_test]
@Test
public void helloHttp_urlParamsGet() throws IOException {
when(request.getFirstQueryParameter("name")).thenReturn(Optional.of("Tom"));
new HelloHttp().service(request, response);
writerOut.flush();
assertThat(responseOut.toString()).isEqualTo("Hello Tom!");
}
@Test
public void helloHttp_bodyParamsPost() throws IOException {
String requestJson = gson.toJson(Map.of("name", "Jane"));
BufferedReader jsonReader = new BufferedReader(new StringReader("{'name': 'Jane'}"));
when(request.getReader()).thenReturn(jsonReader);
new HelloHttp().service(request, response);
writerOut.flush();
assertThat(responseOut.toString()).isEqualTo("Hello Jane!");
}
// [START functions_http_unit_test]
}
// [END functions_http_unit_test]