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 = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Cell "
049      + "column=\"dGVzdGNvbHVtbg==\" timestamp=\"1245219839331\">dGVzdHZhbHVl</Cell>";
050    AS_PB = "Egp0ZXN0Y29sdW1uGOO6i+eeJCIJdGVzdHZhbHVl";
051
052    AS_JSON =
053      "{\"column\":\"dGVzdGNvbHVtbg==\",\"timestamp\":1245219839331,\"$\":\"dGVzdHZhbHVl\"}";
054  }
055
056  @Override
057  protected CellModel buildTestModel() {
058    CellModel model = new CellModel();
059    model.setColumn(COLUMN);
060    model.setTimestamp(TIMESTAMP);
061    model.setValue(VALUE);
062    return model;
063  }
064
065  @Override
066  protected void checkModel(CellModel model) {
067    assertTrue(Bytes.equals(model.getColumn(), COLUMN));
068    assertTrue(Bytes.equals(model.getValue(), VALUE));
069    assertTrue(model.hasUserTimestamp());
070    assertEquals(TIMESTAMP, model.getTimestamp());
071  }
072
073  @Override
074  public void testBuildModel() throws Exception {
075    checkModel(buildTestModel());
076  }
077
078  @Override
079  public void testFromXML() throws Exception {
080    checkModel(fromXML(AS_XML));
081  }
082
083  @Override
084  public void testFromPB() throws Exception {
085    checkModel(fromPB(AS_PB));
086  }
087
088  @Test
089  public void testEquals() throws Exception {
090    CellModel cellModel1 = buildTestModel();
091    CellModel cellModel2 = buildTestModel();
092
093    assertEquals(cellModel1, cellModel2);
094
095    CellModel cellModel3 = new CellModel();
096    assertFalse(cellModel1.equals(cellModel3));
097  }
098
099  @Test
100  public void testToString() throws Exception {
101    String expectedColumn = ToStringBuilder.reflectionToString(COLUMN, ToStringStyle.SIMPLE_STYLE);
102
103    CellModel cellModel = buildTestModel();
104    System.out.println(cellModel);
105
106    assertTrue(StringUtils.contains(cellModel.toString(), expectedColumn));
107  }
108}