|
| 1 | +package s2.services; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | +import static org.mockito.Mockito.*; |
| 5 | + |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 8 | +import org.mockito.Mock; |
| 9 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 10 | + |
| 11 | +import io.grpc.ManagedChannel; |
| 12 | +import io.grpc.CallCredentials; |
| 13 | +import s2.auth.BearerTokenCallCredentials; |
| 14 | + |
| 15 | +@ExtendWith(MockitoExtension.class) |
| 16 | +public class ClientBuilderTest { |
| 17 | + @Mock |
| 18 | + private ManagedChannel mockChannel; |
| 19 | + |
| 20 | + private static final String TEST_HOST = "test.s2.dev"; |
| 21 | + private static final String TEST_TOKEN = "test-token"; |
| 22 | + private static final String TEST_BASIN = "test-basin"; |
| 23 | + |
| 24 | + @Test |
| 25 | + void builderShouldCreateClientWithMinimalParameters() { |
| 26 | + Client client = Client.newBuilder().host(TEST_HOST).bearerToken(TEST_TOKEN).build(); |
| 27 | + |
| 28 | + assertNotNull(client); |
| 29 | + assertNotNull(client.account()); |
| 30 | + assertNotNull(client.basin()); |
| 31 | + assertNotNull(client.stream()); |
| 32 | + assertNotNull(client.streamAsync()); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + void builderShouldCreateClientWithInjectedChannel() { |
| 37 | + CallCredentials credentials = new BearerTokenCallCredentials(TEST_TOKEN); |
| 38 | + Client client = Client.newBuilder().channel(mockChannel).credentials(credentials).build(); |
| 39 | + |
| 40 | + assertNotNull(client); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void builderShouldThrowWhenMissingRequiredParameters() { |
| 45 | + ClientBuilder builder = Client.newBuilder(); |
| 46 | + |
| 47 | + assertThrows(IllegalStateException.class, builder::build, |
| 48 | + "Should throw when no parameters are set"); |
| 49 | + |
| 50 | + assertThrows(IllegalStateException.class, () -> builder.host(TEST_HOST).build(), |
| 51 | + "Should throw when bearer token is missing"); |
| 52 | + |
| 53 | + ClientBuilder tokenBuilder = Client.newBuilder(); |
| 54 | + assertThrows(IllegalStateException.class, () -> tokenBuilder.bearerToken(TEST_TOKEN).build(), |
| 55 | + "Should throw when host is missing"); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void useBasinShouldThrowWithInjectedChannel() { |
| 60 | + CallCredentials credentials = new BearerTokenCallCredentials(TEST_TOKEN); |
| 61 | + Client client = Client.newBuilder().channel(mockChannel).credentials(credentials).build(); |
| 62 | + |
| 63 | + assertThrows(UnsupportedOperationException.class, () -> client.useBasin(TEST_BASIN), |
| 64 | + "Should throw when using injected channel"); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void useBasinShouldWorkWithManagedChannel() { |
| 69 | + Client client = Client.newBuilder().host(TEST_HOST).bearerToken(TEST_TOKEN).build(); |
| 70 | + |
| 71 | + // This should not throw |
| 72 | + assertDoesNotThrow(() -> client.useBasin(TEST_BASIN)); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void closeShouldWorkWithManagedChannel() { |
| 77 | + Client client = Client.newBuilder().host(TEST_HOST).bearerToken(TEST_TOKEN).build(); |
| 78 | + |
| 79 | + // This should not throw |
| 80 | + assertDoesNotThrow(client::close); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void closeShouldNotAffectInjectedChannel() { |
| 85 | + CallCredentials credentials = new BearerTokenCallCredentials(TEST_TOKEN); |
| 86 | + Client client = Client.newBuilder().channel(mockChannel).credentials(credentials).build(); |
| 87 | + |
| 88 | + client.close(); |
| 89 | + |
| 90 | + // Verify channel was not shut down |
| 91 | + verify(mockChannel, never()).shutdown(); |
| 92 | + } |
| 93 | +} |
0 commit comments