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