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 junit.framework.TestCase.assertSame;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertNotEquals;
23 import static org.junit.Assert.assertTrue;
24 
25 import io.grpc.MethodDescriptor.MethodType;
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 /**
33  * Tests for {@link MethodDescriptor}.
34  */
35 @RunWith(JUnit4.class)
36 public class MethodDescriptorTest {
37   @Rule
38   public final ExpectedException thrown = ExpectedException.none();
39 
40   @Test
createMethodDescriptor()41   public void createMethodDescriptor() {
42     MethodDescriptor<String, String> descriptor = MethodDescriptor.<String, String>newBuilder()
43         .setType(MethodType.CLIENT_STREAMING)
44         .setFullMethodName("package.service/method")
45         .setRequestMarshaller(new StringMarshaller())
46         .setResponseMarshaller(new StringMarshaller())
47         .build();
48 
49     assertEquals(MethodType.CLIENT_STREAMING, descriptor.getType());
50     assertEquals("package.service/method", descriptor.getFullMethodName());
51     assertFalse(descriptor.isIdempotent());
52     assertFalse(descriptor.isSafe());
53   }
54 
55   @Test
idempotent()56   public void idempotent() {
57     MethodDescriptor<String, String> descriptor = MethodDescriptor.<String, String>newBuilder()
58         .setType(MethodType.SERVER_STREAMING)
59         .setFullMethodName("package.service/method")
60         .setRequestMarshaller(new StringMarshaller())
61         .setResponseMarshaller(new StringMarshaller())
62         .build();
63 
64     assertFalse(descriptor.isIdempotent());
65 
66     // Create a new desriptor by setting idempotent to true
67     MethodDescriptor<String, String> newDescriptor =
68         descriptor.toBuilder().setIdempotent(true).build();
69     assertTrue(newDescriptor.isIdempotent());
70     // All other fields should staty the same
71     assertEquals(MethodType.SERVER_STREAMING, newDescriptor.getType());
72     assertEquals("package.service/method", newDescriptor.getFullMethodName());
73   }
74 
75   @Test
safe()76   public void safe() {
77     MethodDescriptor<String, String> descriptor = MethodDescriptor.<String, String>newBuilder()
78         .setType(MethodType.UNARY)
79         .setFullMethodName("package.service/method")
80         .setRequestMarshaller(new StringMarshaller())
81         .setResponseMarshaller(new StringMarshaller())
82         .build();
83     assertFalse(descriptor.isSafe());
84 
85     // Create a new desriptor by setting safe to true
86     MethodDescriptor<String, String> newDescriptor = descriptor.toBuilder().setSafe(true).build();
87     assertTrue(newDescriptor.isSafe());
88     // All other fields should staty the same
89     assertEquals(MethodType.UNARY, newDescriptor.getType());
90     assertEquals("package.service/method", newDescriptor.getFullMethodName());
91   }
92 
93   @Test
safeAndNonUnary()94   public void safeAndNonUnary() {
95     MethodDescriptor<String, String> descriptor = MethodDescriptor.<String, String>newBuilder()
96         .setType(MethodType.SERVER_STREAMING)
97         .setFullMethodName("package.service/method")
98         .setRequestMarshaller(new StringMarshaller())
99         .setResponseMarshaller(new StringMarshaller())
100         .build();
101 
102     thrown.expect(IllegalArgumentException.class);
103     MethodDescriptor<String, String> unused = descriptor.toBuilder().setSafe(true).build();
104   }
105 
106   @Test
sampledToLocalTracing()107   public void sampledToLocalTracing() {
108     MethodDescriptor<String, String> md1 = MethodDescriptor.<String, String>newBuilder()
109         .setType(MethodType.SERVER_STREAMING)
110         .setFullMethodName("package.service/method")
111         .setRequestMarshaller(new StringMarshaller())
112         .setResponseMarshaller(new StringMarshaller())
113         .setSampledToLocalTracing(true)
114         .build();
115     assertTrue(md1.isSampledToLocalTracing());
116 
117     MethodDescriptor<String, String> md2 = md1.toBuilder()
118         .setFullMethodName("package.service/method2")
119         .build();
120     assertTrue(md2.isSampledToLocalTracing());
121 
122     // Same method name as md1, but not setting sampledToLocalTracing
123     MethodDescriptor<String, String> md3 = MethodDescriptor.<String, String>newBuilder()
124         .setType(MethodType.SERVER_STREAMING)
125         .setFullMethodName("package.service/method")
126         .setRequestMarshaller(new StringMarshaller())
127         .setResponseMarshaller(new StringMarshaller())
128         .build();
129     assertFalse(md3.isSampledToLocalTracing());
130 
131     MethodDescriptor<String, String> md4 = md3.toBuilder()
132         .setFullMethodName("package.service/method2")
133         .setSampledToLocalTracing(true)
134         .build();
135     assertTrue(md4.isSampledToLocalTracing());
136   }
137 
138   @Test
toBuilderTest()139   public void toBuilderTest() {
140     MethodDescriptor<String, String> md1 = MethodDescriptor.<String, String>newBuilder()
141         .setType(MethodType.UNARY)
142         .setFullMethodName("package.service/method")
143         .setRequestMarshaller(StringMarshaller.INSTANCE)
144         .setResponseMarshaller(StringMarshaller.INSTANCE)
145         .setSampledToLocalTracing(true)
146         .setIdempotent(true)
147         .setSafe(true)
148         .setSchemaDescriptor(new Object())
149         .build();
150     // Verify that we are not using any default builder values, so if md1 and md2 matches,
151     // it's because toBuilder explicitly copied it.
152     MethodDescriptor<String, String> defaults = MethodDescriptor.<String, String>newBuilder()
153         .setType(MethodType.UNARY)
154         .setFullMethodName("package.service/method")
155         .setRequestMarshaller(StringMarshaller.INSTANCE)
156         .setResponseMarshaller(StringMarshaller.INSTANCE)
157         .build();
158     assertNotEquals(md1.isSampledToLocalTracing(), defaults.isSampledToLocalTracing());
159     assertNotEquals(md1.isIdempotent(), defaults.isIdempotent());
160     assertNotEquals(md1.isSafe(), defaults.isSafe());
161     assertNotEquals(md1.getSchemaDescriptor(), defaults.getSchemaDescriptor());
162 
163     // Verify that the builder correctly copied over the values
164     MethodDescriptor<Integer, Integer> md2 = md1.toBuilder(
165         IntegerMarshaller.INSTANCE,
166         IntegerMarshaller.INSTANCE).build();
167     assertSame(md1.getType(), md2.getType());
168     assertSame(md1.getFullMethodName(), md2.getFullMethodName());
169     assertSame(IntegerMarshaller.INSTANCE, md2.getRequestMarshaller());
170     assertSame(IntegerMarshaller.INSTANCE, md2.getResponseMarshaller());
171     assertEquals(md1.isSampledToLocalTracing(), md2.isSampledToLocalTracing());
172     assertEquals(md1.isIdempotent(), md2.isIdempotent());
173     assertEquals(md1.isSafe(), md2.isSafe());
174     assertSame(md1.getSchemaDescriptor(), md2.getSchemaDescriptor());
175   }
176 
177   @Test
toStringTest()178   public void toStringTest() {
179     MethodDescriptor<String, String> descriptor = MethodDescriptor.<String, String>newBuilder()
180         .setType(MethodType.UNARY)
181         .setFullMethodName("package.service/method")
182         .setRequestMarshaller(StringMarshaller.INSTANCE)
183         .setResponseMarshaller(StringMarshaller.INSTANCE)
184         .setSampledToLocalTracing(true)
185         .setIdempotent(true)
186         .setSafe(true)
187         .setSchemaDescriptor(new Object())
188         .build();
189 
190     String toString = descriptor.toString();
191     assertTrue(toString.contains("MethodDescriptor"));
192     assertTrue(toString.contains("fullMethodName=package.service/method"));
193     assertTrue(toString.contains("type=UNARY"));
194     assertTrue(toString.contains("idempotent=true"));
195     assertTrue(toString.contains("safe=true"));
196     assertTrue(toString.contains("sampledToLocalTracing=true"));
197     assertTrue(toString.contains("requestMarshaller=io.grpc.StringMarshaller"));
198     assertTrue(toString.contains("responseMarshaller=io.grpc.StringMarshaller"));
199     assertTrue(toString.contains("schemaDescriptor=java.lang.Object"));
200   }
201 }
202