-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventConsumerUnitTest.java
More file actions
75 lines (66 loc) · 2.25 KB
/
Copy pathEventConsumerUnitTest.java
File metadata and controls
75 lines (66 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.techevents.consumer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.techevents.model.Event;
import com.techevents.service.EventIngestionService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;
class EventConsumerUnitTest {
private ObjectMapper mapper;
private EventIngestionService ingestionService;
private EventConsumer consumer;
@BeforeEach
void setUp() {
mapper = new ObjectMapper().registerModule(new JavaTimeModule());
ingestionService = mock(EventIngestionService.class);
consumer = new EventConsumer(mapper, ingestionService);
}
@Test
void malformedJson_shouldNotCallService() {
String badJson = "{ this is not valid JSON";
consumer.listen("topic1", badJson);
verifyNoInteractions(ingestionService);
}
@Test
void missingRequiredFields_shouldNotCallService() {
String missingFieldsJson = """
{
"description": "missing title and date",
"city": "X"
}
""";
consumer.listen("topic1", missingFieldsJson);
verifyNoInteractions(ingestionService);
}
@Test
void validJson_shouldCallServiceWithEvent() {
String validJson = """
{
"title": "Test Title",
"description": "desc",
"eventDate": "2025-06-01",
"city": "Test City",
"tags": ["tag1", "tag2"]
}
""";
consumer.listen("topic1", validJson);
verify(ingestionService, times(1)).ingest(any(Event.class));
}
@Test
void serviceThrowsException_shouldNotCrashConsumer() {
String validJson = """
{
"title": "Test Title",
"description": "desc",
"eventDate": "2025-06-01",
"city": "Test City",
"tags": ["tag1", "tag2"]
}
""";
doThrow(new RuntimeException("Simulated failure"))
.when(ingestionService).ingest(any());
assertDoesNotThrow(() -> consumer.listen("topic1", validJson));
}
}