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