fieldValidation);
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/com/tw/go/plugin/util/JSONUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 ThoughtWorks, Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.tw.go.plugin.util;
18 |
19 | import com.google.gson.GsonBuilder;
20 |
21 | public class JSONUtils {
22 | public static Object fromJSON(String json) {
23 | return new GsonBuilder().create().fromJson(json, Object.class);
24 | }
25 |
26 | public static String toJSON(Object object) {
27 | return new GsonBuilder().create().toJson(object);
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/main/resources/plugin-settings.template.html:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
20 | {{ GOINPUTNAME[smtp_host].$error.server }}
21 |
22 |
23 |
24 |
25 | {{ GOINPUTNAME[smtp_port].$error.server }}
26 |
27 |
28 |
29 | True
30 | False
31 | {{ GOINPUTNAME[is_tls].$error.server }}
32 |
33 |
34 |
35 |
36 | {{ GOINPUTNAME[sender_email_id].$error.server }}
37 |
38 |
39 |
40 |
41 | {{ GOINPUTNAME[smtp_username].$error.server }}
42 |
43 |
44 |
45 |
46 | {{ GOINPUTNAME[sender_password].$error.server }}
47 |
48 |
49 |
50 |
51 | {{ GOINPUTNAME[receiver_email_id].$error.server }}
52 |
53 |
54 |
55 |
56 | {{ GOINPUTNAME[pipeline_stage_filter].$error.server }}
57 |
--------------------------------------------------------------------------------
/src/test/java/com/tw/go/plugin/EmailNotificationPluginImplUnitTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 ThoughtWorks, Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.tw.go.plugin;
18 |
19 | import com.thoughtworks.go.plugin.api.GoApplicationAccessor;
20 | import com.thoughtworks.go.plugin.api.GoPluginIdentifier;
21 | import com.thoughtworks.go.plugin.api.request.GoApiRequest;
22 | import com.thoughtworks.go.plugin.api.request.GoPluginApiRequest;
23 | import com.thoughtworks.go.plugin.api.response.GoApiResponse;
24 | import com.tw.go.plugin.util.JSONUtils;
25 | import org.junit.jupiter.api.BeforeEach;
26 | import org.junit.jupiter.api.Test;
27 | import org.mockito.ArgumentCaptor;
28 | import org.mockito.Mock;
29 | import org.mockito.MockitoAnnotations;
30 |
31 | import jakarta.mail.Address;
32 | import jakarta.mail.Authenticator;
33 | import jakarta.mail.Message;
34 | import jakarta.mail.Transport;
35 | import jakarta.mail.internet.InternetAddress;
36 | import java.util.Collections;
37 | import java.util.HashMap;
38 | import java.util.Map;
39 | import java.util.Properties;
40 |
41 | import static org.junit.jupiter.api.Assertions.assertEquals;
42 | import static org.junit.jupiter.api.Assertions.assertNotNull;
43 | import static org.mockito.Mockito.*;
44 |
45 | public class EmailNotificationPluginImplUnitTest {
46 |
47 | @Mock
48 | private GoApplicationAccessor goApplicationAccessor;
49 |
50 | @Mock
51 | private SessionWrapper mockSession;
52 |
53 | @Mock
54 | private Transport mockTransport;
55 |
56 | @Mock
57 | private SessionFactory mockSessionFactory;
58 |
59 | private Map settingsResponseMap;
60 |
61 | private Map stateChangeResponseMap;
62 | private EmailNotificationPluginImpl emailNotificationPlugin;
63 |
64 |
65 | @BeforeEach
66 | public void setup() throws Exception {
67 | MockitoAnnotations.openMocks(this);
68 |
69 | when(mockSession.getTransport()).thenReturn(mockTransport);
70 |
71 | when(mockSessionFactory.getInstance(any(Properties.class))).thenReturn(mockSession);
72 | when(mockSessionFactory.getInstance(any(Properties.class), any(Authenticator.class))).thenReturn(mockSession);
73 |
74 | emailNotificationPlugin = new EmailNotificationPluginImpl();
75 | emailNotificationPlugin.initializeGoApplicationAccessor(goApplicationAccessor);
76 | emailNotificationPlugin.setSessionFactory(mockSessionFactory);
77 | }
78 |
79 | @BeforeEach
80 | public void setupDefaultSettingResponse() {
81 | settingsResponseMap = new HashMap<>();
82 |
83 | settingsResponseMap.put("smtp_host", "test-smtp-host");
84 | settingsResponseMap.put("smtp_port", "25");
85 | settingsResponseMap.put("is_tls", "0");
86 | settingsResponseMap.put("sender_email_id", "test-smtp-sender");
87 | settingsResponseMap.put("sender_password", "test-smtp-password");
88 | settingsResponseMap.put("smtp_username", "test-smtp-username");
89 | settingsResponseMap.put("receiver_email_id", "test-smtp-receiver");
90 | }
91 |
92 | @BeforeEach
93 | public void setupDefaultStateChangeResponseMap() {
94 | Map stageResponseMap = new HashMap<>();
95 |
96 | stageResponseMap.put("name", "test-stage-name");
97 | stageResponseMap.put("counter", "test-counter");
98 | stageResponseMap.put("state", "test-state");
99 | stageResponseMap.put("result", "test-result");
100 | stageResponseMap.put("last-transition-time", "test-last-transition-time");
101 | stageResponseMap.put("create-time", "test-last-transition-time");
102 |
103 |
104 | Map pipelineMap = new HashMap<>();
105 |
106 | pipelineMap.put("stage", stageResponseMap);
107 | pipelineMap.put("name", "test-pipeline-name");
108 | pipelineMap.put("counter", "test-pipeline-counter");
109 |
110 | stateChangeResponseMap = new HashMap<>();
111 |
112 | stateChangeResponseMap.put("pipeline", pipelineMap);
113 | }
114 |
115 |
116 | @Test
117 | public void testStageNotificationRequestsSettings() {
118 | GoApiResponse settingsResponse = testSettingsResponse();
119 |
120 | when(goApplicationAccessor.submit(eq(testSettingsRequest()))).thenReturn(settingsResponse);
121 |
122 | GoPluginApiRequest requestFromServer = testStageChangeRequestFromServer();
123 |
124 | emailNotificationPlugin.handle(requestFromServer);
125 |
126 | final ArgumentCaptor settingsRequestCaptor = ArgumentCaptor.forClass(GoApiRequest.class);
127 |
128 | verify(goApplicationAccessor).submit(settingsRequestCaptor.capture());
129 |
130 | final GoApiRequest actualSettingsRequest = settingsRequestCaptor.getValue();
131 |
132 | assertEquals(testSettingsRequest().api(), actualSettingsRequest.api());
133 | assertEquals(testSettingsRequest().apiVersion(), actualSettingsRequest.apiVersion());
134 |
135 | GoPluginIdentifier actualGoPluginIdentifier = actualSettingsRequest.pluginIdentifier();
136 |
137 | assertNotNull(actualGoPluginIdentifier);
138 |
139 | assertEquals(testSettingsRequest().pluginIdentifier().getExtension(), actualGoPluginIdentifier.getExtension());
140 | assertEquals(testSettingsRequest().pluginIdentifier().getSupportedExtensionVersions(), actualGoPluginIdentifier.getSupportedExtensionVersions());
141 | assertEquals(testSettingsRequest().requestBody(), actualSettingsRequest.requestBody());
142 | assertEquals(testSettingsRequest().requestHeaders(), actualSettingsRequest.requestHeaders());
143 | assertEquals(testSettingsRequest().requestParameters(), actualSettingsRequest.requestParameters());
144 | }
145 |
146 | @Test
147 | public void testASingleEmailAddressSendsEmail() throws Exception {
148 | settingsResponseMap.put("receiver_email_id", "test-email@test.co.uk");
149 |
150 | GoApiResponse settingsResponse = testSettingsResponse();
151 |
152 | when(goApplicationAccessor.submit(any(GoApiRequest.class))).thenReturn(settingsResponse);
153 | doCallRealMethod().when(mockSession).createMessage(anyString(), anyString(), anyString(), anyString());
154 |
155 | GoPluginApiRequest requestFromServer = testStageChangeRequestFromServer();
156 |
157 | emailNotificationPlugin.handle(requestFromServer);
158 |
159 | verify(mockTransport).sendMessage(any(Message.class), eq(new Address[]{new InternetAddress("test-email@test.co.uk")}));
160 | verify(mockTransport, times(1)).connect(eq("test-smtp-host"), eq(25), eq("test-smtp-username"), eq("test-smtp-password"));
161 | verify(mockTransport, times(1)).close();
162 | verifyNoMoreInteractions(mockTransport);
163 | }
164 |
165 | @Test
166 | public void testMultipleEmailAddressSendsEmail() throws Exception {
167 | settingsResponseMap.put("receiver_email_id", "test-email@test.co.uk, test-email-2@test.co.uk");
168 |
169 | GoApiResponse settingsResponse = testSettingsResponse();
170 |
171 | when(goApplicationAccessor.submit(any(GoApiRequest.class))).thenReturn(settingsResponse);
172 | doCallRealMethod().when(mockSession).createMessage(anyString(), anyString(), anyString(), anyString());
173 |
174 | GoPluginApiRequest requestFromServer = testStageChangeRequestFromServer();
175 |
176 | emailNotificationPlugin.handle(requestFromServer);
177 |
178 | verify(mockTransport).sendMessage(any(Message.class), eq(new Address[]{new InternetAddress("test-email@test.co.uk")}));
179 | verify(mockTransport).sendMessage(any(Message.class), eq(new Address[]{new InternetAddress("test-email-2@test.co.uk")}));
180 | verify(mockTransport, times(2)).connect(eq("test-smtp-host"), eq(25), eq("test-smtp-username"), eq("test-smtp-password"));
181 | verify(mockTransport, times(2)).close();
182 | verifyNoMoreInteractions(mockTransport);
183 | }
184 |
185 |
186 | private GoPluginApiRequest testStageChangeRequestFromServer() {
187 | GoPluginApiRequest requestFromGoServer = mock(GoPluginApiRequest.class);
188 |
189 | when(requestFromGoServer.requestName()).thenReturn("stage-status");
190 |
191 | when(requestFromGoServer.requestBody()).thenReturn(JSONUtils.toJSON(stateChangeResponseMap));
192 |
193 | return requestFromGoServer;
194 | }
195 |
196 | private static GoApiRequest testSettingsRequest() {
197 |
198 | final Map requestMap = new HashMap<>();
199 | requestMap.put("plugin-id", "email.notifier");
200 |
201 | final String responseBody = JSONUtils.toJSON(requestMap);
202 |
203 | return new GoApiRequest() {
204 | @Override
205 | public String api() {
206 | return "go.processor.plugin-settings.get";
207 | }
208 |
209 | @Override
210 | public String apiVersion() {
211 | return "1.0";
212 | }
213 |
214 | @Override
215 | public GoPluginIdentifier pluginIdentifier() {
216 | return new GoPluginIdentifier("notification", Collections.singletonList("1.0"));
217 | }
218 |
219 | @Override
220 | public Map requestParameters() {
221 | return null;
222 | }
223 |
224 | @Override
225 | public Map requestHeaders() {
226 | return null;
227 | }
228 |
229 | @Override
230 | public String requestBody() {
231 | return responseBody;
232 | }
233 | };
234 | }
235 |
236 | private GoApiResponse testSettingsResponse() {
237 | GoApiResponse settingsResponse = mock(GoApiResponse.class);
238 |
239 | when(settingsResponse.responseBody()).thenReturn(JSONUtils.toJSON(settingsResponseMap));
240 |
241 | return settingsResponse;
242 | }
243 |
244 |
245 | }
--------------------------------------------------------------------------------
/src/test/java/com/tw/go/plugin/FilterConverterTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 ThoughtWorks, Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.tw.go.plugin;
18 |
19 | import org.junit.jupiter.api.BeforeEach;
20 | import org.junit.jupiter.api.Test;
21 |
22 | import java.util.List;
23 |
24 | import static org.junit.jupiter.api.Assertions.assertEquals;
25 | import static org.junit.jupiter.api.Assertions.assertNotNull;
26 |
27 | public class FilterConverterTest {
28 |
29 | private FilterConverter filterConverter;
30 |
31 | @BeforeEach
32 | public void setup() {
33 | this.filterConverter = new FilterConverter();
34 | }
35 |
36 | @Test
37 | public void testSingleFilterConverted() {
38 | String settingInput = "TempTestBaseInf:cloudformation:building";
39 |
40 | List filterList = filterConverter.convertStringToFilterList(settingInput);
41 |
42 | assertNotNull(filterList);
43 | assertEquals(1, filterList.size());
44 |
45 | Filter expectedFilter = new Filter("TempTestBaseInf", "cloudformation", "building");
46 |
47 | assertEquals(expectedFilter, filterList.get(0));
48 | }
49 |
50 | @Test
51 | public void testMultipleFiltersConverted() {
52 | String settingInput = "TempTestBaseInf:cloudformation:building,SmokeTestNetworkInf:ansible:failed";
53 |
54 | List filterList = filterConverter.convertStringToFilterList(settingInput);
55 |
56 | assertNotNull(filterList);
57 | assertEquals(2, filterList.size());
58 |
59 | Filter firstExpectedFilter = new Filter("TempTestBaseInf", "cloudformation", "building");
60 | Filter secondExpectedFilter = new Filter("SmokeTestNetworkInf", "ansible", "failed");
61 |
62 | assertEquals(firstExpectedFilter, filterList.get(0));
63 | assertEquals(secondExpectedFilter, filterList.get(1));
64 | }
65 |
66 | @Test
67 | public void testMultipleFiltersConvertedWithExtraCommas() {
68 | String settingInput = ",,TempTestBaseInf:cloudformation:building,,,,SmokeTestNetworkInf:ansible:failed,,,";
69 |
70 | List filterList = filterConverter.convertStringToFilterList(settingInput);
71 |
72 | assertNotNull(filterList);
73 | assertEquals(2, filterList.size());
74 |
75 | Filter firstExpectedFilter = new Filter("TempTestBaseInf", "cloudformation", "building");
76 | Filter secondExpectedFilter = new Filter("SmokeTestNetworkInf", "ansible", "failed");
77 |
78 | assertEquals(firstExpectedFilter, filterList.get(0));
79 | assertEquals(secondExpectedFilter, filterList.get(1));
80 | }
81 |
82 | @Test
83 | public void testPartiallyDefinedFilter() {
84 | String settingInput = "TempTestBaseInf";
85 |
86 | List filterList = filterConverter.convertStringToFilterList(settingInput);
87 |
88 | assertNotNull(filterList);
89 | assertEquals(1, filterList.size());
90 |
91 | Filter expectedFilter = new Filter("TempTestBaseInf", null, null);
92 |
93 | assertEquals(expectedFilter, filterList.get(0));
94 | }
95 |
96 | @Test
97 | public void testMultiplePartiallyDefinedFilters() {
98 | String settingInput = "TempTestBaseInf,SmokeTest:cloudformation";
99 |
100 | List filterList = filterConverter.convertStringToFilterList(settingInput);
101 |
102 | assertNotNull(filterList);
103 | assertEquals(2, filterList.size());
104 |
105 | Filter firstExpectedFilter = new Filter("TempTestBaseInf", null, null);
106 | Filter secondExpectedFilter = new Filter("SmokeTest", "cloudformation", null);
107 |
108 | assertEquals(firstExpectedFilter, filterList.get(0));
109 | assertEquals(secondExpectedFilter, filterList.get(1));
110 | }
111 |
112 | @Test
113 | public void testConvertMultipleFiltersWithWildcards() {
114 | String settingInput = "*BaseInf*:cloudformation:failed,*NetworkInf*:cloudformation:building";
115 |
116 | List filterList = filterConverter.convertStringToFilterList(settingInput);
117 |
118 | assertNotNull(filterList);
119 | assertEquals(2, filterList.size());
120 |
121 | Filter firstExpectedFilter = new Filter("*BaseInf*", "cloudformation", "failed");
122 | Filter secondExpectedFilter = new Filter("*NetworkInf*", "cloudformation", "building");
123 |
124 | assertEquals(firstExpectedFilter, filterList.get(0));
125 | assertEquals(secondExpectedFilter, filterList.get(1));
126 | }
127 | }
--------------------------------------------------------------------------------
/src/test/java/com/tw/go/plugin/FilterTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 ThoughtWorks, Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.tw.go.plugin;
18 |
19 | import org.junit.jupiter.api.Test;
20 |
21 | import static org.junit.jupiter.api.Assertions.assertFalse;
22 | import static org.junit.jupiter.api.Assertions.assertTrue;
23 |
24 | public class FilterTest {
25 |
26 | @Test
27 | public void testSimpleFilterIsMatched() {
28 | Filter testFilter = new Filter("SmokeTestBaseInf", "cloudformation", "building");
29 |
30 | assertTrue(testFilter.matches("SmokeTestBaseInf", "cloudformation", BuildState.BUILDING));
31 | assertTrue(testFilter.matches("SmokeTestBaseInf", "cloudformation", "building"));
32 |
33 | assertFalse(testFilter.matches("SmokeTestBaseInf", "cloudformation", BuildState.CANCELLED));
34 | assertFalse(testFilter.matches("SmokeTestBaseInf", "cloudformation", BuildState.FAILED));
35 | assertFalse(testFilter.matches("SmokeTestBaseInf", "cloudformation", BuildState.FAILING));
36 | assertFalse(testFilter.matches("SmokeTestBaseInf", "cloudformation", BuildState.PASSED));
37 | assertFalse(testFilter.matches("SmokeTestBaseInf", "cloudformation", BuildState.UNKNOWN));
38 | }
39 |
40 | @Test
41 | public void testFilterWithNullBuildStateMatchesAllStates() {
42 | Filter testFilter = new Filter("SmokeTestBaseInf", "cloudformation", null);
43 |
44 | for(BuildState currentState : BuildState.values()) {
45 | assertTrue(testFilter.matches("SmokeTestBaseInf", "cloudformation", currentState));
46 | assertTrue(testFilter.matches("SmokeTestBaseInf", "ClOuDfOrMaTiOn", currentState));
47 | }
48 | }
49 |
50 | @Test
51 | public void testFilterWithNullStageNameAndBuildStateMatchesAllStates() {
52 | Filter testFilter = new Filter("SmokeTestBaseInf", null, null);
53 |
54 | for(BuildState currentState : BuildState.values()) {
55 | assertTrue(testFilter.matches("SmokeTestBaseInf", null, currentState));
56 | }
57 | }
58 |
59 | @Test
60 | public void testFilterWithNullStageNameAndBuildStateMatchesAllStageNames() {
61 | Filter testFilter = new Filter("SmokeTestBaseInf", null, null);
62 |
63 | assertTrue(testFilter.matches("SmokeTestBaseInf", null, BuildState.BUILDING));
64 | assertTrue(testFilter.matches("SmokeTestBaseInf", "cloudformation", BuildState.BUILDING));
65 | assertTrue(testFilter.matches("SmokeTestBaseInf", "CLOUDforMATION", BuildState.BUILDING));
66 | assertTrue(testFilter.matches("SmokeTestBaseInf", "somethingrandom", BuildState.BUILDING));
67 | }
68 |
69 | @Test
70 | public void testFilterWithWilcardPipelineNameMatchesAllPipelines() {
71 | Filter testFilter = new Filter("*BaseInf*", "cloudformation", "building");
72 |
73 | assertTrue(testFilter.matches("SmokeTestBaseInf", "cloudformation", BuildState.BUILDING));
74 | assertTrue(testFilter.matches("TempTestBaseInf", "cloudformation", BuildState.BUILDING));
75 | assertTrue(testFilter.matches("ProductionBaseInf", "cloudformation", BuildState.BUILDING));
76 | }
77 |
78 | @Test
79 | public void testFilterWithRegexPipelineNameMatchesPipelines() {
80 | Filter testFilter = new Filter(".*BaseInf", "cloudformation", "building");
81 |
82 | assertTrue(testFilter.matches("SmokeTestBaseInf", "cloudformation", BuildState.BUILDING));
83 | assertTrue(testFilter.matches("TempTestBaseInf", "cloudformation", BuildState.BUILDING));
84 | assertTrue(testFilter.matches("ProductionBaseInf", "cloudformation", BuildState.BUILDING));
85 | }
86 |
87 | @Test
88 | public void testFilterWithRegexStageNameMatchesStages() {
89 | Filter testFilter = new Filter("SmokeTestBaseInf", ".*formation", "building");
90 |
91 | assertTrue(testFilter.matches("SmokeTestBaseInf", "bananaformation", BuildState.BUILDING));
92 | assertTrue(testFilter.matches("SmokeTestBaseInf", "pickleformation", BuildState.BUILDING));
93 | assertTrue(testFilter.matches("SmokeTestBaseInf", "cloudformation", BuildState.BUILDING));
94 | }
95 |
96 | @Test
97 | public void testFilterWithRegexStageAndPipelineNameMatchesStages() {
98 | Filter testFilter = new Filter(".*BaseInf", ".*formation", "building");
99 |
100 | assertTrue(testFilter.matches("SmokeTestBaseInf", "bananaformation", BuildState.BUILDING));
101 | assertTrue(testFilter.matches("TempTestBaseInf", "pickleformation", BuildState.BUILDING));
102 | assertTrue(testFilter.matches("ProductionBaseInf", "cloudformation", BuildState.BUILDING));
103 | }
104 | }
--------------------------------------------------------------------------------
/src/test/java/com/tw/go/plugin/SMTPMailSenderTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2018 ThoughtWorks, Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.tw.go.plugin;
18 |
19 | import com.icegreen.greenmail.util.GreenMail;
20 | import com.icegreen.greenmail.util.ServerSetupTest;
21 | import org.junit.jupiter.api.AfterEach;
22 | import org.junit.jupiter.api.BeforeEach;
23 | import org.junit.jupiter.api.Test;
24 |
25 | import jakarta.mail.MessagingException;
26 | import java.io.IOException;
27 |
28 | import static org.hamcrest.CoreMatchers.is;
29 | import static org.hamcrest.MatcherAssert.assertThat;
30 |
31 | public class SMTPMailSenderTest {
32 | private GreenMail mailServer;
33 |
34 | @BeforeEach
35 | public void setUp() {
36 | mailServer = new GreenMail(ServerSetupTest.SMTP);
37 | mailServer.start();
38 | }
39 |
40 | @Test
41 | public void shouldSendEmail() throws MessagingException, IOException {
42 | String userName = "user1";
43 | String emailId = "user1@domain.com";
44 | String password = "password1";
45 |
46 | mailServer.setUser(emailId, userName, password);
47 | SMTPSettings settings = new SMTPSettings("127.0.0.1", ServerSetupTest.SMTP.getPort(), false, emailId, userName, password);
48 | new SMTPMailSender(settings, new SessionFactory()).send("subject", "body", emailId);
49 |
50 | assertThat(mailServer.getReceivedMessages().length, is(1));
51 | assertThat(mailServer.getReceivedMessages()[0].getFrom()[0].toString(), is(emailId));
52 | assertThat(mailServer.getReceivedMessages()[0].getSubject(), is("subject"));
53 | assertThat(mailServer.getReceivedMessages()[0].getContent().toString(), is("body"));
54 | }
55 |
56 | @AfterEach
57 | public void tearDown() {
58 | mailServer.stop();
59 | }
60 | }
--------------------------------------------------------------------------------