• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 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.assertTrue;
20 
21 import io.grpc.MethodDescriptor.MethodType;
22 import io.grpc.testing.TestMethodDescriptors;
23 import java.util.Arrays;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.List;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.rules.ExpectedException;
30 import org.junit.runner.RunWith;
31 import org.junit.runners.JUnit4;
32 
33 /**
34  * Tests for {@link ServiceDescriptor}.
35  */
36 @RunWith(JUnit4.class)
37 public class ServiceDescriptorTest {
38 
39   @SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
40   @Rule
41   public final ExpectedException thrown = ExpectedException.none();
42 
43   @Test
failsOnNullName()44   public void failsOnNullName() {
45     thrown.expect(NullPointerException.class);
46     thrown.expectMessage("name");
47 
48     new ServiceDescriptor(null, Collections.<MethodDescriptor<?, ?>>emptyList());
49   }
50 
51   @Test
failsOnNullMethods()52   public void failsOnNullMethods() {
53     thrown.expect(NullPointerException.class);
54     thrown.expectMessage("methods");
55 
56     new ServiceDescriptor("name", (Collection<MethodDescriptor<?, ?>>) null);
57   }
58 
59   @Test
failsOnNullMethod()60   public void failsOnNullMethod() {
61     thrown.expect(NullPointerException.class);
62     thrown.expectMessage("method");
63 
64     new ServiceDescriptor("name", Collections.<MethodDescriptor<?, ?>>singletonList(null));
65   }
66 
67   @Test
failsOnNonMatchingNames()68   public void failsOnNonMatchingNames() {
69     List<MethodDescriptor<?, ?>> descriptors = Collections.<MethodDescriptor<?, ?>>singletonList(
70         MethodDescriptor.<Void, Void>newBuilder()
71           .setType(MethodType.UNARY)
72           .setFullMethodName(MethodDescriptor.generateFullMethodName("wrongservice", "method"))
73           .setRequestMarshaller(TestMethodDescriptors.voidMarshaller())
74           .setResponseMarshaller(TestMethodDescriptors.voidMarshaller())
75           .build());
76 
77     thrown.expect(IllegalArgumentException.class);
78     thrown.expectMessage("service names");
79 
80     new ServiceDescriptor("name", descriptors);
81   }
82 
83   @Test
failsOnNonDuplicateNames()84   public void failsOnNonDuplicateNames() {
85     List<MethodDescriptor<?, ?>> descriptors = Arrays.<MethodDescriptor<?, ?>>asList(
86         MethodDescriptor.<Void, Void>newBuilder()
87           .setType(MethodType.UNARY)
88           .setFullMethodName(MethodDescriptor.generateFullMethodName("name", "method"))
89           .setRequestMarshaller(TestMethodDescriptors.voidMarshaller())
90           .setResponseMarshaller(TestMethodDescriptors.voidMarshaller())
91           .build(),
92         MethodDescriptor.<Void, Void>newBuilder()
93           .setType(MethodType.UNARY)
94           .setFullMethodName(MethodDescriptor.generateFullMethodName("name", "method"))
95           .setRequestMarshaller(TestMethodDescriptors.voidMarshaller())
96           .setResponseMarshaller(TestMethodDescriptors.voidMarshaller())
97           .build());
98 
99     thrown.expect(IllegalArgumentException.class);
100     thrown.expectMessage("duplicate");
101 
102     new ServiceDescriptor("name", descriptors);
103   }
104 
105   @Test
toStringTest()106   public void toStringTest() {
107     ServiceDescriptor descriptor = new ServiceDescriptor("package.service",
108         Arrays.<MethodDescriptor<?, ?>>asList(
109         MethodDescriptor.<Void, Void>newBuilder()
110           .setType(MethodType.UNARY)
111           .setFullMethodName(MethodDescriptor.generateFullMethodName("package.service",
112             "methodOne"))
113           .setRequestMarshaller(TestMethodDescriptors.voidMarshaller())
114           .setResponseMarshaller(TestMethodDescriptors.voidMarshaller())
115           .build(),
116         MethodDescriptor.<Void, Void>newBuilder()
117           .setType(MethodType.UNARY)
118           .setFullMethodName(MethodDescriptor.generateFullMethodName("package.service",
119             "methodTwo"))
120           .setRequestMarshaller(TestMethodDescriptors.voidMarshaller())
121           .setResponseMarshaller(TestMethodDescriptors.voidMarshaller())
122           .build()));
123 
124     String toString = descriptor.toString();
125     assertTrue(toString.contains("ServiceDescriptor"));
126     assertTrue(toString.contains("name=package.service"));
127     assertTrue(toString.contains("package.service/methodOne"));
128     assertTrue(toString.contains("package.service/methodTwo"));
129   }
130 }
131