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