// Copyright 2017 The Bazel 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 com.google.devtools.build.lib.skyframe.serialization.testutils;

import static com.google.common.truth.Truth.assertThat;

import com.google.devtools.build.lib.skyframe.serialization.ObjectCodec;
import com.google.devtools.build.lib.skyframe.serialization.SerializationException;
import com.google.devtools.build.lib.syntax.Environment.Frame;
import com.google.protobuf.CodedInputStream;
import com.google.protobuf.CodedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/** Helpers for serialization tests. */
public class TestUtils {

  private TestUtils() {}

  /** Serialize a value to a new byte array. */
  public static <T> byte[] toBytes(ObjectCodec<T> codec, T value)
      throws IOException, SerializationException {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    CodedOutputStream codedOut = CodedOutputStream.newInstance(bytes);
    codec.serialize(value, codedOut);
    codedOut.flush();
    return bytes.toByteArray();
  }

  /** Deserialize a value from a byte array. */
  public static <T> T fromBytes(ObjectCodec<T> codec, byte[] bytes)
      throws SerializationException, IOException {
    return codec.deserialize(CodedInputStream.newInstance(bytes));
  }

  /**
   * Asserts that two {@link Frame}s have the same structure. Needed because {@link Frame} doesn't
   * override {@link Object#equals}.
   */
  public static void assertFramesEqual(Frame frame1, Frame frame2) {
    assertThat(frame1.mutability().getAnnotation())
        .isEqualTo(frame2.mutability().getAnnotation());
    assertThat(frame1.getLabel()).isEqualTo(frame2.getLabel());
    assertThat(frame1.getTransitiveBindings())
        .containsExactlyEntriesIn(frame2.getTransitiveBindings()).inOrder();
    if (frame1.getParent() == null || frame2.getParent() == null) {
      assertThat(frame1.getParent()).isNull();
      assertThat(frame2.getParent()).isNull();
    } else {
      assertFramesEqual(frame1.getParent(), frame2.getParent());
    }
  }
}

@@ -896,16 +896,98 @@
 eption;%0A
+import com.google.devtools.build.lib.skyframe.serialization.strings.StringCodecs;%0A
 import c
@@ -2689,11 +2689,760 @@
    %7D%0A  %7D
+%0A%0A  /**%0A   * Fake string codec that replaces all input and output string values with the constant %22dummy%22.%0A   */%0A  public static class ConstantStringCodec implements ObjectCodec%3CString%3E %7B%0A%0A    private static final ObjectCodec%3CString%3E stringCodec = StringCodecs.simple();%0A%0A    @Override%0A    public Class%3CString%3E getEncodedClass() %7B%0A      return String.class;%0A    %7D%0A%0A    @Override%0A    public void serialize(String value, CodedOutputStream codedOut)%0A        throws SerializationException, IOException %7B%0A      stringCodec.serialize(%22dummy%22, codedOut);%0A    %7D%0A%0A    @Override%0A    public String deserialize(CodedInputStream codedIn)%0A        throws SerializationException, IOException %7B%0A      stringCodec.deserialize(codedIn);%0A      return %22dummy%22;%0A    %7D%0A  %7D
 %0A%7D%0A

Add TestUtils#ConstantStringCodec