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