Skip to content

Commit

Permalink
Merge pull request #265 from aol/micro-slack
Browse files Browse the repository at this point in the history
Micro slack
  • Loading branch information
davidartplus authored May 24, 2017
2 parents 45c1860 + 1df7a61 commit 5ab8171
Show file tree
Hide file tree
Showing 12 changed files with 285 additions and 0 deletions.
11 changes: 11 additions & 0 deletions micro-slack/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
description = 'micro-slack'

dependencies {
compile 'com.aol.microservices:micro-grizzly-with-jersey:0.87'
compile 'com.aol.microservices:micro-general-exception-mapper:0.87'

testCompile group: 'junit', name: 'junit', version:'4.10'
testCompile 'org.springframework.boot:spring-boot-starter-test:1.1.5.RELEASE'
testCompile 'com.jayway.restassured:rest-assured:2.8.0'

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.aol.micro.server.slack;

import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
@Getter
public class SlackConfiguration {

private final String webhookUri;

@Autowired
public SlackConfiguration(@Value("${micro.slack.webhook.uri?:}") String webhookUri) {
this.webhookUri = webhookUri;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.aol.micro.server.slack;

import javax.net.ssl.HttpsURLConnection;

import lombok.Getter;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Getter
@Component
public class SlackMessageSender {

private SlackConfiguration slackConfiguration;

@Autowired
public SlackMessageSender(SlackConfiguration slackConfiguration){

this.slackConfiguration = slackConfiguration;
}

public int postMessageToSlack(String msg){

try {
URL obj;
obj = new URL(slackConfiguration.getWebhookUri());
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

//add request header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "com.aol.micro.server");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

String urlParameters = "{\"text\": \"" + msg + "\"}";

// Send post request
con.setDoOutput(true);
DataOutputStream wr;
wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();

int responseCode = con.getResponseCode();

BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();

return responseCode;
} catch (IOException e) {
e.printStackTrace();
return -1;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.aol.micro.server.slack.plugin;

import com.aol.cyclops.data.collections.extensions.persistent.PSetX;
import com.aol.micro.server.Plugin;
import com.aol.micro.server.slack.*;

public class SlackPlugin implements Plugin {

@Override
public PSetX<Class> springClasses() {
return PSetX.of(SlackConfiguration.class, SlackMessageSender.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.aol.micro.server.slack.rest;

import java.io.IOException;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;

import org.springframework.beans.factory.annotation.Autowired;

import com.aol.micro.server.auto.discovery.Rest;
import com.aol.micro.server.slack.SlackConfiguration;
import com.aol.micro.server.slack.SlackMessageSender;

@Rest
@Path("/slack")
public class SlackRest {

SlackMessageSender slackMessageSender;

@Autowired
public SlackRest(SlackConfiguration slackConfiguration){
slackMessageSender = new SlackMessageSender(slackConfiguration);
}

@POST
@Path("/message")
@Consumes("application/json")
public String sendMessage(final String msg) {
slackMessageSender.postMessageToSlack(msg);
return "OK";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.aol.micro.server.slack.plugin.SlackPlugin
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.aol.micro.server.slack;

import static org.junit.Assert.assertTrue;

import org.junit.*;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@Ignore
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/context.xml")
public class SlackMessageSenderTest {

private SlackMessageSender slackMessageSender;

@Value("${slack.webhookUri}")
String webHookUri;

@Before
public void setup(){
slackMessageSender = new SlackMessageSender(new SlackConfiguration(webHookUri));
}

@Test
public void postMessageToSlackTest(){
assertTrue(slackMessageSender.postMessageToSlack("Hello from " + this.getClass().getName()) != -1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.aol.micro.server.slack;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.aol.micro.server.slack.rest.SlackRest;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/context.xml")
public class SlackRestTest {

@Value("${slack.webhookUri}")
String webHookUri;

@Mock
private SlackConfiguration slackConfig;

@Mock
private SlackMessageSender slackMessageSender;

@InjectMocks
SlackRest slackRest;

@Before
public void setup(){
MockitoAnnotations.initMocks(this);
Mockito.when(slackConfig.getWebhookUri()).thenReturn(webHookUri);
Mockito.when(slackMessageSender.postMessageToSlack(Matchers.anyString())).thenReturn(200);
}

@Test
public void pingTest() {
Assert.assertEquals(slackRest.sendMessage("Hello from " + this.getClass().getName()), "OK");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.aol.micro.server.slack;

import static com.jayway.restassured.RestAssured.given;
import static org.junit.Assert.*;

import org.junit.*;

import com.aol.micro.server.MicroserverApp;
import com.aol.micro.server.config.Microserver;
import com.jayway.restassured.builder.RequestSpecBuilder;
import com.jayway.restassured.specification.RequestSpecification;


@Microserver(propertiesName = "application.properties")
public class SlackRunnerTest {

private MicroserverApp server;

@Before
public void startServer() {
server = new MicroserverApp(() -> "simple-app");
server.start();
}

@After
public void stopServer() {
server.stop();
}

@Test
public void simpleMessageTest(){
RequestSpecification requestSpec = new RequestSpecBuilder()
.setBaseUri("http://localhost")
.setBasePath("/simple-app/slack/message")
.setPort(8080)
.setBody("Message from "+this.getClass().getName())
.setContentType("application/json")
.setAccept("text/plain")
.build();

assertEquals("OK",given().spec(requestSpec).when().post().andReturn().body().asString());
}
}
1 change: 1 addition & 0 deletions micro-slack/src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
slack.webhookUri = ""
20 changes: 20 additions & 0 deletions micro-slack/src/test/resources/context.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
default-autowire="byName">

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:application.properties</value>
</list>
</property>
</bean>

<!-- Auto scan the components -->
<context:component-scan base-package="com.aol.advertising.dev.tests" annotation-config="true"/>
</beans>
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ include ':micro-dbcp'
include ':micro-log4j'
include ':micro-logback'
include ':micro-s3'
include ':micro-slack'
include ':micro-tutorial'
include ':micro-dist-lock'
include ':micro-async-data-loader'
Expand Down

0 comments on commit 5ab8171

Please sign in to comment.