1 /* 2 * Copyright (C) 2016 The Dagger Authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package dagger.grpc.functional.server; 18 19 import static com.google.common.truth.Truth.assertWithMessage; 20 import static java.lang.annotation.RetentionPolicy.RUNTIME; 21 22 import com.google.protos.test.BaristaGrpc; 23 import io.grpc.MethodDescriptor; 24 import java.lang.annotation.Retention; 25 import org.junit.rules.TestRule; 26 import org.junit.runner.Description; 27 import org.junit.runners.model.Statement; 28 29 final class VerifyInterceptor implements TestRule { 30 31 @Retention(RUNTIME) 32 @interface MethodName { value()33 String value(); 34 } 35 36 private final CoffeeServerResource coffeeServer; 37 VerifyInterceptor(CoffeeServerResource coffeeServer)38 VerifyInterceptor(CoffeeServerResource coffeeServer) { 39 this.coffeeServer = coffeeServer; 40 } 41 42 @Override apply(final Statement base, Description description)43 public Statement apply(final Statement base, Description description) { 44 MethodName annotation = description.getAnnotation(MethodName.class); 45 if (annotation == null) { 46 return base; 47 } 48 final String fullMethodName = 49 MethodDescriptor.generateFullMethodName(BaristaGrpc.SERVICE_NAME, annotation.value()); 50 return new Statement() { 51 @Override 52 public void evaluate() throws Throwable { 53 int calls = coffeeServer.methodCount(fullMethodName); 54 base.evaluate(); 55 assertWithMessage("Calls to %s", fullMethodName) 56 .that(coffeeServer.methodCount(fullMethodName)) 57 .isEqualTo(calls + 1); 58 } 59 }; 60 } 61 } 62