001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.rest.model;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import com.fasterxml.jackson.core.JsonParseException;
024import com.fasterxml.jackson.databind.JsonMappingException;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.rest.ScannerResultGenerator;
027import org.apache.hadoop.hbase.testclassification.RestTests;
028import org.apache.hadoop.hbase.testclassification.SmallTests;
029import org.apache.hadoop.hbase.util.Bytes;
030import org.junit.ClassRule;
031import org.junit.Test;
032import org.junit.experimental.categories.Category;
033
034@Category({ RestTests.class, SmallTests.class })
035public class TestScannerModel extends TestModelBase<ScannerModel> {
036  @ClassRule
037  public static final HBaseClassTestRule CLASS_RULE =
038    HBaseClassTestRule.forClass(TestScannerModel.class);
039
040  private static final String PRIVATE = "private";
041  private static final String PUBLIC = "public";
042  private static final byte[] START_ROW = Bytes.toBytes("abracadabra");
043  private static final byte[] END_ROW = Bytes.toBytes("zzyzx");
044  private static final byte[] COLUMN1 = Bytes.toBytes("column1");
045  private static final byte[] COLUMN2 = Bytes.toBytes("column2:foo");
046  private static final long START_TIME = 1245219839331L;
047  private static final long END_TIME = 1245393318192L;
048  private static final int CACHING = 1000;
049  private static final int BATCH = 100;
050  private static final boolean CACHE_BLOCKS = false;
051
052  public TestScannerModel() throws Exception {
053    super(ScannerModel.class);
054    AS_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
055      + "<Scanner batch=\"100\" cacheBlocks=\"false\" caching=\"1000\" endRow=\"enp5eng=\" "
056      + "endTime=\"1245393318192\" maxVersions=\"2147483647\" startRow=\"YWJyYWNhZGFicmE=\" "
057      + "startTime=\"1245219839331\">"
058      + "<column>Y29sdW1uMQ==</column><column>Y29sdW1uMjpmb28=</column>"
059      + "<labels>private</labels><labels>public</labels>" + "</Scanner>";
060
061    AS_JSON = "{\"batch\":100,\"caching\":1000,\"cacheBlocks\":false,\"endRow\":\"enp5eng=\","
062      + "\"endTime\":1245393318192,\"maxVersions\":2147483647,\"startRow\":\"YWJyYWNhZGFicmE=\","
063      + "\"startTime\":1245219839331,\"column\":[\"Y29sdW1uMQ==\",\"Y29sdW1uMjpmb28=\"],"
064      + "\"labels\":[\"private\",\"public\"]" + "}";
065
066    AS_PB = "CgthYnJhY2FkYWJyYRIFenp5engaB2NvbHVtbjEaC2NvbHVtbjI6Zm9vIGQo47qL554kMLDi57mf"
067      + "JDj/////B0joB1IHcHJpdmF0ZVIGcHVibGljWAA=";
068  }
069
070  @Override
071  protected ScannerModel buildTestModel() {
072    ScannerModel model = new ScannerModel();
073    model.setStartRow(START_ROW);
074    model.setEndRow(END_ROW);
075    model.addColumn(COLUMN1);
076    model.addColumn(COLUMN2);
077    model.setStartTime(START_TIME);
078    model.setEndTime(END_TIME);
079    model.setBatch(BATCH);
080    model.setCaching(CACHING);
081    model.addLabel(PRIVATE);
082    model.addLabel(PUBLIC);
083    model.setCacheBlocks(CACHE_BLOCKS);
084    return model;
085  }
086
087  @Override
088  protected void checkModel(ScannerModel model) {
089    assertTrue(Bytes.equals(model.getStartRow(), START_ROW));
090    assertTrue(Bytes.equals(model.getEndRow(), END_ROW));
091    boolean foundCol1 = false, foundCol2 = false;
092    for (byte[] column : model.getColumns()) {
093      if (Bytes.equals(column, COLUMN1)) {
094        foundCol1 = true;
095      } else if (Bytes.equals(column, COLUMN2)) {
096        foundCol2 = true;
097      }
098    }
099    assertTrue(foundCol1);
100    assertTrue(foundCol2);
101    assertEquals(START_TIME, model.getStartTime());
102    assertEquals(END_TIME, model.getEndTime());
103    assertEquals(BATCH, model.getBatch());
104    assertEquals(CACHING, model.getCaching());
105    assertEquals(CACHE_BLOCKS, model.getCacheBlocks());
106    boolean foundLabel1 = false;
107    boolean foundLabel2 = false;
108    if (model.getLabels() != null && model.getLabels().size() > 0) {
109      for (String label : model.getLabels()) {
110        if (label.equals(PRIVATE)) {
111          foundLabel1 = true;
112        } else if (label.equals(PUBLIC)) {
113          foundLabel2 = true;
114        }
115      }
116      assertTrue(foundLabel1);
117      assertTrue(foundLabel2);
118    }
119  }
120
121  @Test
122  public void testExistingFilter() throws Exception {
123    final String CORRECT_FILTER = "{\"type\": \"PrefixFilter\", \"value\": \"cg==\"}";
124    verifyException(CORRECT_FILTER);
125  }
126
127  @Test(expected = IllegalArgumentException.class)
128  public void testNonExistingFilter() throws Exception {
129    final String UNKNOWN_FILTER = "{\"type\": \"UnknownFilter\", \"value\": \"cg==\"}";
130    verifyException(UNKNOWN_FILTER);
131  }
132
133  @Test(expected = JsonMappingException.class)
134  public void testIncorrectFilterThrowsJME() throws Exception {
135    final String JME_FILTER = "{\"invalid_tag\": \"PrefixFilter\", \"value\": \"cg==\"}";
136    verifyException(JME_FILTER);
137  }
138
139  @Test(expected = JsonParseException.class)
140  public void tesIncorrecttFilterThrowsJPE() throws Exception {
141    final String JPE_FILTER = "{\"type\": \"PrefixFilter\",, \"value\": \"cg==\"}";
142    verifyException(JPE_FILTER);
143  }
144
145  private void verifyException(final String FILTER) throws Exception {
146    ScannerModel model = new ScannerModel();
147    model.setFilter(FILTER);
148    ScannerResultGenerator.buildFilterFromModel(model);
149  }
150}