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