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.hbtop.screen.top;
019
020import static org.hamcrest.CoreMatchers.is;
021import static org.junit.Assert.assertThat;
022import static org.junit.Assert.assertTrue;
023import static org.mockito.Mockito.when;
024
025import java.io.IOException;
026import java.util.Arrays;
027import java.util.List;
028import java.util.stream.Collectors;
029import org.apache.hadoop.hbase.HBaseClassTestRule;
030import org.apache.hadoop.hbase.client.Admin;
031import org.apache.hadoop.hbase.hbtop.Record;
032import org.apache.hadoop.hbase.hbtop.RecordFilter;
033import org.apache.hadoop.hbase.hbtop.TestUtils;
034import org.apache.hadoop.hbase.hbtop.field.Field;
035import org.apache.hadoop.hbase.hbtop.field.FieldInfo;
036import org.apache.hadoop.hbase.hbtop.field.FieldValue;
037import org.apache.hadoop.hbase.hbtop.mode.Mode;
038import org.apache.hadoop.hbase.testclassification.SmallTests;
039import org.junit.Before;
040import org.junit.ClassRule;
041import org.junit.Test;
042import org.junit.experimental.categories.Category;
043import org.junit.runner.RunWith;
044import org.mockito.Mock;
045import org.mockito.runners.MockitoJUnitRunner;
046
047
048@Category(SmallTests.class)
049@RunWith(MockitoJUnitRunner.class)
050public class TopScreenModelTest {
051
052  @ClassRule
053  public static final HBaseClassTestRule CLASS_RULE =
054    HBaseClassTestRule.forClass(TopScreenModelTest.class);
055
056  @Mock
057  private Admin admin;
058
059  private TopScreenModel topScreenModel;
060
061  private List<Field> fields;
062
063  @Before
064  public void setup() throws IOException {
065    when(admin.getClusterMetrics()).thenReturn(TestUtils.createDummyClusterMetrics());
066    topScreenModel = new TopScreenModel(admin, Mode.REGION);
067
068    fields = Mode.REGION.getFieldInfos().stream()
069      .map(FieldInfo::getField)
070      .collect(Collectors.toList());
071  }
072
073  @Test
074  public void testSummary() {
075    topScreenModel.refreshMetricsData();
076    Summary summary = topScreenModel.getSummary();
077    TestUtils.assertSummary(summary);
078  }
079
080  @Test
081  public void testRecords() {
082    // Region Mode
083    topScreenModel.refreshMetricsData();
084    TestUtils.assertRecordsInRegionMode(topScreenModel.getRecords());
085
086    // Namespace Mode
087    topScreenModel.switchMode(Mode.NAMESPACE, null, false);
088    topScreenModel.refreshMetricsData();
089    TestUtils.assertRecordsInNamespaceMode(topScreenModel.getRecords());
090
091    // Table Mode
092    topScreenModel.switchMode(Mode.TABLE, null, false);
093    topScreenModel.refreshMetricsData();
094    TestUtils.assertRecordsInTableMode(topScreenModel.getRecords());
095
096    // Namespace Mode
097    topScreenModel.switchMode(Mode.REGION_SERVER, null, false);
098    topScreenModel.refreshMetricsData();
099    TestUtils.assertRecordsInRegionServerMode(topScreenModel.getRecords());
100  }
101
102  @Test
103  public void testSort() {
104    // The sort key is LOCALITY
105    topScreenModel.setSortFieldAndFields(Field.LOCALITY, fields);
106
107    FieldValue previous = null;
108
109    // Test for ascending sort
110    topScreenModel.refreshMetricsData();
111
112    for (Record record : topScreenModel.getRecords()) {
113      FieldValue current = record.get(Field.LOCALITY);
114      if (previous != null) {
115        assertTrue(current.compareTo(previous) < 0);
116      }
117      previous = current;
118    }
119
120    // Test for descending sort
121    topScreenModel.switchSortOrder();
122    topScreenModel.refreshMetricsData();
123
124    previous = null;
125    for (Record record : topScreenModel.getRecords()) {
126      FieldValue current = record.get(Field.LOCALITY);
127      if (previous != null) {
128        assertTrue(current.compareTo(previous) > 0);
129      }
130      previous = current;
131    }
132  }
133
134  @Test
135  public void testFilters() {
136    topScreenModel.addFilter("TABLE==table1", false);
137    topScreenModel.refreshMetricsData();
138    for (Record record : topScreenModel.getRecords()) {
139      FieldValue value = record.get(Field.TABLE);
140      assertThat(value.asString(), is("table1"));
141    }
142
143    topScreenModel.clearFilters();
144    topScreenModel.addFilter("TABLE==TABLE1", false);
145    topScreenModel.refreshMetricsData();
146    assertThat(topScreenModel.getRecords().size(), is(0));
147
148    // Test for ignore case
149    topScreenModel.clearFilters();
150    topScreenModel.addFilter("TABLE==TABLE1", true);
151    topScreenModel.refreshMetricsData();
152    for (Record record : topScreenModel.getRecords()) {
153      FieldValue value = record.get(Field.TABLE);
154      assertThat(value.asString(), is("table1"));
155    }
156  }
157
158  @Test
159  public void testFilterHistories() {
160    topScreenModel.addFilter("TABLE==table1", false);
161    topScreenModel.addFilter("TABLE==table2", false);
162    topScreenModel.addFilter("TABLE==table3", false);
163
164    assertThat(topScreenModel.getFilterHistories().get(0), is("TABLE==table1"));
165    assertThat(topScreenModel.getFilterHistories().get(1), is("TABLE==table2"));
166    assertThat(topScreenModel.getFilterHistories().get(2), is("TABLE==table3"));
167  }
168
169  @Test
170  public void testSwitchMode() {
171    topScreenModel.switchMode(Mode.TABLE, null, false);
172    assertThat(topScreenModel.getCurrentMode(), is(Mode.TABLE));
173
174    // Test for initialFilters
175    List<RecordFilter> initialFilters = Arrays.asList(
176      RecordFilter.parse("TABLE==table1", fields, true),
177      RecordFilter.parse("TABLE==table2", fields, true));
178
179    topScreenModel.switchMode(Mode.TABLE, initialFilters, false);
180
181    assertThat(topScreenModel.getFilters().size(), is(initialFilters.size()));
182    for (int i = 0; i < topScreenModel.getFilters().size(); i++) {
183      assertThat(topScreenModel.getFilters().get(i).toString(),
184        is(initialFilters.get(i).toString()));
185    }
186
187    // Test when keepSortFieldAndSortOrderIfPossible is true
188    topScreenModel.setSortFieldAndFields(Field.NAMESPACE, fields);
189    topScreenModel.switchMode(Mode.NAMESPACE, null, true);
190    assertThat(topScreenModel.getCurrentSortField(), is(Field.NAMESPACE));
191  }
192
193  @Test
194  public void testDrillDown() {
195    topScreenModel.switchMode(Mode.TABLE, null, false);
196    topScreenModel.setSortFieldAndFields(Field.NAMESPACE, fields);
197    topScreenModel.refreshMetricsData();
198
199    boolean success = topScreenModel.drillDown(topScreenModel.getRecords().get(0));
200    assertThat(success, is(true));
201
202    assertThat(topScreenModel.getFilters().get(0).toString(), is("NAMESPACE==namespace"));
203    assertThat(topScreenModel.getFilters().get(1).toString(), is("TABLE==table3"));
204    assertThat(topScreenModel.getCurrentSortField(), is(Field.NAMESPACE));
205  }
206}