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