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.rest.model;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertFalse;
022import static org.junit.jupiter.api.Assertions.assertTrue;
023
024import java.util.Iterator;
025import org.apache.hadoop.hbase.testclassification.RestTests;
026import org.apache.hadoop.hbase.testclassification.SmallTests;
027import org.apache.hadoop.hbase.util.Bytes;
028import org.junit.jupiter.api.Tag;
029import org.junit.jupiter.api.Test;
030
031@Tag(RestTests.TAG)
032@Tag(SmallTests.TAG)
033public class TestCellSetModel extends TestModelBase<CellSetModel> {
034
035  private static final byte[] ROW1 = Bytes.toBytes("testrow1");
036  private static final byte[] COLUMN1 = Bytes.toBytes("testcolumn1");
037  private static final byte[] VALUE1 = Bytes.toBytes("testvalue1");
038  private static final long TIMESTAMP1 = 1245219839331L;
039  private static final byte[] ROW2 = Bytes.toBytes("testrow1");
040  private static final byte[] COLUMN2 = Bytes.toBytes("testcolumn2");
041  private static final byte[] VALUE2 = Bytes.toBytes("testvalue2");
042  private static final long TIMESTAMP2 = 1245239813319L;
043  private static final byte[] COLUMN3 = Bytes.toBytes("testcolumn3");
044  private static final byte[] VALUE3 = Bytes.toBytes("testvalue3");
045  private static final long TIMESTAMP3 = 1245393318192L;
046
047  public TestCellSetModel() throws Exception {
048    super(CellSetModel.class);
049
050    AS_PB = "CiwKCHRlc3Ryb3cxEiASC3Rlc3Rjb2x1bW4xGOO6i+eeJCIKdGVzdHZhbHVlMQpOCgh0ZXN0cm93"
051      + "MRIgEgt0ZXN0Y29sdW1uMhjHyc7wniQiCnRlc3R2YWx1ZTISIBILdGVzdGNvbHVtbjMYsOLnuZ8k"
052      + "Igp0ZXN0dmFsdWUz";
053
054    AS_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><CellSet>"
055      + "<Row key=\"dGVzdHJvdzE=\"><Cell column=\"dGVzdGNvbHVtbjE=\" timestamp=\"1245219839331\">"
056      + "dGVzdHZhbHVlMQ==</Cell></Row><Row key=\"dGVzdHJvdzE=\">"
057      + "<Cell column=\"dGVzdGNvbHVtbjI=\" timestamp=\"1245239813319\">" + "dGVzdHZhbHVlMg==</Cell>"
058      + "<Cell column=\"dGVzdGNvbHVtbjM=\" timestamp=\"1245393318192\">dGVzdHZhbHVlMw==</Cell>"
059      + "</Row></CellSet>";
060
061    AS_JSON = "{\"Row\":[{\"key\":\"dGVzdHJvdzE=\","
062      + "\"Cell\":[{\"column\":\"dGVzdGNvbHVtbjE=\",\"timestamp\":1245219839331,"
063      + "\"$\":\"dGVzdHZhbHVlMQ==\"}]},{\"key\":\"dGVzdHJvdzE=\","
064      + "\"Cell\":[{\"column\":\"dGVzdGNvbHVtbjI=\",\"timestamp\":1245239813319,"
065      + "\"$\":\"dGVzdHZhbHVlMg==\"},{\"column\":\"dGVzdGNvbHVtbjM=\","
066      + "\"timestamp\":1245393318192,\"$\":\"dGVzdHZhbHVlMw==\"}]}]}";
067  }
068
069  @Override
070  protected CellSetModel buildTestModel() {
071    CellSetModel model = new CellSetModel();
072    RowModel row;
073    row = new RowModel();
074    row.setKey(ROW1);
075    row.addCell(new CellModel(COLUMN1, TIMESTAMP1, VALUE1));
076    model.addRow(row);
077    row = new RowModel();
078    row.setKey(ROW2);
079    row.addCell(new CellModel(COLUMN2, TIMESTAMP2, VALUE2));
080    row.addCell(new CellModel(COLUMN3, TIMESTAMP3, VALUE3));
081    model.addRow(row);
082    return model;
083  }
084
085  @Override
086  protected void checkModel(CellSetModel model) {
087    Iterator<RowModel> rows = model.getRows().iterator();
088    RowModel row = rows.next();
089    assertTrue(Bytes.equals(ROW1, row.getKey()));
090    Iterator<CellModel> cells = row.getCells().iterator();
091    CellModel cell = cells.next();
092    assertTrue(Bytes.equals(COLUMN1, cell.getColumn()));
093    assertTrue(Bytes.equals(VALUE1, cell.getValue()));
094    assertTrue(cell.hasUserTimestamp());
095    assertEquals(TIMESTAMP1, cell.getTimestamp());
096    assertFalse(cells.hasNext());
097    row = rows.next();
098    assertTrue(Bytes.equals(ROW2, row.getKey()));
099    cells = row.getCells().iterator();
100    cell = cells.next();
101    assertTrue(Bytes.equals(COLUMN2, cell.getColumn()));
102    assertTrue(Bytes.equals(VALUE2, cell.getValue()));
103    assertTrue(cell.hasUserTimestamp());
104    assertEquals(TIMESTAMP2, cell.getTimestamp());
105    cell = cells.next();
106    assertTrue(Bytes.equals(COLUMN3, cell.getColumn()));
107    assertTrue(Bytes.equals(VALUE3, cell.getValue()));
108    assertTrue(cell.hasUserTimestamp());
109    assertEquals(TIMESTAMP3, cell.getTimestamp());
110    assertFalse(cells.hasNext());
111  }
112
113  @Override
114  @Test
115  public void testBuildModel() throws Exception {
116    checkModel(buildTestModel());
117  }
118
119  @Override
120  @Test
121  public void testFromXML() throws Exception {
122    checkModel(fromXML(AS_XML));
123  }
124
125  @Override
126  @Test
127  public void testFromPB() throws Exception {
128    checkModel(fromPB(AS_PB));
129  }
130}