View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.rest.model;
21  
22  import java.util.Iterator;
23  
24  import org.apache.hadoop.hbase.testclassification.SmallTests;
25  import org.apache.hadoop.hbase.util.Bytes;
26  
27  import org.junit.experimental.categories.Category;
28  
29  @Category(SmallTests.class)
30  public class TestCellSetModel extends TestModelBase<CellSetModel> {
31  
32    private static final byte[] ROW1 = Bytes.toBytes("testrow1");
33    private static final byte[] COLUMN1 = Bytes.toBytes("testcolumn1");
34    private static final byte[] VALUE1 = Bytes.toBytes("testvalue1");
35    private static final long TIMESTAMP1 = 1245219839331L;
36    private static final byte[] ROW2 = Bytes.toBytes("testrow1");
37    private static final byte[] COLUMN2 = Bytes.toBytes("testcolumn2");
38    private static final byte[] VALUE2 = Bytes.toBytes("testvalue2");
39    private static final long TIMESTAMP2 = 1245239813319L;
40    private static final byte[] COLUMN3 = Bytes.toBytes("testcolumn3");
41    private static final byte[] VALUE3 = Bytes.toBytes("testvalue3");
42    private static final long TIMESTAMP3 = 1245393318192L;
43  
44    public TestCellSetModel() throws Exception {
45      super(CellSetModel.class);
46      AS_XML =
47        "<CellSet>" +
48          "<Row key=\"dGVzdHJvdzE=\">" +
49            "<Cell timestamp=\"1245219839331\" column=\"dGVzdGNvbHVtbjE=\">" +
50              "dGVzdHZhbHVlMQ==</Cell>" +
51            "</Row>" +
52          "<Row key=\"dGVzdHJvdzE=\">" +
53            "<Cell timestamp=\"1245239813319\" column=\"dGVzdGNvbHVtbjI=\">" +
54              "dGVzdHZhbHVlMg==</Cell>" +
55            "<Cell timestamp=\"1245393318192\" column=\"dGVzdGNvbHVtbjM=\">" +
56              "dGVzdHZhbHVlMw==</Cell>" +
57            "</Row>" +
58          "</CellSet>";
59  
60      AS_PB =
61        "CiwKCHRlc3Ryb3cxEiASC3Rlc3Rjb2x1bW4xGOO6i+eeJCIKdGVzdHZhbHVlMQpOCgh0ZXN0cm93" +
62        "MRIgEgt0ZXN0Y29sdW1uMhjHyc7wniQiCnRlc3R2YWx1ZTISIBILdGVzdGNvbHVtbjMYsOLnuZ8k" +
63        "Igp0ZXN0dmFsdWUz";
64  
65      AS_XML =
66        "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><CellSet>" +
67        "<Row key=\"dGVzdHJvdzE=\"><Cell column=\"dGVzdGNvbHVtbjE=\" timestamp=\"1245219839331\">" +
68        "dGVzdHZhbHVlMQ==</Cell></Row><Row key=\"dGVzdHJvdzE=\">" +
69        "<Cell column=\"dGVzdGNvbHVtbjI=\" timestamp=\"1245239813319\">" +
70        "dGVzdHZhbHVlMg==</Cell>" +
71        "<Cell column=\"dGVzdGNvbHVtbjM=\" timestamp=\"1245393318192\">dGVzdHZhbHVlMw==</Cell>" +
72        "</Row></CellSet>";
73  
74      AS_JSON =
75        "{\"Row\":[{\"key\":\"dGVzdHJvdzE=\"," +
76        "\"Cell\":[{\"column\":\"dGVzdGNvbHVtbjE=\",\"timestamp\":1245219839331," +
77        "\"$\":\"dGVzdHZhbHVlMQ==\"}]},{\"key\":\"dGVzdHJvdzE=\"," +
78        "\"Cell\":[{\"column\":\"dGVzdGNvbHVtbjI=\",\"timestamp\":1245239813319," +
79        "\"$\":\"dGVzdHZhbHVlMg==\"},{\"column\":\"dGVzdGNvbHVtbjM=\"," +
80        "\"timestamp\":1245393318192,\"$\":\"dGVzdHZhbHVlMw==\"}]}]}";
81    }
82  
83    protected CellSetModel buildTestModel() {
84      CellSetModel model = new CellSetModel();
85      RowModel row;
86      row = new RowModel();
87      row.setKey(ROW1);
88      row.addCell(new CellModel(COLUMN1, TIMESTAMP1, VALUE1));
89      model.addRow(row);
90      row = new RowModel();
91      row.setKey(ROW2);
92      row.addCell(new CellModel(COLUMN2, TIMESTAMP2, VALUE2));
93      row.addCell(new CellModel(COLUMN3, TIMESTAMP3, VALUE3));
94      model.addRow(row);
95      return model;
96    }
97  
98    protected void checkModel(CellSetModel model) {
99      Iterator<RowModel> rows = model.getRows().iterator();
100     RowModel row = rows.next();
101     assertTrue(Bytes.equals(ROW1, row.getKey()));
102     Iterator<CellModel> cells = row.getCells().iterator();
103     CellModel cell = cells.next();
104     assertTrue(Bytes.equals(COLUMN1, cell.getColumn()));
105     assertTrue(Bytes.equals(VALUE1, cell.getValue()));
106     assertTrue(cell.hasUserTimestamp());
107     assertEquals(cell.getTimestamp(), TIMESTAMP1);
108     assertFalse(cells.hasNext());
109     row = rows.next();
110     assertTrue(Bytes.equals(ROW2, row.getKey()));
111     cells = row.getCells().iterator();
112     cell = cells.next();
113     assertTrue(Bytes.equals(COLUMN2, cell.getColumn()));
114     assertTrue(Bytes.equals(VALUE2, cell.getValue()));
115     assertTrue(cell.hasUserTimestamp());
116     assertEquals(cell.getTimestamp(), TIMESTAMP2);
117     cell = cells.next();
118     assertTrue(Bytes.equals(COLUMN3, cell.getColumn()));
119     assertTrue(Bytes.equals(VALUE3, cell.getValue()));
120     assertTrue(cell.hasUserTimestamp());
121     assertEquals(cell.getTimestamp(), TIMESTAMP3);
122     assertFalse(cells.hasNext());
123   }
124 
125   public void testBuildModel() throws Exception {
126     checkModel(buildTestModel());
127   }
128 
129   public void testFromXML() throws Exception {
130     checkModel(fromXML(AS_XML));
131   }
132 
133   public void testFromPB() throws Exception {
134     checkModel(fromPB(AS_PB));
135   }
136 
137 }
138