Skip to content

Commit

Permalink
Move Push Taskqueue sample program to Github
Browse files Browse the repository at this point in the history
Some minor corrections to Requests testing and configuration.
  • Loading branch information
Annie29 committed Apr 27, 2016
1 parent efc2c93 commit c1058b3
Show file tree
Hide file tree
Showing 11 changed files with 300 additions and 9 deletions.
1 change: 0 additions & 1 deletion appengine/requests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ Copyright 2016 Google Inc. All Rights Reserved.
<!-- for hot reload of the web application -->
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
<plugins>

<plugin>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-maven-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,6 @@ public void testListLogs() throws Exception {
assertThat(out).contains("An informational message.");
assertThat(out).contains("A warning message.");
assertThat(out).contains("An error message.");

// We expect three log messages to be created
// with the following severities.
// Since there's no guarantee of case, use lowercase
String lcOut = out.toLowerCase();
assertThat(lcOut).contains("info");
assertThat(lcOut).contains("warning");
assertThat(lcOut).contains("severe");
}

}
39 changes: 39 additions & 0 deletions appengine/taskqueue-push/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# A Java Task Queue example for Google App Engine

This sample demonstrates how to use the [TaskQueue API][taskqueue-api] on [Google App
Engine][ae-docs].

[taskqueue-api]: https://cloud.google.com/appengine/docs/java/javadoc/com/google/appengine/api/taskqueue/package-summary
[ae-docs]: https://cloud.google.com/appengine/docs/java/

## Running locally
This example uses the
[Maven gcloud plugin](https://cloud.google.com/appengine/docs/java/managed-vms/maven).
To run this sample locally:

$ mvn appengine:devserver

Go to the site `localhost:8080` to add elements to the queue. They will appear in the log as the result of the Enqueue servlet transmitting the data to the Worker servlet.

## Deploying
In the following command, replace YOUR-PROJECT-ID with your
[Google Cloud Project ID](https://support.google.com/cloud/answer/6158840).

$ mvn appengine:update -Dappengine.appId=YOUR-PROJECT-ID -Dappengine.version=SOME-VERSION

## Setup
To save your project settings so that you don't need to enter the
parameters, you can:

1. Update the `<application>` tag in src/main/webapp/WEB-INF/appengine-web.xml
with your project name.

2. Update the `<version>` tag in src/main/webapp/WEB-INF/appengine-web.xml
with a valid version number.


You will now be able to run

$ mvn appengine:update

without the need for any additional parameters.
98 changes: 98 additions & 0 deletions appengine/taskqueue-push/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2015 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project>
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<groupId>com.example.appengine</groupId>
<artifactId>taskqueue-push</artifactId>
<!-- Parent POM defines ${appengine.sdk.version} (updates frequently). -->
<parent>
<groupId>com.google.cloud</groupId>
<artifactId>doc-samples</artifactId>
<version>1.0.0</version>
<relativePath>../..</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>${appengine.sdk.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20151123</version>
</dependency>
<!-- Test Dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-testing</artifactId>
<version>${appengine.sdk.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-api-stubs</artifactId>
<version>${appengine.sdk.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-tools-sdk</artifactId>
<version>${appengine.sdk.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>0.28</version>
<scope>test</scope>
</dependency>

</dependencies>
<build>
<!-- for hot reload of the web application -->
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
<plugins>
<plugin>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>${appengine.sdk.version}</version>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.appengine.taskqueue.push;

import com.google.appengine.api.taskqueue.Queue;
import com.google.appengine.api.taskqueue.QueueFactory;
import com.google.appengine.api.taskqueue.TaskOptions;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

// [START enqueue]
// The Enqueue servlet should be mapped to the "/enqueue" URL.
public class Enqueue extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String key = request.getParameter("key");

// Add the task to the default queue.
Queue queue = QueueFactory.getDefaultQueue();
queue.add(TaskOptions.Builder.withUrl("/worker").param("key", key));

response.sendRedirect("/");
}
}
// [END enqueue]
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.example.appengine.taskqueue.push;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

// [START worker]
// The Worker servlet should be mapped to the "/worker" URL.
public class Worker extends HttpServlet {
private static final Logger log = Logger.getLogger(Worker.class.getName());

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String key = request.getParameter("key");

// Do something with key.
// [START_EXCLUDE]
log.info("Worker is processing " + key);
// [END_EXCLUDE]

}
}
// [END worker]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application>YOUR-PROJECT-ID</application>
<version>YOUR-VERSION-ID</version>
<threadsafe>true</threadsafe>
</appengine-web-app>
24 changes: 24 additions & 0 deletions appengine/taskqueue-push/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>enqueue</servlet-name>
<servlet-class>com.example.appengine.taskqueue.push.Enqueue</servlet-class>
</servlet>
<servlet>
<servlet-name>worker</servlet-name>
<servlet-class>com.example.appengine.taskqueue.push.Worker</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>enqueue</servlet-name>
<url-pattern>/enqueue</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>worker</servlet-name>
<url-pattern>/worker</url-pattern>
</servlet-mapping>
</web-app>
12 changes: 12 additions & 0 deletions appengine/taskqueue-push/src/main/webapp/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!--[START index_file] -->
<!-- A basic index.html file served from the "/" URL. -->
<html>
<body>
<p>Enqueue a value, to be processed by a worker.</p>
<form action="/enqueue" method="post">
<input type="text" name="key">
<input type="submit">
</form>
</body>
</html>
<!--[END index_file] -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.example.appengine.taskqueue.push;

import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Unit tests for {@link Worker}.
*/
@RunWith(JUnit4.class)
public class WorkerTest {
private static final String FAKE_KEY_VALUE = "KEY";

// To capture and restore stderr
private final ByteArrayOutputStream stderr = new ByteArrayOutputStream();
private static final PrintStream REAL_ERR = System.err;

@Mock
private HttpServletRequest mockRequest;
@Mock
private HttpServletResponse mockResponse;
private Worker servletUnderTest;

@Before
public void setUp() throws Exception {
// Capture stderr to examine messages written to it
System.setErr(new PrintStream(stderr));

MockitoAnnotations.initMocks(this);

when(mockRequest.getParameter("key")).thenReturn(FAKE_KEY_VALUE);

servletUnderTest = new Worker();
}

@After
public void tearDown() {
// Restore stderr
System.setErr(WorkerTest.REAL_ERR);
}

@Test
public void doPost_writesResponse() throws Exception {
servletUnderTest.doPost(mockRequest, mockResponse);

String out = stderr.toString();
// We expect a log message to be created
// with the following message.
assertThat(out).contains("Worker is processing " + FAKE_KEY_VALUE);

}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<module>appengine/remote/remote-client</module>
<module>appengine/remote/remote-server</module>
<module>appengine/static-files</module>
<module>appengine/taskqueue-push</module>
<module>appengine/twilio</module>
<module>appengine/urlfetch</module>
<module>appengine/users</module>
Expand Down

0 comments on commit c1058b3

Please sign in to comment.