|
| 1 | +package io.neonbee.endpoint.odatav4.internal.olingo.processor; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertSame; |
| 4 | +import static org.mockito.Mockito.mock; |
| 5 | + |
| 6 | +import java.util.ArrayDeque; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.Deque; |
| 9 | +import java.util.List; |
| 10 | +import java.util.stream.Stream; |
| 11 | + |
| 12 | +import org.apache.olingo.server.api.OData; |
| 13 | +import org.apache.olingo.server.api.ServiceMetadata; |
| 14 | +import org.junit.jupiter.api.BeforeEach; |
| 15 | +import org.junit.jupiter.params.ParameterizedTest; |
| 16 | +import org.junit.jupiter.params.provider.MethodSource; |
| 17 | + |
| 18 | +import io.vertx.core.Context; |
| 19 | +import io.vertx.core.Future; |
| 20 | +import io.vertx.core.Promise; |
| 21 | +import io.vertx.core.Vertx; |
| 22 | +import io.vertx.ext.web.RoutingContext; |
| 23 | + |
| 24 | +class AsynchronousProcessorTest { |
| 25 | + |
| 26 | + private Context context; |
| 27 | + |
| 28 | + @BeforeEach |
| 29 | + void setUp() { |
| 30 | + // Initialize Vertx instance and Context once |
| 31 | + Vertx vertx = Vertx.vertx(); |
| 32 | + context = vertx.getOrCreateContext(); |
| 33 | + |
| 34 | + // Mock dependencies |
| 35 | + Promise<Void> processPromiseMock = Promise.promise(); |
| 36 | + |
| 37 | + // Set up a real processing stack in the context |
| 38 | + Deque<List<Future<Void>>> processingStack = new ArrayDeque<>(); |
| 39 | + processingStack.add(new ArrayList<>()); |
| 40 | + processingStack.peek().add(processPromiseMock.future()); // Add the future to the stack |
| 41 | + |
| 42 | + context.put("processingStack", processingStack); |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + // Parameterized test method to verify the processor initialization |
| 47 | + @ParameterizedTest(name = "{index}: {0}") |
| 48 | + @MethodSource("processors") |
| 49 | + void testInitResetsSubProcessPromise(AsynchronousProcessor processor) { |
| 50 | + context.runOnContext(v -> { |
| 51 | + // Ensure getProcessPromise returns the same instance before and after init |
| 52 | + Promise<Void> firstProcessPromise = processor.getProcessPromise(); |
| 53 | + assertSame(firstProcessPromise, processor.getProcessPromise(), |
| 54 | + "The two promises should be the same instance."); |
| 55 | + |
| 56 | + // Mock OData and ServiceMetadata |
| 57 | + OData odataMock = mock(OData.class); |
| 58 | + ServiceMetadata serviceMetadataMock = mock(ServiceMetadata.class); |
| 59 | + |
| 60 | + // Call init to reset subProcessPromise |
| 61 | + processor.init(odataMock, serviceMetadataMock); |
| 62 | + assertSame(firstProcessPromise, processor.getProcessPromise(), |
| 63 | + "The two promises should be the same instance."); |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + // Method to provide processors for parameterized test |
| 68 | + private static Stream<AsynchronousProcessor> processors() { |
| 69 | + // Return a stream of different processor types for testing |
| 70 | + return Stream.of( |
| 71 | + new BatchProcessor(Vertx.vertx(), mock(RoutingContext.class), Promise.promise()), |
| 72 | + new CountEntityCollectionProcessor(Vertx.vertx(), mock(RoutingContext.class), Promise.promise()), |
| 73 | + new EntityProcessor(Vertx.vertx(), mock(RoutingContext.class), Promise.promise()), |
| 74 | + new PrimitiveProcessor(Vertx.vertx(), mock(RoutingContext.class), Promise.promise())); |
| 75 | + } |
| 76 | +} |
0 commit comments