diff --git a/appengine/requests/README.md b/appengine/requests/README.md new file mode 100644 index 00000000000..45ea04fdb8b --- /dev/null +++ b/appengine/requests/README.md @@ -0,0 +1,43 @@ +# Request Handling sample for Google App Engine + +This sample provides Java code samples in support of the "Handling Requests" description [Requests][requests-doc] on [Google App +Engine][ae-docs]. + +[requests-doc]: https://cloud.google.com/appengine/docs/java/requests +[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 + +To see the results of the RequestsServlet, open `localhost:8080` in a WWW browser. + +To see the results of the LoggingServlet, open `localhost:8080/logs` in a WWW browser +and examine the logs to see the actual messages. + +## Deploying +In the following command, replace YOUR-PROJECT-ID with your +[Google Cloud Project ID](https://developers.google.com/console/help/new/#projectnumber) +and SOME-VERSION with a valid version number. + + $ 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 `` tag in src/main/webapp/WEB-INF/appengine-web.xml + with your project name. + +2. Update the `` 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. diff --git a/appengine/requests/pom.xml b/appengine/requests/pom.xml new file mode 100644 index 00000000000..68dd81c2cd3 --- /dev/null +++ b/appengine/requests/pom.xml @@ -0,0 +1,104 @@ + + + + 4.0.0 + war + 1.0-SNAPSHOT + com.example.appengine + appengine-requests + + + com.google.cloud + doc-samples + 1.0.0 + ../.. + + + + com.google.appengine + appengine-maven-plugin + ${appengine.sdk.version} + + + com.google.guava + guava + 19.0 + + + javax.servlet + servlet-api + 2.5 + jar + provided + + + org.json + json + 20151123 + + + + junit + junit + 4.10 + test + + + org.mockito + mockito-all + 1.10.19 + test + + + com.google.appengine + appengine-testing + ${appengine.sdk.version} + test + + + com.google.appengine + appengine-api-stubs + ${appengine.sdk.version} + test + + + com.google.appengine + appengine-tools-sdk + ${appengine.sdk.version} + test + + + com.google.truth + truth + 0.28 + test + + + + + + ${project.build.directory}/${project.build.finalName}/WEB-INF/classes + + + + com.google.appengine + appengine-maven-plugin + ${appengine.sdk.version} + + + + diff --git a/appengine/requests/src/main/java/com/example/appengine/requests/LoggingServlet.java b/appengine/requests/src/main/java/com/example/appengine/requests/LoggingServlet.java new file mode 100644 index 00000000000..434796b7c9e --- /dev/null +++ b/appengine/requests/src/main/java/com/example/appengine/requests/LoggingServlet.java @@ -0,0 +1,41 @@ +/* Copyright 2016 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. + */ +package com.example.appengine.requests; + +import java.io.IOException; +import java.util.logging.Logger; + +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +// [START simple_logging_example] +public class LoggingServlet extends HttpServlet { + private static final Logger log = Logger.getLogger(LoggingServlet.class.getName()); + + @Override + public void doGet(HttpServletRequest req, HttpServletResponse resp) + throws IOException { + log.info("An informational message."); + log.warning("A warning message."); + log.severe("An error message."); + // [START_EXCLUDE] + resp.setContentType("text/plain"); + resp.getWriter().println("Check logs for results"); + // [END_EXCLUDE] + } +} +// [END simple_logging_example] + diff --git a/appengine/requests/src/main/java/com/example/appengine/requests/RequestsServlet.java b/appengine/requests/src/main/java/com/example/appengine/requests/RequestsServlet.java new file mode 100644 index 00000000000..7c7f562bf6e --- /dev/null +++ b/appengine/requests/src/main/java/com/example/appengine/requests/RequestsServlet.java @@ -0,0 +1,33 @@ +/* Copyright 2016 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. + */ +package com.example.appengine.requests; + +import java.io.IOException; + +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +// [START simple_request_example] +public class RequestsServlet extends HttpServlet { + @Override + public void doGet(HttpServletRequest req, HttpServletResponse resp) + throws IOException { + resp.setContentType("text/plain"); + resp.getWriter().println("Hello, world"); + } +} +// [END simple_request_example] + diff --git a/appengine/requests/src/main/webapp/WEB-INF/appengine-web.xml b/appengine/requests/src/main/webapp/WEB-INF/appengine-web.xml new file mode 100644 index 00000000000..c9e245399bf --- /dev/null +++ b/appengine/requests/src/main/webapp/WEB-INF/appengine-web.xml @@ -0,0 +1,6 @@ + + + YOUR-PROJECT-ID + YOUR-VERSION-ID + true + diff --git a/appengine/requests/src/main/webapp/WEB-INF/web.xml b/appengine/requests/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 00000000000..7784c6a56c6 --- /dev/null +++ b/appengine/requests/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,26 @@ + + + + requests + com.example.appengine.requests.RequestsServlet + + + logging + com.example.appengine.requests.LoggingServlet + + + requests + / + + + requests + /requests + + + logging + /logs + + diff --git a/appengine/requests/src/test/java/com/example/appengine/requests/LoggingServletTest.java b/appengine/requests/src/test/java/com/example/appengine/requests/LoggingServletTest.java new file mode 100644 index 00000000000..e84c3d1db19 --- /dev/null +++ b/appengine/requests/src/test/java/com/example/appengine/requests/LoggingServletTest.java @@ -0,0 +1,93 @@ +/* + * Copyright 2016 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. + */ + +package com.example.appengine.requests; + +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 java.io.PrintWriter; +import java.io.StringWriter; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * Unit tests for {@link LoggingServlet}. + */ +@RunWith(JUnit4.class) +public class LoggingServletTest { + // 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 StringWriter responseWriter; + private LoggingServlet servletUnderTest; + + @Before + public void setUp() throws Exception { + // Capture stderr to examine messages written to it + System.setErr(new PrintStream(stderr)); + + MockitoAnnotations.initMocks(this); + + // Set up a fake HTTP response. + responseWriter = new StringWriter(); + when(mockResponse.getWriter()).thenReturn(new PrintWriter(responseWriter)); + + servletUnderTest = new LoggingServlet(); + } + + @After + public void tearDown() { + // Restore stderr + System.setErr(LoggingServletTest.REAL_ERR); + } + + @Test + public void testListLogs() throws Exception { + servletUnderTest.doGet(mockRequest, mockResponse); + + String out = stderr.toString(); + + // We expect three log messages to be created + // with the following messages. + 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"); + } + +} diff --git a/appengine/requests/src/test/java/com/example/appengine/requests/RequestsServletTest.java b/appengine/requests/src/test/java/com/example/appengine/requests/RequestsServletTest.java new file mode 100644 index 00000000000..4e27eebbb3b --- /dev/null +++ b/appengine/requests/src/test/java/com/example/appengine/requests/RequestsServletTest.java @@ -0,0 +1,65 @@ +/* + * Copyright 2016 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. + */ + +package com.example.appengine.requests; + +import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Mockito.when; + +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.PrintWriter; +import java.io.StringWriter; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * Unit tests for {@link RequestsServlet}. + */ +@RunWith(JUnit4.class) +public class RequestsServletTest { + @Mock private HttpServletRequest mockRequest; + @Mock private HttpServletResponse mockResponse; + private StringWriter responseWriter; + private RequestsServlet servletUnderTest; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + + // Set up a fake HTTP response. + responseWriter = new StringWriter(); + when(mockResponse.getWriter()).thenReturn(new PrintWriter(responseWriter)); + + servletUnderTest = new RequestsServlet(); + } + + @Test + public void doGet_writesResponse() throws Exception { + servletUnderTest.doGet(mockRequest, mockResponse); + + // We expect a greeting to be returned. + assertThat(responseWriter.toString()) + .named("RequestsServlet response") + .contains("Hello, world"); + } +} diff --git a/pom.xml b/pom.xml index cf0cb626aab..cb181a14a9e 100644 --- a/pom.xml +++ b/pom.xml @@ -53,8 +53,11 @@ appengine/mailjet appengine/memcache appengine/oauth2 + appengine/requests appengine/search appengine/sendgrid + appengine/remote/remote-client + appengine/remote/remote-server appengine/static-files appengine/twilio appengine/urlfetch