View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.hbtop.screen.top;
19  
20  import static org.hamcrest.CoreMatchers.is;
21  import static org.junit.Assert.assertThat;
22  import static org.junit.Assert.assertTrue;
23  import static org.mockito.Mockito.when;
24  
25  import java.io.IOException;
26  import java.util.ArrayList;
27  import java.util.Arrays;
28  import java.util.List;
29  import org.apache.hadoop.hbase.client.Admin;
30  import org.apache.hadoop.hbase.hbtop.Record;
31  import org.apache.hadoop.hbase.hbtop.RecordFilter;
32  import org.apache.hadoop.hbase.hbtop.TestUtils;
33  import org.apache.hadoop.hbase.hbtop.field.Field;
34  import org.apache.hadoop.hbase.hbtop.field.FieldInfo;
35  import org.apache.hadoop.hbase.hbtop.field.FieldValue;
36  import org.apache.hadoop.hbase.hbtop.mode.Mode;
37  import org.apache.hadoop.hbase.testclassification.SmallTests;
38  import org.junit.Before;
39  import org.junit.Test;
40  import org.junit.experimental.categories.Category;
41  import org.junit.runner.RunWith;
42  import org.mockito.Mock;
43  import org.mockito.runners.MockitoJUnitRunner;
44  
45  @Category(SmallTests.class)
46  @RunWith(MockitoJUnitRunner.class)
47  public class TestTopScreenModel {
48  
49    @Mock
50    private Admin admin;
51  
52    private TopScreenModel topScreenModel;
53  
54    private List<Field> fields;
55  
56    @Before
57    public void setup() throws IOException {
58      when(admin.getClusterStatus()).thenReturn(TestUtils.createDummyClusterStatus());
59      topScreenModel = new TopScreenModel(admin, Mode.REGION, null, null, null, null);
60  
61      fields = new ArrayList<>();
62      for (FieldInfo fieldInfo : Mode.REGION.getFieldInfos()) {
63        fields.add(fieldInfo.getField());
64      }
65    }
66  
67    @Test
68    public void testSummary() {
69      topScreenModel.refreshMetricsData();
70      Summary summary = topScreenModel.getSummary();
71      TestUtils.assertSummary(summary);
72    }
73  
74    @Test
75    public void testRecords() {
76      // Region Mode
77      topScreenModel.refreshMetricsData();
78      TestUtils.assertRecordsInRegionMode(topScreenModel.getRecords());
79  
80      // Namespace Mode
81      topScreenModel.switchMode(Mode.NAMESPACE, false, null);
82      topScreenModel.refreshMetricsData();
83      TestUtils.assertRecordsInNamespaceMode(topScreenModel.getRecords());
84  
85      // Table Mode
86      topScreenModel.switchMode(Mode.TABLE, false, null);
87      topScreenModel.refreshMetricsData();
88      TestUtils.assertRecordsInTableMode(topScreenModel.getRecords());
89  
90      // Namespace Mode
91      topScreenModel.switchMode(Mode.REGION_SERVER, false, null);
92      topScreenModel.refreshMetricsData();
93      TestUtils.assertRecordsInRegionServerMode(topScreenModel.getRecords());
94    }
95  
96    @Test
97    public void testSort() {
98      // The sort key is LOCALITY
99      topScreenModel.setSortFieldAndFields(Field.LOCALITY, fields);
100 
101     FieldValue previous = null;
102 
103     // Test for ascending sort
104     topScreenModel.refreshMetricsData();
105 
106     for (Record record : topScreenModel.getRecords()) {
107       FieldValue current = record.get(Field.LOCALITY);
108       if (previous != null) {
109         assertTrue(current.compareTo(previous) < 0);
110       }
111       previous = current;
112     }
113 
114     // Test for descending sort
115     topScreenModel.switchSortOrder();
116     topScreenModel.refreshMetricsData();
117 
118     previous = null;
119     for (Record record : topScreenModel.getRecords()) {
120       FieldValue current = record.get(Field.LOCALITY);
121       if (previous != null) {
122         assertTrue(current.compareTo(previous) > 0);
123       }
124       previous = current;
125     }
126   }
127 
128   @Test
129   public void testFilters() {
130     topScreenModel.addFilter("TABLE==table1", false);
131     topScreenModel.refreshMetricsData();
132     for (Record record : topScreenModel.getRecords()) {
133       FieldValue value = record.get(Field.TABLE);
134       assertThat(value.asString(), is("table1"));
135     }
136 
137     topScreenModel.clearFilters();
138     topScreenModel.addFilter("TABLE==TABLE1", false);
139     topScreenModel.refreshMetricsData();
140     assertThat(topScreenModel.getRecords().size(), is(0));
141 
142     // Test for ignore case
143     topScreenModel.clearFilters();
144     topScreenModel.addFilter("TABLE==TABLE1", true);
145     topScreenModel.refreshMetricsData();
146     for (Record record : topScreenModel.getRecords()) {
147       FieldValue value = record.get(Field.TABLE);
148       assertThat(value.asString(), is("table1"));
149     }
150   }
151 
152   @Test
153   public void testFilterHistories() {
154     topScreenModel.addFilter("TABLE==table1", false);
155     topScreenModel.addFilter("TABLE==table2", false);
156     topScreenModel.addFilter("TABLE==table3", false);
157 
158     assertThat(topScreenModel.getFilterHistories().get(0), is("TABLE==table1"));
159     assertThat(topScreenModel.getFilterHistories().get(1), is("TABLE==table2"));
160     assertThat(topScreenModel.getFilterHistories().get(2), is("TABLE==table3"));
161   }
162 
163   @Test
164   public void testSwitchMode() {
165     topScreenModel.switchMode(Mode.TABLE, false, null);
166     assertThat(topScreenModel.getCurrentMode(), is(Mode.TABLE));
167 
168     // Test for initialFilters
169     List<RecordFilter> initialFilters = Arrays.asList(
170       RecordFilter.parse("TABLE==table1", fields, true),
171       RecordFilter.parse("TABLE==table2", fields, true));
172 
173     topScreenModel.switchMode(Mode.TABLE, false, initialFilters);
174 
175     assertThat(topScreenModel.getFilters().size(), is(initialFilters.size()));
176     for (int i = 0; i < topScreenModel.getFilters().size(); i++) {
177       assertThat(topScreenModel.getFilters().get(i).toString(),
178         is(initialFilters.get(i).toString()));
179     }
180 
181     // Test when keepSortFieldAndSortOrderIfPossible is true
182     topScreenModel.setSortFieldAndFields(Field.NAMESPACE, fields);
183     topScreenModel.switchMode(Mode.NAMESPACE, true, null);
184     assertThat(topScreenModel.getCurrentSortField(), is(Field.NAMESPACE));
185   }
186 
187   @Test
188   public void testDrillDown() {
189     topScreenModel.switchMode(Mode.TABLE, false, null);
190     topScreenModel.setSortFieldAndFields(Field.NAMESPACE, fields);
191     topScreenModel.refreshMetricsData();
192 
193     boolean success = topScreenModel.drillDown(topScreenModel.getRecords().get(0));
194     assertThat(success, is(true));
195 
196     assertThat(topScreenModel.getFilters().get(0).toString(), is("NAMESPACE==namespace"));
197     assertThat(topScreenModel.getFilters().get(1).toString(), is("TABLE==table3"));
198     assertThat(topScreenModel.getCurrentSortField(), is(Field.NAMESPACE));
199   }
200 }