Skip to content

Commit

Permalink
Changes to allow unit testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
kannanjgithub committed May 24, 2024
1 parent a002bc2 commit b2bb9e1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.grpc.protobuf.services;

import com.google.common.annotations.VisibleForTesting;
import com.google.protobuf.InvalidProtocolBufferException;
import io.grpc.BindableService;
import io.grpc.ExperimentalApi;
import io.grpc.protobuf.services.util.ReflectionServiceProtoAdapter;
Expand All @@ -18,14 +20,20 @@
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/2222")
public class ProtoReflectionService extends ServerReflectionGrpc.ServerReflectionImplBase {
private final ProtoReflectionServiceV1 protoReflectionServiceV1 = (ProtoReflectionServiceV1) ProtoReflectionServiceV1.newInstance();
private ProtoReflectionServiceV1 protoReflectionServiceV1 = (ProtoReflectionServiceV1) ProtoReflectionServiceV1.newInstance();
/**
* Creates a instance of {@link ProtoReflectionServiceV1}.
*/
public static BindableService newInstance() {
return new ProtoReflectionService();
}

private ProtoReflectionService() {}

@VisibleForTesting
void setProtoReflectionServiceV1(ProtoReflectionServiceV1 protoReflectionServiceV1) {
this.protoReflectionServiceV1 = protoReflectionServiceV1;
}
@Override
public StreamObserver<ServerReflectionRequest> serverReflectionInfo(
final StreamObserver<ServerReflectionResponse> responseObserver) {
Expand All @@ -34,8 +42,13 @@ public StreamObserver<ServerReflectionRequest> serverReflectionInfo(
@Override
public void onNext(
io.grpc.reflection.v1.ServerReflectionResponse serverReflectionResponse) {
responseObserver.onNext(
ReflectionServiceProtoAdapter.toV1AlphaResponse(serverReflectionResponse));
try {
responseObserver.onNext(
ReflectionServiceProtoAdapter.toV1AlphaResponse(serverReflectionResponse));
} catch (InvalidProtocolBufferException e) {
// Should never happen as long as v1 and v1alpha protos have the same fields and ordering.
throw new RuntimeException(e);
}
}

@Override
Expand All @@ -51,7 +64,12 @@ public void onCompleted() {
return new StreamObserver<ServerReflectionRequest>() {
@Override
public void onNext(ServerReflectionRequest serverReflectionRequest) {
v1RequestObserver.onNext(ReflectionServiceProtoAdapter.toV1Request(serverReflectionRequest));
try {
v1RequestObserver.onNext(ReflectionServiceProtoAdapter.toV1Request(serverReflectionRequest));
} catch (InvalidProtocolBufferException e) {
// Should never happen as long as v1 and v1alpha protos have the same fields and ordering.
throw new RuntimeException(e);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package io.grpc.protobuf.services.util;


import com.google.protobuf.InvalidProtocolBufferException;
import io.grpc.reflection.v1.ServerReflectionRequest;
import io.grpc.reflection.v1.ServerReflectionResponse;

public class ReflectionServiceProtoAdapter {
public static ServerReflectionRequest toV1Request(
io.grpc.reflection.v1alpha.ServerReflectionRequest serverReflectionRequest) {
return null;
io.grpc.reflection.v1alpha.ServerReflectionRequest serverReflectionRequest)
throws InvalidProtocolBufferException {
return ServerReflectionRequest.parseFrom(serverReflectionRequest.toByteArray());
}

public static io.grpc.reflection.v1alpha.ServerReflectionResponse toV1AlphaResponse(
ServerReflectionResponse serverReflectionResponse) {
return null;
ServerReflectionResponse serverReflectionResponse) throws InvalidProtocolBufferException {
return io.grpc.reflection.v1alpha.ServerReflectionResponse.parseFrom(serverReflectionResponse.toByteArray());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.grpc.protobuf.services;

import io.grpc.reflection.v1alpha.ServerReflectionRequest;
import org.junit.Before;
import org.junit.Rule;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

@RunWith(JUnit4.class)
public class ProtoReflectionServiceTest {
@Rule
public final MockitoRule mocks = MockitoJUnit.rule();
ProtoReflectionService protoReflectionService = (ProtoReflectionService) ProtoReflectionService.newInstance();
@Mock
ProtoReflectionServiceV1 mockProtoReflectionServiceV1;
private final ServerReflectionRequest serverReflectionRequestV1Alpha = ServerReflectionRequest.newBuilder()
.build();
@Before
public void setUp() {
protoReflectionService.setProtoReflectionServiceV1(mockProtoReflectionServiceV1);
}


}

0 comments on commit b2bb9e1

Please sign in to comment.