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.StringReader;
23  import java.io.StringWriter;
24  import java.util.Iterator;
25  
26  import javax.xml.bind.JAXBContext;
27  import javax.xml.bind.JAXBException;
28  
29  import org.apache.hadoop.hbase.SmallTests;
30  import org.apache.hadoop.hbase.util.Bytes;
31  
32  import junit.framework.TestCase;
33  import org.junit.experimental.categories.Category;
34  
35  @Category(SmallTests.class)
36  public class TestRowModel 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  
43    private static final String AS_XML =
44      "<Row key=\"dGVzdHJvdzE=\">" + 
45        "<Cell timestamp=\"1245219839331\" column=\"dGVzdGNvbHVtbjE=\">" + 
46          "dGVzdHZhbHVlMQ==</Cell>" + 
47        "</Row>";
48  
49    private JAXBContext context;
50  
51    public TestRowModel() throws JAXBException {
52      super();
53      context = JAXBContext.newInstance(
54          CellModel.class,
55          RowModel.class);
56    }
57  
58    private RowModel buildTestModel() {
59      RowModel model = new RowModel();
60      model.setKey(ROW1);
61      model.addCell(new CellModel(COLUMN1, TIMESTAMP1, VALUE1));
62      return model;
63    }
64  
65    @SuppressWarnings("unused")
66    private String toXML(RowModel model) throws JAXBException {
67      StringWriter writer = new StringWriter();
68      context.createMarshaller().marshal(model, writer);
69      return writer.toString();
70    }
71  
72    private RowModel fromXML(String xml) throws JAXBException {
73      return (RowModel)
74        context.createUnmarshaller().unmarshal(new StringReader(xml));
75    }
76  
77    private void checkModel(RowModel model) {
78      assertTrue(Bytes.equals(ROW1, model.getKey()));
79      Iterator<CellModel> cells = model.getCells().iterator();
80      CellModel cell = cells.next();
81      assertTrue(Bytes.equals(COLUMN1, cell.getColumn()));
82      assertTrue(Bytes.equals(VALUE1, cell.getValue()));
83      assertTrue(cell.hasUserTimestamp());
84      assertEquals(cell.getTimestamp(), TIMESTAMP1);
85      assertFalse(cells.hasNext());
86    }
87  
88    public void testBuildModel() throws Exception {
89      checkModel(buildTestModel());
90    }
91  
92    public void testFromXML() throws Exception {
93      checkModel(fromXML(AS_XML));
94    }
95  
96  }
97