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