Skip to content

Commit a91af14

Browse files
author
Ace Nassri
authored
GCF file split: logging (GoogleCloudPlatform#2665)
* GCF file split: logging * Address comments + fix tests
1 parent b4a9899 commit a91af14

File tree

9 files changed

+386
-73
lines changed

9 files changed

+386
-73
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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-logging-log-hello-world</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+
</dependencies>
48+
49+
<!-- Disable tests during GCF builds (from parent POM) -->
50+
<!-- You can remove this profile to run tests -->
51+
<!-- when deploying, but we recommend creating -->
52+
<!-- a CI/CD pipeline via Cloud Build instead -->
53+
<profiles>
54+
<profile>
55+
<id>skip_tests_on_gcf</id>
56+
<activation>
57+
<property>
58+
<name>env.NEW_BUILD</name>
59+
</property>
60+
</activation>
61+
<properties>
62+
<skipTests>true</skipTests>
63+
</properties>
64+
</profile>
65+
</profiles>
66+
67+
<build>
68+
<plugins>
69+
<plugin>
70+
<!--
71+
Google Cloud Functions Framework Maven plugin
72+
73+
This plugin allows you to run Cloud Functions Java code
74+
locally. Use the following terminal command to run a
75+
given function locally:
76+
77+
mvn function:run -Drun.functionTarget=your.package.yourFunction
78+
-->
79+
<groupId>com.google.cloud.functions</groupId>
80+
<artifactId>function-maven-plugin</artifactId>
81+
<version>0.9.1</version>
82+
<configuration>
83+
<functionTarget>functions.LogHelloWorld</functionTarget>
84+
</configuration>
85+
</plugin>
86+
<plugin>
87+
<groupId>org.apache.maven.plugins</groupId>
88+
<artifactId>maven-surefire-plugin</artifactId>
89+
<!-- version 3.0.0-M4 does not load JUnit5 correctly -->
90+
<!-- see https://issues.apache.org/jira/browse/SUREFIRE-1750 -->
91+
<version>3.0.0-M3</version>
92+
<configuration>
93+
<includes>
94+
<include>**/*Test.java</include>
95+
</includes>
96+
<skipTests>${skipTests}</skipTests>
97+
<reportNameSuffix>sponge_log</reportNameSuffix>
98+
<trimStackTrace>false</trimStackTrace>
99+
</configuration>
100+
</plugin>
101+
<plugin> <!-- Required for Java 8 (Alpha) functions in the inline editor -->
102+
<groupId>org.apache.maven.plugins</groupId>
103+
<artifactId>maven-compiler-plugin</artifactId>
104+
<executions>
105+
<execution>
106+
<id>compile</id>
107+
<phase>compile</phase>
108+
<goals>
109+
<goal>compile</goal>
110+
</goals>
111+
</execution>
112+
<execution>
113+
<id>testCompile</id>
114+
<phase>test-compile</phase>
115+
<goals>
116+
<goal>testCompile</goal>
117+
</goals>
118+
</execution>
119+
</executions>
120+
<configuration>
121+
<excludes>
122+
<exclude>.google/</exclude>
123+
</excludes>
124+
</configuration>
125+
</plugin>
126+
</plugins>
127+
</build>
128+
</project>

functions/logging/src/main/java/com/example/functions/logging/LogHelloWorld.java renamed to functions/logging/log-hello-world/src/main/java/functions/LogHelloWorld.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.logging;
17+
package functions;
1818

1919
// [START functions_log_helloworld]
2020

Lines changed: 41 additions & 29 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-logging</artifactId>
25+
<artifactId>functions-logging-retrieve-logs</artifactId>
2626

2727
<parent>
2828
<groupId>com.google.cloud.samples</groupId>
@@ -37,39 +37,32 @@
3737
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3838
</properties>
3939

40+
<dependencyManagement>
41+
<dependencies>
42+
<dependency>
43+
<groupId>com.google.cloud</groupId>
44+
<artifactId>libraries-bom</artifactId>
45+
<version>4.4.1</version>
46+
<type>pom</type>
47+
<scope>import</scope>
48+
</dependency>
49+
</dependencies>
50+
</dependencyManagement>
51+
4052
<dependencies>
4153
<dependency>
4254
<groupId>com.google.cloud</groupId>
4355
<artifactId>google-cloud-logging</artifactId>
44-
<version>1.101.1</version>
4556
</dependency>
4657

58+
<!-- Required for Function primitives -->
4759
<dependency>
48-
<groupId>junit</groupId>
49-
<artifactId>junit</artifactId>
50-
<version>4.13</version>
51-
<scope>test</scope>
60+
<groupId>com.google.cloud.functions</groupId>
61+
<artifactId>functions-framework-api</artifactId>
62+
<version>1.0.1</version>
5263
</dependency>
5364

5465
<!-- 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>
61-
<dependency>
62-
<groupId>com.google.truth</groupId>
63-
<artifactId>truth</artifactId>
64-
<version>1.0.1</version>
65-
<scope>test</scope>
66-
</dependency>
67-
<dependency>
68-
<groupId>com.google.guava</groupId>
69-
<artifactId>guava-testlib</artifactId>
70-
<version>29.0-jre</version>
71-
<scope>test</scope>
72-
</dependency>
7366
<dependency>
7467
<groupId>org.powermock</groupId>
7568
<artifactId>powermock-core</artifactId>
@@ -88,12 +81,23 @@
8881
<version>${powermock.version}</version>
8982
<scope>test</scope>
9083
</dependency>
91-
92-
<!-- Required for Function primitives -->
9384
<dependency>
94-
<groupId>com.google.cloud.functions</groupId>
95-
<artifactId>functions-framework-api</artifactId>
85+
<groupId>org.junit.jupiter</groupId>
86+
<artifactId>junit-jupiter-api</artifactId>
87+
<version>5.6.2</version>
88+
<scope>test</scope>
89+
</dependency>
90+
<dependency>
91+
<groupId>com.google.truth</groupId>
92+
<artifactId>truth</artifactId>
9693
<version>1.0.1</version>
94+
<scope>test</scope>
95+
</dependency>
96+
<dependency>
97+
<groupId>com.google.guava</groupId>
98+
<artifactId>guava-testlib</artifactId>
99+
<version>29.0-jre</version>
100+
<scope>test</scope>
97101
</dependency>
98102
</dependencies>
99103

@@ -130,12 +134,20 @@
130134
<groupId>com.google.cloud.functions</groupId>
131135
<artifactId>function-maven-plugin</artifactId>
132136
<version>0.9.1</version>
137+
<configuration>
138+
<functionTarget>functions.RetrieveLogs</functionTarget>
139+
</configuration>
133140
</plugin>
134141
<plugin>
135142
<groupId>org.apache.maven.plugins</groupId>
136143
<artifactId>maven-surefire-plugin</artifactId>
137-
<version>3.0.0-M4</version>
144+
<!-- version 3.0.0-M4 does not load JUnit5 correctly -->
145+
<!-- see https://issues.apache.org/jira/browse/SUREFIRE-1750 -->
146+
<version>3.0.0-M3</version>
138147
<configuration>
148+
<includes>
149+
<include>**/*Test.java</include>
150+
</includes>
139151
<skipTests>${skipTests}</skipTests>
140152
<reportNameSuffix>sponge_log</reportNameSuffix>
141153
<trimStackTrace>false</trimStackTrace>

functions/logging/src/main/java/com/example/functions/logging/RetrieveLogs.java renamed to functions/logging/retrieve-logs/src/main/java/functions/RetrieveLogs.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.logging;
17+
package functions;
1818

1919
// [START functions_log_retrieve]
2020

functions/logging/src/test/java/com/example/functions/logging/LogsTest.java renamed to functions/logging/retrieve-logs/src/test/java/functions/RetrieveLogsTest.java

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

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

1919
import static com.google.common.truth.Truth.assertThat;
2020
import static org.powermock.api.mockito.PowerMockito.mock;
21-
import static org.powermock.api.mockito.PowerMockito.when;
2221

23-
import com.example.functions.logging.eventpojos.PubSubMessage;
2422
import com.google.cloud.functions.HttpRequest;
2523
import com.google.cloud.functions.HttpResponse;
2624
import com.google.common.testing.TestLogHandler;
27-
import com.google.gson.Gson;
28-
import java.io.BufferedReader;
2925
import java.io.BufferedWriter;
3026
import java.io.IOException;
31-
import java.io.StringReader;
3227
import java.io.StringWriter;
3328
import java.util.logging.Logger;
34-
import org.junit.After;
3529
import org.junit.Before;
3630
import org.junit.BeforeClass;
37-
import org.junit.Rule;
3831
import org.junit.Test;
39-
import org.junit.contrib.java.lang.system.EnvironmentVariables;
4032
import org.junit.runner.RunWith;
4133
import org.junit.runners.JUnit4;
4234
import org.mockito.Mock;
4335
import org.mockito.Mockito;
4436
import org.powermock.api.mockito.PowerMockito;
4537

4638
@RunWith(JUnit4.class)
47-
public class LogsTest {
39+
public class RetrieveLogsTest {
4840
@Mock private HttpRequest request;
4941
@Mock private HttpResponse response;
5042

@@ -53,17 +45,10 @@ public class LogsTest {
5345

5446
// Loggers + handlers for various tested classes
5547
// (Must be declared at class-level, or LoggingHandler won't detect log records!)
56-
private static final Logger LOGGER = Logger.getLogger(
57-
StackdriverLogging.class.getName());
48+
private static final Logger LOGGER = Logger.getLogger(RetrieveLogs.class.getName());
5849

5950
private static final TestLogHandler LOG_HANDLER = new TestLogHandler();
6051

61-
// Use GSON (https://github.com/google/gson) to parse JSON content.
62-
private static final Gson gson = new Gson();
63-
64-
@Rule
65-
public EnvironmentVariables environmentVariables = new EnvironmentVariables();
66-
6752
@BeforeClass
6853
public static void beforeClass() {
6954
LOGGER.addHandler(LOG_HANDLER);
@@ -76,20 +61,9 @@ public void beforeTest() throws IOException {
7661
request = mock(HttpRequest.class);
7762
response = mock(HttpResponse.class);
7863

79-
BufferedReader reader = new BufferedReader(new StringReader("{}"));
80-
when(request.getReader()).thenReturn(reader);
81-
8264
responseOut = new StringWriter();
8365
writerOut = new BufferedWriter(responseOut);
8466
PowerMockito.when(response.getWriter()).thenReturn(writerOut);
85-
86-
LOG_HANDLER.clear();
87-
}
88-
89-
@After
90-
public void afterTest() {
91-
System.out.flush();
92-
LOG_HANDLER.clear();
9367
}
9468

9569
@Test
@@ -99,14 +73,4 @@ public void retrieveLogsTest() throws IOException {
9973
writerOut.flush();
10074
assertThat(responseOut.toString()).contains("Logs retrieved successfully.");
10175
}
102-
103-
@Test
104-
public void stackdriverLogging() throws IOException {
105-
PubSubMessage pubsubMessage = gson.fromJson(
106-
"{\"data\":\"ZGF0YQ==\",\"messageId\":\"id\"}", PubSubMessage.class);
107-
new StackdriverLogging().accept(pubsubMessage, null);
108-
109-
String logMessage = LOG_HANDLER.getStoredLogRecords().get(0).getMessage();
110-
assertThat("Hello, data").isEqualTo(logMessage);
111-
}
11276
}

0 commit comments

Comments
 (0)