001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.ipc;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import java.io.IOException;
024import java.util.ArrayList;
025import java.util.List;
026import org.apache.hadoop.hbase.Cell;
027import org.apache.hadoop.hbase.CellScanner;
028import org.apache.hadoop.hbase.ExtendedCell;
029import org.apache.hadoop.hbase.ExtendedCellScannable;
030import org.apache.hadoop.hbase.ExtendedCellScanner;
031import org.apache.hadoop.hbase.HBaseClassTestRule;
032import org.apache.hadoop.hbase.testclassification.ClientTests;
033import org.apache.hadoop.hbase.testclassification.SmallTests;
034import org.apache.hadoop.hbase.util.Bytes;
035import org.junit.ClassRule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038
039@Category({ ClientTests.class, SmallTests.class })
040public class TestHBaseRpcControllerImpl {
041  @ClassRule
042  public static final HBaseClassTestRule CLASS_RULE =
043    HBaseClassTestRule.forClass(TestHBaseRpcControllerImpl.class);
044
045  @Test
046  public void testListOfCellScannerables() throws IOException {
047    final int count = 10;
048    List<ExtendedCellScannable> cells = new ArrayList<>(count);
049
050    for (int i = 0; i < count; i++) {
051      cells.add(createCell(i));
052    }
053    HBaseRpcController controller = new HBaseRpcControllerImpl(null, cells);
054    CellScanner cellScanner = controller.cellScanner();
055    int index = 0;
056    for (; cellScanner.advance(); index++) {
057      Cell cell = cellScanner.current();
058      byte[] indexBytes = Bytes.toBytes(index);
059      assertTrue("" + index, Bytes.equals(indexBytes, 0, indexBytes.length, cell.getValueArray(),
060        cell.getValueOffset(), cell.getValueLength()));
061    }
062    assertEquals(count, index);
063  }
064
065  /**
066   * @param index the index of the cell to use as its value
067   * @return A faked out 'Cell' that does nothing but return index as its value
068   */
069  static ExtendedCellScannable createCell(final int index) {
070    return new ExtendedCellScannable() {
071      @Override
072      public ExtendedCellScanner cellScanner() {
073        return new ExtendedCellScanner() {
074          @Override
075          public ExtendedCell current() {
076            // Fake out a Cell. All this Cell has is a value that is an int in size and equal
077            // to the above 'index' param serialized as an int.
078            return new ExtendedCell() {
079              @Override
080              public long heapSize() {
081                return 0;
082              }
083
084              private final int i = index;
085
086              @Override
087              public byte[] getRowArray() {
088                return null;
089              }
090
091              @Override
092              public int getRowOffset() {
093                return 0;
094              }
095
096              @Override
097              public short getRowLength() {
098                return 0;
099              }
100
101              @Override
102              public byte[] getFamilyArray() {
103                return null;
104              }
105
106              @Override
107              public int getFamilyOffset() {
108                return 0;
109              }
110
111              @Override
112              public byte getFamilyLength() {
113                return 0;
114              }
115
116              @Override
117              public byte[] getQualifierArray() {
118                return null;
119              }
120
121              @Override
122              public int getQualifierOffset() {
123                return 0;
124              }
125
126              @Override
127              public int getQualifierLength() {
128                return 0;
129              }
130
131              @Override
132              public long getTimestamp() {
133                return 0;
134              }
135
136              @Override
137              public byte getTypeByte() {
138                return 0;
139              }
140
141              @Override
142              public long getSequenceId() {
143                return 0;
144              }
145
146              @Override
147              public byte[] getValueArray() {
148                return Bytes.toBytes(this.i);
149              }
150
151              @Override
152              public int getValueOffset() {
153                return 0;
154              }
155
156              @Override
157              public int getValueLength() {
158                return Bytes.SIZEOF_INT;
159              }
160
161              @Override
162              public int getSerializedSize() {
163                return 0;
164              }
165
166              @Override
167              public int getTagsOffset() {
168                return 0;
169              }
170
171              @Override
172              public int getTagsLength() {
173                return 0;
174              }
175
176              @Override
177              public byte[] getTagsArray() {
178                return null;
179              }
180
181              @Override
182              public Type getType() {
183                return null;
184              }
185
186              @Override
187              public void setSequenceId(long seqId) throws IOException {
188              }
189
190              @Override
191              public void setTimestamp(long ts) throws IOException {
192              }
193
194              @Override
195              public void setTimestamp(byte[] ts) throws IOException {
196              }
197            };
198          }
199
200          private boolean hasCell = true;
201
202          @Override
203          public boolean advance() {
204            // We have one Cell only so return true first time then false ever after.
205            if (!hasCell) {
206              return hasCell;
207            }
208
209            hasCell = false;
210            return true;
211          }
212        };
213      }
214    };
215  }
216}