ServerInterceptors { public static ServerServiceDefinition intercept(ServerServiceDefinition serviceDef, ServerInterceptor... interceptors) { return intercept(serviceDef, Arrays.asList(interceptors)); } private  ServerInterceptors(); static ServerServiceDefinition interceptForward(ServerServiceDefinition serviceDef,
                                                         ServerInterceptor... interceptors); @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1701") static ServerServiceDefinition interceptForward(BindableService bindableService,
      ServerInterceptor... interceptors); static ServerServiceDefinition interceptForward(
      ServerServiceDefinition serviceDef,
      List<? extends ServerInterceptor> interceptors); static ServerServiceDefinition intercept(ServerServiceDefinition serviceDef,
                                                  ServerInterceptor... interceptors); @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1701") static ServerServiceDefinition intercept(BindableService bindableService,
      ServerInterceptor... interceptors); static ServerServiceDefinition intercept(ServerServiceDefinition serviceDef,
                                                  List<? extends ServerInterceptor> interceptors); @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1712") static ServerServiceDefinition useInputStreamMessages(
      final ServerServiceDefinition serviceDef); @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1712") static ServerServiceDefinition useMarshalledMessages(
      final ServerServiceDefinition serviceDef,
      final MethodDescriptor.Marshaller<T> marshaller);  }
@Test(expected = NullPointerException.class) public void npeForNullServiceDefinition() { ServerInterceptors.intercept(null, Arrays.<ServerInterceptor>asList()); }
@Test(expected = NullPointerException.class) public void npeForNullInterceptorList() { ServerInterceptors.intercept(serviceDefinition, (List<ServerInterceptor>) null); }
@Test(expected = NullPointerException.class) public void npeForNullInterceptor() { ServerInterceptors.intercept(serviceDefinition, Arrays.asList((ServerInterceptor) null)); }
@Test public void noop() { assertSame(serviceDefinition, ServerInterceptors.intercept(serviceDefinition, Arrays.<ServerInterceptor>asList())); }
@Test public void multipleInvocationsOfHandler() { ServerInterceptor interceptor = Mockito.spy(new NoopInterceptor()); ServerServiceDefinition intercepted = ServerInterceptors.intercept(serviceDefinition, Arrays.asList(interceptor)); assertSame(listener, getSoleMethod(intercepted).getServerCallHandler().startCall(method, call, headers)); verify(interceptor).interceptCall( same(method), same(call), same(headers), anyCallHandler()); verify(handler).startCall(method, call, headers); verifyNoMoreInteractions(interceptor, handler); assertSame(listener, getSoleMethod(intercepted).getServerCallHandler().startCall(method, call, headers)); verify(interceptor, times(2)) .interceptCall(same(method), same(call), same(headers), anyCallHandler()); verify(handler, times(2)).startCall(method, call, headers); verifyNoMoreInteractions(interceptor, handler); }
@Test public void correctHandlerCalled() { @SuppressWarnings("unchecked") ServerCallHandler<String, Integer> handler2 = mock(ServerCallHandler.class); serviceDefinition = ServerServiceDefinition.builder("basic") .addMethod(MethodDescriptor.create(MethodType.UNKNOWN, "basic/flow", requestMarshaller, responseMarshaller), handler) .addMethod(MethodDescriptor.create(MethodType.UNKNOWN, "basic/flow2", requestMarshaller, responseMarshaller), handler2).build(); ServerServiceDefinition intercepted = ServerInterceptors.intercept( serviceDefinition, Arrays.<ServerInterceptor>asList(new NoopInterceptor())); getMethod(intercepted, "basic/flow").getServerCallHandler().startCall( method, call, headers); verify(handler).startCall(method, call, headers); verifyNoMoreInteractions(handler); verifyZeroInteractions(handler2); getMethod(intercepted, "basic/flow2").getServerCallHandler().startCall( method, call, headers); verify(handler2).startCall(method, call, headers); verifyNoMoreInteractions(handler); verifyNoMoreInteractions(handler2); }
@Test public void callNextTwice() { ServerInterceptor interceptor = new ServerInterceptor() { @Override public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall( MethodDescriptor<ReqT, RespT> method, ServerCall<RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) { assertSame(listener, next.startCall(method, call, headers)); return next.startCall(method, call, headers); } }; ServerServiceDefinition intercepted = ServerInterceptors.intercept(serviceDefinition, interceptor); assertSame(listener, getSoleMethod(intercepted).getServerCallHandler().startCall(method, call, headers)); verify(handler, times(2)).startCall(same(method), same(call), same(headers)); verifyNoMoreInteractions(handler); }
@Test public void ordered() { final List<String> order = new ArrayList<String>(); handler = new ServerCallHandler<String, Integer>() { @Override public ServerCall.Listener<String> startCall( MethodDescriptor<String, Integer> method, ServerCall<Integer> call, Metadata headers) { order.add("handler"); return listener; } }; ServerInterceptor interceptor1 = new ServerInterceptor() { @Override public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall( MethodDescriptor<ReqT, RespT> method, ServerCall<RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) { order.add("i1"); return next.startCall(method, call, headers); } }; ServerInterceptor interceptor2 = new ServerInterceptor() { @Override public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall( MethodDescriptor<ReqT, RespT> method, ServerCall<RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) { order.add("i2"); return next.startCall(method, call, headers); } }; ServerServiceDefinition serviceDefinition = ServerServiceDefinition.builder("basic") .addMethod(MethodDescriptor.create(MethodType.UNKNOWN, "basic/flow", requestMarshaller, responseMarshaller), handler).build(); ServerServiceDefinition intercepted = ServerInterceptors.intercept( serviceDefinition, Arrays.asList(interceptor1, interceptor2)); assertSame(listener, getSoleMethod(intercepted).getServerCallHandler().startCall(method, call, headers)); assertEquals(Arrays.asList("i2", "i1", "handler"), order); }
@Test public void argumentsPassed() { final MethodDescriptor<String, Integer> method2 = MethodDescriptor.create( MethodType.UNKNOWN, "someOtherRandom/Method", requestMarshaller, responseMarshaller); @SuppressWarnings("unchecked") final ServerCall<Integer> call2 = mock(ServerCall.class); @SuppressWarnings("unchecked") final ServerCall.Listener<String> listener2 = mock(ServerCall.Listener.class); ServerInterceptor interceptor = new ServerInterceptor() { @SuppressWarnings("unchecked") @Override public <R1, R2> ServerCall.Listener<R1> interceptCall( MethodDescriptor<R1, R2> methodDescriptor, ServerCall<R2> call, Metadata headers, ServerCallHandler<R1, R2> next) { assertSame(method, methodDescriptor); assertSame(call, ServerInterceptorsTest.this.call); assertSame(listener, next.startCall((MethodDescriptor<R1, R2>)method2, (ServerCall<R2>)call2, headers)); return (ServerCall.Listener<R1>) listener2; } }; ServerServiceDefinition intercepted = ServerInterceptors.intercept( serviceDefinition, Arrays.asList(interceptor)); assertSame(listener2, getSoleMethod(intercepted).getServerCallHandler().startCall(method, call, headers)); verify(handler).startCall(method2, call2, headers); }