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  
26  import javax.xml.bind.JAXBContext;
27  import javax.xml.bind.JAXBException;
28  
29  import com.sun.jersey.api.json.JSONJAXBContext;
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 TestModelBase<CellModel> {
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    public TestCellModel() throws Exception {
45      super(CellModel.class);
46      AS_XML =
47        "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><Cell " +
48            "column=\"dGVzdGNvbHVtbg==\" timestamp=\"1245219839331\">dGVzdHZhbHVl</Cell>";
49      AS_PB =
50        "Egp0ZXN0Y29sdW1uGOO6i+eeJCIJdGVzdHZhbHVl";
51  
52      AS_JSON =
53        "{\"column\":\"dGVzdGNvbHVtbg==\",\"timestamp\":1245219839331,\"$\":\"dGVzdHZhbHVl\"}";
54    }
55  
56    protected 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    protected void checkModel(CellModel model) {
65      assertTrue(Bytes.equals(model.getColumn(), COLUMN));
66      assertTrue(Bytes.equals(model.getValue(), VALUE));
67      assertTrue(model.hasUserTimestamp());
68      assertEquals(model.getTimestamp(), TIMESTAMP);
69    }
70  
71    public void testBuildModel() throws Exception {
72      checkModel(buildTestModel());
73    }
74  
75    public void testFromXML() throws Exception {
76      checkModel(fromXML(AS_XML));
77    }
78  
79    public void testFromPB() throws Exception {
80      checkModel(fromPB(AS_PB));
81    }
82  
83  }
84