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 TestScannerModel extends TestCase {
39    private static final byte[] START_ROW = Bytes.toBytes("abracadabra");
40    private static final byte[] END_ROW = Bytes.toBytes("zzyzx");
41    private static final byte[] COLUMN1 = Bytes.toBytes("column1");
42    private static final byte[] COLUMN2 = Bytes.toBytes("column2:foo");
43    private static final long START_TIME = 1245219839331L;
44    private static final long END_TIME = 1245393318192L;
45    private static final int BATCH = 100;
46  
47    private static final String AS_XML =
48      "<Scanner startTime=\"1245219839331\"" +
49        " startRow=\"YWJyYWNhZGFicmE=\"" + 
50        " endTime=\"1245393318192\"" +
51        " endRow=\"enp5eng=\"" +
52        " batch=\"100\">" +
53          "<column>Y29sdW1uMQ==</column>" +
54          "<column>Y29sdW1uMjpmb28=</column>" +
55        "</Scanner>";
56  
57    private static final String AS_PB = 
58      "CgthYnJhY2FkYWJyYRIFenp5engaB2NvbHVtbjEaC2NvbHVtbjI6Zm9vIGQo47qL554kMLDi57mf" +
59      "JA==";
60  
61    private JAXBContext context;
62  
63    public TestScannerModel() throws JAXBException {
64      super();
65      context = JAXBContext.newInstance(ScannerModel.class);
66    }
67  
68    private ScannerModel buildTestModel() {
69      ScannerModel model = new ScannerModel();
70      model.setStartRow(START_ROW);
71      model.setEndRow(END_ROW);
72      model.addColumn(COLUMN1);
73      model.addColumn(COLUMN2);
74      model.setStartTime(START_TIME);
75      model.setEndTime(END_TIME);
76      model.setBatch(BATCH);
77      return model;
78    }
79  
80    @SuppressWarnings("unused")
81    private String toXML(ScannerModel model) throws JAXBException {
82      StringWriter writer = new StringWriter();
83      context.createMarshaller().marshal(model, writer);
84      return writer.toString();
85    }
86  
87    private ScannerModel fromXML(String xml) throws JAXBException {
88      return (ScannerModel)
89        context.createUnmarshaller().unmarshal(new StringReader(xml));
90    }
91  
92    @SuppressWarnings("unused")
93    private byte[] toPB(ScannerModel model) {
94      return model.createProtobufOutput();
95    }
96  
97    private ScannerModel fromPB(String pb) throws IOException {
98      return (ScannerModel) 
99        new ScannerModel().getObjectFromMessage(Base64.decode(AS_PB));
100   }
101 
102   private void checkModel(ScannerModel model) {
103     assertTrue(Bytes.equals(model.getStartRow(), START_ROW));
104     assertTrue(Bytes.equals(model.getEndRow(), END_ROW));
105     boolean foundCol1 = false, foundCol2 = false;
106     for (byte[] column: model.getColumns()) {
107       if (Bytes.equals(column, COLUMN1)) {
108         foundCol1 = true;
109       } else if (Bytes.equals(column, COLUMN2)) {
110         foundCol2 = true;
111       }
112     }
113     assertTrue(foundCol1);
114     assertTrue(foundCol2);
115     assertEquals(model.getStartTime(), START_TIME);
116     assertEquals(model.getEndTime(), END_TIME);
117     assertEquals(model.getBatch(), BATCH);
118   }
119 
120   public void testBuildModel() throws Exception {
121     checkModel(buildTestModel());
122   }
123 
124   public void testFromXML() throws Exception {
125     checkModel(fromXML(AS_XML));
126   }
127 
128   public void testFromPB() throws Exception {
129     checkModel(fromPB(AS_PB));
130   }
131 
132   @org.junit.Rule
133   public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
134     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
135 }
136