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 org.apache.commons.lang3.StringUtils;
025import org.apache.commons.lang3.builder.ToStringBuilder;
026import org.apache.commons.lang3.builder.ToStringStyle;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.testclassification.RestTests;
029import org.apache.hadoop.hbase.testclassification.SmallTests;
030import org.apache.hadoop.hbase.util.Bytes;
031import org.junit.ClassRule;
032import org.junit.Test;
033import org.junit.experimental.categories.Category;
034
035@Category({RestTests.class, SmallTests.class})
036public class TestCellModel extends TestModelBase<CellModel> {
037
038  @ClassRule
039  public static final HBaseClassTestRule CLASS_RULE =
040      HBaseClassTestRule.forClass(TestCellModel.class);
041
042  private static final long TIMESTAMP = 1245219839331L;
043  private static final byte[] COLUMN = Bytes.toBytes("testcolumn");
044  private static final byte[] VALUE = Bytes.toBytes("testvalue");
045
046  public TestCellModel() throws Exception {
047    super(CellModel.class);
048    AS_XML =
049      "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Cell " +
050          "column=\"dGVzdGNvbHVtbg==\" timestamp=\"1245219839331\">dGVzdHZhbHVl</Cell>";
051    AS_PB =
052      "Egp0ZXN0Y29sdW1uGOO6i+eeJCIJdGVzdHZhbHVl";
053
054    AS_JSON =
055      "{\"column\":\"dGVzdGNvbHVtbg==\",\"timestamp\":1245219839331,\"$\":\"dGVzdHZhbHVl\"}";
056  }
057
058  @Override
059  protected CellModel buildTestModel() {
060    CellModel model = new CellModel();
061    model.setColumn(COLUMN);
062    model.setTimestamp(TIMESTAMP);
063    model.setValue(VALUE);
064    return model;
065  }
066
067  @Override
068  protected void checkModel(CellModel model) {
069    assertTrue(Bytes.equals(model.getColumn(), COLUMN));
070    assertTrue(Bytes.equals(model.getValue(), VALUE));
071    assertTrue(model.hasUserTimestamp());
072    assertEquals(TIMESTAMP, model.getTimestamp());
073  }
074
075  @Override
076  public void testBuildModel() throws Exception {
077    checkModel(buildTestModel());
078  }
079
080  @Override
081  public void testFromXML() throws Exception {
082    checkModel(fromXML(AS_XML));
083  }
084
085  @Override
086  public void testFromPB() throws Exception {
087    checkModel(fromPB(AS_PB));
088  }
089
090  @Test
091  public void testEquals() throws Exception {
092    CellModel cellModel1 = buildTestModel();
093    CellModel cellModel2 = buildTestModel();
094
095    assertEquals(cellModel1, cellModel2);
096
097    CellModel cellModel3 = new CellModel();
098    assertFalse(cellModel1.equals(cellModel3));
099  }
100
101  @Test
102  public void testToString() throws Exception {
103    String expectedColumn = ToStringBuilder.reflectionToString(COLUMN, ToStringStyle.SIMPLE_STYLE);
104
105    CellModel cellModel = buildTestModel();
106    System.out.println(cellModel);
107
108    assertTrue(StringUtils.contains(cellModel.toString(), expectedColumn));
109  }
110}
111