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 TestCase {
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    private static final String AS_XML =
53      "<CellSet>" + 
54        "<Row key=\"dGVzdHJvdzE=\">" + 
55          "<Cell timestamp=\"1245219839331\" column=\"dGVzdGNvbHVtbjE=\">" + 
56            "dGVzdHZhbHVlMQ==</Cell>" + 
57          "</Row>" + 
58        "<Row key=\"dGVzdHJvdzE=\">" + 
59          "<Cell timestamp=\"1245239813319\" column=\"dGVzdGNvbHVtbjI=\">" +
60            "dGVzdHZhbHVlMg==</Cell>" + 
61          "<Cell timestamp=\"1245393318192\" column=\"dGVzdGNvbHVtbjM=\">" + 
62            "dGVzdHZhbHVlMw==</Cell>" + 
63          "</Row>" +
64        "</CellSet>";
65  
66    private static final String AS_PB = 
67      "CiwKCHRlc3Ryb3cxEiASC3Rlc3Rjb2x1bW4xGOO6i+eeJCIKdGVzdHZhbHVlMQpOCgh0ZXN0cm93" +
68      "MRIgEgt0ZXN0Y29sdW1uMhjHyc7wniQiCnRlc3R2YWx1ZTISIBILdGVzdGNvbHVtbjMYsOLnuZ8k" +
69      "Igp0ZXN0dmFsdWUz";
70  
71    private JAXBContext context;
72  
73    public TestCellSetModel() throws JAXBException {
74      super();
75      context = JAXBContext.newInstance(
76          CellModel.class,
77          CellSetModel.class,
78          RowModel.class);
79    }
80  
81    private CellSetModel buildTestModel() {
82      CellSetModel model = new CellSetModel();
83      RowModel row;
84      row = new RowModel();
85      row.setKey(ROW1);
86      row.addCell(new CellModel(COLUMN1, TIMESTAMP1, VALUE1));
87      model.addRow(row);
88      row = new RowModel();
89      row.setKey(ROW2);
90      row.addCell(new CellModel(COLUMN2, TIMESTAMP2, VALUE2));
91      row.addCell(new CellModel(COLUMN3, TIMESTAMP3, VALUE3));
92      model.addRow(row);
93      return model;
94    }
95  
96    @SuppressWarnings("unused")
97    private String toXML(CellSetModel model) throws JAXBException {
98      StringWriter writer = new StringWriter();
99      context.createMarshaller().marshal(model, writer);
100     return writer.toString();
101   }
102 
103   private CellSetModel fromXML(String xml) throws JAXBException {
104     return (CellSetModel)
105       context.createUnmarshaller().unmarshal(new StringReader(xml));
106   }
107 
108   @SuppressWarnings("unused")
109   private byte[] toPB(CellSetModel model) {
110     return model.createProtobufOutput();
111   }
112 
113   private CellSetModel fromPB(String pb) throws IOException {
114     return (CellSetModel) 
115       new CellSetModel().getObjectFromMessage(Base64.decode(AS_PB));
116   }
117 
118   private void checkModel(CellSetModel model) {
119     Iterator<RowModel> rows = model.getRows().iterator();
120     RowModel row = rows.next();
121     assertTrue(Bytes.equals(ROW1, row.getKey()));
122     Iterator<CellModel> cells = row.getCells().iterator();
123     CellModel cell = cells.next();
124     assertTrue(Bytes.equals(COLUMN1, cell.getColumn()));
125     assertTrue(Bytes.equals(VALUE1, cell.getValue()));
126     assertTrue(cell.hasUserTimestamp());
127     assertEquals(cell.getTimestamp(), TIMESTAMP1);
128     assertFalse(cells.hasNext());
129     row = rows.next();
130     assertTrue(Bytes.equals(ROW2, row.getKey()));
131     cells = row.getCells().iterator();
132     cell = cells.next();
133     assertTrue(Bytes.equals(COLUMN2, cell.getColumn()));
134     assertTrue(Bytes.equals(VALUE2, cell.getValue()));
135     assertTrue(cell.hasUserTimestamp());
136     assertEquals(cell.getTimestamp(), TIMESTAMP2);
137     cell = cells.next();
138     assertTrue(Bytes.equals(COLUMN3, cell.getColumn()));
139     assertTrue(Bytes.equals(VALUE3, cell.getValue()));
140     assertTrue(cell.hasUserTimestamp());
141     assertEquals(cell.getTimestamp(), TIMESTAMP3);
142     assertFalse(cells.hasNext());
143   }
144 
145   public void testBuildModel() throws Exception {
146     checkModel(buildTestModel());
147   }
148 
149   public void testFromXML() throws Exception {
150     checkModel(fromXML(AS_XML));
151   }
152 
153   public void testFromPB() throws Exception {
154     checkModel(fromPB(AS_PB));
155   }
156 
157 }
158