/*
 * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
 * (the “License”). You may not use this work except in compliance with the License, which is
 * available at www.apache.org/licenses/LICENSE-2.0
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied, as more fully set forth in the License.
 *
 * See the NOTICE file distributed with this work for information regarding copyright ownership.
 */

package alluxio.wire;

import com.google.common.collect.Lists;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Assert;
import org.junit.Test;

import java.util.List;
import java.util.Random;

public class BlockInfoTest {

  @Test
  public void jsonTest() throws Exception {
    BlockInfo blockInfo = createRandom();
    ObjectMapper mapper = new ObjectMapper();
    BlockInfo other = mapper.readValue(mapper.writeValueAsBytes(blockInfo), BlockInfo.class);
    checkEquality(blockInfo, other);
  }

  @Test
  public void thriftTest() {
    BlockInfo blockInfo = createRandom();
    BlockInfo other = ThriftUtils.fromThrift(ThriftUtils.toThrift(blockInfo));
    checkEquality(blockInfo, other);
  }

  public void checkEquality(BlockInfo a, BlockInfo b) {
    Assert.assertEquals(a.getBlockId(), b.getBlockId());
    Assert.assertEquals(a.getLength(), b.getLength());
    Assert.assertEquals(a.getLocations(), b.getLocations());
    Assert.assertEquals(a, b);
  }

  public static BlockInfo createRandom() {
    BlockInfo result = new BlockInfo();
    Random random = new Random();

    long blockId = random.nextLong();
    long length = random.nextLong();
    List<BlockLocation> locations = Lists.newArrayList();
    long numLocations = random.nextInt(10);
    for (int i = 0; i < numLocations; i++) {
      locations.add(BlockLocationTest.createRandom());
    }

    result.setBlockId(blockId);
    result.setLength(length);
    result.setLocations(locations);

    return result;
  }
}

@@ -744,24 +744,198 @@
 Test %7B%0A%0A
+  /**%0A   * Test to convert between a BlockInfo type and a json type.%0A   *%0A   * @throws Exception if an error occurs during convert between BlockInfo type and json type%0A   */%0A
   @Test%0A
   public
@@ -918,32 +918,32 @@
 e%0A   */%0A  @Test%0A
-
   public void js
@@ -1194,16 +1194,88 @@
 );%0A  %7D%0A%0A
+  /**%0A   * Test to convert between a thrift type and a wire type.%0A   */%0A
   @Test%0A
@@ -1458,32 +1458,210 @@
 o, other);%0A  %7D%0A%0A
+  /**%0A   * Check if the two BlockInfo object are equal.%0A   *%0A   * @param a the first BlockInfo object to be checked%0A   * @param b the second BlockInfo object to be checked%0A   */%0A
   public void ch
@@ -1909,16 +1909,116 @@
 );%0A  %7D%0A%0A
+  /**%0A   * Randomly create a BlockInfo object.%0A   *%0A   * @return the created BlockInfo object%0A   */%0A
   public

add javadoc