Skip to content
This repository has been archived by the owner on Jan 14, 2023. It is now read-only.

Add Java samples for slash commands. #14

Merged
merged 6 commits into from
Nov 11, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2016 Google Inc.
#
# 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.
machine:
java:
version: oraclejdk8

test:
override:
- cd java && mvn clean verify
27 changes: 27 additions & 0 deletions java/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
*.class

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.ear

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

# IntelliJ
.idea/
*.iml

# Maven
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
18 changes: 18 additions & 0 deletions java/command/1-start/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Running a slash command for Slack on Google Cloud Platform - Step 1

This is the solution directory for step one in the tutorial for running a [slash command for
Slack](https://api.slack.com/slash-commands) on [Google Cloud Platform](https://cloud.google.com/).

This tutorial uses [App Engine flexible environment][flexible] for more details on running an app
on App Engine, follow the [quickstart tutorial][flexible-quickstart].

[flexible]: https://cloud.google.com/appengine/docs/flexible/java/
[flexible-quickstart]: https://cloud.google.com/appengine/docs/flexible/java/quickstart

## Running locally

$ mvn jetty:run

## Deploying

$ mvn appengine:deploy
80 changes: 80 additions & 0 deletions java/command/1-start/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<!--
Copyright 2016 Google Inc.

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.google.cloud.samples</groupId>
<artifactId>slack-command-1-start</artifactId>

<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>

<appengine.maven.plugin>1.0.0</appengine.maven.plugin>
<jetty-maven-plugin-version>9.3.7.v20160115</jetty-maven-plugin-version>

<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<type>jar</type>
<scope>provided</scope>
</dependency>

<!-- Test Dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</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.truth</groupId>
<artifactId>truth</artifactId>
<version>0.30</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.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>${appengine.maven.plugin}</version>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty-maven-plugin-version}</version>
</plugin>
</plugins>
</build>
</project>
20 changes: 20 additions & 0 deletions java/command/1-start/src/main/appengine/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 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.
runtime: java
vm: true

handlers:
- url: /.*
script: this field is required, but ignored
secure: always
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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.slashcommand;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "greet", urlPatterns = "/send-greeting")
@SuppressWarnings("serial")
public class GreetServlet extends HttpServlet {

private String getGreeting(String greetTarget) {
if (greetTarget == null || greetTarget.isEmpty()) {
greetTarget = "world";
}
return String.format("Hello, %s.", greetTarget);
}

private void sendGreeting(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
resp.setContentType("text/plain");
String commandText = req.getParameter("text");
String greeting = getGreeting(commandText);
resp.getWriter().write(greeting);
}

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
sendGreeting(req, resp);
}

@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
sendGreeting(req, resp);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.slashcommand;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "index", urlPatterns = "")
@SuppressWarnings("serial")
public class IndexServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
req.getRequestDispatcher("/index.jsp").forward(req, resp);
}
}
26 changes: 26 additions & 0 deletions java/command/1-start/src/main/webapp/index.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>Slack Slash Command Sample on Google App Engine for Java</title>

<!--
Copyright 2016 Google Inc.

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.
-->

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<p>Follow the
<a href="https://github.com/GoogleCloudPlatform/slack-samples/tree/master/java/command">instructions
for this sample</a> to configure and modify this sample.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newLine

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2016 Google Inc.
*
* 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.slashcommand;

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.PrintWriter;
import java.io.StringWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Unit tests for {@link GreetServlet}.
*/
@RunWith(JUnit4.class)
public class GreetServletTest {
@Mock private HttpServletRequest mockRequest;
@Mock private HttpServletResponse mockResponse;
private StringWriter responseWriter;
private GreetServlet 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 GreetServlet();
}

@Test
public void doGet_noParameters_writesHelloWorld() throws Exception {
servletUnderTest.doGet(mockRequest, mockResponse);
assertThat(responseWriter.toString()).isEqualTo("Hello, world.");
}

@Test
public void doGet_withName_writesHelloName() throws Exception {
when(mockRequest.getParameter("text")).thenReturn("John Doe");
servletUnderTest.doGet(mockRequest, mockResponse);
assertThat(responseWriter.toString()).isEqualTo("Hello, John Doe.");
}

@Test
public void doPost_noParameters_writesHelloWorld() throws Exception {
when(mockRequest.getParameter("text")).thenReturn("Jane Doe");
servletUnderTest.doPost(mockRequest, mockResponse);
assertThat(responseWriter.toString()).isEqualTo("Hello, Jane Doe.");
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newLine

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Loading