Skip to content

Commit 4cb65d9

Browse files
author
Ace Nassri
authored
functions: remove http helloBackground in favor of helloPubSub (GoogleCloudPlatform#2813)
* JVM langs: helloBackground -> helloPubSub * Remove Java helloBackground sample
1 parent 7338555 commit 4cb65d9

File tree

22 files changed

+382
-619
lines changed

22 files changed

+382
-619
lines changed

functions/helloworld/groovy-hello-background/src/main/groovy/functions/GroovyHelloBackground.groovy

Lines changed: 0 additions & 20 deletions
This file was deleted.

functions/helloworld/groovy-hello-background/src/test/java/functions/GroovyHelloBackgroundTest.java

Lines changed: 0 additions & 100 deletions
This file was deleted.

functions/helloworld/groovy-hello-background/pom.xml renamed to functions/helloworld/groovy-hello-pubsub/pom.xml

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
</parent>
3232

3333
<properties>
34-
<powermock.version>2.0.7</powermock.version>
3534
<maven.compiler.target>11</maven.compiler.target>
3635
<maven.compiler.source>11</maven.compiler.source>
3736
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -56,20 +55,12 @@
5655
</dependency>
5756

5857
<!-- The following dependencies are only required for testing -->
59-
<dependency>
60-
<groupId>org.mockito</groupId>
61-
<artifactId>mockito-core</artifactId>
62-
<version>3.3.3</version>
63-
<scope>test</scope>
64-
</dependency>
6558
<dependency>
6659
<groupId>junit</groupId>
6760
<artifactId>junit</artifactId>
6861
<version>4.13</version>
6962
<scope>test</scope>
7063
</dependency>
71-
72-
<!-- Required for mocking env vars -->
7364
<dependency>
7465
<groupId>com.google.truth</groupId>
7566
<artifactId>truth</artifactId>
@@ -82,24 +73,6 @@
8273
<version>29.0-jre</version>
8374
<scope>test</scope>
8475
</dependency>
85-
<dependency>
86-
<groupId>org.powermock</groupId>
87-
<artifactId>powermock-core</artifactId>
88-
<version>${powermock.version}</version>
89-
<scope>test</scope>
90-
</dependency>
91-
<dependency>
92-
<groupId>org.powermock</groupId>
93-
<artifactId>powermock-module-junit4</artifactId>
94-
<version>${powermock.version}</version>
95-
<scope>test</scope>
96-
</dependency>
97-
<dependency>
98-
<groupId>org.powermock</groupId>
99-
<artifactId>powermock-api-mockito2</artifactId>
100-
<version>${powermock.version}</version>
101-
<scope>test</scope>
102-
</dependency>
10376
</dependencies>
10477

10578
<!-- Disable tests during GCF builds (from parent POM) -->
@@ -136,7 +109,7 @@
136109
<artifactId>function-maven-plugin</artifactId>
137110
<version>0.9.2</version>
138111
<configuration>
139-
<functionTarget>functions.GroovyHelloBackground</functionTarget>
112+
<functionTarget>functions.GroovyHelloPubSub</functionTarget>
140113
</configuration>
141114
</plugin>
142115
<plugin>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package functions
2+
3+
// [START functions_helloworld_pubsub]
4+
import com.google.cloud.functions.BackgroundFunction
5+
import com.google.cloud.functions.Context
6+
import functions.eventpojos.PubSubMessage
7+
import java.nio.charset.StandardCharsets
8+
import java.util.logging.Logger;
9+
10+
class GroovyHelloPubSub implements BackgroundFunction<PubSubMessage> {
11+
private static final Logger LOGGER = Logger.getLogger(GroovyHelloPubSub.class.name)
12+
13+
@Override
14+
void accept(PubSubMessage message, Context context) {
15+
// name's default value is "world"
16+
String name = "world"
17+
18+
if (message?.data != null) {
19+
name = new String(Base64.getDecoder().decode(message.data), StandardCharsets.UTF_8)
20+
}
21+
22+
LOGGER.info(String.format("Hello %s!", name))
23+
return
24+
}
25+
}
26+
// [END functions_helloworld_pubsub]
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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.eventpojos
18+
19+
// [START functions_helloworld_pubsub_message]
20+
class PubSubMessage {
21+
// Cloud Functions uses GSON to populate this object.
22+
// Field types/names are specified by Cloud Functions
23+
// Changing them may break your code!
24+
private String data;
25+
private Map<String, String> attributes;
26+
private String messageId;
27+
private String publishTime;
28+
// [END functions_helloworld_pubsub_message]
29+
30+
// Manually-defined getters and setters are required for
31+
// inter-operation with Java files, but are not necessary
32+
// for pure-Groovy codebases
33+
String getData() {
34+
return data;
35+
}
36+
37+
void setData(String data) {
38+
this.data = data;
39+
}
40+
41+
Map<String, String> getAttributes() {
42+
return attributes;
43+
}
44+
45+
void setAttributes(Map<String, String> attributes) {
46+
this.attributes = attributes;
47+
}
48+
49+
String getMessageId() {
50+
return messageId;
51+
}
52+
53+
void setMessageId(String messageId) {
54+
this.messageId = messageId;
55+
}
56+
57+
String getPublishTime() {
58+
return publishTime;
59+
}
60+
61+
def setPublishTime = { String publishTime ->
62+
this.publishTime = publishTime;
63+
}
64+
// [START functions_helloworld_pubsub_message]
65+
}
66+
// [END functions_helloworld_pubsub_message]
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.common.testing.TestLogHandler;
22+
import functions.eventpojos.MockContext;
23+
import functions.eventpojos.PubSubMessage;
24+
import java.io.IOException;
25+
import java.nio.charset.StandardCharsets;
26+
import java.util.Base64;
27+
import java.util.logging.Logger;
28+
import org.junit.After;
29+
import org.junit.Before;
30+
import org.junit.BeforeClass;
31+
import org.junit.Test;
32+
import org.junit.runner.RunWith;
33+
import org.junit.runners.JUnit4;
34+
35+
@RunWith(JUnit4.class)
36+
public class GroovyHelloPubSubTest {
37+
private static final Logger logger = Logger.getLogger(
38+
GroovyHelloPubSub.class.getName());
39+
private static final TestLogHandler LOG_HANDLER = new TestLogHandler();
40+
41+
@BeforeClass
42+
public static void beforeClass() {
43+
logger.addHandler(LOG_HANDLER);
44+
}
45+
46+
@Before
47+
public void beforeTest() throws IOException {
48+
LOG_HANDLER.clear();
49+
}
50+
51+
@After
52+
public void afterTest() {
53+
System.out.flush();
54+
LOG_HANDLER.flush();
55+
}
56+
57+
@Test
58+
public void functionsHelloworldPubsubGroovy_shouldPrintName() throws Exception {
59+
PubSubMessage message = new PubSubMessage();
60+
message.setData(Base64.getEncoder().encodeToString(
61+
"John".getBytes(StandardCharsets.UTF_8)));
62+
63+
new GroovyHelloPubSub().accept(message, new MockContext());
64+
65+
assertThat("Hello John!").isEqualTo(
66+
LOG_HANDLER.getStoredLogRecords().get(0).getMessage());
67+
}
68+
69+
@Test
70+
public void functionsHelloworldPubsubGroovy_shouldPrintHelloWorld() throws Exception {
71+
new GroovyHelloPubSub().accept(new PubSubMessage(), new MockContext());
72+
73+
assertThat("Hello world!").isEqualTo(
74+
LOG_HANDLER.getStoredLogRecords().get(0).getMessage());
75+
}
76+
}

0 commit comments

Comments
 (0)