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