|
9 | 9 |
|
10 | 10 | package org.opensearch.dataprepper.plugins.source.microsoft_office365; |
11 | 11 |
|
| 12 | +import io.micrometer.core.instrument.Counter; |
| 13 | +import io.micrometer.core.instrument.DistributionSummary; |
| 14 | +import io.micrometer.core.instrument.Timer; |
12 | 15 | import org.junit.jupiter.api.BeforeEach; |
13 | 16 | import org.junit.jupiter.api.Test; |
14 | 17 | import org.junit.jupiter.api.extension.ExtendWith; |
15 | | -import io.micrometer.core.instrument.Counter; |
| 18 | + |
16 | 19 | import org.mockito.ArgumentCaptor; |
17 | 20 | import org.mockito.Mock; |
18 | 21 | import org.mockito.junit.jupiter.MockitoExtension; |
@@ -347,4 +350,147 @@ void testGetAuditLogFailureCounterIncrementsOnEachRetry() throws Exception { |
347 | 350 | // Verify counter.increment() was called exactly 6 times (once for each retry attempt) |
348 | 351 | verify(mockCounter, times(6)).increment(); |
349 | 352 | } |
| 353 | + |
| 354 | + @Test |
| 355 | + void testMetricsInitialization() { |
| 356 | + // Test metrics initialization during construction. This approach is used for metrics that are called |
| 357 | + // inside RetryHandler.executeWithRetry() static method calls, which would require complex static mocking |
| 358 | + // to test for invocation. Testing initialization ensures the metrics infrastructure is properly set up. |
| 359 | + |
| 360 | + // Mock all required timers and counters for Office365RestClient constructor |
| 361 | + PluginMetrics mockPluginMetrics = org.mockito.Mockito.mock(PluginMetrics.class); |
| 362 | + Timer mockAuditLogFetchLatencyTimer = org.mockito.Mockito.mock(Timer.class); |
| 363 | + Timer mockSearchCallLatencyTimer = org.mockito.Mockito.mock(Timer.class); |
| 364 | + Counter mockAuditLogsRequestedCounter = org.mockito.Mockito.mock(Counter.class); |
| 365 | + Counter mockAuditLogRequestsFailedCounter = org.mockito.Mockito.mock(Counter.class); |
| 366 | + Counter mockAuditLogRequestsSuccessCounter = org.mockito.Mockito.mock(Counter.class); |
| 367 | + Counter mockSearchRequestsFailedCounter = org.mockito.Mockito.mock(Counter.class); |
| 368 | + Counter mockSearchRequestsSuccessCounter = org.mockito.Mockito.mock(Counter.class); |
| 369 | + DistributionSummary mockAuditLogRequestSizeSummary = org.mockito.Mockito.mock(DistributionSummary.class); |
| 370 | + DistributionSummary mockSearchRequestSizeSummary = org.mockito.Mockito.mock(DistributionSummary.class); |
| 371 | + |
| 372 | + when(mockPluginMetrics.timer("auditLogFetchLatency")).thenReturn(mockAuditLogFetchLatencyTimer); |
| 373 | + when(mockPluginMetrics.timer("searchCallLatency")).thenReturn(mockSearchCallLatencyTimer); |
| 374 | + when(mockPluginMetrics.counter("auditLogsRequested")).thenReturn(mockAuditLogsRequestedCounter); |
| 375 | + when(mockPluginMetrics.counter("auditLogRequestsFailed")).thenReturn(mockAuditLogRequestsFailedCounter); |
| 376 | + when(mockPluginMetrics.counter("auditLogRequestsSuccess")).thenReturn(mockAuditLogRequestsSuccessCounter); |
| 377 | + when(mockPluginMetrics.counter("searchRequestsFailed")).thenReturn(mockSearchRequestsFailedCounter); |
| 378 | + when(mockPluginMetrics.counter("searchRequestsSuccess")).thenReturn(mockSearchRequestsSuccessCounter); |
| 379 | + when(mockPluginMetrics.summary("auditLogResponseSizeBytes")).thenReturn(mockAuditLogRequestSizeSummary); |
| 380 | + when(mockPluginMetrics.summary("searchResponseSizeBytes")).thenReturn(mockSearchRequestSizeSummary); |
| 381 | + |
| 382 | + // Create Office365RestClient with mocked metrics |
| 383 | + Office365RestClient testClient = new Office365RestClient(authConfig, mockPluginMetrics); |
| 384 | + |
| 385 | + // Verify all metrics were requested during construction |
| 386 | + verify(mockPluginMetrics).timer("auditLogFetchLatency"); |
| 387 | + verify(mockPluginMetrics).timer("searchCallLatency"); |
| 388 | + verify(mockPluginMetrics).counter("auditLogsRequested"); |
| 389 | + verify(mockPluginMetrics).counter("auditLogRequestsFailed"); |
| 390 | + verify(mockPluginMetrics).counter("auditLogRequestsSuccess"); |
| 391 | + verify(mockPluginMetrics).counter("searchRequestsFailed"); |
| 392 | + verify(mockPluginMetrics).counter("searchRequestsSuccess"); |
| 393 | + verify(mockPluginMetrics).summary("auditLogResponseSizeBytes"); |
| 394 | + verify(mockPluginMetrics).summary("searchResponseSizeBytes"); |
| 395 | + } |
| 396 | + |
| 397 | + @Test |
| 398 | + void testGetAuditLogMetricsInvocation() throws NoSuchFieldException, IllegalAccessException { |
| 399 | + // Test metrics for getAuditLog() method - both success and failure scenarios |
| 400 | + |
| 401 | + // Create mock metrics and inject them |
| 402 | + Counter mockAuditLogsRequestedCounter = org.mockito.Mockito.mock(Counter.class); |
| 403 | + Counter mockAuditLogRequestsFailedCounter = org.mockito.Mockito.mock(Counter.class); |
| 404 | + Timer mockAuditLogFetchLatencyTimer = org.mockito.Mockito.mock(Timer.class); |
| 405 | + DistributionSummary mockAuditLogRequestSizeSummary = org.mockito.Mockito.mock(DistributionSummary.class); |
| 406 | + |
| 407 | + ReflectivelySetField.setField(Office365RestClient.class, office365RestClient, "auditLogsRequestedCounter", mockAuditLogsRequestedCounter); |
| 408 | + ReflectivelySetField.setField(Office365RestClient.class, office365RestClient, "auditLogRequestsFailedCounter", mockAuditLogRequestsFailedCounter); |
| 409 | + ReflectivelySetField.setField(Office365RestClient.class, office365RestClient, "auditLogFetchLatencyTimer", mockAuditLogFetchLatencyTimer); |
| 410 | + ReflectivelySetField.setField(Office365RestClient.class, office365RestClient, "auditLogResponseSizeSummary", mockAuditLogRequestSizeSummary); |
| 411 | + |
| 412 | + // Mock timer.record() to execute the lambda |
| 413 | + when(mockAuditLogFetchLatencyTimer.record(any(java.util.function.Supplier.class))).thenAnswer(invocation -> { |
| 414 | + java.util.function.Supplier<?> supplier = invocation.getArgument(0); |
| 415 | + return supplier.get(); |
| 416 | + }); |
| 417 | + |
| 418 | + String contentUri = "https://manage.office.com/api/v1.0/test-tenant/activity/feed/audit/123"; |
| 419 | + |
| 420 | + // Test success scenario |
| 421 | + String mockAuditLog = "{\"id\":\"123\",\"contentType\":\"Audit.AzureActiveDirectory\"}"; |
| 422 | + ResponseEntity<String> mockResponse = new ResponseEntity<>(mockAuditLog, HttpStatus.OK); |
| 423 | + when(restTemplate.exchange(eq(contentUri), eq(HttpMethod.GET), any(), eq(String.class))) |
| 424 | + .thenReturn(mockResponse); |
| 425 | + |
| 426 | + office365RestClient.getAuditLog(contentUri); |
| 427 | + |
| 428 | + // Verify success metrics |
| 429 | + verify(mockAuditLogsRequestedCounter).increment(); // Called directly before RetryHandler |
| 430 | + verify(mockAuditLogFetchLatencyTimer).record(any(java.util.function.Supplier.class)); // Timer wrapper |
| 431 | + verify(mockAuditLogRequestSizeSummary).record(mockAuditLog.getBytes(java.nio.charset.StandardCharsets.UTF_8).length); // Size metric inside RetryHandler |
| 432 | + |
| 433 | + // Test failure scenario |
| 434 | + when(restTemplate.exchange(eq(contentUri), eq(HttpMethod.GET), any(), eq(String.class))) |
| 435 | + .thenThrow(new HttpClientErrorException(HttpStatus.INTERNAL_SERVER_ERROR)); |
| 436 | + |
| 437 | + assertThrows(RuntimeException.class, () -> office365RestClient.getAuditLog(contentUri)); |
| 438 | + |
| 439 | + // Verify failure metrics |
| 440 | + verify(mockAuditLogsRequestedCounter, times(2)).increment(); // Called again before retry |
| 441 | + verify(mockAuditLogRequestsFailedCounter, times(6)).increment(); // Called 6 times (once for each retry attempt) |
| 442 | + } |
| 443 | + |
| 444 | + @Test |
| 445 | + void testSearchAuditLogsMetricsInvocation() throws NoSuchFieldException, IllegalAccessException { |
| 446 | + // Test metrics for searchAuditLogs() method - both success and failure scenarios |
| 447 | + |
| 448 | + // Create mock metrics and inject them |
| 449 | + Counter mockSearchRequestsFailedCounter = org.mockito.Mockito.mock(Counter.class); |
| 450 | + Timer mockSearchCallLatencyTimer = org.mockito.Mockito.mock(Timer.class); |
| 451 | + DistributionSummary mockSearchRequestSizeSummary = org.mockito.Mockito.mock(DistributionSummary.class); |
| 452 | + |
| 453 | + ReflectivelySetField.setField(Office365RestClient.class, office365RestClient, "searchRequestsFailedCounter", mockSearchRequestsFailedCounter); |
| 454 | + ReflectivelySetField.setField(Office365RestClient.class, office365RestClient, "searchCallLatencyTimer", mockSearchCallLatencyTimer); |
| 455 | + ReflectivelySetField.setField(Office365RestClient.class, office365RestClient, "searchResponseSizeSummary", mockSearchRequestSizeSummary); |
| 456 | + |
| 457 | + // Mock timer.record() to execute the lambda |
| 458 | + when(mockSearchCallLatencyTimer.record(any(java.util.function.Supplier.class))).thenAnswer(invocation -> { |
| 459 | + java.util.function.Supplier<?> supplier = invocation.getArgument(0); |
| 460 | + return supplier.get(); |
| 461 | + }); |
| 462 | + |
| 463 | + // Test success scenario |
| 464 | + List<Map<String, Object>> mockResults = Collections.singletonList(new HashMap<>()); |
| 465 | + HttpHeaders responseHeaders = new HttpHeaders(); |
| 466 | + responseHeaders.setContentLength(1024L); // Mock content length |
| 467 | + ResponseEntity<List<Map<String, Object>>> mockResponse = new ResponseEntity<>(mockResults, responseHeaders, HttpStatus.OK); |
| 468 | + when(restTemplate.exchange(anyString(), eq(HttpMethod.GET), any(), any(ParameterizedTypeReference.class))) |
| 469 | + .thenReturn(mockResponse); |
| 470 | + |
| 471 | + office365RestClient.searchAuditLogs( |
| 472 | + "Audit.AzureActiveDirectory", |
| 473 | + Instant.now().minus(1, ChronoUnit.HOURS), |
| 474 | + Instant.now(), |
| 475 | + null |
| 476 | + ); |
| 477 | + |
| 478 | + // Verify success metrics |
| 479 | + verify(mockSearchCallLatencyTimer).record(any(java.util.function.Supplier.class)); // Timer wrapper |
| 480 | + verify(mockSearchRequestSizeSummary).record(1024L); // Size metric inside RetryHandler |
| 481 | + |
| 482 | + // Test failure scenario |
| 483 | + when(restTemplate.exchange(anyString(), eq(HttpMethod.GET), any(), any(ParameterizedTypeReference.class))) |
| 484 | + .thenThrow(new HttpClientErrorException(HttpStatus.INTERNAL_SERVER_ERROR)); |
| 485 | + |
| 486 | + assertThrows(RuntimeException.class, () -> office365RestClient.searchAuditLogs( |
| 487 | + "Audit.AzureActiveDirectory", |
| 488 | + Instant.now().minus(1, ChronoUnit.HOURS), |
| 489 | + Instant.now(), |
| 490 | + null |
| 491 | + )); |
| 492 | + |
| 493 | + // Verify failure metrics |
| 494 | + verify(mockSearchRequestsFailedCounter, times(6)).increment(); // Called 6 times (once for each retry attempt) |
| 495 | + } |
350 | 496 | } |
0 commit comments