|
| 1 | +import static org.junit.jupiter.api.Assertions.*; |
| 2 | + |
1 | 3 | import dev.aikido.agent_api.Track; |
2 | 4 | import dev.aikido.agent_api.background.cloud.api.events.APIEvent; |
3 | 5 | import dev.aikido.agent_api.background.cloud.api.events.CustomEvent; |
4 | 6 | import dev.aikido.agent_api.context.Context; |
5 | 7 | import dev.aikido.agent_api.context.ContextObject; |
6 | 8 | import dev.aikido.agent_api.storage.AttackQueue; |
7 | 9 | import org.junit.jupiter.api.*; |
| 10 | +import org.junitpioneer.jupiter.ClearEnvironmentVariable; |
8 | 11 | import org.junitpioneer.jupiter.SetEnvironmentVariable; |
9 | 12 | import org.junitpioneer.jupiter.StdIo; |
10 | 13 | import org.junitpioneer.jupiter.StdOut; |
11 | 14 | import utils.EmptySampleContextObject; |
12 | 15 |
|
13 | | -import static org.junit.jupiter.api.Assertions.*; |
14 | | - |
15 | 16 | @SetEnvironmentVariable(key = "AIKIDO_LOG_LEVEL", value = "trace") |
16 | 17 | @SetEnvironmentVariable(key = "AIKIDO_TOKEN", value = "invalid-token-2") |
17 | 18 | public class TrackTest { |
@@ -58,10 +59,22 @@ public void testTrackWithoutContext(StdOut out) throws Exception { |
58 | 59 | public void testTrackWithoutContextOnlyLogsOnce(StdOut out) throws Exception { |
59 | 60 | Track.track("my-event"); |
60 | 61 | Track.track("my-event"); |
61 | | - int occurrences = out.capturedString().split("track\\(\\.\\.\\.\\) was called without a context\\.", -1).length - 1; |
| 62 | + int occurrences = |
| 63 | + out.capturedString().split("track\\(\\.\\.\\.\\) was called without a context\\.", -1).length - 1; |
62 | 64 | assertEquals(1, occurrences); |
63 | 65 | } |
64 | 66 |
|
| 67 | + @Test |
| 68 | + @ClearEnvironmentVariable(key = "AIKIDO_TOKEN") |
| 69 | + public void testTrackWithoutTokenDoesNotQueueEvent() { |
| 70 | + ContextObject context = new EmptySampleContextObject("test", "/track-me", "POST"); |
| 71 | + Context.set(context); |
| 72 | + |
| 73 | + Track.track("my-custom-event"); |
| 74 | + |
| 75 | + assertEquals(0, AttackQueue.getSize()); |
| 76 | + } |
| 77 | + |
65 | 78 | @Test |
66 | 79 | public void testTrackSendsEventToQueue() throws InterruptedException { |
67 | 80 | ContextObject context = new EmptySampleContextObject("test", "/track-me", "POST"); |
@@ -93,15 +106,20 @@ public void testTrackAllowsUpToLimitPerRequest() { |
93 | 106 |
|
94 | 107 | @Test |
95 | 108 | @StdIo |
96 | | - public void testTrackDropsEventsOverLimitPerRequest(StdOut out) { |
| 109 | + public void testTrackDropsEventsOverLimitAndLogsWarningOnlyOnce(StdOut out) { |
97 | 110 | ContextObject context = new EmptySampleContextObject("test", "/track-me", "POST"); |
98 | 111 | Context.set(context); |
99 | 112 |
|
100 | | - for (int i = 0; i < 26; i++) { |
| 113 | + for (int i = 0; i < 30; i++) { |
101 | 114 | Track.track("event-" + i); |
102 | 115 | } |
103 | 116 |
|
104 | 117 | assertEquals(25, AttackQueue.getSize()); |
105 | | - assertTrue(out.capturedString().contains("Dropping event: event-25")); |
| 118 | + long warningCount = out.capturedString() |
| 119 | + .lines() |
| 120 | + .filter(line -> line.contains("Only the first 25 events were tracked.")) |
| 121 | + .count(); |
| 122 | + assertEquals(1, warningCount); |
| 123 | + assertFalse(out.capturedString().contains("event-25")); |
106 | 124 | } |
107 | 125 | } |
0 commit comments