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