Skip to content

Commit 85fb6b2

Browse files
committed
Add an App Identity example.
This moves the first example from: https://cloud.google.com/appengine/docs/java/appidentity/ to GitHub. I also add a simple unit test to at least make sure this code builds & runs. Since this sample just wraps: ApiProxy.getCurrentEnvironment() .getAttributes() .get("com.google.appengine.runtime.default_version_hostname")) there isn't really much more we can test in this example. I will move the other examples from that page over in later PRs.
1 parent e0fe437 commit 85fb6b2

File tree

8 files changed

+259
-0
lines changed

8 files changed

+259
-0
lines changed

appengine/appidentity/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Eclipse files
2+
.project
3+
.classpath
4+
.settings
5+
6+
# Target folders
7+
target/

appengine/appidentity/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# App Identity sample for Google App Engine
2+
This sample demonstrates how to use the App Identity APIs on Google App Engine
3+
4+
## Setup
5+
1. Update the <application> tag in src/main/webapp/WEB-INF/appengine-web.xml with your project name
6+
1. Update the <version> tag in src/main/webapp/WEB-INF/appengine-web.xml with your version name
7+
8+
## Running locally
9+
$ mvn appengine:devserver
10+
11+
## Deploying
12+
$ mvn appengine:update

appengine/appidentity/pom.xml

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2015 Google Inc. All Rights Reserved.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
19+
<modelVersion>4.0.0</modelVersion>
20+
<packaging>war</packaging>
21+
<version>1.0-SNAPSHOT</version>
22+
<groupId>com.example.appengine</groupId>
23+
<artifactId>appidentity</artifactId>
24+
25+
<properties>
26+
<appengine.target.version>1.9.30</appengine.target.version>
27+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
28+
</properties>
29+
30+
<dependencies>
31+
<dependency>
32+
<groupId>com.google.appengine</groupId>
33+
<artifactId>appengine-api-1.0-sdk</artifactId>
34+
<version>${appengine.target.version}</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>javax.servlet</groupId>
38+
<artifactId>servlet-api</artifactId>
39+
<version>2.5</version>
40+
<type>jar</type>
41+
<scope>provided</scope>
42+
</dependency>
43+
44+
<!-- Test Dependencies -->
45+
<dependency>
46+
<groupId>junit</groupId>
47+
<artifactId>junit</artifactId>
48+
<version>4.10</version>
49+
<scope>test</scope>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.mockito</groupId>
53+
<artifactId>mockito-all</artifactId>
54+
<version>1.10.19</version>
55+
<scope>test</scope>
56+
</dependency>
57+
<dependency>
58+
<groupId>com.google.appengine</groupId>
59+
<artifactId>appengine-testing</artifactId>
60+
<version>${appengine.target.version}</version>
61+
<scope>test</scope>
62+
</dependency>
63+
<dependency>
64+
<groupId>com.google.appengine</groupId>
65+
<artifactId>appengine-api-stubs</artifactId>
66+
<version>${appengine.target.version}</version>
67+
<scope>test</scope>
68+
</dependency>
69+
<dependency>
70+
<groupId>com.google.appengine</groupId>
71+
<artifactId>appengine-tools-sdk</artifactId>
72+
<version>${appengine.target.version}</version>
73+
<scope>test</scope>
74+
</dependency>
75+
<dependency>
76+
<groupId>com.google.truth</groupId>
77+
<artifactId>truth</artifactId>
78+
<version>0.27</version>
79+
<scope>test</scope>
80+
</dependency>
81+
</dependencies>
82+
<build>
83+
<!-- for hot reload of the web application -->
84+
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
85+
<plugins>
86+
<plugin>
87+
<groupId>org.apache.maven.plugins</groupId>
88+
<version>3.3</version>
89+
<artifactId>maven-compiler-plugin</artifactId>
90+
<configuration>
91+
<source>1.7</source>
92+
<target>1.7</target>
93+
</configuration>
94+
</plugin>
95+
<plugin>
96+
<groupId>com.google.appengine</groupId>
97+
<artifactId>appengine-maven-plugin</artifactId>
98+
<version>1.9.28</version>
99+
</plugin>
100+
</plugins>
101+
</build>
102+
</project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Copyright 2015 Google Inc. All Rights Reserved.
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.appengine.appidentity;
17+
18+
import com.google.apphosting.api.ApiProxy;
19+
import com.google.apphosting.api.ApiProxy.Environment;
20+
21+
import java.io.IOException;
22+
import java.io.PrintWriter;
23+
24+
import javax.servlet.http.HttpServlet;
25+
import javax.servlet.http.HttpServletRequest;
26+
import javax.servlet.http.HttpServletResponse;
27+
28+
@SuppressWarnings("serial")
29+
public class IdentityServlet extends HttpServlet {
30+
31+
// [START versioned_hostnames]
32+
@Override
33+
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
34+
resp.setContentType("text/plain");
35+
ApiProxy.Environment env = ApiProxy.getCurrentEnvironment();
36+
resp.getWriter().print("default_version_hostname: ");
37+
resp.getWriter()
38+
.println(env.getAttributes().get("com.google.appengine.runtime.default_version_hostname"));
39+
}
40+
// [END versioned_hostnames]
41+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
3+
<application>YOUR-PROJECT-ID</application>
4+
<version>YOUR-VERSION-ID</version>
5+
<threadsafe>true</threadsafe>
6+
</appengine-web-app>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
3+
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
4+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
5+
version="2.5">
6+
<servlet>
7+
<servlet-name>appidentity</servlet-name>
8+
<servlet-class>com.example.appengine.appidentity.IdentityServlet</servlet-class>
9+
</servlet>
10+
<servlet-mapping>
11+
<servlet-name>appidentity</servlet-name>
12+
<url-pattern>/</url-pattern>
13+
</servlet-mapping>
14+
</web-app>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* Copyright 2015 Google Inc. All Rights Reserved.
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.appengine.appidentity;
17+
18+
import static com.google.common.truth.Truth.assertThat;
19+
import static org.junit.Assert.fail;
20+
import static org.mockito.Mockito.mock;
21+
import static org.mockito.Mockito.when;
22+
23+
import com.google.appengine.tools.development.ApiProxyLocal;
24+
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
25+
import com.google.apphosting.api.ApiProxy;
26+
import org.mockito.Mock;
27+
import org.mockito.Mockito;
28+
import org.mockito.MockitoAnnotations;
29+
import org.junit.Before;
30+
import org.junit.Test;
31+
import org.junit.runner.RunWith;
32+
import org.junit.runners.JUnit4;
33+
34+
import java.io.File;
35+
import java.io.PrintWriter;
36+
import java.io.StringWriter;
37+
38+
import javax.servlet.http.HttpServletRequest;
39+
import javax.servlet.http.HttpServletResponse;
40+
41+
/** Unit tests for {@link IdentityServlet}. */
42+
@RunWith(JUnit4.class)
43+
public class IdentityServletTest {
44+
45+
// Set up a helper so that the ApiProxy returns a valid environment for local testing.
46+
private final LocalServiceTestHelper helper = new LocalServiceTestHelper();
47+
48+
@Mock private HttpServletRequest mockRequest;
49+
@Mock private HttpServletResponse mockResponse;
50+
private StringWriter responseWriter;
51+
private IdentityServlet servletUnderTest;
52+
53+
@Before
54+
public void setUp() throws Exception {
55+
MockitoAnnotations.initMocks(this);
56+
helper.setUp();
57+
58+
// Set up a fake HTTP response.
59+
responseWriter = new StringWriter();
60+
when(mockResponse.getWriter()).thenReturn(new PrintWriter(responseWriter));
61+
62+
servletUnderTest = new IdentityServlet();
63+
}
64+
65+
@Test
66+
public void doGet_defaultEnvironment_writesResponse() throws Exception {
67+
servletUnderTest.doGet(mockRequest, mockResponse);
68+
69+
// We don't have any guarantee over what the local App Engine environment returns for
70+
// "com.google.appengine.runtime.default_version_hostname". Only assert that the response
71+
// contains part of the string we have control over.
72+
assertThat(responseWriter.toString())
73+
.named("IdentityServlet response")
74+
.contains("default_version_hostname:");
75+
}
76+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
</prerequisites>
2424

2525
<modules>
26+
<module>appengine/appidentity</module>
2627
<module>taskqueue/deferred</module>
2728
<module>unittests</module>
2829
<module>bigquery</module>

0 commit comments

Comments
 (0)