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 LIMIT = 10000; 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 056 AS_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" 057 + "<Scanner batch=\"100\" cacheBlocks=\"false\" caching=\"1000\" endRow=\"enp5eng=\" endTime=\"1245393318192\"" 058 + " limit=\"10000\" maxVersions=\"2147483647\" startRow=\"YWJyYWNhZGFicmE=\" startTime=\"1245219839331\">" 059 + "<column>Y29sdW1uMQ==</column> <column>Y29sdW1uMjpmb28=</column>" 060 + "<labels>private</labels> <labels>public</labels></Scanner>"; 061 062 AS_JSON = "{\"batch\":100,\"caching\":1000,\"cacheBlocks\":false,\"endRow\":\"enp5eng=\"," 063 + "\"endTime\":1245393318192,\"maxVersions\":2147483647,\"startRow\":\"YWJyYWNhZGFicmE=\"," 064 + "\"startTime\":1245219839331,\"column\":[\"Y29sdW1uMQ==\",\"Y29sdW1uMjpmb28=\"]," 065 + "\"labels\":[\"private\",\"public\"]," + "\"limit\":10000}"; 066 067 AS_PB = "CgthYnJhY2FkYWJyYRIFenp5engaB2NvbHVtbjEaC2NvbHVtbjI6Zm9vIGQo47qL554kMLDi57mfJDj" 068 + "/////B0joB1IHcHJpdmF0ZVIGcHVibGljWABgkE4="; 069 } 070 071 @Override 072 protected ScannerModel buildTestModel() { 073 ScannerModel model = new ScannerModel(); 074 model.setStartRow(START_ROW); 075 model.setEndRow(END_ROW); 076 model.addColumn(COLUMN1); 077 model.addColumn(COLUMN2); 078 model.setStartTime(START_TIME); 079 model.setEndTime(END_TIME); 080 model.setBatch(BATCH); 081 model.setCaching(CACHING); 082 model.addLabel(PRIVATE); 083 model.addLabel(PUBLIC); 084 model.setCacheBlocks(CACHE_BLOCKS); 085 model.setLimit(LIMIT); 086 return model; 087 } 088 089 @Override 090 protected void checkModel(ScannerModel model) { 091 assertTrue(Bytes.equals(model.getStartRow(), START_ROW)); 092 assertTrue(Bytes.equals(model.getEndRow(), END_ROW)); 093 boolean foundCol1 = false, foundCol2 = false; 094 for (byte[] column : model.getColumns()) { 095 if (Bytes.equals(column, COLUMN1)) { 096 foundCol1 = true; 097 } else if (Bytes.equals(column, COLUMN2)) { 098 foundCol2 = true; 099 } 100 } 101 assertTrue(foundCol1); 102 assertTrue(foundCol2); 103 assertEquals(START_TIME, model.getStartTime()); 104 assertEquals(END_TIME, model.getEndTime()); 105 assertEquals(BATCH, model.getBatch()); 106 assertEquals(LIMIT, model.getLimit()); 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}