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