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.commons.lang3.StringUtils;
026import org.apache.commons.lang3.builder.ToStringBuilder;
027import org.apache.commons.lang3.builder.ToStringStyle;
028import org.apache.hadoop.hbase.HBaseClassTestRule;
029import org.apache.hadoop.hbase.testclassification.RestTests;
030import org.apache.hadoop.hbase.testclassification.SmallTests;
031import org.apache.hadoop.hbase.util.Bytes;
032import org.junit.ClassRule;
033import org.junit.Test;
034import org.junit.experimental.categories.Category;
035
036@Category({RestTests.class, SmallTests.class})
037public class TestRowModel extends TestModelBase<RowModel> {
038
039  @ClassRule
040  public static final HBaseClassTestRule CLASS_RULE =
041      HBaseClassTestRule.forClass(TestRowModel.class);
042
043  private static final byte[] ROW1 = Bytes.toBytes("testrow1");
044  private static final byte[] COLUMN1 = Bytes.toBytes("testcolumn1");
045  private static final byte[] VALUE1 = Bytes.toBytes("testvalue1");
046  private static final long TIMESTAMP1 = 1245219839331L;
047
048  public TestRowModel() throws Exception {
049    super(RowModel.class);
050    AS_XML =
051      "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Row key=\"dGVzdHJvdzE=\">" +
052      "<Cell column=\"dGVzdGNvbHVtbjE=\" timestamp=\"1245219839331\">dGVzdHZhbHVlMQ==</Cell></Row>";
053
054    AS_JSON =
055      "{\"key\":\"dGVzdHJvdzE=\",\"Cell\":[{\"column\":\"dGVzdGNvbHVtbjE=\"," +
056      "\"timestamp\":1245219839331,\"$\":\"dGVzdHZhbHVlMQ==\"}]}";
057  }
058
059  @Override
060  protected RowModel buildTestModel() {
061    RowModel model = new RowModel();
062    model.setKey(ROW1);
063    model.addCell(new CellModel(COLUMN1, TIMESTAMP1, VALUE1));
064    return model;
065  }
066
067  @Override
068  protected void checkModel(RowModel model) {
069    assertTrue(Bytes.equals(ROW1, model.getKey()));
070    Iterator<CellModel> cells = model.getCells().iterator();
071    CellModel cell = cells.next();
072    assertTrue(Bytes.equals(COLUMN1, cell.getColumn()));
073    assertTrue(Bytes.equals(VALUE1, cell.getValue()));
074    assertTrue(cell.hasUserTimestamp());
075    assertEquals(TIMESTAMP1, cell.getTimestamp());
076    assertFalse(cells.hasNext());
077  }
078
079  @Override
080  public void testFromPB() throws Exception {
081    //do nothing row model has no PB
082  }
083
084  @Test
085  public void testEquals() throws Exception {
086    RowModel rowModel1 = buildTestModel();
087    RowModel rowModel2 = buildTestModel();
088
089    assertEquals(rowModel1, rowModel2);
090
091    RowModel rowModel3 = new RowModel();
092    assertFalse(rowModel1.equals(rowModel3));
093  }
094
095  @Test
096  public void testToString() throws Exception {
097    String expectedRowKey = ToStringBuilder.reflectionToString(ROW1, ToStringStyle.SIMPLE_STYLE);
098
099    RowModel rowModel = buildTestModel();
100    System.out.println(rowModel);
101
102    assertTrue(StringUtils.contains(rowModel.toString(), expectedRowKey));
103  }
104}
105