8000 Add GCF JVM language samples (Kotlin/Scala/Groovy) (#2485) · georg78sf/java-docs-samples@41fa541 · GitHub
[go: up one dir, main page]

Skip to content

Commit 41fa541

Browse files
author
Ace Nassri
authored
Add GCF JVM language samples (Kotlin/Scala/Groovy) (GoogleCloudPlatform#2485)
* Add Kotlin/Scala samples * Add region tags * Downgrade kotlin version to match build environment * Add Groovy sample + remove cruft from Kotlin * Add missing region tags * pom.xml: move surefire after Kotlin compilation * Move non-Java languages into separate paths + fix test failures * Follow DRIFT test naming convention * Add newlines where appropriate
1 parent daa2ce4 commit 41fa541

File tree

12 files changed

+577
-2
lines changed

12 files changed

+577
-2
lines changed

functions/snippets/pom.xml

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
<maven.compiler.target>11</maven.compiler.target>
2424
<maven.compiler.source>11</maven.compiler.source>
2525
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
26+
<kotlin.version>1.2.41</kotlin.version>
27+
<groovy.version>3.0.2</groovy.version>
2628
</properties>
2729

2830
<dependencies>
@@ -52,6 +54,22 @@
5254
<version>3.4.2</version>
5355
</dependency>
5456

57+
<!-- Required for scala samples -->
58+
<dependency>
59+
<groupId>org.scala-lang</groupId>
60+
<artifactId>scala-library</artifactId>
61+
<version>2.13.1</version>
62+
</dependency>
63+
64+
<!-- Required for groovy samples -->
65+
<dependency>
66+
<groupId>org.codehaus.groovy</groupId>
67+
<artifactId>groovy-all</artifactId>
68+
<version>${groovy.version}</version>
69+
<type>pom</type>
70+
</dependency>
71+
72+
5573
<!-- The following dependencies are only required for testing -->
5674
<dependency>
5775
<groupId>junit</groupId>
@@ -112,6 +130,17 @@
112130
<version>1.0.0-alpha-2-rc3</version>
113131
<type>jar</type>
114132
</dependency>
133+
<dependency>
134+
<groupId>org.jetbrains.kotlin</groupId>
135+
<artifactId>kotlin-stdlib-jdk8</artifactId>
136+
<version>${kotlin.version}</version>
137+
</dependency>
138+
<dependency>
139+
<groupId>org.jetbrains.kotlin</groupId>
140+
<artifactId>kotlin-test</artifactId>
141+
<version>${kotlin.version}</version>
142+
<scope>test</scope>
143+
</dependency>
115144
</dependencies>
116145

117146
<!-- Disable tests during GCF builds -->
@@ -133,6 +162,76 @@
133162

134163
<build>
135164
<plugins>
165+
<plugin>
166+
<groupId>org.codehaus.gmavenplus</groupId>
167+
<artifactId>gmavenplus-plugin</artifactId>
168+
<version>1.7.0</version>
169+
<executions>
170+
<execution>
171+
<id>groovy-compile</id>
172+
<phase>process-resources</phase>
173+
<goals>
174+
<goal>addSources</goal>
175+
<goal>compile</goal>
176+
</goals>
177+
</execution>
178+
</executions>
179+
<dependencies>
180+
<dependency>
181+
<groupId>org.codehaus.groovy</groupId>
182+
<artifactId>groovy-all</artifactId>
183+
<!-- any version of Groovy \>= 1.5.0 should work here -->
184+
<version>${groovy.version}</version>
185+
<scope>runtime</scope>
186+
<type>pom</type>
187+
</dependency>
188+
</dependencies>
189+
</plugin>
190+
<plugin>
191+
<groupId>net.alchim31.maven</groupId>
192+
<artifactId>scala-maven-plugin</artifactId>
193+
<version>4.3.1</version>
194+
<executions>
195+
<execution>
196+
<id>scala-compile</id>
197+
<phase>process-resources</phase>
198+
<goals>
199+
<goal>add-source</goal>
200+
<goal>compile</goal>
201+
</goals>
202+
</execution>
203+
</executions>
204+
</plugin>
205+
<plugin>
206+
<groupId>org.jetbrains.kotlin</groupId>
207+
<artifactId>kotlin-maven-plugin</artifactId>
208+
<version>${kotlin.version}</version>
209+
<executions>
210+
<execution>
211+
<id>compile</id>
212+
<phase>compile</phase>
213+
<goals>
214+
<goal>compile</goal>
215+
</goals>
216+
<configuration>
217+
<sourceDirs>
218+
<source>src/main/kotlin</source>
219+
<source>target/generated-sources/annotations</source>
220+
</sourceDirs>
221+
</configuration>
222+
</execution>
223+
<execution>
224+
<id>test-compile</id>
225+
<phase>test-compile</phase>
226+
<goals>
227+
<goal>test-compile</goal>
228+
</goals>
229+
</execution>
230+
</executions>
231+
<configuration>
232+
<jvmTarget>1.8</jvmTarget>
233+
</configuration>
234+
</plugin>
136235
<plugin>
137236
<groupId>org.apache.maven.plugins</groupId>
138237
<artifactId>maven-surefire-plugin</artifactId>
@@ -146,6 +245,22 @@
146245
<plugin> <!-- Required for Java 8 (Alpha) functions in the inline editor -->
147246
<groupId>org.apache.maven.plugins</groupId>
148247
<artifactId>maven-compiler-plugin</artifactId>
248+
<executions>
249+
<execution>
250+
<id>compile</id>
251+
<phase>compile</phase>
252+
<goals>
253+
<goal>compile</goal>
254+
</goals>
255+
</execution>
256+
<execution>
257+
<id>testCompile</id>
258+
<phase>test-compile</phase>
259+
<goals>
260+
<goal>testCompile</goal>
261+
</goals>
262+
</execution>
263+
</executions>
149264
<configuration>
150265
<excludes>
151266
<exclude>.google/</exclude>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.example.functions
2+
3+
// [START functions_helloworld_background_groovy]
4+
import com.google.cloud.functions.BackgroundFunction
5+
import com.google.cloud.functions.Context
6+
import com.google.cloud.functions.HttpRequest
7+
8+
import java.util.logging.Logger;
9+
10+
class GroovyHelloBackground implements BackgroundFunction<HttpRequest> {
11+
private static final Logger LOGGER = Logger.getLogger(GroovyHelloBackground.class.name)
12+
13+
@Override
14+
void accept(HttpRequest request, Context context) {
15+
String name = "world"
16+
if (request.getFirstQueryParameter("name").isPresent()) {
17+
name = request.getFirstQueryParameter("name").get()
18+
}
19+
LOGGER.info(String.format("Hello %s!", name))
20+
}
21+
}
22+
// [END functions_helloworld_background_groovy]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.example.functions
2+
3+
// [START functions_helloworld_get_groovy]
4+
import com.google.cloud.functions.HttpFunction
5+
import com.google.cloud.functions.HttpRequest
6+
import com.google.cloud.functions.HttpResponse
7+
8+
class GroovyHelloWorld implements HttpFunction {
9+
@Override
10+
void service(HttpRequest request, HttpResponse response) {
11+
response.writer.write("Hello World!")
12+
}
13+
}
14+
// [END functions_helloworld_get_groovy]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.example.functions
2+
3+
// [START functions_helloworld_background]
4+
import com.google.cloud.functions.{BackgroundFunction, Context, HttpRequest}
5+
import java.util.logging.Logger
6+
7+
class ScalaHelloBackground extends BackgroundFunction[HttpRequest] {
8+
9+
val LOGGER = Logger.getLogger(this.getClass.getName)
10+
11+
override def accept(t: HttpRequest, context: Context): Unit = {
12+
var name = "world"
13+
if (t.getFirstQueryParameter("name").isPresent) {
14+
name = t.getFirstQueryParameter("name").get()
15+
}
16+
LOGGER.info(String.format("Hello %s!", name))
17+
}
18+
}
19+
// [END functions_helloworld_background]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.example.functions
2+
3+
import com.google.cloud.functions.{HttpFunction, HttpRequest, HttpResponse}
4+
5+
// [START functions_helloworld_get]
6+
class ScalaHelloWorld extends HttpFunction {
7+
override def service(httpRequest: HttpRequest, httpResponse: HttpResponse): Unit = {
8+
httpResponse.getWriter.write("Hello World!")
9+
}
10+
}
11+
// [END functions_helloworld_get]
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
package com.example.functions
17+
18+
// [START functions_helloworld_background_kotlin]
19+
import com.google.cloud.functions.BackgroundFunction
20+
import com.google.cloud.functions.Context
21+
import com.google.cloud.functions.HttpRequest
22+
import java.util.logging.Logger
23+
24+
class KotlinHelloBackground : BackgroundFunction<HttpRequest> {
25+
override fun accept(request: HttpRequest, context: Context) {
26+
var name = "world"
27+
if (request.getFirstQueryParameter("name").isPresent) {
28+
name = request.getFirstQueryParameter("name").get()
29+
}
30+
LOGGER.info(String.format("Hello %s!", name))
31+
}
32+
33< F438 /td>+
companion object {
34+
private val LOGGER = Logger.getLogger(KotlinHelloBackground::class.java.name)
35+
}
36+
}
37+
// [END functions_helloworld_background_kotlin]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
package com.example.functions
17+
18+
// [START functions_helloworld_get_kotlin]
19+
import com.google.cloud.functions.HttpFunction
20+
import com.google.cloud.functions.HttpRequest
21+
import com.google.cloud.functions.HttpResponse
22+
import java.io.IOException
23+
import java.util.logging.Logger
24+
25+
class KotlinHelloWorld : HttpFunction {
26+
// Simple function to return "Hello World"
27+
@Throws(IOException::class)
28+
override fun service(request: HttpRequest, response: HttpResponse) {
29+
response.writer.write("Hello World!")
30+
}
31+
}
32+
// [END functions_helloworld_get_kotlin]
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static org.mockito.Mockito.when;
21+
22+
import com.google.cloud.functions.HttpRequest;
23+
import com.google.cloud.functions.HttpResponse;
24+
import com.google.common.testing.TestLogHandler;
25+
import java.io.BufferedReader;
26+
import java.io.BufferedWriter;
27+
import java.io.IOException;
28+
import java.io.StringReader;
29+
import java.io.StringWriter;
30+
import java.util.Optional;
31+
import java.util.logging.Logger;
32+
import org.junit.After;
33+
import org.junit.Before;
34+
import org.junit.BeforeClass;
35+
import org.junit.Test;
36+
import org.junit.runner.RunWith;
37+
import org.junit.runners.JUnit4;
38+
import org.mockito.Mock;
39+
import org.mockito.Mockito;
40+
import org.powermock.api.mockito.PowerMockito;
41+
42+
@RunWith(JUnit4.class)
43+
public class GroovyTests {
44+
@Mock private HttpRequest request;
45+
@Mock private HttpResponse response;
46+
47+
private static final Logger BACKGROUND_LOGGER = Logger.getLogger(
48+
GroovyHelloBackground.class.getName());
49+
private static final TestLogHandler logHandler = new TestLogHandler();
50+
51+
private BufferedWriter writerOut;
52+
private StringWriter responseOut;
53+
54+
@BeforeClass
55+
public static void beforeClass() {
56+
BACKGROUND_LOGGER.addHandler(logHandler);
57+
}
58+
59+
@Before
60+
public void beforeTest() throws IOException {
61+
Mockito.mockitoSession().initMocks(this);
62+
63+
request = PowerMockito.mock(HttpRequest.class);
64+
response = PowerMockito.mock(HttpResponse.class);
65+
66+
BufferedReader reader = new BufferedReader(new StringReader("{}"));
67+
PowerMockito.when(request.getReader()).thenReturn(reader);
68+
69+
responseOut = new StringWriter();
70+
writerOut = new BufferedWriter(responseOut);
71+
PowerMockito.when(response.getWriter()).thenReturn(writerOut);
72+
73+
logHandler.clear();
74+
}
75+
76+
@After
77+
public void afterTest() {
78+
System.out.flush();
79+
logHandler.flush();
80+
}
81+
82+
@Test
83+
public void functionsHelloworldGetGroovy_shouldPrintHelloWorld() throws IOException {
84+
new GroovyHelloWorld().service(request, response);
85+
86+
writerOut.flush();
87+
assertThat(responseOut.toString()).contains("Hello World!");
88+
}
89+
90+
@Test
91+
public void functionsHelloworldBackgroundGroovy_shouldPrintName() throws Exception {
92+
when(request.getFirstQueryParameter("name")).thenReturn(Optional.of("John"));
93+
94+
new GroovyHelloBackground().accept(request, new MockContext());
95+
96+
String message = logHandler.getStoredLogRecords().get(0).getMessage();
97+
assertThat("Hello John!").isEqualTo(message);
98+
}
99+
100+
@Test
101+
public void functionsHelloworldBackgroundGroovy_shouldPrintHelloWorld() throws Exception {
102+
new GroovyHelloBackground().accept(request, new MockContext());
103+
104+
String message = logHandler.getStoredLogRecords().get(0).getMessage();
105+
assertThat("Hello world!").isEqualTo(message);
106+
}
107+
}

0 commit comments

Comments
 (0)
0