url = ArgumentCaptor.forClass(URL.class);
30 | verify(httpConnectionUtil).getConnection(
31 | url.capture()
32 | );
33 | assertThat(url.getValue().toString(), is("https://example.org/go/api/pipelines/pipeline-test/history"));
34 | }
35 |
36 | @Test
37 | public void testGetPipelineHistoryEvenWhenGoServerHostHasTrailingSlash() throws Exception {
38 | HttpConnectionUtil httpConnectionUtil = mockConnection();
39 |
40 | Rules rules = new Rules();
41 | rules.setGoServerHost("https://example.org/");
42 | Server server = new Server(rules, httpConnectionUtil);
43 |
44 | server.getPipelineHistory("pipeline-test");
45 |
46 | ArgumentCaptor
url = ArgumentCaptor.forClass(URL.class);
47 | verify(httpConnectionUtil).getConnection(
48 | url.capture()
49 | );
50 | assertThat(url.getValue().toString(), is("https://example.org/go/api/pipelines/pipeline-test/history"));
51 | }
52 |
53 | @Test
54 | public void testGetPipelineInstance() throws Exception {
55 | HttpConnectionUtil httpConnectionUtil = mockConnection();
56 |
57 | Rules rules = new Rules();
58 | rules.setGoServerHost("https://example.org");
59 | Server server = new Server(rules, httpConnectionUtil);
60 |
61 | server.getPipelineInstance("pipeline-test", 42);
62 |
63 | ArgumentCaptor url = ArgumentCaptor.forClass(URL.class);
64 | verify(httpConnectionUtil).getConnection(
65 | url.capture()
66 | );
67 | assertThat(url.getValue().toString(), is("https://example.org/go/api/pipelines/pipeline-test/42"));
68 | }
69 |
70 | @Test
71 | public void shouldConnectWithAPIToken() throws IOException {
72 | HttpConnectionUtil httpConnectionUtil = mockConnection();
73 | Rules rules = new Rules();
74 | Server server = new Server(rules, httpConnectionUtil);
75 | rules.setGoAPIToken("a-valid-token-from-gocd-server");
76 |
77 | HttpURLConnection conn = mock(HttpURLConnection.class);
78 | when(httpConnectionUtil.getConnection(any(URL.class))).thenReturn(conn);
79 | when(conn.getContent()).thenReturn(new Object());
80 |
81 | server.getUrl(new URL("http://exmaple.org/"));
82 |
83 | verify(conn).setRequestProperty(eq("User-Agent"), anyString());
84 | verify(conn).setRequestProperty("Authorization", "Bearer a-valid-token-from-gocd-server");
85 | }
86 |
87 | @Test
88 | public void shouldConnectWithUserPassCredentials() throws IOException {
89 | HttpConnectionUtil httpConnectionUtil = mockConnection();
90 | Rules rules = new Rules();
91 | Server server = new Server(rules, httpConnectionUtil);
92 | rules.setGoLogin("login");
93 | rules.setGoPassword("pass");
94 |
95 | HttpURLConnection conn = mock(HttpURLConnection.class);
96 | when(httpConnectionUtil.getConnection(any(URL.class))).thenReturn(conn);
97 | when(conn.getContent()).thenReturn(new Object());
98 |
99 | server.getUrl(new URL("http://exmaple.org/"));
100 |
101 | verify(conn).setRequestProperty(eq("User-Agent"), anyString());
102 | verify(conn).setRequestProperty("Authorization", "Basic bG9naW46cGFzcw==");
103 | }
104 |
105 | @Test
106 | public void shouldConnectWithAPITokenFavoringOverUserPassCredential() throws IOException {
107 | HttpConnectionUtil httpConnectionUtil = mockConnection();
108 | Rules rules = new Rules();
109 | Server server = new Server(rules, httpConnectionUtil);
110 | rules.setGoAPIToken("a-valid-token-from-gocd-server");
111 | rules.setGoLogin("login");
112 | rules.setGoPassword("pass");
113 |
114 | HttpURLConnection conn = mock(HttpURLConnection.class);
115 | when(httpConnectionUtil.getConnection(any(URL.class))).thenReturn(conn);
116 | when(conn.getContent()).thenReturn(new Object());
117 |
118 | server.getUrl(new URL("http://exmaple.org/"));
119 |
120 | verify(conn).setRequestProperty(eq("User-Agent"), anyString());
121 | verify(conn).setRequestProperty("Authorization", "Bearer a-valid-token-from-gocd-server");
122 | }
123 |
124 | @Test
125 | public void shouldNotSetAuthorizationHeaderWithoutCredentials() throws IOException {
126 | HttpConnectionUtil httpConnectionUtil = mockConnection();
127 | Rules rules = new Rules();
128 | Server server = new Server(rules, httpConnectionUtil);
129 |
130 | HttpURLConnection conn = mock(HttpURLConnection.class);
131 | when(httpConnectionUtil.getConnection(any(URL.class))).thenReturn(conn);
132 | when(conn.getContent()).thenReturn(new Object());
133 |
134 | server.getUrl(new URL("http://exmaple.org/"));
135 |
136 | verify(conn).setRequestProperty(eq("User-Agent"), anyString());
137 | verify(conn, never()).setRequestProperty(eq("Authorization"), anyString());
138 | }
139 |
140 | @Test
141 | public void shouldNotSetAuthorizationHeaderWithEmptyPassword() throws IOException {
142 | HttpConnectionUtil httpConnectionUtil = mockConnection();
143 | Rules rules = new Rules();
144 | rules.setGoLogin("login");
145 | rules.setGoPassword(null);
146 | Server server = new Server(rules, httpConnectionUtil);
147 |
148 | HttpURLConnection conn = mock(HttpURLConnection.class);
149 | when(httpConnectionUtil.getConnection(any(URL.class))).thenReturn(conn);
150 | when(conn.getContent()).thenReturn(new Object());
151 |
152 | server.getUrl(new URL("http://exmaple.org/"));
153 |
154 | verify(conn).setRequestProperty(eq("User-Agent"), anyString());
155 | verify(conn, never()).setRequestProperty(eq("Authorization"), anyString());
156 | }
157 |
158 | @Test
159 | public void shouldNotSetAuthorizationHeaderWithEmptyLoginName() throws IOException {
160 | HttpConnectionUtil httpConnectionUtil = mockConnection();
161 | Rules rules = new Rules();
162 | rules.setGoLogin(null);
163 | rules.setGoPassword("pass");
164 | Server server = new Server(rules, httpConnectionUtil);
165 |
166 | HttpURLConnection conn = mock(HttpURLConnection.class);
167 | when(httpConnectionUtil.getConnection(any(URL.class))).thenReturn(conn);
168 | when(conn.getContent()).thenReturn(new Object());
169 |
170 | server.getUrl(new URL("http://exmaple.org/"));
171 |
172 | verify(conn).setRequestProperty(eq("User-Agent"), anyString());
173 | verify(conn, never()).setRequestProperty(eq("Authorization"), anyString());
174 | }
175 |
176 | @Test
177 | public void shouldNotSetAuthorizationHeaderWithEmptyPasswordCredentials() throws IOException {
178 | HttpConnectionUtil httpConnectionUtil = mockConnection();
179 | Rules rules = new Rules();
180 | rules.setGoLogin("");
181 | rules.setGoPassword("");
182 | Server server = new Server(rules, httpConnectionUtil);
183 |
184 | HttpURLConnection conn = mock(HttpURLConnection.class);
185 | when(httpConnectionUtil.getConnection(any(URL.class))).thenReturn(conn);
186 | when(conn.getContent()).thenReturn(new Object());
187 |
188 | server.getUrl(new URL("http://exmaple.org/"));
189 |
190 | verify(conn).setRequestProperty(eq("User-Agent"), anyString());
191 | verify(conn, never()).setRequestProperty(eq("Authorization"), anyString());
192 | }
193 |
194 | private HttpConnectionUtil mockConnection() throws IOException {
195 | HttpConnectionUtil httpConnectionUtil = mock(HttpConnectionUtil.class);
196 |
197 | HttpURLConnection conn = mock(HttpURLConnection.class);
198 | when(httpConnectionUtil.getConnection(any(URL.class))).thenReturn(conn);
199 | when(conn.getContent()).thenReturn(new Object());
200 |
201 | return httpConnectionUtil;
202 | }
203 | }
204 |
--------------------------------------------------------------------------------
/src/test/java/in/ashwanthkumar/gocd/slack/ruleset/PipelineRuleTest.java:
--------------------------------------------------------------------------------
1 | package in.ashwanthkumar.gocd.slack.ruleset;
2 |
3 | import com.typesafe.config.Config;
4 | import com.typesafe.config.ConfigFactory;
5 | import in.ashwanthkumar.utils.collections.Sets;
6 | import org.junit.Test;
7 |
8 | import static in.ashwanthkumar.gocd.slack.ruleset.PipelineStatus.FAILED;
9 | import static in.ashwanthkumar.gocd.slack.ruleset.PipelineStatus.PASSED;
10 | import static junit.framework.Assert.assertFalse;
11 | import static junit.framework.Assert.assertTrue;
12 | import static org.hamcrest.core.Is.is;
13 | import static org.hamcrest.core.IsCollectionContaining.hasItem;
14 | import static org.junit.Assert.assertThat;
15 |
16 | public class PipelineRuleTest {
17 | @Test
18 | public void shouldGenerateRuleFromConfig() {
19 | Config config = ConfigFactory.parseResources("configs/pipeline-rule-1.conf").getConfig("pipeline");
20 | PipelineRule build = PipelineRule.fromConfig(config);
21 | assertThat(build.getNameRegex(), is(".*"));
22 | assertThat(build.getStageRegex(), is(".*"));
23 | assertThat(build.getGroupRegex(), is(".*"));
24 | assertThat(build.getStatus(), hasItem(FAILED));
25 | assertThat(build.getChannel(), is("#gocd"));
26 | assertThat(build.getWebhookUrl(), is("https://hooks.slack.com/services/"));
27 | assertThat(build.getOwners(), is(Sets.of("ashwanthkumar", "gobot")));
28 | }
29 |
30 | @Test
31 | public void shouldSetValuesFromDefaultsWhenPropertiesAreNotDefined() {
32 | Config defaultConf = ConfigFactory.parseResources("configs/default-pipeline-rule.conf").getConfig("pipeline");
33 | PipelineRule defaultRule = PipelineRule.fromConfig(defaultConf);
34 |
35 | Config config = ConfigFactory.parseResources("configs/pipeline-rule-2.conf").getConfig("pipeline");
36 | PipelineRule build = PipelineRule.fromConfig(config);
37 |
38 | PipelineRule mergedRule = PipelineRule.merge(build, defaultRule);
39 | assertThat(mergedRule.getNameRegex(), is("gocd-slack-build-notifier"));
40 | assertThat(mergedRule.getGroupRegex(), is("ci"));
41 | assertThat(mergedRule.getStageRegex(), is("build"));
42 | assertThat(mergedRule.getStatus(), hasItem(FAILED));
43 | assertThat(mergedRule.getChannel(), is("#gocd"));
44 | assertThat(mergedRule.getOwners(), is(Sets.of("ashwanthkumar", "gobot")));
45 | }
46 |
47 | @Test
48 | public void shouldMatchThePipelineAndStageAgainstRegex() {
49 | PipelineRule pipelineRule = new PipelineRule("gocd-.*", ".*").setGroupRegex("ci").setLabelRegex(".*").setStatus(Sets.of(FAILED, PASSED));
50 | assertTrue(pipelineRule.matches("gocd-slack-build-notifier", "build", "ci", ".*", "failed"));
51 | assertTrue(pipelineRule.matches("gocd-slack-build-notifier", "package", "ci", ".*", "passed"));
52 | assertTrue(pipelineRule.matches("gocd-slack-build-notifier", "publish", "ci", ".*", "passed"));
53 |
54 | assertFalse(pipelineRule.matches("gocd", "publish", "ci", ".*", "failed"));
55 | }
56 |
57 |
58 | }
--------------------------------------------------------------------------------
/src/test/java/in/ashwanthkumar/gocd/slack/ruleset/RulesReaderTest.java:
--------------------------------------------------------------------------------
1 | package in.ashwanthkumar.gocd.slack.ruleset;
2 |
3 | import in.ashwanthkumar.utils.collections.Sets;
4 | import org.junit.Test;
5 |
6 | import java.net.InetSocketAddress;
7 | import java.net.Proxy;
8 |
9 | import static org.hamcrest.CoreMatchers.*;
10 | import static org.junit.Assert.assertThat;
11 |
12 | public class RulesReaderTest {
13 |
14 | @Test
15 | public void shouldReadTestConfig() {
16 | Rules rules = RulesReader.read("configs/test-config-1.conf");
17 | assertThat(rules.isEnabled(), is(true));
18 | assertThat(rules.getSlackChannel(), is("#gocd"));
19 | assertThat(rules.getGoServerHost(), is("http://localhost:8080/"));
20 | assertThat(rules.getPipelineRules().size(), is(2));
21 | assertThat(rules.getPipelineRules().size(), is(2));
22 | assertThat(rules.getDisplayConsoleLogLinks(), is(false));
23 | assertThat(rules.getDisplayMaterialChanges(), is(false));
24 |
25 | PipelineRule pipelineRule1 = new PipelineRule()
26 | .setNameRegex("gocd-slack-build-notifier")
27 | .setStageRegex(".*")
28 | .setGroupRegex(".*")
29 | .setLabelRegex(".*")
30 | .setChannel("#gocd")
31 | .setStatus(Sets.of(PipelineStatus.FAILED));
32 | assertThat(rules.getPipelineRules(), hasItem(pipelineRule1));
33 |
34 | PipelineRule pipelineRule2 = new PipelineRule()
35 | .setNameRegex("my-java-utils")
36 | .setStageRegex("build")
37 | .setGroupRegex("ci")
38 | .setLabelRegex(".*")
39 | .setChannel("#gocd-build")
40 | .setStatus(Sets.of(PipelineStatus.FAILED));
41 | assertThat(rules.getPipelineRules(), hasItem(pipelineRule2));
42 |
43 | assertThat(rules.getPipelineListener(), notNullValue());
44 | }
45 |
46 | @Test
47 | public void shouldReadMinimalConfig() {
48 | Rules rules = RulesReader.read("configs/test-config-minimal.conf");
49 |
50 | assertThat(rules.isEnabled(), is(true));
51 |
52 | assertThat(rules.getGoLogin(), is("someuser"));
53 | assertThat(rules.getGoPassword(), is("somepassword"));
54 | assertThat(rules.getGoAPIToken(), is("a-valid-token-from-gocd-server"));
55 | assertThat(rules.getGoServerHost(), is("http://localhost:8153/"));
56 | assertThat(rules.getWebHookUrl(), is("https://hooks.slack.com/services/"));
57 |
58 | assertThat(rules.getSlackChannel(), is("#build"));
59 | assertThat(rules.getSlackDisplayName(), is("gocd-slack-bot"));
60 | assertThat(rules.getSlackUserIcon(), is("http://example.com/slack-bot.png"));
61 |
62 | // Default rules
63 | assertThat(rules.getPipelineRules().size(), is(1));
64 | assertThat(rules.getDisplayConsoleLogLinks(), is(true));
65 | assertThat(rules.getDisplayMaterialChanges(), is(true));
66 |
67 | PipelineRule pipelineRule = new PipelineRule()
68 | .setNameRegex(".*")
69 | .setStageRegex(".*")
70 | .setGroupRegex(".*")
71 | .setLabelRegex(".*")
72 | .setChannel("#build")
73 | .setStatus(Sets.of(PipelineStatus.CANCELLED, PipelineStatus.BROKEN, PipelineStatus.FAILED, PipelineStatus.FIXED));
74 | assertThat(rules.getPipelineRules(), hasItem(pipelineRule));
75 |
76 | assertThat(rules.getPipelineListener(), notNullValue());
77 | }
78 |
79 | @Test
80 | public void shouldReadMinimalConfigWithPipeline() {
81 | Rules rules = RulesReader.read("configs/test-config-minimal-with-pipeline.conf");
82 | assertThat(rules.isEnabled(), is(true));
83 | assertThat(rules.getSlackChannel(), nullValue());
84 | assertThat(rules.getGoServerHost(), is("https://go-instance:8153/"));
85 | assertThat(rules.getWebHookUrl(), is("https://hooks.slack.com/services/"));
86 | assertThat(rules.getPipelineRules().size(), is(1));
87 |
88 | PipelineRule pipelineRule = new PipelineRule()
89 | .setNameRegex(".*")
90 | .setStageRegex(".*")
91 | .setGroupRegex(".*")
92 | .setLabelRegex(".*")
93 | .setChannel("#foo")
94 | .setStatus(Sets.of(PipelineStatus.FAILED))
95 | .setWebhookUrl("https://hooks.slack.com/services/for-pipeline");
96 | assertThat(rules.getPipelineRules(), hasItem(pipelineRule));
97 |
98 | assertThat(rules.getPipelineListener(), notNullValue());
99 | }
100 |
101 | @Test
102 | public void shouldReadMinimalConfigWithPipelineAndEnvironmentVariables() {
103 | Rules rules = RulesReader.read("configs/test-config-minimal-with-env-variables.conf");
104 | assertThat(rules.isEnabled(), is(true));
105 | assertThat(rules.getSlackChannel(), nullValue());
106 | assertThat(rules.getGoServerHost(), is("https://go-instance:8153/"));
107 | assertThat(rules.getWebHookUrl(), is("https://hooks.slack.com/services/"));
108 | assertThat(rules.getPipelineRules().size(), is(1));
109 | assertThat(rules.getGoLogin(), is(System.getenv("HOME")));
110 |
111 | PipelineRule pipelineRule = new PipelineRule()
112 | .setNameRegex(".*")
113 | .setStageRegex(".*")
114 | .setGroupRegex(".*")
115 | .setLabelRegex(".*")
116 | .setChannel("#foo")
117 | .setStatus(Sets.of(PipelineStatus.FAILED));
118 | assertThat(rules.getPipelineRules(), hasItem(pipelineRule));
119 |
120 | assertThat(rules.getPipelineListener(), notNullValue());
121 | assertThat(rules.getProxy(), nullValue());
122 | }
123 |
124 | @Test(expected = RuntimeException.class)
125 | public void shouldThrowExceptionIfConfigInvalid() {
126 | RulesReader.read("test-config-invalid.conf");
127 | }
128 |
129 | @Test
130 | public void shouldReadProxyConfig() {
131 | Rules rules = RulesReader.read("configs/test-config-with-proxy.conf");
132 | assertThat(rules.isEnabled(), is(true));
133 | Proxy expectedProxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("localhost", 5555));
134 | assertThat(rules.getProxy(), is(expectedProxy));
135 | }
136 |
137 |
138 | }
139 |
--------------------------------------------------------------------------------
/src/test/java/in/ashwanthkumar/gocd/slack/ruleset/RulesTest.java:
--------------------------------------------------------------------------------
1 | package in.ashwanthkumar.gocd.slack.ruleset;
2 |
3 | import in.ashwanthkumar.gocd.slack.Status;
4 | import org.junit.Test;
5 |
6 | import java.util.Arrays;
7 | import java.util.List;
8 | import java.util.HashSet;
9 | import java.util.Set;
10 |
11 | import static org.hamcrest.MatcherAssert.assertThat;
12 | import static org.hamcrest.core.Is.is;
13 |
14 | public class RulesTest {
15 |
16 | @Test
17 | public void shouldFindMatch() {
18 | Rules rules = new Rules();
19 |
20 | rules.setPipelineRules(Arrays.asList(
21 | pipelineRule("pipeline1", "stage1", "ch1", statuses(PipelineStatus.BUILDING, PipelineStatus.FAILED)),
22 | pipelineRule("pipeline1", "stage2", "ch2", statuses(PipelineStatus.FIXED, PipelineStatus.PASSED)),
23 | pipelineRule("pipeline2", "stage2", "ch3", statuses(PipelineStatus.CANCELLED, PipelineStatus.BROKEN))
24 | ));
25 |
26 | List foundRules1 = rules.find("pipeline1", "stage1", "ci", ".*", Status.Building.getStatus());
27 | assertThat(foundRules1.size(), is(1));
28 | assertThat(foundRules1.get(0).getNameRegex(), is("pipeline1"));
29 | assertThat(foundRules1.get(0).getStageRegex(), is("stage1"));
30 |
31 | List foundRules2 = rules.find("pipeline2", "stage2", "ci", ".*", Status.Cancelled.getStatus());
32 | assertThat(foundRules2.size(), is(1));
33 | assertThat(foundRules2.get(0).getNameRegex(), is("pipeline2"));
34 | assertThat(foundRules2.get(0).getStageRegex(), is("stage2"));
35 |
36 | List foundRules3 = rules.find("pipeline2", "stage2", "ci", ".*", Status.Passed.getStatus());
37 | assertThat(foundRules3.size(), is(0));
38 | }
39 |
40 | @Test
41 | public void shouldFindMatchWithRegexp() {
42 | Rules rules = new Rules();
43 |
44 | rules.setPipelineRules(Arrays.asList(
45 | pipelineRule("[a-z]*", "[a-z]*", "ch1", statuses(PipelineStatus.BUILDING)),
46 | pipelineRule("\\d*", "\\d*", "ch2", statuses(PipelineStatus.BUILDING)),
47 | pipelineRule("\\d*", "\\d*", "ch3", statuses(PipelineStatus.PASSED)),
48 | pipelineRule("\\d*", "[a-z]*", "ch4", statuses(PipelineStatus.BUILDING))
49 | ));
50 |
51 | List foundRules1 = rules.find("abc", "efg", "ci", ".*", Status.Building.getStatus());
52 | assertThat(foundRules1.size(), is(1));
53 | assertThat(foundRules1.get(0).getNameRegex(), is("[a-z]*"));
54 | assertThat(foundRules1.get(0).getStageRegex(), is("[a-z]*"));
55 | assertThat(foundRules1.get(0).getChannel(), is("ch1"));
56 |
57 | List foundRules2 = rules.find("123", "456", "ci", ".*", Status.Building.getStatus());
58 | assertThat(foundRules2.size(), is(1));
59 | assertThat(foundRules2.get(0).getNameRegex(), is("\\d*"));
60 | assertThat(foundRules2.get(0).getStageRegex(), is("\\d*"));
61 | assertThat(foundRules2.get(0).getChannel(), is("ch2"));
62 |
63 | List foundRules3 = rules.find("123", "456", "ci", ".*", Status.Passed.getStatus());
64 | assertThat(foundRules3.size(), is(1));
65 | assertThat(foundRules3.get(0).getNameRegex(), is("\\d*"));
66 | assertThat(foundRules3.get(0).getStageRegex(), is("\\d*"));
67 | assertThat(foundRules3.get(0).getChannel(), is("ch3"));
68 |
69 | List foundRules4 = rules.find("pipeline1", "stage1", "ci", ".*", Status.Passed.getStatus());
70 | assertThat(foundRules4.size(), is(0));
71 | }
72 |
73 | @Test
74 | public void shouldFindAllMatchesIfProcessAllRules() {
75 | Rules rules = new Rules();
76 | rules.setProcessAllRules(true);
77 |
78 | rules.setPipelineRules(Arrays.asList(
79 | pipelineRule("[a-z]*", "stage\\d+", "ch1", statuses(PipelineStatus.BUILDING)),
80 | pipelineRule("[a-z]*", "stage2", "ch2", statuses(PipelineStatus.BUILDING))
81 | ));
82 |
83 | List foundRules1 = rules.find("abc", "stage1", "ci", ".*", Status.Building.getStatus());
84 | assertThat(foundRules1.size(), is(1));
85 | assertThat(foundRules1.get(0).getChannel(), is("ch1"));
86 |
87 | List foundRules2 = rules.find("abc", "stage2", "ci", ".*", Status.Building.getStatus());
88 | assertThat(foundRules2.size(), is(2));
89 | assertThat(foundRules2.get(0).getChannel(), is("ch1"));
90 | assertThat(foundRules2.get(1).getChannel(), is("ch2"));
91 |
92 | List foundRules3 = rules.find("abc1", "stage2", "ci", ".*", Status.Building.getStatus());
93 | assertThat(foundRules3.size(), is(0));
94 | }
95 |
96 | @Test
97 | public void shouldFindMatchAll() {
98 | Rules rules = new Rules();
99 |
100 | rules.setPipelineRules(Arrays.asList(
101 | pipelineRule("p1", "s1", "ch1", statuses(PipelineStatus.ALL))
102 | ));
103 |
104 | assertThat(rules.find("p1", "s1", "ci", ".*", Status.Building.getStatus()).size(), is(1));
105 | assertThat(rules.find("p1", "s1", "ci", ".*", Status.Broken.getStatus()).size(), is(1));
106 | assertThat(rules.find("p1", "s1", "ci", ".*", Status.Cancelled.getStatus()).size(), is(1));
107 | assertThat(rules.find("p1", "s1", "ci", ".*", Status.Failed.getStatus()).size(), is(1));
108 | assertThat(rules.find("p1", "s1", "ci", ".*", Status.Failing.getStatus()).size(), is(1));
109 | assertThat(rules.find("p1", "s1", "ci", ".*", Status.Fixed.getStatus()).size(), is(1));
110 | assertThat(rules.find("p1", "s1", "ci", ".*", Status.Passed.getStatus()).size(), is(1));
111 | assertThat(rules.find("p1", "s1", "ci", ".*", Status.Unknown.getStatus()).size(), is(1));
112 | }
113 |
114 | @Test
115 | public void shouldGetAPIServerHost() {
116 | Rules rules = new Rules();
117 |
118 | rules.setGoServerHost("https://gocd.com");
119 | assertThat(rules.getGoAPIServerHost(), is("https://gocd.com"));
120 |
121 | rules.setGoAPIServerHost("http://localhost");
122 | assertThat(rules.getGoAPIServerHost(), is("http://localhost"));
123 | }
124 |
125 | @Test
126 | public void shouldGetAPIToken() {
127 | Rules rules = new Rules();
128 |
129 | rules.setGoAPIToken("a-valid-token-from-gocd-server");
130 | assertThat(rules.getGoAPIToken(), is("a-valid-token-from-gocd-server"));
131 | }
132 |
133 | private static PipelineRule pipelineRule(String pipeline, String stage, String channel, Set statuses) {
134 | PipelineRule pipelineRule = new PipelineRule(pipeline, stage);
135 | pipelineRule.setStatus(statuses);
136 | pipelineRule.setChannel(channel);
137 | pipelineRule.setLabelRegex(".*");
138 | return pipelineRule;
139 | }
140 |
141 | private static Set statuses(PipelineStatus... statuses) {
142 | return new HashSet(Arrays.asList(statuses));
143 | }
144 |
145 | }
146 |
--------------------------------------------------------------------------------
/src/test/java/in/ashwanthkumar/gocd/slack/util/TestUtils.java:
--------------------------------------------------------------------------------
1 | package in.ashwanthkumar.gocd.slack.util;
2 |
3 | import in.ashwanthkumar.gocd.slack.jsonapi.Server;
4 | import in.ashwanthkumar.gocd.slack.jsonapi.ServerFactory;
5 | import in.ashwanthkumar.gocd.slack.ruleset.Rules;
6 |
7 | import static org.mockito.Matchers.any;
8 | import static org.mockito.Mockito.mock;
9 | import static org.mockito.Mockito.when;
10 |
11 | public class TestUtils {
12 |
13 | public static ServerFactory createMockServerFactory(Server server) {
14 | ServerFactory factory = mock(ServerFactory.class);
15 | when(factory.getServer(any(Rules.class))).thenReturn(server);
16 | return factory;
17 | }
18 |
19 | public static String getResourceDirectory(String resource) {
20 | ClassLoader ldr = Thread.currentThread().getContextClassLoader();
21 | String url = ldr.getResource(resource).toString();
22 | return url.substring("file:".length(), url.lastIndexOf('/'));
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/test/java/in/ashwanthkumar/gocd/teams/TeamsTest.java:
--------------------------------------------------------------------------------
1 | package in.ashwanthkumar.gocd.teams;
2 |
3 | import com.typesafe.config.Config;
4 | import com.typesafe.config.ConfigFactory;
5 | import in.ashwanthkumar.gocd.slack.ruleset.Rules;
6 | import org.junit.Assert;
7 | import org.junit.Test;
8 |
9 | import java.io.ByteArrayOutputStream;
10 | import java.io.IOException;
11 |
12 | public class TeamsTest {
13 |
14 | private TeamsCard buildCard() {
15 | TeamsCard card = new TeamsCard();
16 | card.setTitle("title");
17 | card.setColor(MessageCardSchema.Color.GREEN);
18 | card.addFact("k", "v");
19 | card.addLinkAction("name", "uri");
20 | return card;
21 | }
22 |
23 | @Test
24 | public void testCardHttpContent() throws IOException {
25 | TeamsCard card = buildCard();
26 | ByteArrayOutputStream baos = new ByteArrayOutputStream();
27 | new CardHttpContent(card).writeTo(baos);
28 | final String result = baos.toString();
29 |
30 | Assert.assertEquals(card.toString(), result);
31 | }
32 |
33 | @Test
34 | public void testCardToString() {
35 | TeamsCard card = buildCard();
36 | String result = card.toString();
37 |
38 | String expected = ("{'@type':'MessageCard','themeColor':'009900','title':'title'," +
39 | "'summary':'GoCD build update','sections':[{'facts':[{'name':'k'," +
40 | "'value':'v'}]}],'potentialAction':[{'@type':'OpenUri','name':'name'," +
41 | "'targets':[{'os':'default','uri':'uri'}]}]}")
42 | .replace('\'', '"');
43 |
44 | Assert.assertEquals(expected, result);
45 | }
46 |
47 | @Test
48 | public void testTeamsListener() {
49 | Config config = ConfigFactory.parseResources("configs/test-config-teams.conf")
50 | .withFallback(ConfigFactory.load(getClass().getClassLoader()))
51 | .getConfig("gocd.slack");
52 | Rules rules = Rules.fromConfig(config);
53 |
54 | Assert.assertEquals(TeamsPipelineListener.class, rules.getPipelineListener().getClass());
55 | Assert.assertEquals("https://example.com/default", rules.getWebHookUrl());
56 | Assert.assertEquals("https://example.com/pipeline-override",
57 | rules.getPipelineRules().get(0).getWebhookUrl());
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/src/test/resources/configs/default-pipeline-rule.conf:
--------------------------------------------------------------------------------
1 | pipeline {
2 | name = "defaultRule"
3 | group = "ci"
4 | stage = "build"
5 | # you can provide multiple values by separating them with | (pipe) symbol - failed|broken
6 | state = "failed" # accepted values - failed / broken / fixed / passed / all
7 | channel = "#gocd" # optional field
8 | owners = ["ashwanthkumar", "gobot"] # optional field
9 | }
--------------------------------------------------------------------------------
/src/test/resources/configs/go_notify.conf:
--------------------------------------------------------------------------------
1 | gocd.slack {
2 | # feature flag for notification plugin, turning this false will not post anything to Slack
3 | # quite useful while testing / debugging
4 | enabled = true
5 | # Global default channel for all pipelines, these can be overriden at a pipeline level as well
6 | channel = "#gocd"
7 | webhookUrl: "https://hooks.slack.com/services/abcd/efgh/lmnopqrst12345" # Mandatory field
8 | # Enter full FQDN of your GoCD instance. We'll be sending links on your slack channel using this as the base uri.
9 | server-host = "http://localhost:8080/"
10 |
11 | # Default settings for pipelines
12 | default {
13 | name = ".*"
14 | stage = ".*"
15 | group = ".*"
16 | # you can provide multiple values by separating them with | (pipe) symbol - failed|broken
17 | state = "failed" # accepted values - failed / broken / fixed / passed / all
18 | #channel = "gocd" # Mandatory field
19 | }
20 |
21 | # Example settings would be like
22 | # pipelines = [{
23 | # nameRegex = "upc14"
24 | # channel = "#"
25 | # state = "failed|broken"
26 | # }]
27 | pipelines = [{
28 | name = "gocd-slack-build-notifier"
29 | }, {
30 | name = "my-java-utils"
31 | stage = "build"
32 | # you can provide multiple values by separating them with | (pipe) symbol - failed|broken
33 | state = "failed" # accepted values - failed / broken / fixed / passed / all
34 | channel = "#gocd-build" # Mandatory field
35 | }]
36 | }
37 |
--------------------------------------------------------------------------------
/src/test/resources/configs/pipeline-rule-1.conf:
--------------------------------------------------------------------------------
1 | pipeline {
2 | name = ".*"
3 | stage = ".*"
4 | group = ".*"
5 | # you can provide multiple values by separating them with | (pipe) symbol - failed|broken
6 | state = "failed" # accepted values - failed / broken / fixed / passed / all
7 | channel = "#gocd" # optional field
8 | owners = ["ashwanthkumar", "gobot"] # optional field
9 | webhookUrl = "https://hooks.slack.com/services/"
10 | }
--------------------------------------------------------------------------------
/src/test/resources/configs/pipeline-rule-2.conf:
--------------------------------------------------------------------------------
1 | pipeline {
2 | name = "gocd-slack-build-notifier"
3 | }
--------------------------------------------------------------------------------
/src/test/resources/configs/test-config-1.conf:
--------------------------------------------------------------------------------
1 | gocd.slack {
2 | # feature flag for notification plugin, turning this false will not post anything to Slack
3 | # quite useful while testing / debugging
4 | enabled = true
5 | # Global default channel for all pipelines, these can be overriden at a pipeline level as well
6 | channel = "#gocd"
7 | webhookUrl: "https://hooks.slack.com/services/abcd/efgh/lmnopqrst12345" # Mandatory field
8 | # Enter full FQDN of your GoCD instance. We'll be sending links on your slack channel using this as the base uri.
9 | server-host = "http://localhost:8080/"
10 |
11 | display-console-log-links = false
12 |
13 | # If you don't want to see the revision changes in the notification (for size or confidentiality concerns)
14 | # defaults to true
15 | displayMaterialChanges = false
16 |
17 | # Default settings for pipelines
18 | default {
19 | name = ".*"
20 | stage = ".*"
21 | group = ".*"
22 | # you can provide multiple values by separating them with | (pipe) symbol - failed|broken
23 | state = "failed" # accepted values - failed / broken / fixed / passed / all
24 | #channel = "gocd" # Mandatory field
25 | }
26 |
27 | # Example settings would be like
28 | # pipelines = [{
29 | # nameRegex = "upc14"
30 | # channel = "#"
31 | # state = "failed|broken"
32 | # }]
33 | pipelines = [{
34 | name = "gocd-slack-build-notifier"
35 | }, {
36 | name = "my-java-utils"
37 | stage = "build"
38 | group = "ci"
39 | # you can provide multiple values by separating them with | (pipe) symbol - failed|broken
40 | state = "failed" # accepted values - failed / broken / fixed / passed / all
41 | channel = "#gocd-build" # Mandatory field
42 | }]
43 | }
44 |
--------------------------------------------------------------------------------
/src/test/resources/configs/test-config-invalid.conf:
--------------------------------------------------------------------------------
1 | gocd.slack {
2 | login = "foo
3 | password = "foo-bar"
4 | server-host = "https://go-instance:8153/"
5 | webhookUrl = "http://slack.com/"
6 |
7 | pipelines = [{
8 | name = ".*"
9 | stage = ".*"
10 | state = "failed"
11 | channel = "#foo"
12 | }]
13 | }
14 |
--------------------------------------------------------------------------------
/src/test/resources/configs/test-config-minimal-with-env-variables.conf:
--------------------------------------------------------------------------------
1 | gocd.slack {
2 | login = ${HOME} # HOME is the only environment variable that I could find which would be always present
3 | password = "password"
4 | server-host = "https://go-instance:8153/"
5 | webhookUrl = "https://hooks.slack.com/services/"
6 |
7 | pipelines = [{
8 | name = ".*"
9 | stage = ".*"
10 | state = "failed"
11 | channel = "#foo"
12 | }]
13 | }
14 |
--------------------------------------------------------------------------------
/src/test/resources/configs/test-config-minimal-with-pipeline.conf:
--------------------------------------------------------------------------------
1 | gocd.slack {
2 | login = "foo"
3 | password = "foo-bar"
4 | server-host = "https://go-instance:8153/"
5 | webhookUrl = "https://hooks.slack.com/services/"
6 |
7 | pipelines = [{
8 | name = ".*"
9 | stage = ".*"
10 | state = "failed"
11 | channel = "#foo"
12 | webhookUrl = "https://hooks.slack.com/services/for-pipeline"
13 | }]
14 | }
15 |
--------------------------------------------------------------------------------
/src/test/resources/configs/test-config-minimal.conf:
--------------------------------------------------------------------------------
1 | gocd.slack {
2 | login = "someuser"
3 | password = "somepassword"
4 | api-token = "a-valid-token-from-gocd-server"
5 | server-host = "http://localhost:8153/"
6 | webhookUrl = "https://hooks.slack.com/services/"
7 |
8 | # optional fields
9 | channel = "#build"
10 | slackDisplayName = "gocd-slack-bot"
11 | slackUserIconURL = "http://example.com/slack-bot.png"
12 | }
13 |
--------------------------------------------------------------------------------
/src/test/resources/configs/test-config-teams.conf:
--------------------------------------------------------------------------------
1 | gocd.slack {
2 | login = "foo"
3 | password = "foo-bar"
4 | server-host = "https://go-instance:8153/"
5 |
6 | # Teams specific configuration
7 | listener = "in.ashwanthkumar.gocd.teams.TeamsPipelineListener"
8 | webhookUrl = "https://example.com/default"
9 |
10 | pipelines = [{
11 | name = ".*"
12 | stage = ".*"
13 | state = "failed"
14 | # Optionally send these messages to another channel using a different webhook.
15 | webhookUrl = "https://example.com/pipeline-override"
16 | }]
17 | }
18 |
--------------------------------------------------------------------------------
/src/test/resources/configs/test-config-with-proxy.conf:
--------------------------------------------------------------------------------
1 | gocd.slack {
2 | login = "someuser"
3 | password = "somepassword"
4 | server-host = "http://localhost:8153/"
5 | webhookUrl = "https://hooks.slack.com/services/"
6 |
7 | # optional fields
8 | channel = "#build"
9 | slackDisplayName = "gocd-slack-bot"
10 | slackUserIconURL = "http://example.com/slack-bot.png"
11 | proxy {
12 | hostname = "localhost"
13 | port = "5555"
14 | type = "socks" # acceptable values are http / socks
15 | }
16 | }
17 |
--------------------------------------------------------------------------------