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  
27  import javax.xml.bind.JAXBContext;
28  import javax.xml.bind.JAXBException;
29  
30  import org.apache.hadoop.hbase.util.Base64;
31  import org.apache.hadoop.hbase.util.Bytes;
32  
33  import junit.framework.TestCase;
34  
35  public class TestCellModel extends TestCase {
36  
37    private static final long TIMESTAMP = 1245219839331L;
38    private static final byte[] COLUMN = Bytes.toBytes("testcolumn");
39    private static final byte[] VALUE = Bytes.toBytes("testvalue");
40  
41    private static final String AS_XML =
42      "<Cell timestamp=\"1245219839331\"" +
43        " column=\"dGVzdGNvbHVtbg==\">" +
44        "dGVzdHZhbHVl</Cell>";
45  
46    private static final String AS_PB = 
47      "Egp0ZXN0Y29sdW1uGOO6i+eeJCIJdGVzdHZhbHVl";
48  
49    private JAXBContext context;
50  
51    public TestCellModel() throws JAXBException {
52      super();
53      context = JAXBContext.newInstance(CellModel.class);
54    }
55  
56    private CellModel buildTestModel() {
57      CellModel model = new CellModel();
58      model.setColumn(COLUMN);
59      model.setTimestamp(TIMESTAMP);
60      model.setValue(VALUE);
61      return model;
62    }
63  
64    @SuppressWarnings("unused")
65    private String toXML(CellModel model) throws JAXBException {
66      StringWriter writer = new StringWriter();
67      context.createMarshaller().marshal(model, writer);
68      return writer.toString();
69    }
70  
71    private CellModel fromXML(String xml) throws JAXBException {
72      return (CellModel)
73        context.createUnmarshaller().unmarshal(new StringReader(xml));
74    }
75  
76    @SuppressWarnings("unused")
77    private byte[] toPB(CellModel model) {
78      return model.createProtobufOutput();
79    }
80  
81    private CellModel fromPB(String pb) throws IOException {
82      return (CellModel) 
83        new CellModel().getObjectFromMessage(Base64.decode(AS_PB));
84    }
85  
86    private void checkModel(CellModel model) {
87      assertTrue(Bytes.equals(model.getColumn(), COLUMN));
88      assertTrue(Bytes.equals(model.getValue(), VALUE));
89      assertTrue(model.hasUserTimestamp());
90      assertEquals(model.getTimestamp(), TIMESTAMP);
91    }
92  
93    public void testBuildModel() throws Exception {
94      checkModel(buildTestModel());
95    }
96  
97    public void testFromXML() throws Exception {
98      checkModel(fromXML(AS_XML));
99    }
100 
101   public void testFromPB() throws Exception {
102     checkModel(fromPB(AS_PB));
103   }
104 }