Skip to content

Commit 7b872c3

Browse files
author
Ace Nassri
authored
GCF: Add HTTP unit test sample (GoogleCloudPlatform#2629)
* Add HTTP unit test sample * Add newline
1 parent 003bca6 commit 7b872c3

1 file changed

Lines changed: 99 additions & 0 deletions

File tree

  • functions/snippets/helloworld/src/test/java/com/example/functions/helloworld
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.functions.helloworld;
18+
19+
// [START functions_http_unit_test]
20+
import static com.google.common.truth.Truth.assertThat;
21+
import static org.powermock.api.mockito.PowerMockito.mock;
22+
import static org.powermock.api.mockito.PowerMockito.when;
23+
24+
import com.google.cloud.functions.HttpRequest;
25+
import com.google.cloud.functions.HttpResponse;
26+
import com.google.common.testing.TestLogHandler;
27+
import java.io.BufferedReader;
28+
import java.io.BufferedWriter;
29+
import java.io.IOException;
30+
import java.io.StringReader;
31+
import java.io.StringWriter;
32+
import java.util.Optional;
33+
import org.junit.Before;
34+
import org.junit.Test;
35+
import org.junit.runner.RunWith;
36+
import org.junit.runners.JUnit4;
37+
import org.mockito.Mock;
38+
import org.mockito.Mockito;
39+
40+
@RunWith(JUnit4.class)
41+
public class HelloHttpTest {
42+
@Mock private HttpRequest request;
43+
@Mock private HttpResponse response;
44+
45+
private BufferedWriter writerOut;
46+
private StringWriter responseOut;
47+
48+
private static final TestLogHandler logHandler = new TestLogHandler();
49+
50+
@Before
51+
public void beforeTest() throws IOException {
52+
Mockito.mockitoSession().initMocks(this);
53+
54+
request = mock(HttpRequest.class);
55+
response = mock(HttpResponse.class);
56+
57+
BufferedReader reader = new BufferedReader(new StringReader("{}"));
58+
when(request.getReader()).thenReturn(reader);
59+
60+
responseOut = new StringWriter();
61+
writerOut = new BufferedWriter(responseOut);
62+
when(response.getWriter()).thenReturn(writerOut);
63+
64+
logHandler.clear();
65+
}
66+
67+
@Test
68+
public void helloHttp_noParamsGet() throws IOException {
69+
new HelloHttp().service(request, response);
70+
71+
writerOut.flush();
72+
assertThat(responseOut.toString()).isEqualTo("Hello world!");
73+
}
74+
// [END functions_http_unit_test]
75+
76+
@Test
77+
public void helloHttp_urlParamsGet() throws IOException {
78+
when(request.getFirstQueryParameter("name")).thenReturn(Optional.of("Tom"));
79+
80+
new HelloHttp().service(request, response);
81+
82+
writerOut.flush();
83+
assertThat(responseOut.toString()).isEqualTo("Hello Tom!");
84+
}
85+
86+
@Test
87+
public void helloHttp_bodyParamsPost() throws IOException {
88+
BufferedReader jsonReader = new BufferedReader(new StringReader("{'name': 'Jane'}"));
89+
90+
when(request.getReader()).thenReturn(jsonReader);
91+
92+
new HelloHttp().service(request, response);
93+
writerOut.flush();
94+
95+
assertThat(responseOut.toString()).isEqualTo("Hello Jane!");
96+
}
97+
// [START functions_http_unit_test]
98+
}
99+
// [END functions_http_unit_test]

0 commit comments

Comments
 (0)