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 junit.framework.TestCase;
28  import org.junit.experimental.categories.Category;
29  
30  @Category(SmallTests.class)
31  public class TestCellSetModel extends TestModelBase<CellSetModel> {
32  
33    private static final byte[] ROW1 = Bytes.toBytes("testrow1");
34    private static final byte[] COLUMN1 = Bytes.toBytes("testcolumn1");
35    private static final byte[] VALUE1 = Bytes.toBytes("testvalue1");
36    private static final long TIMESTAMP1 = 1245219839331L;
37    private static final byte[] ROW2 = Bytes.toBytes("testrow1");
38    private static final byte[] COLUMN2 = Bytes.toBytes("testcolumn2");
39    private static final byte[] VALUE2 = Bytes.toBytes("testvalue2");
40    private static final long TIMESTAMP2 = 1245239813319L;
41    private static final byte[] COLUMN3 = Bytes.toBytes("testcolumn3");
42    private static final byte[] VALUE3 = Bytes.toBytes("testvalue3");
43    private static final long TIMESTAMP3 = 1245393318192L;
44  
45    public TestCellSetModel() throws Exception {
46      super(CellSetModel.class);
47      AS_XML =
48        "<CellSet>" +
49          "<Row key=\"dGVzdHJvdzE=\">" +
50            "<Cell timestamp=\"1245219839331\" column=\"dGVzdGNvbHVtbjE=\">" +
51              "dGVzdHZhbHVlMQ==</Cell>" +
52            "</Row>" +
53          "<Row key=\"dGVzdHJvdzE=\">" +
54            "<Cell timestamp=\"1245239813319\" column=\"dGVzdGNvbHVtbjI=\">" +
55              "dGVzdHZhbHVlMg==</Cell>" +
56            "<Cell timestamp=\"1245393318192\" column=\"dGVzdGNvbHVtbjM=\">" +
57              "dGVzdHZhbHVlMw==</Cell>" +
58            "</Row>" +
59          "</CellSet>";
60  
61      AS_PB =
62        "CiwKCHRlc3Ryb3cxEiASC3Rlc3Rjb2x1bW4xGOO6i+eeJCIKdGVzdHZhbHVlMQpOCgh0ZXN0cm93" +
63        "MRIgEgt0ZXN0Y29sdW1uMhjHyc7wniQiCnRlc3R2YWx1ZTISIBILdGVzdGNvbHVtbjMYsOLnuZ8k" +
64        "Igp0ZXN0dmFsdWUz";
65  
66      AS_XML =
67        "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><CellSet>" +
68        "<Row key=\"dGVzdHJvdzE=\"><Cell column=\"dGVzdGNvbHVtbjE=\" timestamp=\"1245219839331\">" +
69        "dGVzdHZhbHVlMQ==</Cell></Row><Row key=\"dGVzdHJvdzE=\">" +
70        "<Cell column=\"dGVzdGNvbHVtbjI=\" timestamp=\"1245239813319\">" +
71        "dGVzdHZhbHVlMg==</Cell>" +
72        "<Cell column=\"dGVzdGNvbHVtbjM=\" timestamp=\"1245393318192\">dGVzdHZhbHVlMw==</Cell>" +
73        "</Row></CellSet>";
74  
75      AS_JSON =
76        "{\"Row\":[{\"key\":\"dGVzdHJvdzE=\"," +
77        "\"Cell\":[{\"column\":\"dGVzdGNvbHVtbjE=\",\"timestamp\":1245219839331," +
78        "\"$\":\"dGVzdHZhbHVlMQ==\"}]},{\"key\":\"dGVzdHJvdzE=\"," +
79        "\"Cell\":[{\"column\":\"dGVzdGNvbHVtbjI=\",\"timestamp\":1245239813319," +
80        "\"$\":\"dGVzdHZhbHVlMg==\"},{\"column\":\"dGVzdGNvbHVtbjM=\"," +
81        "\"timestamp\":1245393318192,\"$\":\"dGVzdHZhbHVlMw==\"}]}]}";
82    }
83  
84    protected CellSetModel buildTestModel() {
85      CellSetModel model = new CellSetModel();
86      RowModel row;
87      row = new RowModel();
88      row.setKey(ROW1);
89      row.addCell(new CellModel(COLUMN1, TIMESTAMP1, VALUE1));
90      model.addRow(row);
91      row = new RowModel();
92      row.setKey(ROW2);
93      row.addCell(new CellModel(COLUMN2, TIMESTAMP2, VALUE2));
94      row.addCell(new CellModel(COLUMN3, TIMESTAMP3, VALUE3));
95      model.addRow(row);
96      return model;
97    }
98  
99    protected void checkModel(CellSetModel model) {
100     Iterator<RowModel> rows = model.getRows().iterator();
101     RowModel row = rows.next();
102     assertTrue(Bytes.equals(ROW1, row.getKey()));
103     Iterator<CellModel> cells = row.getCells().iterator();
104     CellModel cell = cells.next();
105     assertTrue(Bytes.equals(COLUMN1, cell.getColumn()));
106     assertTrue(Bytes.equals(VALUE1, cell.getValue()));
107     assertTrue(cell.hasUserTimestamp());
108     assertEquals(cell.getTimestamp(), TIMESTAMP1);
109     assertFalse(cells.hasNext());
110     row = rows.next();
111     assertTrue(Bytes.equals(ROW2, row.getKey()));
112     cells = row.getCells().iterator();
113     cell = cells.next();
114     assertTrue(Bytes.equals(COLUMN2, cell.getColumn()));
115     assertTrue(Bytes.equals(VALUE2, cell.getValue()));
116     assertTrue(cell.hasUserTimestamp());
117     assertEquals(cell.getTimestamp(), TIMESTAMP2);
118     cell = cells.next();
119     assertTrue(Bytes.equals(COLUMN3, cell.getColumn()));
120     assertTrue(Bytes.equals(VALUE3, cell.getValue()));
121     assertTrue(cell.hasUserTimestamp());
122     assertEquals(cell.getTimestamp(), TIMESTAMP3);
123     assertFalse(cells.hasNext());
124   }
125 
126   public void testBuildModel() throws Exception {
127     checkModel(buildTestModel());
128   }
129 
130   public void testFromXML() throws Exception {
131     checkModel(fromXML(AS_XML));
132   }
133 
134   public void testFromPB() throws Exception {
135     checkModel(fromPB(AS_PB));
136   }
137 
138 }
139