1 /* 2 * Copyright 2016 The gRPC 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 io.grpc; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertSame; 21 import static org.junit.Assert.fail; 22 23 import java.util.Collections; 24 import java.util.HashSet; 25 import org.junit.Rule; 26 import org.junit.Test; 27 import org.junit.rules.ExpectedException; 28 import org.junit.runner.RunWith; 29 import org.junit.runners.JUnit4; 30 31 /** Unit tests for {@link ServerServiceDefinition}. */ 32 @RunWith(JUnit4.class) 33 public class ServerServiceDefinitionTest { 34 private String serviceName = "com.example.service"; 35 private MethodDescriptor<String, Integer> method1 = MethodDescriptor.<String, Integer>newBuilder() 36 .setType(MethodDescriptor.MethodType.UNKNOWN) 37 .setFullMethodName(MethodDescriptor.generateFullMethodName(serviceName, "method1")) 38 .setRequestMarshaller(StringMarshaller.INSTANCE) 39 .setResponseMarshaller(IntegerMarshaller.INSTANCE) 40 .build(); 41 private MethodDescriptor<String, Integer> diffMethod1 = 42 method1.toBuilder().setIdempotent(true).build(); 43 private MethodDescriptor<String, Integer> method2 = method1.toBuilder() 44 .setFullMethodName(MethodDescriptor.generateFullMethodName(serviceName, "method2")) 45 .build(); 46 private ServerCallHandler<String, Integer> methodHandler1 47 = new NoopServerCallHandler<String, Integer>(); 48 private ServerCallHandler<String, Integer> methodHandler2 49 = new NoopServerCallHandler<String, Integer>(); 50 private ServerMethodDefinition<String, Integer> methodDef1 51 = ServerMethodDefinition.create(method1, methodHandler1); 52 private ServerMethodDefinition<String, Integer> methodDef2 53 = ServerMethodDefinition.create(method2, methodHandler2); 54 @Rule 55 public ExpectedException thrown = ExpectedException.none(); 56 57 @Test 58 public void noMethods() { 59 ServiceDescriptor sd = new ServiceDescriptor(serviceName); 60 ServerServiceDefinition ssd = ServerServiceDefinition.builder(sd) 61 .build(); 62 assertSame(sd, ssd.getServiceDescriptor()); 63 assertEquals(Collections.<MethodDescriptor<?, ?>>emptyList(), 64 ssd.getServiceDescriptor().getMethods()); 65 } 66 67 @Test 68 public void addMethod_twoArg() { 69 ServiceDescriptor sd = new ServiceDescriptor(serviceName, method1, method2); 70 ServerServiceDefinition ssd = ServerServiceDefinition.builder(sd) 71 .addMethod(method1, methodHandler1) 72 .addMethod(method2, methodHandler2) 73 .build(); 74 assertSame(sd, ssd.getServiceDescriptor()); 75 for (ServerMethodDefinition<?, ?> serverMethod : ssd.getMethods()) { 76 MethodDescriptor<?, ?> method = serverMethod.getMethodDescriptor(); 77 if (method1.equals(method)) { 78 assertSame(methodHandler1, serverMethod.getServerCallHandler()); 79 } else if (method2.equals(method)) { 80 assertSame(methodHandler2, serverMethod.getServerCallHandler()); 81 } else { 82 fail("Unexpected method descriptor: " + method.getFullMethodName()); 83 } 84 } 85 } 86 87 @Test 88 public void addMethod_duplicateName() { 89 ServiceDescriptor sd = new ServiceDescriptor(serviceName, method1); 90 ServerServiceDefinition.Builder ssd = ServerServiceDefinition.builder(sd) 91 .addMethod(method1, methodHandler1); 92 thrown.expect(IllegalStateException.class); 93 ssd.addMethod(diffMethod1, methodHandler2) 94 .build(); 95 } 96 97 @Test 98 public void buildMisaligned_extraMethod() { 99 ServiceDescriptor sd = new ServiceDescriptor(serviceName); 100 ServerServiceDefinition.Builder ssd = ServerServiceDefinition.builder(sd) 101 .addMethod(methodDef1); 102 thrown.expect(IllegalStateException.class); 103 ssd.build(); 104 } 105 106 @Test 107 public void buildMisaligned_diffMethodInstance() { 108 ServiceDescriptor sd = new ServiceDescriptor(serviceName, method1); 109 ServerServiceDefinition.Builder ssd = ServerServiceDefinition.builder(sd) 110 .addMethod(diffMethod1, methodHandler1); 111 thrown.expect(IllegalStateException.class); 112 ssd.build(); 113 } 114 115 @Test 116 public void buildMisaligned_missingMethod() { 117 ServiceDescriptor sd = new ServiceDescriptor(serviceName, method1); 118 ServerServiceDefinition.Builder ssd = ServerServiceDefinition.builder(sd); 119 thrown.expect(IllegalStateException.class); 120 ssd.build(); 121 } 122 123 @Test 124 public void builderWithServiceName() { 125 ServerServiceDefinition ssd = ServerServiceDefinition.builder(serviceName) 126 .addMethod(methodDef1) 127 .addMethod(methodDef2) 128 .build(); 129 assertEquals(serviceName, ssd.getServiceDescriptor().getName()); 130 131 HashSet<MethodDescriptor<?, ?>> goldenMethods = new HashSet<MethodDescriptor<?, ?>>(); 132 goldenMethods.add(method1); 133 goldenMethods.add(method2); 134 assertEquals(goldenMethods, 135 new HashSet<MethodDescriptor<?, ?>>(ssd.getServiceDescriptor().getMethods())); 136 137 HashSet<ServerMethodDefinition<?, ?>> goldenMethodDefs 138 = new HashSet<ServerMethodDefinition<?, ?>>(); 139 goldenMethodDefs.add(methodDef1); 140 goldenMethodDefs.add(methodDef2); 141 assertEquals(goldenMethodDefs, new HashSet<ServerMethodDefinition<?, ?>>(ssd.getMethods())); 142 } 143 144 @Test 145 public void builderWithServiceName_noMethods() { 146 ServerServiceDefinition ssd = ServerServiceDefinition.builder(serviceName) 147 .build(); 148 assertEquals(Collections.<MethodDescriptor<?, ?>>emptyList(), 149 ssd.getServiceDescriptor().getMethods()); 150 assertEquals(Collections.<ServerMethodDefinition<?, ?>>emptySet(), 151 new HashSet<ServerMethodDefinition<?, ?>>(ssd.getMethods())); 152 } 153 154 private static class NoopServerCallHandler<ReqT, RespT> 155 implements ServerCallHandler<ReqT, RespT> { 156 @Override 157 public ServerCall.Listener<ReqT> startCall(ServerCall<ReqT, RespT> call, Metadata headers) { 158 throw new UnsupportedOperationException(); 159 } 160 } 161 } 162