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