File size: 6,799 Bytes
442299c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package com.dalab.discovery.common;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;

import com.dalab.discovery.common.notification.INotificationService;
import com.dalab.discovery.common.notification.dto.NotificationDTO;
import com.dalab.discovery.common.util.health.HealthStatus;
import com.dalab.discovery.common.util.health.IHealthCheckService;
import com.dalab.discovery.crawler.service.event.IDiscoveryEventService;
import com.dalab.discovery.crawler.service.event.dto.DiscoveryEventDTO;;

/**
 * Unit test using mocks for crawler services.
 */
class CrawlerIntegrationTest {

        // Mock services
        private IDiscoveryEventService eventService;
        private INotificationService notificationService;
        private IHealthCheckService healthCheckService;

        // Simple local cache for testing
        private final Map<String, Object> localCache = new ConcurrentHashMap<>();

        @BeforeEach
        void setUp() {
                // Create mock services
                eventService = mock(IDiscoveryEventService.class);
                notificationService = mock(INotificationService.class);
                healthCheckService = mock(IHealthCheckService.class);

                // Clear cache between tests
                localCache.clear();
        }

        // Cache helper methods
        private <T> Optional<T> getCachedValue(String key, Class<T> type) {
                Object value = localCache.get(key);
                if (value != null && type.isInstance(value)) {
                        return Optional.of(type.cast(value));
                }
                return Optional.empty();
        }

        private <T> void cacheValue(String key, T value, Duration ttl) {
                localCache.put(key, value);
        }

        private void invalidateCache(String key) {
                localCache.remove(key);
        }

        @Test
        void testEndToEndEventFlow() {
                // Prepare test event
                DiscoveryEventDTO event = new DiscoveryEventDTO();
                event.setEventType("test.event");
                event.setResourceId("test-resource");
                event.setSeverity(DiscoveryEventDTO.EventSeverity.INFO);

                Map<String, Object> payload = new HashMap<>();
                payload.put("testData", "example");
                event.setPayload(payload);

                // Capture the event handler to manually trigger it
                ArgumentCaptor<Consumer<DiscoveryEventDTO>> handlerCaptor = ArgumentCaptor.forClass(Consumer.class);
                when(eventService.subscribeToEvents(eq("test.event"), handlerCaptor.capture()))
                                .thenReturn("test-subscription-id");

                // Call the method being tested
                String subscriptionId = eventService.subscribeToEvents("test.event", e -> {
                        // This will be captured
                });

                // Verify subscription was made
                assertEquals("test-subscription-id", subscriptionId);

                // Simulate publishing event and triggering handler
                eventService.publishEvent(event);
                Consumer<DiscoveryEventDTO> handler = handlerCaptor.getValue();
                handler.accept(event);

                // Verify unsubscribe works
                eventService.unsubscribe(subscriptionId);
                verify(eventService).unsubscribe(subscriptionId);
        }

        @Test
        void testCacheOperations() {
                // Test caching
                String key = "test-key";
                String value = "test-value";

                cacheValue(key, value, Duration.ofMinutes(10));

                // Verify cache hit
                assertEquals(value, getCachedValue(key, String.class).orElse(null),
                                "Should retrieve cached value");

                // Test cache invalidation
                invalidateCache(key);

                // Verify cache miss after invalidation
                assertFalse(getCachedValue(key, String.class).isPresent(),
                                "Cache should be invalidated");
        }

        @Test
        void testHealthChecks() {
                // Set up mock behavior
                when(healthCheckService.isServiceHealthy("test-service")).thenReturn(true);

                HealthStatus testStatus = HealthStatus.up("test-service")
                                .withDisplayName("Test Service")
                                .withMessage("Service is healthy")
                                .withDetail("testMetric", 100);

                when(healthCheckService.checkServiceHealth("test-service")).thenReturn(testStatus);

                // Test service health status
                assertTrue(healthCheckService.isServiceHealthy("test-service"),
                                "Test service should be healthy");

                // Test detailed health status
                HealthStatus status = healthCheckService.checkServiceHealth("test-service");
                assertEquals(HealthStatus.Status.UP, status.getStatus(),
                                "Status should be UP");
                assertEquals("Test Service", status.getDisplayName(),
                                "Display name should match");
        }

        @Test
        void testNotificationSystem() {
                // Create a test notification
                NotificationDTO notification = new NotificationDTO();
                notification.setTitle("Test Notification");
                notification.setMessage("This is a test notification");
                notification.setType(NotificationDTO.NotificationType.INFO);

                // Set up mock behavior
                when(notificationService.sendNotification(any(NotificationDTO.class),
                                eq(INotificationService.NotificationChannel.EMAIL))).thenReturn(true);

                // Send notification
                boolean sent = notificationService.sendNotification(notification,
                                INotificationService.NotificationChannel.EMAIL);

                // Verify result
                assertTrue(sent, "Notification should be sent");
                verify(notificationService).sendNotification(eq(notification),
                                eq(INotificationService.NotificationChannel.EMAIL));
        }
}