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.testing; 18 19 import io.grpc.ExperimentalApi; 20 import io.grpc.MethodDescriptor; 21 import io.grpc.MethodDescriptor.MethodType; 22 import java.io.ByteArrayInputStream; 23 import java.io.InputStream; 24 25 /** 26 * A collection of method descriptor constructors useful for tests. These are useful if you need 27 * a descriptor, but don't really care how it works. 28 * 29 * @since 1.1.0 30 */ 31 @ExperimentalApi("https://github.com/grpc/grpc-java/issues/2600") 32 public final class TestMethodDescriptors { TestMethodDescriptors()33 private TestMethodDescriptors() {} 34 35 /** 36 * Creates a new method descriptor that always creates zero length messages, and always parses to 37 * null objects. 38 * 39 * @since 1.1.0 40 */ 41 @ExperimentalApi("https://github.com/grpc/grpc-java/issues/2600") voidMethod()42 public static MethodDescriptor<Void, Void> voidMethod() { 43 return MethodDescriptor.<Void, Void>newBuilder() 44 .setType(MethodType.UNARY) 45 .setFullMethodName(MethodDescriptor.generateFullMethodName("service_foo", "method_bar")) 46 .setRequestMarshaller(TestMethodDescriptors.voidMarshaller()) 47 .setResponseMarshaller(TestMethodDescriptors.voidMarshaller()) 48 .build(); 49 } 50 51 /** 52 * Creates a new marshaller that does nothing. 53 * 54 * @since 1.1.0 55 */ 56 @ExperimentalApi("https://github.com/grpc/grpc-java/issues/2600") voidMarshaller()57 public static MethodDescriptor.Marshaller<Void> voidMarshaller() { 58 return new NoopMarshaller(); 59 } 60 61 private static final class NoopMarshaller implements MethodDescriptor.Marshaller<Void> { 62 @Override stream(Void value)63 public InputStream stream(Void value) { 64 return new ByteArrayInputStream(new byte[]{}); 65 } 66 67 @Override parse(InputStream stream)68 public Void parse(InputStream stream) { 69 return null; 70 } 71 } 72 } 73