Skip to content

Commit 98f045a

Browse files
committed
Added unit tests for EventListenerValidator and GatewayEventListener
1 parent 15fbdf9 commit 98f045a

File tree

4 files changed

+190
-5
lines changed

4 files changed

+190
-5
lines changed

core/src/main/java/com/javadiscord/jdi/core/Discord.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,6 @@ private static Gateway getGatewayURL(String authentication) {
188188
}
189189
}
190190

191-
public static String getBaseUrl() {
192-
return BASE_URL;
193-
}
194-
195191
public DiscordRequestDispatcher getDiscordRequestDispatcher() {
196192
return discordRequestDispatcher;
197193
}

core/src/main/java/com/javadiscord/jdi/core/processor/EventListenerValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ public boolean validate(Class<?> clazz) {
350350
return hasZeroArgsConstructor(clazz) && validateMethods(clazz);
351351
}
352352

353-
private boolean hasZeroArgsConstructor(Class<?> clazz) {
353+
public boolean hasZeroArgsConstructor(Class<?> clazz) {
354354
Constructor<?>[] constructors = clazz.getConstructors();
355355
for (Constructor<?> constructor : constructors) {
356356
if (constructor.getParameterCount() == 0) {
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.javadiscord.jdi.core;
2+
3+
import static org.mockito.Mockito.*;
4+
5+
import com.javadiscord.jdi.core.annotations.ChannelCreate;
6+
import com.javadiscord.jdi.core.annotations.EventListener;
7+
import com.javadiscord.jdi.core.annotations.MessageCreate;
8+
import com.javadiscord.jdi.core.models.channel.Channel;
9+
import com.javadiscord.jdi.core.models.message.Message;
10+
import com.javadiscord.jdi.internal.cache.Cache;
11+
import com.javadiscord.jdi.internal.cache.CacheType;
12+
import com.javadiscord.jdi.internal.gateway.handlers.events.EventType;
13+
14+
import org.junit.jupiter.api.Test;
15+
16+
import java.util.List;
17+
18+
class GatewayEventListenerTest {
19+
20+
@EventListener
21+
public static class TestEventListener {
22+
@MessageCreate
23+
public void dummy(Message ignore) {}
24+
25+
@ChannelCreate
26+
public void dummy(Channel ignore) {}
27+
}
28+
29+
@Test
30+
void shouldInvokeListenerOnMessageCreate() {
31+
TestEventListener testEventListener = mock(TestEventListener.class);
32+
33+
Discord mockDiscord = mock(Discord.class);
34+
Message mockMessage = mock(Message.class);
35+
when(mockDiscord.getEventListeners()).thenReturn(List.of(testEventListener));
36+
37+
Cache cache = new Cache(CacheType.FULL);
38+
GatewayEventListener gatewayEventListener = new GatewayEventListener(mockDiscord, cache);
39+
gatewayEventListener.receive(EventType.MESSAGE_CREATE, mockMessage);
40+
41+
verify(testEventListener, times(1)).dummy(any(Message.class));
42+
}
43+
44+
@Test
45+
void shouldInvokeListenerOnChannelCreate() {
46+
TestEventListener testEventListener = mock(TestEventListener.class);
47+
48+
Discord mockDiscord = mock(Discord.class);
49+
Channel mockMessage = mock(Channel.class);
50+
when(mockDiscord.getEventListeners()).thenReturn(List.of(testEventListener));
51+
52+
Cache cache = new Cache(CacheType.FULL);
53+
GatewayEventListener gatewayEventListener = new GatewayEventListener(mockDiscord, cache);
54+
gatewayEventListener.receive(EventType.CHANNEL_CREATE, mockMessage);
55+
56+
verify(testEventListener, times(1)).dummy(any(Channel.class));
57+
}
58+
59+
@Test
60+
void shouldNotInvokeListenerOnChannelCreateWhenReceivingMessageCreate() {
61+
TestEventListener testEventListener = mock(TestEventListener.class);
62+
63+
Discord mockDiscord = mock(Discord.class);
64+
Message mockMessage = mock(Message.class);
65+
when(mockDiscord.getEventListeners()).thenReturn(List.of(testEventListener));
66+
67+
Cache cache = new Cache(CacheType.FULL);
68+
GatewayEventListener gatewayEventListener = new GatewayEventListener(mockDiscord, cache);
69+
gatewayEventListener.receive(EventType.MESSAGE_CREATE, mockMessage);
70+
71+
verify(testEventListener, times(0)).dummy(any(Channel.class));
72+
}
73+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.javadiscord.jdi.core.processor;
2+
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import com.javadiscord.jdi.core.Discord;
7+
import com.javadiscord.jdi.core.Guild;
8+
import com.javadiscord.jdi.core.annotations.EventListener;
9+
import com.javadiscord.jdi.core.annotations.MessageCreate;
10+
import com.javadiscord.jdi.core.models.channel.Channel;
11+
import com.javadiscord.jdi.core.models.message.Message;
12+
13+
import org.junit.jupiter.api.Test;
14+
15+
class EventListenerValidatorTest {
16+
17+
@Test
18+
void testValidationFailsWhenZeroArgConstructorDoesNotExist() {
19+
class Test {}
20+
21+
EventListenerValidator eventListenerValidator = new EventListenerValidator();
22+
assertFalse(eventListenerValidator.validate(Test.class));
23+
}
24+
25+
public static class ClassWithConstructor {
26+
public ClassWithConstructor() {}
27+
}
28+
29+
@Test
30+
void testValidationPassesWhenZeroArgConstructorExists() {
31+
EventListenerValidator eventListenerValidator = new EventListenerValidator();
32+
assertTrue(eventListenerValidator.validate(ClassWithConstructor.class));
33+
}
34+
35+
@EventListener
36+
public static class ClassWithEventListenerAndMessageCreate {
37+
public ClassWithEventListenerAndMessageCreate() {}
38+
39+
@MessageCreate
40+
public void dummy() {}
41+
}
42+
43+
@Test
44+
void testValidationPassWhenMethodAnnotatedWithNoParameters() {
45+
EventListenerValidator eventListenerValidator = new EventListenerValidator();
46+
assertTrue(eventListenerValidator.validate(ClassWithEventListenerAndMessageCreate.class));
47+
}
48+
49+
@EventListener
50+
public static class ClassWithMethodParameterAsModel {
51+
public ClassWithMethodParameterAsModel() {}
52+
53+
@MessageCreate
54+
public void dummy(Message ignore) {}
55+
}
56+
57+
@Test
58+
void testValidationPassWhenMethodAnnotatedWithOneParameter() {
59+
EventListenerValidator eventListenerValidator = new EventListenerValidator();
60+
assertTrue(eventListenerValidator.validate(ClassWithMethodParameterAsModel.class));
61+
}
62+
63+
@EventListener
64+
public static class ClassWithMethodParameterDiscord {
65+
public ClassWithMethodParameterDiscord() {}
66+
67+
@MessageCreate
68+
public void dummy(Message ignore, Discord ignore1) {}
69+
}
70+
71+
@Test
72+
void testValidationPassWhenMethodAnnotatedWithDiscordParameter() {
73+
EventListenerValidator eventListenerValidator = new EventListenerValidator();
74+
assertTrue(eventListenerValidator.validate(ClassWithMethodParameterDiscord.class));
75+
}
76+
77+
@EventListener
78+
public static class ClassWithVarietyOfSuccessMethods {
79+
public ClassWithVarietyOfSuccessMethods() {}
80+
81+
@MessageCreate
82+
public void dummy(Message ignore, Discord ignore1) {}
83+
84+
@MessageCreate
85+
public void dummy(Discord ignore) {}
86+
87+
@MessageCreate
88+
public void dummy(Guild ignore) {}
89+
90+
@MessageCreate
91+
public void dummy(Guild ignore, Guild ignore1) {}
92+
93+
@MessageCreate
94+
public void dummy(Message ignore, Discord ignore2, Guild ignore3) {}
95+
}
96+
97+
@Test
98+
void testValidationPassWhenMethodAnnotatedWithValidParameters() {
99+
EventListenerValidator eventListenerValidator = new EventListenerValidator();
100+
assertTrue(eventListenerValidator.validate(ClassWithVarietyOfSuccessMethods.class));
101+
}
102+
103+
@EventListener
104+
public static class ClassWithInvalidParameter {
105+
public ClassWithInvalidParameter() {}
106+
107+
@MessageCreate
108+
public void dummy(Channel ignore) {}
109+
}
110+
111+
@Test
112+
void testValidationFailWhenMethodAnnotatedWithInvalidParameters() {
113+
EventListenerValidator eventListenerValidator = new EventListenerValidator();
114+
assertFalse(eventListenerValidator.validate(ClassWithInvalidParameter.class));
115+
}
116+
}

0 commit comments

Comments
 (0)