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