Skip to content

Commit b2bb9e1

Browse files
committed
Changes to allow unit testing.
1 parent a002bc2 commit b2bb9e1

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

services/src/main/java/io/grpc/protobuf/services/ProtoReflectionService.java

+22-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.grpc.protobuf.services;
22

3+
import com.google.common.annotations.VisibleForTesting;
4+
import com.google.protobuf.InvalidProtocolBufferException;
35
import io.grpc.BindableService;
46
import io.grpc.ExperimentalApi;
57
import io.grpc.protobuf.services.util.ReflectionServiceProtoAdapter;
@@ -18,14 +20,20 @@
1820
*/
1921
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/2222")
2022
public class ProtoReflectionService extends ServerReflectionGrpc.ServerReflectionImplBase {
21-
private final ProtoReflectionServiceV1 protoReflectionServiceV1 = (ProtoReflectionServiceV1) ProtoReflectionServiceV1.newInstance();
23+
private ProtoReflectionServiceV1 protoReflectionServiceV1 = (ProtoReflectionServiceV1) ProtoReflectionServiceV1.newInstance();
2224
/**
2325
* Creates a instance of {@link ProtoReflectionServiceV1}.
2426
*/
2527
public static BindableService newInstance() {
2628
return new ProtoReflectionService();
2729
}
2830

31+
private ProtoReflectionService() {}
32+
33+
@VisibleForTesting
34+
void setProtoReflectionServiceV1(ProtoReflectionServiceV1 protoReflectionServiceV1) {
35+
this.protoReflectionServiceV1 = protoReflectionServiceV1;
36+
}
2937
@Override
3038
public StreamObserver<ServerReflectionRequest> serverReflectionInfo(
3139
final StreamObserver<ServerReflectionResponse> responseObserver) {
@@ -34,8 +42,13 @@ public StreamObserver<ServerReflectionRequest> serverReflectionInfo(
3442
@Override
3543
public void onNext(
3644
io.grpc.reflection.v1.ServerReflectionResponse serverReflectionResponse) {
37-
responseObserver.onNext(
38-
ReflectionServiceProtoAdapter.toV1AlphaResponse(serverReflectionResponse));
45+
try {
46+
responseObserver.onNext(
47+
ReflectionServiceProtoAdapter.toV1AlphaResponse(serverReflectionResponse));
48+
} catch (InvalidProtocolBufferException e) {
49+
// Should never happen as long as v1 and v1alpha protos have the same fields and ordering.
50+
throw new RuntimeException(e);
51+
}
3952
}
4053

4154
@Override
@@ -51,7 +64,12 @@ public void onCompleted() {
5164
return new StreamObserver<ServerReflectionRequest>() {
5265
@Override
5366
public void onNext(ServerReflectionRequest serverReflectionRequest) {
54-
v1RequestObserver.onNext(ReflectionServiceProtoAdapter.toV1Request(serverReflectionRequest));
67+
try {
68+
v1RequestObserver.onNext(ReflectionServiceProtoAdapter.toV1Request(serverReflectionRequest));
69+
} catch (InvalidProtocolBufferException e) {
70+
// Should never happen as long as v1 and v1alpha protos have the same fields and ordering.
71+
throw new RuntimeException(e);
72+
}
5573
}
5674

5775
@Override
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package io.grpc.protobuf.services.util;
22

33

4+
import com.google.protobuf.InvalidProtocolBufferException;
45
import io.grpc.reflection.v1.ServerReflectionRequest;
56
import io.grpc.reflection.v1.ServerReflectionResponse;
67

78
public class ReflectionServiceProtoAdapter {
89
public static ServerReflectionRequest toV1Request(
9-
io.grpc.reflection.v1alpha.ServerReflectionRequest serverReflectionRequest) {
10-
return null;
10+
io.grpc.reflection.v1alpha.ServerReflectionRequest serverReflectionRequest)
11+
throws InvalidProtocolBufferException {
12+
return ServerReflectionRequest.parseFrom(serverReflectionRequest.toByteArray());
1113
}
1214

1315
public static io.grpc.reflection.v1alpha.ServerReflectionResponse toV1AlphaResponse(
14-
ServerReflectionResponse serverReflectionResponse) {
15-
return null;
16+
ServerReflectionResponse serverReflectionResponse) throws InvalidProtocolBufferException {
17+
return io.grpc.reflection.v1alpha.ServerReflectionResponse.parseFrom(serverReflectionResponse.toByteArray());
1618
}
1719
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package io.grpc.protobuf.services;
2+
3+
import io.grpc.reflection.v1alpha.ServerReflectionRequest;
4+
import org.junit.Before;
5+
import org.junit.Rule;
6+
import org.junit.runner.RunWith;
7+
import org.junit.runners.JUnit4;
8+
import org.mockito.Mock;
9+
import org.mockito.junit.MockitoJUnit;
10+
import org.mockito.junit.MockitoRule;
11+
12+
@RunWith(JUnit4.class)
13+
public class ProtoReflectionServiceTest {
14+
@Rule
15+
public final MockitoRule mocks = MockitoJUnit.rule();
16+
ProtoReflectionService protoReflectionService = (ProtoReflectionService) ProtoReflectionService.newInstance();
17+
@Mock
18+
ProtoReflectionServiceV1 mockProtoReflectionServiceV1;
19+
private final ServerReflectionRequest serverReflectionRequestV1Alpha = ServerReflectionRequest.newBuilder()
20+
.build();
21+
@Before
22+
public void setUp() {
23+
protoReflectionService.setProtoReflectionServiceV1(mockProtoReflectionServiceV1);
24+
}
25+
26+
27+
}

0 commit comments

Comments
 (0)