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.master.http;
019
020import static org.hamcrest.MatcherAssert.assertThat;
021import static org.hamcrest.Matchers.contains;
022import static org.hamcrest.Matchers.containsInAnyOrder;
023import static org.junit.Assert.assertEquals;
024import static org.junit.Assert.assertNull;
025
026import javax.servlet.http.HttpServletRequest;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.client.AsyncConnection;
030import org.apache.hadoop.hbase.master.RegionState;
031import org.apache.hadoop.hbase.master.http.TestMetaBrowser.MockRequestBuilder;
032import org.apache.hadoop.hbase.testclassification.MasterTests;
033import org.apache.hadoop.hbase.testclassification.SmallTests;
034import org.junit.Before;
035import org.junit.ClassRule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038import org.mockito.Mock;
039import org.mockito.MockitoAnnotations;
040
041/**
042 * Cluster-backed correctness tests for the functionality provided by {@link MetaBrowser}.
043 */
044@Category({ MasterTests.class, SmallTests.class })
045public class TestMetaBrowserNoCluster {
046
047  @ClassRule
048  public static final HBaseClassTestRule testRule =
049    HBaseClassTestRule.forClass(TestMetaBrowserNoCluster.class);
050
051  @Mock
052  private AsyncConnection connection;
053
054  @Before
055  public void before() {
056    MockitoAnnotations.initMocks(this);
057  }
058
059  @Test
060  public void buildFirstPageQueryStringNoParams() {
061    final HttpServletRequest request = new MockRequestBuilder().build();
062    final MetaBrowser metaBrowser = new MetaBrowser(connection, request);
063
064    assertEquals("hbase:meta", metaBrowser.getName());
065    assertNull(metaBrowser.getScanLimit());
066    assertNull(metaBrowser.getScanRegionState());
067    assertNull(metaBrowser.getScanStart());
068    assertNull(metaBrowser.getScanTable());
069    assertEquals("/table.jsp?name=hbase%3Ameta", metaBrowser.buildFirstPageUrl());
070  }
071
072  @Test
073  public void buildFirstPageQueryStringNonNullParams() {
074    final HttpServletRequest request = new MockRequestBuilder().setLimit(50)
075      .setRegionState(RegionState.State.ABNORMALLY_CLOSED).setTable("foo%3Abar").build();
076    final MetaBrowser metaBrowser = new MetaBrowser(connection, request);
077
078    assertEquals(50, metaBrowser.getScanLimit().intValue());
079    assertEquals(RegionState.State.ABNORMALLY_CLOSED, metaBrowser.getScanRegionState());
080    assertEquals(TableName.valueOf("foo", "bar"), metaBrowser.getScanTable());
081    assertEquals("/table.jsp?name=hbase%3Ameta" + "&scan_limit=50"
082      + "&scan_region_state=ABNORMALLY_CLOSED" + "&scan_table=foo%3Abar",
083      metaBrowser.buildNextPageUrl(null));
084  }
085
086  @Test
087  public void buildNextPageQueryString() {
088    final HttpServletRequest request = new MockRequestBuilder().build();
089    final MetaBrowser metaBrowser = new MetaBrowser(connection, request);
090
091    assertEquals("/table.jsp?name=hbase%3Ameta&scan_start=%255Cx80%255Cx00%255Cx7F",
092      metaBrowser.buildNextPageUrl(new byte[] { Byte.MIN_VALUE, (byte) 0, Byte.MAX_VALUE }));
093  }
094
095  @Test
096  public void unparseableLimitParam() {
097    final HttpServletRequest request = new MockRequestBuilder().setLimit("foo").build();
098    final MetaBrowser metaBrowser = new MetaBrowser(connection, request);
099    assertNull(metaBrowser.getScanLimit());
100    assertThat(metaBrowser.getErrorMessages(),
101      contains("Requested SCAN_LIMIT value 'foo' cannot be parsed as an integer."));
102  }
103
104  @Test
105  public void zeroLimitParam() {
106    final HttpServletRequest request = new MockRequestBuilder().setLimit(0).build();
107    final MetaBrowser metaBrowser = new MetaBrowser(connection, request);
108    assertEquals(MetaBrowser.SCAN_LIMIT_DEFAULT, metaBrowser.getScanLimit().intValue());
109    assertThat(metaBrowser.getErrorMessages(), contains("Requested SCAN_LIMIT value 0 is <= 0."));
110  }
111
112  @Test
113  public void negativeLimitParam() {
114    final HttpServletRequest request = new MockRequestBuilder().setLimit(-10).build();
115    final MetaBrowser metaBrowser = new MetaBrowser(connection, request);
116    assertEquals(MetaBrowser.SCAN_LIMIT_DEFAULT, metaBrowser.getScanLimit().intValue());
117    assertThat(metaBrowser.getErrorMessages(), contains("Requested SCAN_LIMIT value -10 is <= 0."));
118  }
119
120  @Test
121  public void excessiveLimitParam() {
122    final HttpServletRequest request = new MockRequestBuilder().setLimit(10_001).build();
123    final MetaBrowser metaBrowser = new MetaBrowser(connection, request);
124    assertEquals(MetaBrowser.SCAN_LIMIT_MAX, metaBrowser.getScanLimit().intValue());
125    assertThat(metaBrowser.getErrorMessages(),
126      contains("Requested SCAN_LIMIT value 10001 exceeds maximum value 10000."));
127  }
128
129  @Test
130  public void invalidRegionStateParam() {
131    final HttpServletRequest request = new MockRequestBuilder().setRegionState("foo").build();
132    final MetaBrowser metaBrowser = new MetaBrowser(connection, request);
133    assertNull(metaBrowser.getScanRegionState());
134    assertThat(metaBrowser.getErrorMessages(),
135      contains("Requested SCAN_REGION_STATE value 'foo' cannot be parsed as a RegionState."));
136  }
137
138  @Test
139  public void multipleErrorMessages() {
140    final HttpServletRequest request =
141      new MockRequestBuilder().setLimit("foo").setRegionState("bar").build();
142    final MetaBrowser metaBrowser = new MetaBrowser(connection, request);
143    assertThat(metaBrowser.getErrorMessages(),
144      containsInAnyOrder("Requested SCAN_LIMIT value 'foo' cannot be parsed as an integer.",
145        "Requested SCAN_REGION_STATE value 'bar' cannot be parsed as a RegionState."));
146  }
147}