Skip to content

Commit effb326

Browse files
author
Ace Nassri
authored
GCF file split: firebase (GoogleCloudPlatform#2661)
* GCF file split: firebase * Add back missing helloworld files
1 parent 8371e04 commit effb326

File tree

20 files changed

+1193
-268
lines changed

20 files changed

+1193
-268
lines changed

functions/firebase/auth/pom.xml

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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-firebase-auth</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+
<dependency>
41+
<groupId>com.google.code.gson</groupId>
42+
<artifactId>gson</artifactId>
43+
<version>2.8.6</version>
44+
</dependency>
45+
46+
<!-- Required for Function primitives -->
47+
<dependency>
48+
<groupId>com.google.cloud.functions</groupId>
49+
<artifactId>functions-framework-api</artifactId>
50+
<version>1.0.1</version>
51+
</dependency>
52+
53+
<!-- The following dependencies are only required for testing -->
54+
<dependency>
55+
<groupId>org.junit.jupiter</groupId>
56+
<artifactId>junit-jupiter-api</artifactId>
57+
<version>5.6.2</version>
58+
<scope>test</scope>
59+
</dependency>
60+
<dependency>
61+
<groupId>com.google.truth</groupId>
62+
<artifactId>truth</artifactId>
63+
<version>1.0.1</version>
64+
<scope>test</scope>
65+
</dependency>
66+
<dependency>
67+
<groupId>com.google.guava</groupId>
68+
<artifactId>guava-testlib</artifactId>
69+
<version>29.0-jre</version>
70+
<scope>test</scope>
71+
</dependency>
72+
</dependencies>
73+
74+
<!-- Disable tests during GCF builds (from parent POM) -->
75+
<!-- You can remove this profile to run tests -->
76+
<!-- when deploying, but we recommend creating -->
77+
<!-- a CI/CD pipeline via Cloud Build instead -->
78+
<profiles>
79+
<profile>
80+
<id>skip_tests_on_gcf</id>
81+
<activation>
82+
<property>
83+
<name>env.NEW_BUILD</name>
84+
</property>
85+
</activation>
86+
<properties>
87+
<skipTests>true</skipTests>
88+
</properties>
89+
</profile>
90+
</profiles>
91+
92+
<build>
93+
<plugins>
94+
<plugin>
95+
<!--
96+
Google Cloud Functions Framework Maven plugin
97+
98+
This plugin allows you to run Cloud Functions Java code
99+
locally. Use the following terminal command to run a
100+
given function locally:
101+
102+
mvn function:run -Drun.functionTarget=your.package.yourFunction
103+
-->
104+
<groupId>com.google.cloud.functions</groupId>
105+
<artifactId>function-maven-plugin</artifactId>
106+
<version>0.9.1</version>
107+
<configuration>
108+
<functionTarget>functions.FirebaseAuth</functionTarget>
109+
</configuration>
110+
</plugin>
111+
<plugin>
112+
<groupId>org.apache.maven.plugins</groupId>
113+
<artifactId>maven-surefire-plugin</artifactId>
114+
<!-- version 3.0.0-M4 does not load JUnit5 correctly -->
115+
<!-- see https://issues.apache.org/jira/browse/SUREFIRE-1750 -->
116+
<version>3.0.0-M3</version>
117+
<configuration>
118+
<includes>
119+
<include>**/*Test.java</include>
120+
</includes>
121+
<skipTests>${skipTests}</skipTests>
122+
<reportNameSuffix>sponge_log</reportNameSuffix>
123+
<trimStackTrace>false</trimStackTrace>
124+
</configuration>
125+
</plugin>
126+
<plugin> <!-- Required for Java 8 (Alpha) functions in the inline editor -->
127+
<groupId>org.apache.maven.plugins</groupId>
128+
<artifactId>maven-compiler-plugin</artifactId>
129+
<executions>
130+
<execution>
131+
<id>compile</id>
132+
<phase>compile</phase>
133+
<goals>
134+
<goal>compile</goal>
135+
</goals>
136+
</execution>
137+
<execution>
138+
<id>testCompile</id>
139+
<phase>test-compile</phase>
140+
<goals>
141+
<goal>testCompile</goal>
142+
</goals>
143+
</execution>
144+
</executions>
145+
<configuration>
146+
<excludes>
147+
<exclude>.google/</exclude>
148+
</excludes>
149+
</configuration>
150+
</plugin>
151+
</plugins>
152+
</build>
153+
</project>

functions/firebase/src/main/java/com/example/functions/firebase/FirebaseAuth.java renamed to functions/firebase/auth/src/main/java/functions/FirebaseAuth.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

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

1919
// [START functions_firebase_auth]
2020
import com.google.cloud.functions.Context;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 com.google.common.testing.TestLogHandler;
20+
import com.google.common.truth.Truth;
21+
import java.io.IOException;
22+
import java.util.logging.Logger;
23+
import org.junit.Before;
24+
import org.junit.BeforeClass;
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.junit.runners.JUnit4;
28+
29+
@RunWith(JUnit4.class)
30+
public class FirebaseAuthTest {
31+
32+
// Loggers + handlers for various tested classes
33+
// (Must be declared at class-level, or LoggingHandler won't detect log records!)
34+
private static final Logger LOGGER = Logger.getLogger(FirebaseAuth.class.getName());
35+
36+
private static final TestLogHandler LOG_HANDLER = new TestLogHandler();
37+
38+
@BeforeClass
39+
public static void beforeClass() {
40+
LOGGER.addHandler(LOG_HANDLER);
41+
}
42+
43+
@Before
44+
public void beforeTest() throws IOException {
45+
LOG_HANDLER.clear();
46+
}
47+
48+
@Test
49+
public void functionsFirebaseAuth_shouldShowUserId() {
50+
new FirebaseAuth().accept("{\"uid\": \"foo\"}", null);
51+
52+
Truth.assertThat(LOG_HANDLER.getStoredLogRecords().get(0).getMessage()).isEqualTo(
53+
"Function triggered by change to user: foo");
54+
}
55+
56+
@Test
57+
public void functionsFirebaseAuth_shouldShowOrigin() {
58+
new FirebaseAuth().accept("{\"metadata\": {\"createdAt\": \"123\"}}", null);
59+
60+
Truth.assertThat(LOG_HANDLER.getStoredLogRecords().get(0).getMessage()).isEqualTo(
61+
"Created at: 123");
62+
}
63+
64+
@Test
65+
public void functionsFirebaseAuth_shouldShowVersion() {
66+
new FirebaseAuth().accept("{\"email\": \"foo@google.com\"}", null);
67+
68+
Truth.assertThat(LOG_HANDLER.getStoredLogRecords().get(0).getMessage()).isEqualTo(
69+
"Email: foo@google.com");
70+
}
71+
}

functions/firebase/pom.xml renamed to functions/firebase/firestore-reactive/pom.xml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<modelVersion>4.0.0</modelVersion>
2323

2424
<groupId>com.example.cloud.functions</groupId>
25-
<artifactId>functions-snippets-firebase</artifactId>
25+
<artifactId>functions-firebase-firestore-reactive</artifactId>
2626

2727
<parent>
2828
<groupId>com.google.cloud.samples</groupId>
@@ -52,12 +52,6 @@
5252
</dependency>
5353

5454
<!-- The following dependencies are only required for testing -->
55-
<dependency>
56-
<groupId>com.github.stefanbirkner</groupId>
57-
<artifactId>system-rules</artifactId>
58-
<version>1.19.0</version>
59-
<scope>test</scope>
60-
</dependency>
6155
<dependency>
6256
<groupId>org.powermock</groupId>
6357
<artifactId>powermock-core</artifactId>
@@ -129,6 +123,9 @@
129123
<groupId>com.google.cloud.functions</groupId>
130124
<artifactId>function-maven-plugin</artifactId>
131125
<version>0.9.1</version>
126+
<configuration>
127+
<functionTarget>functions.FirebaseFirestoreReactive</functionTarget>
128+
</configuration>
132129
</plugin>
133130
<plugin>
134131
<groupId>org.apache.maven.plugins</groupId>

functions/firebase/src/main/java/com/example/functions/firebase/FirebaseFirestoreReactive.java renamed to functions/firebase/firestore-reactive/src/main/java/functions/FirebaseFirestoreReactive.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

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

1919
// [START functions_firebase_reactive]
2020

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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 org.powermock.api.mockito.PowerMockito.mock;
20+
import static org.powermock.api.mockito.PowerMockito.when;
21+
22+
import com.google.cloud.firestore.DocumentReference;
23+
import com.google.cloud.firestore.Firestore;
24+
import com.google.common.testing.TestLogHandler;
25+
import com.google.common.truth.Truth;
26+
import functions.eventpojos.MockContext;
27+
import java.io.IOException;
28+
import java.util.logging.Logger;
29+
import org.junit.After;
30+
import org.junit.Before;
31+
import org.junit.BeforeClass;
32+
import org.junit.Test;
33+
import org.junit.jupiter.api.Assertions;
34+
import org.junit.runner.RunWith;
35+
import org.junit.runners.JUnit4;
36+
import org.mockito.ArgumentMatchers;
37+
import org.mockito.Mock;
38+
import org.mockito.Mockito;
39+
import org.powermock.api.mockito.PowerMockito;
40+
import org.powermock.reflect.Whitebox;
41+
42+
@RunWith(JUnit4.class)
43+
public class FirebaseFirestoreReactiveTest {
44+
45+
@Mock private Firestore firestoreMock;
46+
@Mock private DocumentReference referenceMock;
47+
48+
// Loggers + handlers for various tested classes
49+
// (Must be declared at class-level, or LoggingHandler won't detect log records!)
50+
private static final Logger LOGGER = Logger.getLogger(FirebaseFirestoreReactive.class.getName());
51+
52+
private static final TestLogHandler LOG_HANDLER = new TestLogHandler();
53+
54+
@BeforeClass
55+
public static void beforeClass() {
56+
LOGGER.addHandler(LOG_HANDLER);
57+
}
58+
59+
@Before
60+
public void beforeTest() throws IOException {
61+
Mockito.mockitoSession().initMocks(this);
62+
63+
referenceMock = mock(DocumentReference.class, Mockito.RETURNS_DEEP_STUBS);
64+
when(referenceMock.set(ArgumentMatchers.any())).thenReturn(null);
65+
66+
firestoreMock = PowerMockito.mock(Firestore.class);
67+
when(firestoreMock.document(ArgumentMatchers.any())).thenReturn(referenceMock);
68+
69+
LOG_HANDLER.clear();
70+
}
71+
72+
@After
73+
public void afterTest() {
74+
System.out.flush();
75+
LOG_HANDLER.clear();
76+
}
77+
78+
@Test
79+
public void functionsFirebaseReactive_shouldCapitalizeOriginalValue() {
80+
String jsonStr = "{\"value\":{\"fields\":{\"original\":{\"stringValue\":\"foo\"}}}}";
81+
82+
MockContext context = new MockContext();
83+
context.resource = "projects/_/databases/(default)/documents/messages/ABCDE12345";
84+
85+
FirebaseFirestoreReactive functionInstance = new FirebaseFirestoreReactive();
86+
Whitebox.setInternalState(FirebaseFirestoreReactive.class, "FIRESTORE", firestoreMock);
87+
88+
functionInstance.accept(jsonStr, context);
89+
90+
Truth.assertThat(LOG_HANDLER.getStoredLogRecords().get(0).getMessage()).isEqualTo(
91+
"Replacing value: foo --> FOO");
92+
}
93+
94+
@Test
95+
public void functionsFirebaseReactive_shouldReportBadJson() {
96+
String jsonStr = "{\"value\":{\"fields\":{\"original\":{\"missingValue\":\"foo\"}}}}";
97+
98+
MockContext context = new MockContext();
99+
context.resource = "projects/_/databases/(default)/documents/messages/ABCDE12345";
100+
101+
FirebaseFirestoreReactive functionInstance = new FirebaseFirestoreReactive();
102+
Whitebox.setInternalState(FirebaseFirestoreReactive.class, "FIRESTORE", firestoreMock);
103+
104+
IllegalArgumentException e = Assertions.assertThrows(
105+
IllegalArgumentException.class, () -> functionInstance.accept(jsonStr, context));
106+
Truth.assertThat(e).hasMessageThat().startsWith("Malformed JSON");
107+
}
108+
}

functions/firebase/src/test/java/com/example/functions/firebase/eventpojos/MockContext.java renamed to functions/firebase/firestore-reactive/src/test/java/functions/eventpojos/MockContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.example.functions.firebase.eventpojos;
17+
package functions.eventpojos;
1818

1919
import com.google.cloud.functions.Context;
2020

0 commit comments

Comments
 (0)