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