-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #265 from aol/micro-slack
Micro slack
- Loading branch information
Showing
12 changed files
with
285 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
micro-slack/src/main/java/com/aol/micro/server/slack/SlackConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
micro-slack/src/main/java/com/aol/micro/server/slack/SlackMessageSender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
micro-slack/src/main/java/com/aol/micro/server/slack/plugin/SlackPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
micro-slack/src/main/java/com/aol/micro/server/slack/rest/SlackRest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
micro-slack/src/main/resources/META-INF/services/com.aol.micro.server.Plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
com.aol.micro.server.slack.plugin.SlackPlugin |
30 changes: 30 additions & 0 deletions
30
micro-slack/src/test/java/com/aol/micro/server/slack/SlackMessageSenderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
micro-slack/src/test/java/com/aol/micro/server/slack/SlackRestTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
micro-slack/src/test/java/com/aol/micro/server/slack/SlackRunnerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
slack.webhookUri = "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters