Skip to content

Commit b4a9899

Browse files
author
Ace Nassri
authored
GCF file split: http (GoogleCloudPlatform#2666)
* GCF file split: http * Address comments
1 parent babe690 commit b4a9899

File tree

17 files changed

+1067
-74
lines changed

17 files changed

+1067
-74
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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-http-bearer-token-http</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+
<!-- Disable tests during GCF builds (from parent POM) -->
41+
<!-- You can remove this profile to run tests -->
42+
<!-- when deploying, but we recommend creating -->
43+
<!-- a CI/CD pipeline via Cloud Build instead -->
44+
<profiles>
45+
<profile>
46+
<id>skip_tests_on_gcf</id>
47+
<activation>
48+
<property>
49+
<name>env.NEW_BUILD</name>
50+
</property>
51+
</activation>
52+
<properties>
53+
<skipTests>true</skipTests>
54+
</properties>
55+
</profile>
56+
</profiles>
57+
58+
<dependencies>
59+
<dependency>
60+
<groupId>com.google.cloud.functions</groupId>
61+
<artifactId>functions-framework-api</artifactId>
62+
<version>1.0.1</version>
63+
</dependency>
64+
</dependencies>
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.BearerTokenHttp</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</version>
89+
<configuration>
90+
<skipTests>${skipTests}</skipTests>
91+
<reportNameSuffix>sponge_log</reportNameSuffix>
92+
<trimStackTrace>false</trimStackTrace>
93+
</configuration>
94+
</plugin>
95+
<plugin> <!-- Required for Java 8 (Alpha) functions in the inline editor -->
96+
<groupId>org.apache.maven.plugins</groupId>
97+
<artifactId>maven-compiler-plugin</artifactId>
98+
<executions>
99+
<execution>
100+
<id>compile</id>
101+
<phase>compile</phase>
102+
<goals>
103+
<goal>compile</goal>
104+
</goals>
105+
</execution>
106+
<execution>
107+
<id>testCompile</id>
108+
<phase>test-compile</phase>
109+
<goals>
110+
<goal>testCompile</goal>
111+
</goals>
112+
</execution>
113+
</executions>
114+
<configuration>
115+
<excludes>
116+
<exclude>.google/</exclude>
117+
</excludes>
118+
</configuration>
119+
</plugin>
120+
</plugins>
121+
</build>
122+
</project>

functions/http/src/main/java/com/example/functions/http/BearerTokenHttp.java renamed to functions/http/bearer-token-http/src/main/java/functions/BearerTokenHttp.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.http;
17+
package functions;
1818

1919
// [START functions_bearer_token]
2020

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-http-cors-enabled-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+
<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+
<!-- Disable tests during GCF builds (from parent POM) -->
41+
<!-- You can remove this profile to run tests -->
42+
<!-- when deploying, but we recommend creating -->
43+
<!-- a CI/CD pipeline via Cloud Build instead -->
44+
<profiles>
45+
<profile>
46+
<id>skip_tests_on_gcf</id>
47+
<activation>
48+
<property>
49+
<name>env.NEW_BUILD</name>
50+
</property>
51+
</activation>
52+
<properties>
53+
<skipTests>true</skipTests>
54+
</properties>
55+
</profile>
56+
</profiles>
57+
58+
<dependencies>
59+
<dependency>
60+
<groupId>com.google.code.gson</groupId>
61+
<artifactId>gson</artifactId>
62+
<version>2.8.6</version>
63+
</dependency>
64+
<dependency>
65+
<groupId>com.google.cloud.functions</groupId>
66+
<artifactId>functions-framework-api</artifactId>
67+
<version>1.0.1</version>
68+
</dependency>
69+
70+
<!-- The following dependencies are only required for testing -->
71+
<dependency>
72+
<groupId>com.google.truth</groupId>
73+
<artifactId>truth</artifactId>
74+
<version>1.0.1</version>
75+
<scope>test</scope>
76+
</dependency>
77+
<dependency>
78+
<groupId>org.powermock</groupId>
79+
<artifactId>powermock-core</artifactId>
80+
<version>${powermock.version}</version>
81+
<scope>test</scope>
82+
</dependency>
83+
<dependency>
84+
<groupId>org.powermock</groupId>
85+
<artifactId>powermock-module-junit4</artifactId>
86+
<version>${powermock.version}</version>
87+
<scope>test</scope>
88+
</dependency>
89+
<dependency>
90+
<groupId>org.powermock</groupId>
91+
<artifactId>powermock-api-mockito2</artifactId>
92+
<version>${powermock.version}</version>
93+
<scope>test</scope>
94+
</dependency>
95+
96+
<dependency>
97+
<groupId>junit</groupId>
98+
<artifactId>junit</artifactId>
99+
<version>4.13</version>
100+
<scope>test</scope>
101+
</dependency>
102+
</dependencies>
103+
104+
<build>
105+
<plugins>
106+
<plugin>
107+
<!--
108+
Google Cloud Functions Framework Maven plugin
109+
110+
This plugin allows you to run Cloud Functions Java code
111+
locally. Use the following terminal command to run a
112+
given function locally:
113+
114+
mvn function:run -Drun.functionTarget=your.package.yourFunction
115+
-->
116+
<groupId>com.google.cloud.functions</groupId>
117+
<artifactId>function-maven-plugin</artifactId>
118+
<version>0.9.1</version>
119+
<configuration>
120+
<functionTarget>functions.CorsEnabledAuth</functionTarget>
121+
</configuration>
122+
</plugin>
123+
<plugin>
124+
<groupId>org.apache.maven.plugins</groupId>
125+
<artifactId>maven-surefire-plugin</artifactId>
126+
<version>3.0.0-M4</version>
127+
<configuration>
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>

functions/http/src/main/java/com/example/functions/http/CorsEnabledAuth.java renamed to functions/http/cors-enabled-auth/src/main/java/functions/CorsEnabledAuth.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.http;
17+
package functions;
1818

1919
// [START functions_http_cors_auth]
2020

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
import static org.powermock.api.mockito.PowerMockito.mock;
21+
import static org.powermock.api.mockito.PowerMockito.when;
22+
23+
import com.google.cloud.functions.HttpRequest;
24+
import com.google.cloud.functions.HttpResponse;
25+
import java.io.BufferedWriter;
26+
import java.io.IOException;
27+
import java.io.StringWriter;
28+
import org.junit.Before;
29+
import org.junit.Test;
30+
import org.junit.runner.RunWith;
31+
import org.junit.runners.JUnit4;
32+
import org.mockito.Mock;
33+
import org.mockito.Mockito;
34+
import org.powermock.api.mockito.PowerMockito;
35+
36+
@RunWith(JUnit4.class)
37+
public class CorsEnabledAuthTest {
38+
@Mock private HttpRequest request;
39+
@Mock private HttpResponse response;
40+
41+
private BufferedWriter writerOut;
42+
private StringWriter responseOut;
43+
44+
@Before
45+
public void beforeTest() throws IOException {
46+
Mockito.mockitoSession().initMocks(this);
47+
48+
request = mock(HttpRequest.class);
49+
response = mock(HttpResponse.class);
50+
51+
responseOut = new StringWriter();
52+
writerOut = new BufferedWriter(responseOut);
53+
PowerMockito.when(response.getWriter()).thenReturn(writerOut);
54+
}
55+
56+
@Test
57+
public void corsEnabledAuthTest() throws IOException {
58+
when(request.getMethod()).thenReturn("GET");
59+
60+
new CorsEnabledAuth().service(request, response);
61+
62+
writerOut.flush();
63+
assertThat(responseOut.toString()).contains("CORS headers set successfully!");
64+
}
65+
}

0 commit comments

Comments
 (0)