11package dansplugins .activitytracker .services ;
22
33import dansplugins .activitytracker .utils .Logger ;
4+ import org .junit .After ;
45import org .junit .Before ;
56import org .junit .Test ;
67import org .mockito .Mock ;
@@ -21,13 +22,19 @@ public class DiscordWebhookServiceTest {
2122 private Logger logger ;
2223
2324 private DiscordWebhookService discordWebhookService ;
25+ private AutoCloseable mocks ;
2426
2527 @ Before
2628 public void setUp () {
27- MockitoAnnotations .initMocks (this );
29+ mocks = MockitoAnnotations .openMocks (this );
2830 discordWebhookService = new DiscordWebhookService (configService , logger );
2931 }
3032
33+ @ After
34+ public void tearDown () throws Exception {
35+ mocks .close ();
36+ }
37+
3138 @ Test
3239 public void testIsEnabledReturnsFalseWhenDisabled () {
3340 when (configService .getBoolean ("discordWebhookEnabled" )).thenReturn (false );
@@ -77,93 +84,94 @@ public void testIsStaffOnlyReturnsConfigValue() {
7784 }
7885
7986 @ Test
80- public void testSendJoinNotificationSkipsWhenDisabled () {
81- when (configService .getBoolean ( "discordWebhookEnabled " )).thenReturn (false );
87+ public void testGetWebhookUrlReturnsTrimmedUrl () {
88+ when (configService .getString ( "discordWebhookUrl " )).thenReturn (" https://discord.com/api/webhooks/test " );
8289
83- discordWebhookService .sendJoinNotification ("TestPlayer" );
90+ assertEquals ("https://discord.com/api/webhooks/test" , discordWebhookService .getWebhookUrl ());
91+ }
8492
85- // Should not attempt to read join message template when disabled
86- verify (configService , never ()).getString ("discordWebhookJoinMessage" );
93+ @ Test
94+ public void testGetWebhookUrlReturnsNullWhenEmpty () {
95+ when (configService .getString ("discordWebhookUrl" )).thenReturn ("" );
96+
97+ assertNull (discordWebhookService .getWebhookUrl ());
8798 }
8899
89100 @ Test
90- public void testSendQuitNotificationSkipsWhenDisabled () {
91- when (configService .getBoolean ("discordWebhookEnabled" )).thenReturn (false );
101+ public void testGetWebhookUrlReturnsNullWhenNull () {
102+ when (configService .getString ("discordWebhookUrl" )).thenReturn (null );
103+
104+ assertNull (discordWebhookService .getWebhookUrl ());
105+ }
92106
93- discordWebhookService .sendQuitNotification ("TestPlayer" );
107+ @ Test
108+ public void testGetWebhookUrlReturnsNullWhenWhitespaceOnly () {
109+ when (configService .getString ("discordWebhookUrl" )).thenReturn (" " );
94110
95- // Should not attempt to read quit message template when disabled
96- verify (configService , never ()).getString ("discordWebhookQuitMessage" );
111+ assertNull (discordWebhookService .getWebhookUrl ());
97112 }
98113
99114 @ Test
100- public void testSendJoinNotificationSkipsWhenTemplateIsEmpty () {
101- when (configService .getBoolean ("discordWebhookEnabled" )).thenReturn (true );
102- when (configService .getString ("discordWebhookUrl" )).thenReturn ("https://discord.com/api/webhooks/test" );
103- when (configService .getString ("discordWebhookJoinMessage" )).thenReturn ("" );
115+ public void testPrepareJoinMessageReplacesPlayerName () {
116+ when (configService .getString ("discordWebhookJoinMessage" )).thenReturn ("**{player}** joined!" );
104117
105- discordWebhookService .sendJoinNotification ("TestPlayer" );
118+ assertEquals ("**TestPlayer** joined!" , discordWebhookService .prepareJoinMessage ("TestPlayer" ));
119+ }
106120
107- // Should check the template but not attempt to fetch URL for sending
108- verify (configService ).getString ("discordWebhookJoinMessage" );
109- verify (configService , times (1 )).getString ("discordWebhookUrl" );
121+ @ Test
122+ public void testPrepareJoinMessageReturnsNullWhenTemplateIsEmpty () {
123+ when (configService .getString ("discordWebhookJoinMessage" )).thenReturn ("" );
124+
125+ assertNull (discordWebhookService .prepareJoinMessage ("TestPlayer" ));
110126 }
111127
112128 @ Test
113- public void testSendJoinNotificationSkipsWhenTemplateIsNull () {
114- when (configService .getBoolean ("discordWebhookEnabled" )).thenReturn (true );
115- when (configService .getString ("discordWebhookUrl" )).thenReturn ("https://discord.com/api/webhooks/test" );
129+ public void testPrepareJoinMessageReturnsNullWhenTemplateIsNull () {
116130 when (configService .getString ("discordWebhookJoinMessage" )).thenReturn (null );
117131
118- discordWebhookService .sendJoinNotification ("TestPlayer" );
132+ assertNull (discordWebhookService .prepareJoinMessage ("TestPlayer" ));
133+ }
134+
135+ @ Test
136+ public void testPrepareQuitMessageReplacesPlayerName () {
137+ when (configService .getString ("discordWebhookQuitMessage" )).thenReturn ("**{player}** left." );
119138
120- verify (configService ).getString ("discordWebhookJoinMessage" );
121- verify (configService , times (1 )).getString ("discordWebhookUrl" );
139+ assertEquals ("**TestPlayer** left." , discordWebhookService .prepareQuitMessage ("TestPlayer" ));
122140 }
123141
124142 @ Test
125- public void testSendQuitNotificationSkipsWhenTemplateIsEmpty () {
126- when (configService .getBoolean ("discordWebhookEnabled" )).thenReturn (true );
127- when (configService .getString ("discordWebhookUrl" )).thenReturn ("https://discord.com/api/webhooks/test" );
143+ public void testPrepareQuitMessageReturnsNullWhenTemplateIsEmpty () {
128144 when (configService .getString ("discordWebhookQuitMessage" )).thenReturn ("" );
129145
130- discordWebhookService .sendQuitNotification ("TestPlayer" );
131-
132- verify (configService ).getString ("discordWebhookQuitMessage" );
133- verify (configService , times (1 )).getString ("discordWebhookUrl" );
146+ assertNull (discordWebhookService .prepareQuitMessage ("TestPlayer" ));
134147 }
135148
136149 @ Test
137- public void testSendQuitNotificationSkipsWhenTemplateIsNull () {
138- when (configService .getBoolean ("discordWebhookEnabled" )).thenReturn (true );
139- when (configService .getString ("discordWebhookUrl" )).thenReturn ("https://discord.com/api/webhooks/test" );
150+ public void testPrepareQuitMessageReturnsNullWhenTemplateIsNull () {
140151 when (configService .getString ("discordWebhookQuitMessage" )).thenReturn (null );
141152
142- discordWebhookService .sendQuitNotification ("TestPlayer" );
143-
144- verify (configService ).getString ("discordWebhookQuitMessage" );
145- verify (configService , times (1 )).getString ("discordWebhookUrl" );
153+ assertNull (discordWebhookService .prepareQuitMessage ("TestPlayer" ));
146154 }
147155
148156 @ Test
149- public void testSendJoinNotificationHandlesInvalidUrl () {
150- when (configService .getBoolean ("discordWebhookEnabled" )).thenReturn (true );
151- when (configService .getString ("discordWebhookUrl" )).thenReturn ("not-a-valid-url" );
152- when (configService .getString ("discordWebhookJoinMessage" )).thenReturn ("**{player}** joined!" );
153-
154- discordWebhookService .sendJoinNotification ("TestPlayer" );
157+ public void testSendWebhookMessageSkipsWhenUrlIsNull () {
158+ discordWebhookService .sendWebhookMessage (null , "test message" );
155159
156- // Error should be logged gracefully
157- verify (logger ). log ( contains ( "Failed to send Discord webhook message" ) );
160+ // No HTTP call should be attempted, no errors logged
161+ verifyNoInteractions (logger );
158162 }
159163
160164 @ Test
161- public void testSendQuitNotificationHandlesInvalidUrl () {
162- when (configService .getBoolean ("discordWebhookEnabled" )).thenReturn (true );
163- when (configService .getString ("discordWebhookUrl" )).thenReturn ("not-a-valid-url" );
164- when (configService .getString ("discordWebhookQuitMessage" )).thenReturn ("**{player}** left." );
165+ public void testSendWebhookMessageSkipsWhenUrlIsEmpty () {
166+ discordWebhookService .sendWebhookMessage ("" , "test message" );
165167
166- discordWebhookService .sendQuitNotification ("TestPlayer" );
168+ // No HTTP call should be attempted, no errors logged
169+ verifyNoInteractions (logger );
170+ }
171+
172+ @ Test
173+ public void testSendWebhookMessageHandlesInvalidUrl () {
174+ discordWebhookService .sendWebhookMessage ("not-a-valid-url" , "**TestPlayer** joined!" );
167175
168176 // Error should be logged gracefully
169177 verify (logger ).log (contains ("Failed to send Discord webhook message" ));
0 commit comments