1 /* 2 * Copyright 2014 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.integration; 18 19 import com.google.protobuf.MessageLite; 20 import com.google.protobuf.StringValue; 21 import io.grpc.Metadata; 22 import io.grpc.protobuf.lite.ProtoLiteUtils; 23 import java.util.List; 24 import org.junit.Assert; 25 26 /** 27 * Utility methods to support integration testing. 28 */ 29 public class Util { 30 31 public static final Metadata.Key<StringValue> METADATA_KEY = 32 Metadata.Key.of( 33 "google.protobuf.StringValue" + Metadata.BINARY_HEADER_SUFFIX, 34 ProtoLiteUtils.metadataMarshaller(StringValue.getDefaultInstance())); 35 public static final Metadata.Key<String> ECHO_INITIAL_METADATA_KEY 36 = Metadata.Key.of("x-grpc-test-echo-initial", Metadata.ASCII_STRING_MARSHALLER); 37 public static final Metadata.Key<byte[]> ECHO_TRAILING_METADATA_KEY 38 = Metadata.Key.of("x-grpc-test-echo-trailing-bin", Metadata.BINARY_BYTE_MARSHALLER); 39 40 /** Assert that two messages are equal, producing a useful message if not. */ 41 @SuppressWarnings("LiteProtoToString") assertEquals(MessageLite expected, MessageLite actual)42 public static void assertEquals(MessageLite expected, MessageLite actual) { 43 if (expected == null || actual == null) { 44 Assert.assertEquals(expected, actual); 45 } else { 46 if (!expected.equals(actual)) { 47 // This assertEquals should always complete. 48 Assert.assertEquals(expected.toString(), actual.toString()); 49 // But if it doesn't, then this should. 50 Assert.assertEquals(expected, actual); 51 Assert.fail("Messages not equal, but assertEquals didn't throw"); 52 } 53 } 54 } 55 56 /** Assert that two lists of messages are equal, producing a useful message if not. */ assertEquals(List<? extends MessageLite> expected, List<? extends MessageLite> actual)57 public static void assertEquals(List<? extends MessageLite> expected, 58 List<? extends MessageLite> actual) { 59 if (expected == null || actual == null) { 60 Assert.assertEquals(expected, actual); 61 } else if (expected.size() != actual.size()) { 62 Assert.assertEquals(expected, actual); 63 } else { 64 for (int i = 0; i < expected.size(); i++) { 65 assertEquals(expected.get(i), actual.get(i)); 66 } 67 } 68 } 69 } 70