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