/*
 * 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.network.protocol;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
 * Unit tests for {@link RPCBlockReadRequest}.
 */
public class RPCBlockReadRequestTest {
  private static final long BLOCK_ID = 11;
  private static final long OFFSET = 22;
  private static final long LENGTH = 33;
  private static final long LOCK_ID = 44;
  private static final long SESSION_ID = 55;

  private ByteBuf mBuffer = null;

  private void assertValid(long blockId, long offset, long length, long lockId, long sessionId,
      RPCBlockReadRequest req) {
    Assert.assertEquals(RPCMessage.Type.RPC_BLOCK_READ_REQUEST, req.getType());
    Assert.assertEquals(blockId, req.getBlockId());
    Assert.assertEquals(offset, req.getOffset());
    Assert.assertEquals(length, req.getLength());
    Assert.assertEquals(lockId, req.getLockId());
    Assert.assertEquals(sessionId, req.getSessionId());
  }

  private void assertValid(RPCBlockReadRequest req) {
    try {
      req.validate();
    } catch (Exception e) {
      Assert.fail("request should be valid.");
    }
  }

  private void assertInvalid(RPCBlockReadRequest req) {
    try {
      req.validate();
      Assert.fail("request should be invalid.");
    } catch (Exception e) {
      return;
    }
  }

  /**
   * Sets up the buffer before a test runs.
   */
  @Before
  public final void before() {
    mBuffer = Unpooled.buffer();
  }

  /**
   * Tests the {@link RPCBlockReadRequest#getEncodedLength()} method.
   */
  @Test
  public void encodedLength() {
    RPCBlockReadRequest req = new RPCBlockReadRequest(BLOCK_ID, OFFSET, LENGTH, LOCK_ID,
        SESSION_ID);
    int encodedLength = req.getEncodedLength();
    req.encode(mBuffer);
    Assert.assertEquals(encodedLength, mBuffer.readableBytes());
  }

  /**
   * Tests the {@link RPCBlockReadRequest#encode(ByteBuf)} and
   * {@link RPCBlockReadRequest#decode(ByteBuf)} methods.
   */
  @Test
  public void encodeDecode() {
    RPCBlockReadRequest req = new RPCBlockReadRequest(BLOCK_ID, OFFSET, LENGTH, LOCK_ID,
        SESSION_ID);
    req.encode(mBuffer);
    RPCBlockReadRequest req2 = RPCBlockReadRequest.decode(mBuffer);
    assertValid(BLOCK_ID, OFFSET, LENGTH, LOCK_ID, SESSION_ID, req);
    assertValid(BLOCK_ID, OFFSET, LENGTH, LOCK_ID, SESSION_ID, req2);
  }

  /**
   * Tests the {@link RPCBlockReadRequest#validate()} method.
   */
  @Test
  public void validate() {
    RPCBlockReadRequest req = new RPCBlockReadRequest(BLOCK_ID, OFFSET, LENGTH, LOCK_ID,
        SESSION_ID);
    assertValid(req);
  }

  /**
   * Tests the {@link RPCBlockReadRequest#RPCBlockReadRequest(long, long, long, long, long)}
   * constructor with a valid length.
   */
  @Test
  public void validLength() {
    RPCBlockReadRequest req = new RPCBlockReadRequest(BLOCK_ID, OFFSET, -1, LOCK_ID,
        SESSION_ID);
    assertValid(req);
    req = new RPCBlockReadRequest(BLOCK_ID, OFFSET, 0, LOCK_ID, SESSION_ID);
    assertValid(req);
  }

  /**
   * Tests the {@link RPCBlockReadRequest#RPCBlockReadRequest(long, long, long, long, long)}
   * constructor with a negative offset.
   */
  @Test
  public void negativeOffset() {
    RPCBlockReadRequest req = new RPCBlockReadRequest(BLOCK_ID, -1, LENGTH, LOCK_ID,
        SESSION_ID);
    assertInvalid(req);
  }

  /**
   * Tests the {@link RPCBlockReadRequest#RPCBlockReadRequest(long, long, long, long, long)}
   * constructor with an invalid length.
   */
  @Test
  public void invalidLength() {
    RPCBlockReadRequest req = new RPCBlockReadRequest(BLOCK_ID, OFFSET, -100, LOCK_ID,
        SESSION_ID);
    assertInvalid(req);
  }
}

@@ -542,16 +542,99 @@
 tocol;%0A%0A
+import static org.junit.Assert.assertEquals;%0Aimport static org.junit.Assert.fail;%0A%0A
 import i
@@ -657,16 +657,16 @@
 yteBuf;%0A
-
 import i
@@ -694,33 +694,8 @@
 ed;%0A
-import org.junit.Assert;%0A
 impo
@@ -1210,31 +1210,24 @@
  req) %7B%0A    
-Assert.
 assertEquals
@@ -1279,39 +1279,32 @@
 getType());%0A    
-Assert.
 assertEquals(blo
@@ -1324,39 +1324,32 @@
 BlockId());%0A    
-Assert.
 assertEquals(off
@@ -1367,39 +1367,32 @@
 tOffset());%0A    
-Assert.
 assertEquals(len
@@ -1410,39 +1410,32 @@
 tLength());%0A    
-Assert.
 assertEquals(loc
@@ -1457,31 +1457,24 @@
 kId());%0A    
-Assert.
 assertEquals
@@ -1627,31 +1627,24 @@
  e) %7B%0A      
-Assert.
 fail(%22reques
@@ -1770,23 +1770,16 @@
 ;%0A      
-Assert.
 fail(%22re
@@ -2303,19 +2303,12 @@
 r);%0A
+
     
-Assert.
 asse

use static import for RPCBlockReadRequestTest.java