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;
025
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.rest.ScannerResultGenerator;
028import org.apache.hadoop.hbase.testclassification.RestTests;
029import org.apache.hadoop.hbase.testclassification.SmallTests;
030import org.apache.hadoop.hbase.util.Bytes;
031import org.junit.ClassRule;
032import org.junit.Test;
033import org.junit.experimental.categories.Category;
034
035@Category({RestTests.class, SmallTests.class})
036public class TestScannerModel extends TestModelBase<ScannerModel> {
037  @ClassRule
038  public static final HBaseClassTestRule CLASS_RULE =
039      HBaseClassTestRule.forClass(TestScannerModel.class);
040
041  private static final String PRIVATE = "private";
042  private static final String PUBLIC = "public";
043  private static final byte[] START_ROW = Bytes.toBytes("abracadabra");
044  private static final byte[] END_ROW = Bytes.toBytes("zzyzx");
045  private static final byte[] COLUMN1 = Bytes.toBytes("column1");
046  private static final byte[] COLUMN2 = Bytes.toBytes("column2:foo");
047  private static final long START_TIME = 1245219839331L;
048  private static final long END_TIME = 1245393318192L;
049  private static final int CACHING = 1000;
050  private static final int BATCH = 100;
051  private static final boolean CACHE_BLOCKS = false;
052
053  public TestScannerModel() throws Exception {
054    super(ScannerModel.class);
055    AS_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
056        + "<Scanner batch=\"100\" cacheBlocks=\"false\" caching=\"1000\" endRow=\"enp5eng=\" "
057        + "endTime=\"1245393318192\" maxVersions=\"2147483647\" startRow=\"YWJyYWNhZGFicmE=\" "
058        + "startTime=\"1245219839331\">"
059        + "<column>Y29sdW1uMQ==</column><column>Y29sdW1uMjpmb28=</column>"
060        + "<labels>private</labels><labels>public</labels>"
061        + "</Scanner>";
062
063    AS_JSON = "{\"batch\":100,\"caching\":1000,\"cacheBlocks\":false,\"endRow\":\"enp5eng=\","
064        + "\"endTime\":1245393318192,\"maxVersions\":2147483647,\"startRow\":\"YWJyYWNhZGFicmE=\","
065        + "\"startTime\":1245219839331,\"column\":[\"Y29sdW1uMQ==\",\"Y29sdW1uMjpmb28=\"],"
066        +"\"labels\":[\"private\",\"public\"]"
067        +"}";
068
069    AS_PB = "CgthYnJhY2FkYWJyYRIFenp5engaB2NvbHVtbjEaC2NvbHVtbjI6Zm9vIGQo47qL554kMLDi57mf"
070        + "JDj/////B0joB1IHcHJpdmF0ZVIGcHVibGljWAA=";
071  }
072
073  @Override
074  protected ScannerModel buildTestModel() {
075    ScannerModel model = new ScannerModel();
076    model.setStartRow(START_ROW);
077    model.setEndRow(END_ROW);
078    model.addColumn(COLUMN1);
079    model.addColumn(COLUMN2);
080    model.setStartTime(START_TIME);
081    model.setEndTime(END_TIME);
082    model.setBatch(BATCH);
083    model.setCaching(CACHING);
084    model.addLabel(PRIVATE);
085    model.addLabel(PUBLIC);
086    model.setCacheBlocks(CACHE_BLOCKS);
087    return model;
088  }
089
090  @Override
091  protected void checkModel(ScannerModel model) {
092    assertTrue(Bytes.equals(model.getStartRow(), START_ROW));
093    assertTrue(Bytes.equals(model.getEndRow(), END_ROW));
094    boolean foundCol1 = false, foundCol2 = false;
095    for (byte[] column : model.getColumns()) {
096      if (Bytes.equals(column, COLUMN1)) {
097        foundCol1 = true;
098      } else if (Bytes.equals(column, COLUMN2)) {
099        foundCol2 = true;
100      }
101    }
102    assertTrue(foundCol1);
103    assertTrue(foundCol2);
104    assertEquals(START_TIME, model.getStartTime());
105    assertEquals(END_TIME, model.getEndTime());
106    assertEquals(BATCH, model.getBatch());
107    assertEquals(CACHING, model.getCaching());
108    assertEquals(CACHE_BLOCKS, model.getCacheBlocks());
109    boolean foundLabel1 = false;
110    boolean foundLabel2 = false;
111    if (model.getLabels() != null && model.getLabels().size() > 0) {
112      for (String label : model.getLabels()) {
113        if (label.equals(PRIVATE)) {
114          foundLabel1 = true;
115        } else if (label.equals(PUBLIC)) {
116          foundLabel2 = true;
117        }
118      }
119      assertTrue(foundLabel1);
120      assertTrue(foundLabel2);
121    }
122  }
123
124  @Test
125  public void testExistingFilter() throws Exception {
126    final String CORRECT_FILTER = "{\"type\": \"PrefixFilter\", \"value\": \"cg==\"}";
127    verifyException(CORRECT_FILTER);
128  }
129
130  @Test(expected = IllegalArgumentException.class)
131  public void testNonExistingFilter() throws Exception {
132    final String UNKNOWN_FILTER = "{\"type\": \"UnknownFilter\", \"value\": \"cg==\"}";
133    verifyException(UNKNOWN_FILTER);
134  }
135
136  @Test(expected = JsonMappingException.class)
137  public void testIncorrectFilterThrowsJME() throws Exception {
138    final String JME_FILTER = "{\"invalid_tag\": \"PrefixFilter\", \"value\": \"cg==\"}";
139    verifyException(JME_FILTER);
140  }
141
142  @Test(expected = JsonParseException.class)
143  public void tesIncorrecttFilterThrowsJPE() throws Exception {
144    final String JPE_FILTER = "{\"type\": \"PrefixFilter\",, \"value\": \"cg==\"}";
145    verifyException(JPE_FILTER);
146  }
147
148  private void verifyException(final String FILTER) throws Exception {
149    ScannerModel model = new ScannerModel();
150    model.setFilter(FILTER);
151    ScannerResultGenerator.buildFilterFromModel(model);
152  }
153}