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