|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.dataprepper.plugins.ml.processor.common; |
| 7 | + |
| 8 | +import io.micrometer.core.instrument.Counter; |
| 9 | +import org.junit.jupiter.api.BeforeEach; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import org.mockito.Mock; |
| 12 | +import org.mockito.MockitoAnnotations; |
| 13 | +import org.opensearch.dataprepper.aws.api.AwsCredentialsSupplier; |
| 14 | +import org.opensearch.dataprepper.metrics.PluginMetrics; |
| 15 | +import org.opensearch.dataprepper.model.event.Event; |
| 16 | +import org.opensearch.dataprepper.model.record.Record; |
| 17 | +import org.opensearch.dataprepper.plugins.ml.processor.MLProcessorConfig; |
| 18 | +import org.opensearch.dataprepper.plugins.ml.processor.util.RetryUtil; |
| 19 | + |
| 20 | +import java.util.Arrays; |
| 21 | + |
| 22 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 23 | +import static org.mockito.Mockito.*; |
| 24 | +import static org.opensearch.dataprepper.plugins.ml.processor.common.AbstractBatchJobCreator.*; |
| 25 | + |
| 26 | +public class BedrockBatchJobCreatorTest { |
| 27 | + @Mock |
| 28 | + private MLProcessorConfig mlProcessorConfig; |
| 29 | + |
| 30 | + @Mock |
| 31 | + private AwsCredentialsSupplier awsCredentialsSupplier; |
| 32 | + |
| 33 | + @Mock |
| 34 | + private PluginMetrics pluginMetrics; |
| 35 | + |
| 36 | + private BedrockBatchJobCreator bedrockBatchJobCreator; |
| 37 | + private Counter counter;; |
| 38 | + |
| 39 | + @BeforeEach |
| 40 | + void setUp() { |
| 41 | + MockitoAnnotations.openMocks(this); |
| 42 | + when(mlProcessorConfig.getOutputPath()).thenReturn("s3://offlinebatch/output"); |
| 43 | + counter = new Counter() { |
| 44 | + @Override |
| 45 | + public void increment(double v) {} |
| 46 | + |
| 47 | + @Override |
| 48 | + public double count() { |
| 49 | + return 0; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public Id getId() { |
| 54 | + return null; |
| 55 | + } |
| 56 | + }; |
| 57 | + when(pluginMetrics.counter(NUMBER_OF_SUCCESSFUL_BATCH_JOBS_CREATION)).thenReturn(counter); |
| 58 | + when(pluginMetrics.counter(NUMBER_OF_FAILED_BATCH_JOBS_CREATION)).thenReturn(counter); |
| 59 | + bedrockBatchJobCreator = spy(new BedrockBatchJobCreator(mlProcessorConfig, awsCredentialsSupplier, pluginMetrics)); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void testCreateMLBatchJob_Success() { |
| 64 | + Event event = mock(Event.class); |
| 65 | + Record<Event> record = new Record<>(event); |
| 66 | + |
| 67 | + when(event.getJsonNode()).thenReturn(OBJECT_MAPPER.createObjectNode() |
| 68 | + .put("bucket", "test-bucket") |
| 69 | + .put("key", "input.jsonl")); |
| 70 | + |
| 71 | + mockStatic(RetryUtil.class); |
| 72 | + when(RetryUtil.retryWithBackoff(any())).thenReturn(true); |
| 73 | + |
| 74 | + bedrockBatchJobCreator.createMLBatchJob(Arrays.asList(record)); |
| 75 | + |
| 76 | + verify(bedrockBatchJobCreator, times(1)).incrementSuccessCounter(); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void testCreateMLBatchJob_Failure() { |
| 81 | + Event event = mock(Event.class); |
| 82 | + Record<Event> record = new Record<>(event); |
| 83 | + |
| 84 | + when(event.getJsonNode()).thenReturn(OBJECT_MAPPER.createObjectNode() |
| 85 | + .put("bucket", "test-bucket") |
| 86 | + .put("key", "input.jsonl")); |
| 87 | + |
| 88 | + mockStatic(RetryUtil.class); |
| 89 | + when(RetryUtil.retryWithBackoff(any())).thenReturn(false); |
| 90 | + |
| 91 | + bedrockBatchJobCreator.createMLBatchJob(Arrays.asList(record)); |
| 92 | + |
| 93 | + verify(bedrockBatchJobCreator, times(1)).incrementFailureCounter(); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void testInterruptedExceptionHandling() throws InterruptedException { |
| 98 | + Event event = mock(Event.class); |
| 99 | + Record<Event> record = new Record<>(event); |
| 100 | + |
| 101 | + when(event.getJsonNode()).thenReturn(OBJECT_MAPPER.createObjectNode() |
| 102 | + .put("bucket", "test-bucket") |
| 103 | + .put("key", "input.jsonl")); |
| 104 | + |
| 105 | + mockStatic(RetryUtil.class); |
| 106 | + when(RetryUtil.retryWithBackoff(any())).thenReturn(true); |
| 107 | + |
| 108 | + Thread.currentThread().interrupt(); |
| 109 | + bedrockBatchJobCreator.createMLBatchJob(Arrays.asList(record)); |
| 110 | + |
| 111 | + assertTrue(Thread.interrupted()); // Ensure interrupted flag is reset |
| 112 | + } |
| 113 | +} |
0 commit comments