|
| 1 | +package com.linkedin.venice.controller.grpc.server; |
| 2 | + |
| 3 | +import static org.mockito.ArgumentMatchers.any; |
| 4 | +import static org.mockito.Mockito.mock; |
| 5 | +import static org.mockito.Mockito.when; |
| 6 | +import static org.testng.Assert.assertEquals; |
| 7 | +import static org.testng.Assert.assertNotNull; |
| 8 | +import static org.testng.Assert.assertTrue; |
| 9 | +import static org.testng.Assert.expectThrows; |
| 10 | + |
| 11 | +import com.linkedin.venice.controller.grpc.GrpcRequestResponseConverter; |
| 12 | +import com.linkedin.venice.controller.server.SchemaRequestHandler; |
| 13 | +import com.linkedin.venice.exceptions.VeniceException; |
| 14 | +import com.linkedin.venice.protocols.controller.ClusterStoreGrpcInfo; |
| 15 | +import com.linkedin.venice.protocols.controller.ControllerGrpcErrorType; |
| 16 | +import com.linkedin.venice.protocols.controller.GetValueSchemaGrpcRequest; |
| 17 | +import com.linkedin.venice.protocols.controller.GetValueSchemaGrpcResponse; |
| 18 | +import com.linkedin.venice.protocols.controller.SchemaGrpcServiceGrpc; |
| 19 | +import com.linkedin.venice.protocols.controller.SchemaGrpcServiceGrpc.SchemaGrpcServiceBlockingStub; |
| 20 | +import com.linkedin.venice.protocols.controller.VeniceControllerGrpcErrorInfo; |
| 21 | +import io.grpc.ManagedChannel; |
| 22 | +import io.grpc.Server; |
| 23 | +import io.grpc.Status; |
| 24 | +import io.grpc.StatusRuntimeException; |
| 25 | +import io.grpc.inprocess.InProcessChannelBuilder; |
| 26 | +import io.grpc.inprocess.InProcessServerBuilder; |
| 27 | +import org.testng.annotations.AfterMethod; |
| 28 | +import org.testng.annotations.BeforeMethod; |
| 29 | +import org.testng.annotations.Test; |
| 30 | + |
| 31 | + |
| 32 | +public class SchemaGrpcServiceImplTest { |
| 33 | + private static final String TEST_CLUSTER = "test-cluster"; |
| 34 | + private static final String TEST_STORE = "test-store"; |
| 35 | + private static final String VALUE_SCHEMA = "string"; |
| 36 | + |
| 37 | + private Server grpcServer; |
| 38 | + private ManagedChannel grpcChannel; |
| 39 | + private SchemaRequestHandler schemaRequestHandler; |
| 40 | + private SchemaGrpcServiceBlockingStub blockingStub; |
| 41 | + |
| 42 | + @BeforeMethod |
| 43 | + public void setUp() throws Exception { |
| 44 | + schemaRequestHandler = mock(SchemaRequestHandler.class); |
| 45 | + |
| 46 | + // Create a unique server name for the in-process server |
| 47 | + String serverName = InProcessServerBuilder.generateName(); |
| 48 | + |
| 49 | + // Start the gRPC server in-process |
| 50 | + grpcServer = InProcessServerBuilder.forName(serverName) |
| 51 | + .directExecutor() |
| 52 | + .addService(new SchemaGrpcServiceImpl(schemaRequestHandler)) |
| 53 | + .build() |
| 54 | + .start(); |
| 55 | + |
| 56 | + // Create a channel to communicate with the server |
| 57 | + grpcChannel = InProcessChannelBuilder.forName(serverName).directExecutor().build(); |
| 58 | + |
| 59 | + // Create a blocking stub to make calls to the server |
| 60 | + blockingStub = SchemaGrpcServiceGrpc.newBlockingStub(grpcChannel); |
| 61 | + } |
| 62 | + |
| 63 | + @AfterMethod |
| 64 | + public void tearDown() throws Exception { |
| 65 | + if (grpcServer != null) { |
| 66 | + grpcServer.shutdown(); |
| 67 | + } |
| 68 | + if (grpcChannel != null) { |
| 69 | + grpcChannel.shutdown(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testGetValueSchemaReturnsSuccessfulResponse() { |
| 75 | + ClusterStoreGrpcInfo storeInfo = |
| 76 | + ClusterStoreGrpcInfo.newBuilder().setClusterName(TEST_CLUSTER).setStoreName(TEST_STORE).build(); |
| 77 | + GetValueSchemaGrpcRequest request = |
| 78 | + GetValueSchemaGrpcRequest.newBuilder().setStoreInfo(storeInfo).setSchemaId(1).build(); |
| 79 | + GetValueSchemaGrpcResponse expectedResponse = GetValueSchemaGrpcResponse.newBuilder() |
| 80 | + .setStoreInfo(storeInfo) |
| 81 | + .setSchemaId(1) |
| 82 | + .setSchemaStr(VALUE_SCHEMA) |
| 83 | + .build(); |
| 84 | + when(schemaRequestHandler.getValueSchema(any(GetValueSchemaGrpcRequest.class))).thenReturn(expectedResponse); |
| 85 | + |
| 86 | + GetValueSchemaGrpcResponse actualResponse = blockingStub.getValueSchema(request); |
| 87 | + |
| 88 | + assertNotNull(actualResponse, "Response should not be null"); |
| 89 | + assertEquals(actualResponse.getStoreInfo().getClusterName(), TEST_CLUSTER, "Cluster name should match"); |
| 90 | + assertEquals(actualResponse.getStoreInfo().getStoreName(), TEST_STORE, "Store name should match"); |
| 91 | + assertEquals(actualResponse.getSchemaId(), 1, "Schema ID should match"); |
| 92 | + assertEquals(actualResponse.getSchemaStr(), VALUE_SCHEMA, "Schema string should match"); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testGetValueSchemaReturnsErrorWhenSchemaNotFound() { |
| 97 | + ClusterStoreGrpcInfo storeInfo = |
| 98 | + ClusterStoreGrpcInfo.newBuilder().setClusterName(TEST_CLUSTER).setStoreName(TEST_STORE).build(); |
| 99 | + GetValueSchemaGrpcRequest request = |
| 100 | + GetValueSchemaGrpcRequest.newBuilder().setStoreInfo(storeInfo).setSchemaId(99).build(); |
| 101 | + when(schemaRequestHandler.getValueSchema(any(GetValueSchemaGrpcRequest.class))) |
| 102 | + .thenThrow(new IllegalArgumentException("Value schema for schema id: 99 of store: test-store doesn't exist")); |
| 103 | + |
| 104 | + StatusRuntimeException e = expectThrows(StatusRuntimeException.class, () -> blockingStub.getValueSchema(request)); |
| 105 | + |
| 106 | + assertNotNull(e.getStatus(), "Status should not be null"); |
| 107 | + assertEquals(e.getStatus().getCode(), Status.INVALID_ARGUMENT.getCode()); |
| 108 | + VeniceControllerGrpcErrorInfo errorInfo = GrpcRequestResponseConverter.parseControllerGrpcError(e); |
| 109 | + assertNotNull(errorInfo, "Error info should not be null"); |
| 110 | + assertEquals(errorInfo.getErrorType(), ControllerGrpcErrorType.BAD_REQUEST); |
| 111 | + assertTrue(errorInfo.getErrorMessage().contains("Value schema for schema id: 99")); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void testGetValueSchemaReturnsErrorForInvalidInput() { |
| 116 | + ClusterStoreGrpcInfo storeInfo = |
| 117 | + ClusterStoreGrpcInfo.newBuilder().setClusterName("").setStoreName(TEST_STORE).build(); |
| 118 | + GetValueSchemaGrpcRequest request = |
| 119 | + GetValueSchemaGrpcRequest.newBuilder().setStoreInfo(storeInfo).setSchemaId(1).build(); |
| 120 | + when(schemaRequestHandler.getValueSchema(any(GetValueSchemaGrpcRequest.class))) |
| 121 | + .thenThrow(new IllegalArgumentException("Cluster name is mandatory parameter")); |
| 122 | + |
| 123 | + StatusRuntimeException e = expectThrows(StatusRuntimeException.class, () -> blockingStub.getValueSchema(request)); |
| 124 | + |
| 125 | + assertNotNull(e.getStatus(), "Status should not be null"); |
| 126 | + assertEquals(e.getStatus().getCode(), Status.INVALID_ARGUMENT.getCode()); |
| 127 | + VeniceControllerGrpcErrorInfo errorInfo = GrpcRequestResponseConverter.parseControllerGrpcError(e); |
| 128 | + assertNotNull(errorInfo, "Error info should not be null"); |
| 129 | + assertEquals(errorInfo.getErrorType(), ControllerGrpcErrorType.BAD_REQUEST); |
| 130 | + assertTrue(errorInfo.getErrorMessage().contains("Cluster name is mandatory parameter")); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void testGetValueSchemaReturnsGeneralError() { |
| 135 | + ClusterStoreGrpcInfo storeInfo = |
| 136 | + ClusterStoreGrpcInfo.newBuilder().setClusterName(TEST_CLUSTER).setStoreName(TEST_STORE).build(); |
| 137 | + GetValueSchemaGrpcRequest request = |
| 138 | + GetValueSchemaGrpcRequest.newBuilder().setStoreInfo(storeInfo).setSchemaId(1).build(); |
| 139 | + when(schemaRequestHandler.getValueSchema(any(GetValueSchemaGrpcRequest.class))) |
| 140 | + .thenThrow(new VeniceException("Internal error fetching schema")); |
| 141 | + |
| 142 | + StatusRuntimeException e = expectThrows(StatusRuntimeException.class, () -> blockingStub.getValueSchema(request)); |
| 143 | + |
| 144 | + assertNotNull(e.getStatus(), "Status should not be null"); |
| 145 | + assertEquals(e.getStatus().getCode(), Status.INTERNAL.getCode()); |
| 146 | + VeniceControllerGrpcErrorInfo errorInfo = GrpcRequestResponseConverter.parseControllerGrpcError(e); |
| 147 | + assertNotNull(errorInfo, "Error info should not be null"); |
| 148 | + assertEquals(errorInfo.getErrorType(), ControllerGrpcErrorType.GENERAL_ERROR); |
| 149 | + assertTrue(errorInfo.getErrorMessage().contains("Internal error fetching schema")); |
| 150 | + } |
| 151 | +} |
0 commit comments