8000 feat: add functions_env_vars (#1551) · RamsesMartinez/java-docs-samples@5e4fbfe · GitHub
[go: up one dir, main page]

Skip to content

Commit 5e4fbfe

Browse files
authored
feat: add functions_env_vars (GoogleCloudPlatform#1551)
* feat: add functions_env_vars * fix: Address function_ent comments around naming and tests * Comments * fix: Adds org.junit.contrib.java.lang.system.EnvironmentVariables to fix env vars * docs: Convert JavaDoc to single comment * fix: Fix lint issue
1 parent e14f3ba commit 5e4fbfe

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

functions/snippets/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@
6060
<scope>test</scope>
6161
</dependency>
6262

63+
<!-- Required for mocking env vars -->
64+
<dependency>
65+
<groupId>com.github.stefanbirkner</groupId>
66+
<artifactId>system-rules</artifactId>
67+
<version>1.19.0</version>
68+
<scope>test</scope>
69+
</dependency>
6370
</dependencies>
6471

6572
<!-- Required for Java 8 (Alpha) functions in the inline editor -->
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2019 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+
// [START functions_env_vars]
18+
import java.io.IOException;
19+
import java.io.PrintWriter;
20+
import javax.servlet.http.HttpServletRequest;
21+
import javax.servlet.http.HttpServletResponse;
22+
23+
public class EnvVars {
24+
// Returns the environment variable "foo" set during function deployment.
25+
public void envVar(HttpServletRequest request, HttpServletResponse response)
26+
throws IOException {
27+
PrintWriter writer = response.getWriter();
28+
String foo = System.getenv("FOO");
29+
if (foo == null) {
30+
foo = "Specified environment variable is not set.";
31+
}
32+
writer.write(foo);
33+
}
34+
}
35+
36+
// [END functions_env_vars]

functions/snippets/src/test/java/SnippetsTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
import javax.servlet.http.HttpServletResponse;
3333
import org.junit.After;
3434
import org.junit.Before;
35+
import org.junit.Rule;
3536
import org.junit.Test;
37+
import org.junit.contrib.java.lang.system.EnvironmentVariables;
3638
import org.junit.runner.RunWith;
3739
import org.junit.runners.JUnit4;
3840

@@ -45,6 +47,10 @@ public class SnippetsTests {
4547
private ByteArrayOutputStream stdOut;
4648
private StringWriter responseOut;
4749

50+
@Rule
51+
public final EnvironmentVariables environmentVariables
52+
= new EnvironmentVariables();
53+
4854
@Before
4955
public void beforeTest() throws Exception {
5056
// Use a new mock for each test
@@ -167,6 +173,13 @@ public void helloBackgroundTest() throws IOException {
167173
assertThat(responseOut.toString(), containsString("Hello John!"));
168174
}
169175

176+
@Test
177+
public void envTest() throws IOException {
178+
environmentVariables.set("FOO", "BAR");
179+
new EnvVars().envVar(request, response);
180+
assertThat(responseOut.toString(), containsString("BAR"));
181+
}
182+
170183
@Test
171184
public void helloExecutionCount() throws IOException {
172185
new Concepts().executionCount(request, response);

0 commit comments

Comments
 (0)
0