|
| 1 | +/* |
| 2 | + * Copyright (c) 2019, Salesforce.com, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * Licensed under the BSD 3-Clause license. |
| 5 | + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | + */ |
| 7 | + |
| 8 | +package com.salesforce.grpc.contrib.interceptor; |
| 9 | + |
| 10 | +import com.salesforce.grpc.contrib.GreeterGrpc; |
| 11 | +import com.salesforce.grpc.contrib.HelloRequest; |
| 12 | +import com.salesforce.grpc.contrib.HelloResponse; |
| 13 | +import io.grpc.ServerInterceptor; |
| 14 | +import io.grpc.ServerInterceptors; |
| 15 | +import io.grpc.Status; |
| 16 | +import io.grpc.StatusRuntimeException; |
| 17 | +import io.grpc.stub.StreamObserver; |
| 18 | +import io.grpc.testing.GrpcServerRule; |
| 19 | +import org.junit.Rule; |
| 20 | +import org.junit.Test; |
| 21 | + |
| 22 | +import java.util.Iterator; |
| 23 | + |
| 24 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 25 | + |
| 26 | +public class TransmitUnexpectedExceptionInterceptorTest { |
| 27 | + @Rule |
| 28 | + public final GrpcServerRule serverRule = new GrpcServerRule(); |
| 29 | + |
| 30 | + @Test |
| 31 | + public void noExceptionDoesNotInterfere() { |
| 32 | + GreeterGrpc.GreeterImplBase svc = new GreeterGrpc.GreeterImplBase() { |
| 33 | + @Override |
| 34 | + public void sayHello(HelloRequest request, StreamObserver<HelloResponse> responseObserver) { |
| 35 | + responseObserver.onNext(HelloResponse.newBuilder().setMessage("Hello " + request.getName()).build()); |
| 36 | + responseObserver.onCompleted(); |
| 37 | + } |
| 38 | + }; |
| 39 | + |
| 40 | + ServerInterceptor interceptor = new TransmitUnexpectedExceptionInterceptor(); |
| 41 | + |
| 42 | + serverRule.getServiceRegistry().addService(ServerInterceptors.intercept(svc, interceptor)); |
| 43 | + GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(serverRule.getChannel()); |
| 44 | + |
| 45 | + stub.sayHello(HelloRequest.newBuilder().setName("World").build()); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void exactTypeMatches() { |
| 50 | + GreeterGrpc.GreeterImplBase svc = new GreeterGrpc.GreeterImplBase() { |
| 51 | + @Override |
| 52 | + public void sayHello(HelloRequest request, StreamObserver<HelloResponse> responseObserver) { |
| 53 | + responseObserver.onError(new ArithmeticException("Divide by zero")); |
| 54 | + } |
| 55 | + }; |
| 56 | + |
| 57 | + ServerInterceptor interceptor = new TransmitUnexpectedExceptionInterceptor().forExactType(ArithmeticException.class); |
| 58 | + |
| 59 | + serverRule.getServiceRegistry().addService(ServerInterceptors.intercept(svc, interceptor)); |
| 60 | + GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(serverRule.getChannel()); |
| 61 | + |
| 62 | + assertThatThrownBy(() -> stub.sayHello(HelloRequest.newBuilder().setName("World").build())) |
| 63 | + .isInstanceOf(StatusRuntimeException.class) |
| 64 | + .matches(sre -> ((StatusRuntimeException) sre).getStatus().getCode().equals(Status.INTERNAL.getCode()), "is Status.INTERNAL") |
| 65 | + .hasMessageContaining("Divide by zero"); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void parentTypeMatches() { |
| 70 | + GreeterGrpc.GreeterImplBase svc = new GreeterGrpc.GreeterImplBase() { |
| 71 | + @Override |
| 72 | + public void sayHello(HelloRequest request, StreamObserver<HelloResponse> responseObserver) { |
| 73 | + responseObserver.onError(new ArithmeticException("Divide by zero")); |
| 74 | + } |
| 75 | + }; |
| 76 | + |
| 77 | + ServerInterceptor interceptor = new TransmitUnexpectedExceptionInterceptor().forParentType(RuntimeException.class); |
| 78 | + |
| 79 | + serverRule.getServiceRegistry().addService(ServerInterceptors.intercept(svc, interceptor)); |
| 80 | + GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(serverRule.getChannel()); |
| 81 | + |
| 82 | + assertThatThrownBy(() -> stub.sayHello(HelloRequest.newBuilder().setName("World").build())) |
| 83 | + .isInstanceOf(StatusRuntimeException.class) |
| 84 | + .matches(sre -> ((StatusRuntimeException) sre).getStatus().getCode().equals(Status.INTERNAL.getCode()), "is Status.INTERNAL") |
| 85 | + .hasMessageContaining("Divide by zero"); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void parentTypeMatchesExactly() { |
| 90 | + GreeterGrpc.GreeterImplBase svc = new GreeterGrpc.GreeterImplBase() { |
| 91 | + @Override |
| 92 | + public void sayHello(HelloRequest request, StreamObserver<HelloResponse> responseObserver) { |
| 93 | + responseObserver.onError(new RuntimeException("Divide by zero")); |
| 94 | + } |
| 95 | + }; |
| 96 | + |
| 97 | + ServerInterceptor interceptor = new TransmitUnexpectedExceptionInterceptor().forParentType(RuntimeException.class); |
| 98 | + |
| 99 | + serverRule.getServiceRegistry().addService(ServerInterceptors.intercept(svc, interceptor)); |
| 100 | + GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(serverRule.getChannel()); |
| 101 | + |
| 102 | + assertThatThrownBy(() -> stub.sayHello(HelloRequest.newBuilder().setName("World").build())) |
| 103 | + .isInstanceOf(StatusRuntimeException.class) |
| 104 | + .matches(sre -> ((StatusRuntimeException) sre).getStatus().getCode().equals(Status.INTERNAL.getCode()), "is Status.INTERNAL") |
| 105 | + .hasMessageContaining("Divide by zero"); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void alleMatches() { |
| 110 | + GreeterGrpc.GreeterImplBase svc = new GreeterGrpc.GreeterImplBase() { |
| 111 | + @Override |
| 112 | + public void sayHello(HelloRequest request, StreamObserver<HelloResponse> responseObserver) { |
| 113 | + responseObserver.onError(new ArithmeticException("Divide by zero")); |
| 114 | + } |
| 115 | + }; |
| 116 | + |
| 117 | + ServerInterceptor interceptor = new TransmitUnexpectedExceptionInterceptor().forAllExceptions(); |
| 118 | + |
| 119 | + serverRule.getServiceRegistry().addService(ServerInterceptors.intercept(svc, interceptor)); |
| 120 | + GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(serverRule.getChannel()); |
| 121 | + |
| 122 | + assertThatThrownBy(() -> stub.sayHello(HelloRequest.newBuilder().setName("World").build())) |
| 123 | + .isInstanceOf(StatusRuntimeException.class) |
| 124 | + .matches(sre -> ((StatusRuntimeException) sre).getStatus().getCode().equals(Status.INTERNAL.getCode()), "is Status.INTERNAL") |
| 125 | + .hasMessageContaining("Divide by zero"); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void unknownTypeDoesNotMatch() { |
| 130 | + GreeterGrpc.GreeterImplBase svc = new GreeterGrpc.GreeterImplBase() { |
| 131 | + @Override |
| 132 | + public void sayHello(HelloRequest request, StreamObserver<HelloResponse> responseObserver) { |
| 133 | + responseObserver.onError(new NullPointerException("Bananas!")); |
| 134 | + } |
| 135 | + }; |
| 136 | + |
| 137 | + ServerInterceptor interceptor = new TransmitUnexpectedExceptionInterceptor().forExactType(ArithmeticException.class); |
| 138 | + |
| 139 | + serverRule.getServiceRegistry().addService(ServerInterceptors.intercept(svc, interceptor)); |
| 140 | + GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(serverRule.getChannel()); |
| 141 | + |
| 142 | + assertThatThrownBy(() -> stub.sayHello(HelloRequest.newBuilder().setName("World").build())) |
| 143 | + .isInstanceOf(StatusRuntimeException.class) |
| 144 | + .matches(sre -> ((StatusRuntimeException) sre).getStatus().getCode().equals(Status.UNKNOWN.getCode()), "is Status.UNKNOWN") |
| 145 | + .hasMessageContaining("UNKNOWN"); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + public void unexpectedExceptionCanMatch() { |
| 150 | + GreeterGrpc.GreeterImplBase svc = new GreeterGrpc.GreeterImplBase() { |
| 151 | + @Override |
| 152 | + public void sayHello(HelloRequest request, StreamObserver<HelloResponse> responseObserver) { |
| 153 | + throw new ArithmeticException("Divide by zero"); |
| 154 | + } |
| 155 | + }; |
| 156 | + |
| 157 | + ServerInterceptor interceptor = new TransmitUnexpectedExceptionInterceptor().forExactType(ArithmeticException.class); |
| 158 | + |
| 159 | + serverRule.getServiceRegistry().addService(ServerInterceptors.intercept(svc, interceptor)); |
| 160 | + GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(serverRule.getChannel()); |
| 161 | + |
| 162 | + assertThatThrownBy(() -> stub.sayHello(HelloRequest.newBuilder().setName("World").build())) |
| 163 | + .isInstanceOf(StatusRuntimeException.class) |
| 164 | + .matches(sre -> ((StatusRuntimeException) sre).getStatus().getCode().equals(Status.INTERNAL.getCode()), "is Status.INTERNAL") |
| 165 | + .hasMessageContaining("Divide by zero"); |
| 166 | + } |
| 167 | + |
| 168 | + @Test |
| 169 | + public void unexpectedExceptionCanNotMatch() { |
| 170 | + GreeterGrpc.GreeterImplBase svc = new GreeterGrpc.GreeterImplBase() { |
| 171 | + @Override |
| 172 | + public void sayHello(HelloRequest request, StreamObserver<HelloResponse> responseObserver) { |
| 173 | + throw new ArithmeticException("Divide by zero"); |
| 174 | + } |
| 175 | + }; |
| 176 | + |
| 177 | + ServerInterceptor interceptor = new TransmitUnexpectedExceptionInterceptor().forExactType(NullPointerException.class); |
| 178 | + |
| 179 | + serverRule.getServiceRegistry().addService(ServerInterceptors.intercept(svc, interceptor)); |
| 180 | + GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(serverRule.getChannel()); |
| 181 | + |
| 182 | + assertThatThrownBy(() -> stub.sayHello(HelloRequest.newBuilder().setName("World").build())) |
| 183 | + .isInstanceOf(StatusRuntimeException.class) |
| 184 | + .matches(sre -> ((StatusRuntimeException) sre).getStatus().getCode().equals(Status.UNKNOWN.getCode()), "is Status.UNKNOWN") |
| 185 | + .hasMessageContaining("UNKNOWN"); |
| 186 | + } |
| 187 | + |
| 188 | + @Test |
| 189 | + public void unexpectedExceptionCanMatchStreaming() { |
| 190 | + GreeterGrpc.GreeterImplBase svc = new GreeterGrpc.GreeterImplBase() { |
| 191 | + @Override |
| 192 | + public void sayHelloStream(HelloRequest request, StreamObserver<HelloResponse> responseObserver) { |
| 193 | + responseObserver.onNext(HelloResponse.getDefaultInstance()); |
| 194 | + responseObserver.onNext(HelloResponse.getDefaultInstance()); |
| 195 | + throw new ArithmeticException("Divide by zero"); |
| 196 | + } |
| 197 | + }; |
| 198 | + |
| 199 | + ServerInterceptor interceptor = new TransmitUnexpectedExceptionInterceptor().forExactType(ArithmeticException.class); |
| 200 | + |
| 201 | + serverRule.getServiceRegistry().addService(ServerInterceptors.intercept(svc, interceptor)); |
| 202 | + GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(serverRule.getChannel()); |
| 203 | + |
| 204 | + Iterator<HelloResponse> it = stub.sayHelloStream(HelloRequest.newBuilder().setName("World").build()); |
| 205 | + it.next(); |
| 206 | + it.next(); |
| 207 | + assertThatThrownBy(it::next) |
| 208 | + .isInstanceOf(StatusRuntimeException.class) |
| 209 | + .matches(sre -> ((StatusRuntimeException) sre).getStatus().getCode().equals(Status.INTERNAL.getCode()), "is Status.INTERNAL") |
| 210 | + .hasMessageContaining("Divide by zero"); |
| 211 | + } |
| 212 | +} |
0 commit comments