/*
 * Licensed to the University of California, Berkeley under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The ASF licenses this file to You 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 tachyon.worker.netty.protocol;

import java.nio.ByteBuffer;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import tachyon.worker.netty.protocol.buffer.DataByteBuffer;

public class RPCBlockResponseTest {
  private static final long BLOCK_ID = 1;
  private static final long OFFSET = 2;
  // The RPCMessageEncoder sends the payload separately from the message, so these unit tests only
  // test the message encoding part. Therefore, the 'length' should be 0.
  private static final long LENGTH = 0;

  private ByteBuf mBuffer = null;

  private void assertValid(long blockId, long offset, long length, RPCBlockResponse resp) {
    Assert.assertEquals(RPCMessage.Type.RPC_BLOCK_RESPONSE, resp.getType());
    Assert.assertEquals(blockId, resp.getBlockId());
    Assert.assertEquals(offset, resp.getOffset());
    Assert.assertEquals(length, resp.getLength());
  }

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

  @Before
  public final void before() {
    mBuffer = Unpooled.buffer();
  }

  @Test
  public void encodedLengthTest() {
    RPCBlockResponse req = new RPCBlockResponse(BLOCK_ID, OFFSET, LENGTH, null);
    int encodedLength = req.getEncodedLength();
    req.encode(mBuffer);
    Assert.assertEquals(encodedLength, mBuffer.readableBytes());
  }

  @Test
  public void encodeDecodeTest() {
    RPCBlockResponse req = new RPCBlockResponse(BLOCK_ID, OFFSET, LENGTH, null);
    req.encode(mBuffer);
    RPCBlockResponse req2 = RPCBlockResponse.decode(mBuffer);
    assertValid(BLOCK_ID, OFFSET, LENGTH, req);
    assertValid(BLOCK_ID, OFFSET, LENGTH, req2);
  }

  @Test
  public void validateTest() {
    RPCBlockResponse req = new RPCBlockResponse(BLOCK_ID, OFFSET, LENGTH, null);
    assertValid(req);
  }

  @Test
  public void getPayloadDataBufferTest() {
    int length = 10;
    DataByteBuffer payload = new DataByteBuffer(ByteBuffer.allocate(length), length);
    RPCBlockResponse req = new RPCBlockResponse(BLOCK_ID, OFFSET, LENGTH, payload);
    assertValid(req);
    Assert.assertEquals(payload, req.getPayloadDataBuffer());
  }
}

@@ -1798,17 +1798,18 @@
 ponse re
-q
+sp
 ) %7B%0A    
@@ -1822,17 +1822,18 @@
       re
-q
+sp
 .validat
@@ -1890,13 +1890,14 @@
 (%22re
-quest
+sponse
  sho
@@ -2062,33 +2062,34 @@
 BlockResponse re
-q
+sp
  = new RPCBlockR
@@ -2155,17 +2155,18 @@
 gth = re
-q
+sp
 .getEnco
@@ -2176,33 +2176,34 @@
 Length();%0A    re
-q
+sp
 .encode(mBuffer)
@@ -2332,33 +2332,34 @@
 BlockResponse re
-q
+sp
  = new RPCBlockR
@@ -2405,17 +2405,18 @@
 ;%0A    re
-q
+sp
 .encode(
@@ -2448,17 +2448,18 @@
 ponse re
-q
+sp
 2 = RPCB
@@ -2532,17 +2532,18 @@
 NGTH, re
-q
+sp
 );%0A    a
@@ -2581,17 +2581,18 @@
 NGTH, re
-q
+sp
 2);%0A  %7D%0A
@@ -2646,33 +2646,34 @@
 BlockResponse re
-q
+sp
  = new RPCBlockR
@@ -2727,25 +2727,26 @@
 sertValid(re
-q
+sp
 );%0A  %7D%0A%0A  @T
@@ -2922,17 +2922,18 @@
 ponse re
-q
+sp
  = new R
@@ -3002,17 +3002,18 @@
 Valid(re
-q
+sp
 );%0A    A
@@ -3042,17 +3042,18 @@
 load, re
-q
+sp
 .getPayl

rename req to resp