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 =
054      "<CellSet>" +
055        "<Row key=\"dGVzdHJvdzE=\">" +
056          "<Cell timestamp=\"1245219839331\" column=\"dGVzdGNvbHVtbjE=\">" +
057            "dGVzdHZhbHVlMQ==</Cell>" +
058          "</Row>" +
059        "<Row key=\"dGVzdHJvdzE=\">" +
060          "<Cell timestamp=\"1245239813319\" column=\"dGVzdGNvbHVtbjI=\">" +
061            "dGVzdHZhbHVlMg==</Cell>" +
062          "<Cell timestamp=\"1245393318192\" column=\"dGVzdGNvbHVtbjM=\">" +
063            "dGVzdHZhbHVlMw==</Cell>" +
064          "</Row>" +
065        "</CellSet>";
066
067    AS_PB =
068      "CiwKCHRlc3Ryb3cxEiASC3Rlc3Rjb2x1bW4xGOO6i+eeJCIKdGVzdHZhbHVlMQpOCgh0ZXN0cm93" +
069      "MRIgEgt0ZXN0Y29sdW1uMhjHyc7wniQiCnRlc3R2YWx1ZTISIBILdGVzdGNvbHVtbjMYsOLnuZ8k" +
070      "Igp0ZXN0dmFsdWUz";
071
072    AS_XML =
073      "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><CellSet>" +
074      "<Row key=\"dGVzdHJvdzE=\"><Cell column=\"dGVzdGNvbHVtbjE=\" timestamp=\"1245219839331\">" +
075      "dGVzdHZhbHVlMQ==</Cell></Row><Row key=\"dGVzdHJvdzE=\">" +
076      "<Cell column=\"dGVzdGNvbHVtbjI=\" timestamp=\"1245239813319\">" +
077      "dGVzdHZhbHVlMg==</Cell>" +
078      "<Cell column=\"dGVzdGNvbHVtbjM=\" timestamp=\"1245393318192\">dGVzdHZhbHVlMw==</Cell>" +
079      "</Row></CellSet>";
080
081    AS_JSON =
082      "{\"Row\":[{\"key\":\"dGVzdHJvdzE=\"," +
083      "\"Cell\":[{\"column\":\"dGVzdGNvbHVtbjE=\",\"timestamp\":1245219839331," +
084      "\"$\":\"dGVzdHZhbHVlMQ==\"}]},{\"key\":\"dGVzdHJvdzE=\"," +
085      "\"Cell\":[{\"column\":\"dGVzdGNvbHVtbjI=\",\"timestamp\":1245239813319," +
086      "\"$\":\"dGVzdHZhbHVlMg==\"},{\"column\":\"dGVzdGNvbHVtbjM=\"," +
087      "\"timestamp\":1245393318192,\"$\":\"dGVzdHZhbHVlMw==\"}]}]}";
088  }
089
090  @Override
091  protected CellSetModel buildTestModel() {
092    CellSetModel model = new CellSetModel();
093    RowModel row;
094    row = new RowModel();
095    row.setKey(ROW1);
096    row.addCell(new CellModel(COLUMN1, TIMESTAMP1, VALUE1));
097    model.addRow(row);
098    row = new RowModel();
099    row.setKey(ROW2);
100    row.addCell(new CellModel(COLUMN2, TIMESTAMP2, VALUE2));
101    row.addCell(new CellModel(COLUMN3, TIMESTAMP3, VALUE3));
102    model.addRow(row);
103    return model;
104  }
105
106  @Override
107  protected void checkModel(CellSetModel model) {
108    Iterator<RowModel> rows = model.getRows().iterator();
109    RowModel row = rows.next();
110    assertTrue(Bytes.equals(ROW1, row.getKey()));
111    Iterator<CellModel> cells = row.getCells().iterator();
112    CellModel cell = cells.next();
113    assertTrue(Bytes.equals(COLUMN1, cell.getColumn()));
114    assertTrue(Bytes.equals(VALUE1, cell.getValue()));
115    assertTrue(cell.hasUserTimestamp());
116    assertEquals(TIMESTAMP1, cell.getTimestamp());
117    assertFalse(cells.hasNext());
118    row = rows.next();
119    assertTrue(Bytes.equals(ROW2, row.getKey()));
120    cells = row.getCells().iterator();
121    cell = cells.next();
122    assertTrue(Bytes.equals(COLUMN2, cell.getColumn()));
123    assertTrue(Bytes.equals(VALUE2, cell.getValue()));
124    assertTrue(cell.hasUserTimestamp());
125    assertEquals(TIMESTAMP2, cell.getTimestamp());
126    cell = cells.next();
127    assertTrue(Bytes.equals(COLUMN3, cell.getColumn()));
128    assertTrue(Bytes.equals(VALUE3, cell.getValue()));
129    assertTrue(cell.hasUserTimestamp());
130    assertEquals(TIMESTAMP3, cell.getTimestamp());
131    assertFalse(cells.hasNext());
132  }
133
134  @Override
135  public void testBuildModel() throws Exception {
136    checkModel(buildTestModel());
137  }
138
139  @Override
140  public void testFromXML() throws Exception {
141    checkModel(fromXML(AS_XML));
142  }
143
144  @Override
145  public void testFromPB() throws Exception {
146    checkModel(fromPB(AS_PB));
147  }
148
149}
150