|
| 1 | +package org.hypertrace.core.grpcutils.client.rx; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertIterableEquals; |
| 5 | +import static org.mockito.Mockito.times; |
| 6 | +import static org.mockito.Mockito.verify; |
| 7 | +import static org.mockito.Mockito.verifyNoInteractions; |
| 8 | +import static org.mockito.Mockito.when; |
| 9 | + |
| 10 | +import io.reactivex.rxjava3.core.Completable; |
| 11 | +import io.reactivex.rxjava3.core.Observable; |
| 12 | +import io.reactivex.rxjava3.core.Single; |
| 13 | +import io.reactivex.rxjava3.observers.TestObserver; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Optional; |
| 16 | +import org.hypertrace.core.grpcutils.context.RequestContext; |
| 17 | +import org.junit.jupiter.api.BeforeEach; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 20 | +import org.mockito.Mock; |
| 21 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 22 | + |
| 23 | +@ExtendWith(MockitoExtension.class) |
| 24 | +class DefaultGrpcRxExecutionContextTest { |
| 25 | + |
| 26 | + private static final Optional<String> TEST_TENANT_ID_OPTIONAL = Optional.of("test-tenant-id"); |
| 27 | + @Mock RequestContext mockContext; |
| 28 | + |
| 29 | + @BeforeEach |
| 30 | + void beforeEach() { |
| 31 | + when(mockContext.getTenantId()).thenReturn(TEST_TENANT_ID_OPTIONAL); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + void canRunInContext() { |
| 36 | + Completable completable = |
| 37 | + new DefaultGrpcRxExecutionContext(this.mockContext) |
| 38 | + .run(() -> RequestContext.CURRENT.get().getTenantId()); |
| 39 | + verifyNoInteractions(this.mockContext); |
| 40 | + completable.subscribe(); |
| 41 | + verify(this.mockContext, times(1)).getTenantId(); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void canCallInContext() { |
| 46 | + Single<?> single = |
| 47 | + new DefaultGrpcRxExecutionContext(this.mockContext) |
| 48 | + .call(() -> RequestContext.CURRENT.get().getTenantId()); |
| 49 | + verifyNoInteractions(this.mockContext); |
| 50 | + assertEquals(TEST_TENANT_ID_OPTIONAL, single.blockingGet()); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void canStreamInContext() { |
| 55 | + Observable<Optional<String>> observable = |
| 56 | + new DefaultGrpcRxExecutionContext(this.mockContext) |
| 57 | + .stream( |
| 58 | + observer -> { |
| 59 | + observer.onNext(RequestContext.CURRENT.get().getTenantId()); |
| 60 | + observer.onCompleted(); |
| 61 | + }); |
| 62 | + verifyNoInteractions(this.mockContext); |
| 63 | + assertIterableEquals(List.of(TEST_TENANT_ID_OPTIONAL), observable.blockingIterable()); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void canPropagateErrors() { |
| 68 | + Completable completable = |
| 69 | + new DefaultGrpcRxExecutionContext(this.mockContext) |
| 70 | + .run( |
| 71 | + () -> { |
| 72 | + RequestContext.CURRENT.get().getTenantId(); |
| 73 | + throw new UnsupportedOperationException(); |
| 74 | + }); |
| 75 | + TestObserver<?> testObserver = new TestObserver<>(); |
| 76 | + completable.subscribe(testObserver); |
| 77 | + testObserver.assertError(UnsupportedOperationException.class); |
| 78 | + } |
| 79 | +} |
0 commit comments