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