Skip to content

Commit c238962

Browse files
author
Ace Nassri
authored
GCF file split: helloworld + add jvm-langs (for non-Java samples) (GoogleCloudPlatform#2667)
1 parent 8be0357 commit c238962

46 files changed

Lines changed: 4005 additions & 7 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!--
4+
Copyright 2020 Google LLC
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
-->
18+
19+
<project xmlns="http://maven.apache.org/POM/4.0.0"
20+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
22+
<modelVersion>4.0.0</modelVersion>
23+
24+
<groupId>com.example.cloud.functions</groupId>
25+
<artifactId>functions-hello-background</artifactId>
26+
27+
<parent>
28+
<groupId>com.google.cloud.samples</groupId>
29+
<artifactId>shared-configuration</artifactId>
30+
<version>1.0.15</version>
31+
</parent>
32+
33+
<properties>
34+
<powermock.version>2.0.7</powermock.version>
35+
<maven.compiler.target>11</maven.compiler.target>
36+
<maven.compiler.source>11</maven.compiler.source>
37+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
38+
</properties>
39+
40+
<dependencies>
41+
<!-- Required for Function primitives -->
42+
<dependency>
43+
<groupId>com.google.cloud.functions</groupId>
44+
<artifactId>functions-framework-api</artifactId>
45+
<version>1.0.1</version>
46+
</dependency>
47+
48+
<!-- The following dependencies are only required for testing -->
49+
<dependency>
50+
<groupId>org.powermock</groupId>
51+
<artifactId>powermock-core</artifactId>
52+
<version>${powermock.version}</version>
53+
<scope>test</scope>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.powermock</groupId>
57+
<artifactId>powermock-module-junit4</artifactId>
58+
<version>${powermock.version}</version>
59+
<scope>test</scope>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.powermock</groupId>
63+
<artifactId>powermock-api-mockito2</artifactId>
64+
<version>${powermock.version}</version>
65+
<scope>test</scope>
66+
</dependency>
67+
<dependency>
68+
<groupId>com.google.truth</groupId>
69+
<artifactId>truth</artifactId>
70+
<version>1.0.1</version>
71+
<scope>test</scope>
72+
</dependency>
73+
<dependency>
74+
<groupId>com.google.guava</groupId>
75+
<artifactId>guava-testlib</artifactId>
76+
<version>29.0-jre</version>
77+
<scope>test</scope>
78+
</dependency>
79+
</dependencies>
80+
81+
<!-- Disable tests during GCF builds (from parent POM) -->
82+
<!-- You can remove this profile to run tests -->
83+
<!-- when deploying, but we recommend creating -->
84+
<!-- a CI/CD pipeline via Cloud Build instead -->
85+
<profiles>
86+
<profile>
87+
<id>skip_tests_on_gcf</id>
88+
<activation>
89+
<property>
90+
<name>env.NEW_BUILD</name>
91+
</property>
92+
</activation>
93+
<properties>
94+
<skipTests>true</skipTests>
95+
</properties>
96+
</profile>
97+
</profiles>
98+
99+
<build>
100+
<plugins>
101+
<plugin>
102+
<!--
103+
Google Cloud Functions Framework Maven plugin
104+
105+
This plugin allows you to run Cloud Functions Java code
106+
locally. Use the following terminal command to run a
107+
given function locally:
108+
109+
mvn function:run -Drun.functionTarget=your.package.yourFunction
110+
-->
111+
<groupId>com.google.cloud.functions</groupId>
112+
<artifactId>function-maven-plugin</artifactId>
113+
<version>0.9.1</version>
114+
<configuration>
115+
<functionTarget>functions.HelloBackground</functionTarget>
116+
</configuration>
117+
</plugin>
118+
<plugin>
119+
<groupId>org.apache.maven.plugins</groupId>
120+
<artifactId>maven-surefire-plugin</artifactId>
121+
<!-- version 3.0.0-M4 does not load JUnit5 correctly -->
122+
<!-- see https://issues.apache.org/jira/browse/SUREFIRE-1750 -->
123+
<version>3.0.0-M3</version>
124+
<configuration>
125+
<includes>
126+
<include>**/*Test.java</include>
127+
</includes>
128+
<skipTests>${skipTests}</skipTests>
129+
<reportNameSuffix>sponge_log</reportNameSuffix>
130+
<trimStackTrace>false</trimStackTrace>
131+
</configuration>
132+
</plugin>
133+
<plugin> <!-- Required for Java 8 (Alpha) functions in the inline editor -->
134+
<groupId>org.apache.maven.plugins</groupId>
135+
<artifactId>maven-compiler-plugin</artifactId>
136+
<executions>
137+
<execution>
138+
<id>compile</id>
139+
<phase>compile</phase>
140+
<goals>
141+
<goal>compile</goal>
142+
</goals>
143+
</execution>
144+
<execution>
145+
<id>testCompile</id>
146+
<phase>test-compile</phase>
147+
<goals>
148+
<goal>testCompile</goal>
149+
</goals>
150+
</execution>
151+
</executions>
152+
<configuration>
153+
<excludes>
154+
<exclude>.google/</exclude>
155+
</excludes>
156+
</configuration>
157+
</plugin>
158+
</plugins>
159+
</build>
160+
</project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 functions;
18+
19+
// [START functions_helloworld_background]
20+
21+
import com.google.cloud.functions.BackgroundFunction;
22+
import com.google.cloud.functions.Context;
23+
import com.google.cloud.functions.HttpRequest;
24+
import java.util.logging.Logger;
25+
26+
public class HelloBackground implements BackgroundFunction<HttpRequest> {
27+
private static final Logger LOGGER = Logger.getLogger(HelloBackground.class.getName());
28+
29+
@Override
30+
public void accept(HttpRequest request, Context context) {
31+
// name's default value is "world"
32+
String name = request.getFirstQueryParameter("name").orElse("world");
33+
LOGGER.info(String.format("Hello %s!", name));
34+
}
35+
}
36+
// [END functions_helloworld_background]

functions/helloworld/src/test/java/com/example/functions/helloworld/HelloBackgroundTest.java renamed to functions/helloworld/hello-background/src/test/java/functions/HelloBackgroundTest.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,16 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.example.functions.helloworld;
17+
package functions;
1818

1919
import static com.google.common.truth.Truth.assertThat;
2020
import static org.mockito.Mockito.mock;
2121
import static org.mockito.Mockito.when;
2222

23-
import com.example.functions.helloworld.HelloBackground;
2423
import com.google.cloud.functions.HttpRequest;
2524
import com.google.cloud.functions.HttpResponse;
2625
import com.google.common.testing.TestLogHandler;
27-
import java.io.BufferedReader;
2826
import java.io.BufferedWriter;
29-
import java.io.StringReader;
3027
import java.io.StringWriter;
3128
import java.util.Optional;
3229
import java.util.logging.Logger;
@@ -41,7 +38,9 @@ public class HelloBackgroundTest {
4138
@Mock private HttpRequest request;
4239
@Mock private HttpResponse response;
4340

41+
// Must be declared at class-level, or LoggingHandler won't detect log records!
4442
private static final Logger LOGGER = Logger.getLogger(HelloBackground.class.getName());
43+
4544
private static final TestLogHandler LOG_HANDLER = new TestLogHandler();
4645

4746
@Before
@@ -52,9 +51,6 @@ public void beforeTest() throws Exception {
5251
request = mock(HttpRequest.class);
5352
response = mock(HttpResponse.class);
5453

55-
BufferedReader reader = new BufferedReader(new StringReader("{}"));
56-
when(request.getReader()).thenReturn(reader);
57-
5854
BufferedWriter writer = new BufferedWriter(new StringWriter());
5955
when(response.getWriter()).thenReturn(writer);
6056
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!--
4+
Copyright 2020 Google LLC
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
-->
18+
19+
<project xmlns="http://maven.apache.org/POM/4.0.0"
20+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
22+
<modelVersion>4.0.0</modelVersion>
23+
24+
<groupId>com.example.cloud.functions</groupId>
25+
<artifactId>functions-hello-error</artifactId>
26+
27+
<parent>
28+
<groupId>com.google.cloud.samples</groupId>
29+
<artifactId>shared-configuration</artifactId>
30+
<version>1.0.15</version>
31+
</parent>
32+
33+
<properties>
34+
<maven.compiler.target>11</maven.compiler.target>
35+
<maven.compiler.source>11</maven.compiler.source>
36+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
37+
</properties>
38+
39+
<dependencies>
40+
<!-- Required for Function primitives -->
41+
<dependency>
42+
<groupId>com.google.cloud.functions</groupId>
43+
<artifactId>functions-framework-api</artifactId>
44+
<version>1.0.1</version>
45+
</dependency>
46+
</dependencies>
47+
48+
<!-- Disable tests during GCF builds (from parent POM) -->
49+
<!-- You can remove this profile to run tests -->
50+
<!-- when deploying, but we recommend creating -->
51+
<!-- a CI/CD pipeline via Cloud Build instead -->
52+
<profiles>
53+
<profile>
54+
<id>skip_tests_on_gcf</id>
55+
<activation>
56+
<property>
57+
<name>env.NEW_BUILD</name>
58+
</property>
59+
</activation>
60+
<properties>
61+
<skipTests>true</skipTests>
62+
</properties>
63+
</profile>
64+
</profiles>
65+
66+
<build>
67+
<plugins>
68+
<plugin>
69+
<!--
70+
Google Cloud Functions Framework Maven plugin
71+
72+
This plugin allows you to run Cloud Functions Java code
73+
locally. Use the following terminal command to run a
74+
given function locally:
75+
76+
mvn function:run -Drun.functionTarget=your.package.yourFunction
77+
-->
78+
<groupId>com.google.cloud.functions</groupId>
79+
<artifactId>function-maven-plugin</artifactId>
80+
<version>0.9.1</version>
81+
<configuration>
82+
<functionTarget>functions.HelloError</functionTarget>
83+
</configuration>
84+
</plugin>
85+
<plugin>
86+
<groupId>org.apache.maven.plugins</groupId>
87+
<artifactId>maven-surefire-plugin</artifactId>
88+
<!-- version 3.0.0-M4 does not load JUnit5 correctly -->
89+
<!-- see https://issues.apache.org/jira/browse/SUREFIRE-1750 -->
90+
<version>3.0.0-M3</version>
91+
<configuration>
92+
<includes>
93+
<include>**/*Test.java</include>
94+
</includes>
95+
<skipTests>${skipTests}</skipTests>
96+
<reportNameSuffix>sponge_log</reportNameSuffix>
97+
<trimStackTrace>false</trimStackTrace>
98+
</configuration>
99+
</plugin>
100+
<plugin> <!-- Required for Java 8 (Alpha) functions in the inline editor -->
101+
<groupId>org.apache.maven.plugins</groupId>
102+
<artifactId>maven-compiler-plugin</artifactId>
103+
<executions>
104+
<execution>
105+
<id>compile</id>
106+
<phase>compile</phase>
107+
<goals>
108+
<goal>compile</goal>
109+
</goals>
110+
</execution>
111+
<execution>
112+
<id>testCompile</id>
113+
<phase>test-compile</phase>
114+
<goals>
115+
<goal>testCompile</goal>
116+
</goals>
117+
</execution>
118+
</executions>
119+
<configuration>
120+
<excludes>
121+
<exclude>.google/</exclude>
122+
</excludes>
123+
</configuration>
124+
</plugin>
125+
</plugins>
126+
</build>
127+
</project>

0 commit comments

Comments
 (0)