@@ -51,6 +51,14 @@ public void testIsEnabledReturnsFalseWhenUrlIsNull() {
5151 assertFalse (discordWebhookService .isEnabled ());
5252 }
5353
54+ @ Test
55+ public void testIsEnabledReturnsFalseWhenUrlIsWhitespaceOnly () {
56+ when (configService .getBoolean ("discordWebhookEnabled" )).thenReturn (true );
57+ when (configService .getString ("discordWebhookUrl" )).thenReturn (" " );
58+
59+ assertFalse (discordWebhookService .isEnabled ());
60+ }
61+
5462 @ Test
5563 public void testIsEnabledReturnsTrueWhenConfigured () {
5664 when (configService .getBoolean ("discordWebhookEnabled" )).thenReturn (true );
@@ -68,71 +76,96 @@ public void testIsStaffOnlyReturnsConfigValue() {
6876 assertFalse (discordWebhookService .isStaffOnly ());
6977 }
7078
79+ @ Test
80+ public void testSendJoinNotificationSkipsWhenDisabled () {
81+ when (configService .getBoolean ("discordWebhookEnabled" )).thenReturn (false );
82+
83+ discordWebhookService .sendJoinNotification ("TestPlayer" );
84+
85+ // Should not attempt to read join message template when disabled
86+ verify (configService , never ()).getString ("discordWebhookJoinMessage" );
87+ }
88+
89+ @ Test
90+ public void testSendQuitNotificationSkipsWhenDisabled () {
91+ when (configService .getBoolean ("discordWebhookEnabled" )).thenReturn (false );
92+
93+ discordWebhookService .sendQuitNotification ("TestPlayer" );
94+
95+ // Should not attempt to read quit message template when disabled
96+ verify (configService , never ()).getString ("discordWebhookQuitMessage" );
97+ }
98+
7199 @ Test
72100 public void testSendJoinNotificationSkipsWhenTemplateIsEmpty () {
101+ when (configService .getBoolean ("discordWebhookEnabled" )).thenReturn (true );
102+ when (configService .getString ("discordWebhookUrl" )).thenReturn ("https://discord.com/api/webhooks/test" );
73103 when (configService .getString ("discordWebhookJoinMessage" )).thenReturn ("" );
74104
75- // Should not throw and should not attempt to send
76105 discordWebhookService .sendJoinNotification ("TestPlayer" );
106+
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" );
77110 }
78111
79112 @ Test
80113 public void testSendJoinNotificationSkipsWhenTemplateIsNull () {
114+ when (configService .getBoolean ("discordWebhookEnabled" )).thenReturn (true );
115+ when (configService .getString ("discordWebhookUrl" )).thenReturn ("https://discord.com/api/webhooks/test" );
81116 when (configService .getString ("discordWebhookJoinMessage" )).thenReturn (null );
82117
83- // Should not throw and should not attempt to send
84118 discordWebhookService .sendJoinNotification ("TestPlayer" );
119+
120+ verify (configService ).getString ("discordWebhookJoinMessage" );
121+ verify (configService , times (1 )).getString ("discordWebhookUrl" );
85122 }
86123
87124 @ Test
88125 public void testSendQuitNotificationSkipsWhenTemplateIsEmpty () {
126+ when (configService .getBoolean ("discordWebhookEnabled" )).thenReturn (true );
127+ when (configService .getString ("discordWebhookUrl" )).thenReturn ("https://discord.com/api/webhooks/test" );
89128 when (configService .getString ("discordWebhookQuitMessage" )).thenReturn ("" );
90129
91- // Should not throw and should not attempt to send
92130 discordWebhookService .sendQuitNotification ("TestPlayer" );
131+
132+ verify (configService ).getString ("discordWebhookQuitMessage" );
133+ verify (configService , times (1 )).getString ("discordWebhookUrl" );
93134 }
94135
95136 @ Test
96137 public void testSendQuitNotificationSkipsWhenTemplateIsNull () {
138+ when (configService .getBoolean ("discordWebhookEnabled" )).thenReturn (true );
139+ when (configService .getString ("discordWebhookUrl" )).thenReturn ("https://discord.com/api/webhooks/test" );
97140 when (configService .getString ("discordWebhookQuitMessage" )).thenReturn (null );
98141
99- // Should not throw and should not attempt to send
100142 discordWebhookService .sendQuitNotification ("TestPlayer" );
101- }
102-
103- @ Test
104- public void testSendJoinNotificationSkipsWhenUrlIsEmpty () {
105- when (configService .getString ("discordWebhookJoinMessage" )).thenReturn ("**{player}** joined!" );
106- when (configService .getString ("discordWebhookUrl" )).thenReturn ("" );
107-
108- // Should not throw
109- discordWebhookService .sendJoinNotification ("TestPlayer" );
110- }
111143
112- @ Test
113- public void testSendQuitNotificationSkipsWhenUrlIsEmpty () {
114- when (configService .getString ("discordWebhookQuitMessage" )).thenReturn ("**{player}** left." );
115- when (configService .getString ("discordWebhookUrl" )).thenReturn ("" );
116-
117- // Should not throw
118- discordWebhookService .sendQuitNotification ("TestPlayer" );
144+ verify (configService ).getString ("discordWebhookQuitMessage" );
145+ verify (configService , times (1 )).getString ("discordWebhookUrl" );
119146 }
120147
121148 @ Test
122149 public void testSendJoinNotificationHandlesInvalidUrl () {
123- when (configService .getString ( "discordWebhookJoinMessage " )).thenReturn ("**{player}** joined!" );
150+ when (configService .getBoolean ( "discordWebhookEnabled " )).thenReturn (true );
124151 when (configService .getString ("discordWebhookUrl" )).thenReturn ("not-a-valid-url" );
152+ when (configService .getString ("discordWebhookJoinMessage" )).thenReturn ("**{player}** joined!" );
125153
126- // Should not throw - errors are logged gracefully
127154 discordWebhookService .sendJoinNotification ("TestPlayer" );
155+
156+ // Error should be logged gracefully
157+ verify (logger ).log (contains ("Failed to send Discord webhook message" ));
128158 }
129159
130160 @ Test
131161 public void testSendQuitNotificationHandlesInvalidUrl () {
132- when (configService .getString ( "discordWebhookQuitMessage " )).thenReturn ("**{player}** left." );
162+ when (configService .getBoolean ( "discordWebhookEnabled " )).thenReturn (true );
133163 when (configService .getString ("discordWebhookUrl" )).thenReturn ("not-a-valid-url" );
164+ when (configService .getString ("discordWebhookQuitMessage" )).thenReturn ("**{player}** left." );
134165
135- // Should not throw - errors are logged gracefully
136166 discordWebhookService .sendQuitNotification ("TestPlayer" );
167+
168+ // Error should be logged gracefully
169+ verify (logger ).log (contains ("Failed to send Discord webhook message" ));
137170 }
138171}
0 commit comments