/*
 * Copyright 2015, gRPC Authors All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.grpc.protobuf;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

import com.google.common.io.ByteStreams;
import com.google.protobuf.Type;
import io.grpc.MethodDescriptor.Marshaller;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Unit tests for {@link ProtoUtils}. */
@RunWith(JUnit4.class)
public class ProtoUtilsTest {
  private Type proto = Type.newBuilder().setName("value").build();

  @Test
  public void testRoundtrip() throws Exception {
    Marshaller<Type> marshaller = ProtoUtils.marshaller(Type.getDefaultInstance());
    InputStream is = marshaller.stream(proto);
    is = new ByteArrayInputStream(ByteStreams.toByteArray(is));
    assertEquals(proto, marshaller.parse(is));
  }

  @Test
  public void keyForProto() {
    assertEquals("google.protobuf.Type-bin",
        ProtoUtils.keyForProto(Type.getDefaultInstance()).originalName());
  }

  @Test
  public void testJsonRoundtrip() throws Exception {
    Marshaller<Type> marshaller = ProtoUtils.jsonMarshaller(Type.getDefaultInstance());
    InputStream is = marshaller.stream(proto);
    is = new ByteArrayInputStream(ByteStreams.toByteArray(is));
    assertEquals(proto, marshaller.parse(is));
  }

  @Test
  public void testJsonRepresentation() throws Exception {
    Marshaller<Type> marshaller = ProtoUtils.jsonMarshaller(Type.getDefaultInstance());
    InputStream is = marshaller.stream(proto);
    String s = new String(ByteStreams.toByteArray(is), "UTF-8");
    assertEquals("{\"name\":\"value\"}", s.replaceAll("\\s", ""));
  }

  @Ignore("https://github.com/google/protobuf/issues/1470")
  @Test
  public void testJsonInvalid() throws Exception {
    Marshaller<Type> marshaller = ProtoUtils.jsonMarshaller(Type.getDefaultInstance());
    try {
      marshaller.parse(new ByteArrayInputStream("{]".getBytes("UTF-8")));
      fail("Expected exception");
    } catch (StatusRuntimeException ex) {
      assertEquals(Status.Code.INTERNAL, ex.getStatus().getCode());
      assertNotNull(ex.getCause());
    }
  }

  @Test
  public void testJsonInvalidProto() throws Exception {
    Marshaller<Type> marshaller = ProtoUtils.jsonMarshaller(Type.getDefaultInstance());
    try {
      marshaller.parse(new ByteArrayInputStream("{\"\":3}".getBytes("UTF-8")));
      fail("Expected exception");
    } catch (StatusRuntimeException ex) {
      assertEquals(Status.Code.INTERNAL, ex.getStatus().getCode());
      assertNotNull(ex.getCause());
    }
  }

  @Test
  public void testJsonIoException() throws Exception {
    Marshaller<Type> marshaller = ProtoUtils.jsonMarshaller(Type.getDefaultInstance());
    final IOException ioe = new IOException();
    try {
      marshaller.parse(new ByteArrayInputStream("{}".getBytes("UTF-8")) {
        @Override
        public void close() throws IOException {
          throw ioe;
        }
      });
      fail("Exception expected");
    } catch (StatusRuntimeException ex) {
      assertEquals(Status.Code.INTERNAL, ex.getStatus().getCode());
      assertEquals(ioe, ex.getCause());
    }
  }
}

@@ -809,16 +809,87 @@
 treams;%0A
+import com.google.protobuf.Any;%0Aimport com.google.protobuf.ByteString;%0A
 import c
@@ -2161,32 +2161,530 @@
 arse(is));%0A  %7D%0A%0A
+  @Test%0A  public void testJsonMarshallerFailToPrint() %7B%0A    Marshaller%3CAny%3E marshaller = ProtoUtils.jsonMarshaller(Any.getDefaultInstance());%0A    try %7B%0A      marshaller.stream(%0A          Any.newBuilder().setValue(ByteString.copyFromUtf8(%22invalid (no type url)%22)).build());%0A      fail(%22Expected exception%22);%0A    %7D catch (StatusRuntimeException e) %7B%0A      assertNotNull(e.getCause());%0A      assertNotNull(e.getMessage());%0A      assertEquals(Status.Code.INTERNAL, e.getStatus().getCode());%0A    %7D%0A  %7D%0A%0A
   @Test%0A  public

add test for ProtoUtils (#3838)