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.field;
019
020import static org.hamcrest.CoreMatchers.is;
021import static org.hamcrest.MatcherAssert.assertThat;
022import static org.mockito.ArgumentMatchers.any;
023import static org.mockito.ArgumentMatchers.anyBoolean;
024import static org.mockito.ArgumentMatchers.anyInt;
025import static org.mockito.ArgumentMatchers.eq;
026import static org.mockito.Mockito.inOrder;
027import static org.mockito.Mockito.verify;
028
029import java.util.EnumMap;
030import java.util.List;
031import java.util.stream.Collectors;
032import org.apache.hadoop.hbase.HBaseClassTestRule;
033import org.apache.hadoop.hbase.hbtop.field.Field;
034import org.apache.hadoop.hbase.hbtop.field.FieldInfo;
035import org.apache.hadoop.hbase.hbtop.mode.Mode;
036import org.apache.hadoop.hbase.hbtop.screen.top.TopScreenView;
037import org.apache.hadoop.hbase.testclassification.SmallTests;
038import org.junit.Before;
039import org.junit.ClassRule;
040import org.junit.Test;
041import org.junit.experimental.categories.Category;
042import org.junit.runner.RunWith;
043import org.mockito.InOrder;
044import org.mockito.Mock;
045import org.mockito.runners.MockitoJUnitRunner;
046
047@Category(SmallTests.class)
048@RunWith(MockitoJUnitRunner.class)
049public class TestFieldScreenPresenter {
050
051  @ClassRule
052  public static final HBaseClassTestRule CLASS_RULE =
053    HBaseClassTestRule.forClass(TestFieldScreenPresenter.class);
054
055  @Mock
056  private FieldScreenView fieldScreenView;
057
058  private int sortFieldPosition = -1;
059  private List<Field> fields;
060  private EnumMap<Field, Boolean> fieldDisplayMap;
061
062  @Mock
063  private FieldScreenPresenter.ResultListener resultListener;
064
065  @Mock
066  private TopScreenView topScreenView;
067
068  private FieldScreenPresenter fieldScreenPresenter;
069
070  @Before
071  public void setup() {
072    Field sortField = Mode.REGION.getDefaultSortField();
073    fields =
074      Mode.REGION.getFieldInfos().stream().map(FieldInfo::getField).collect(Collectors.toList());
075
076    fieldDisplayMap = Mode.REGION.getFieldInfos().stream().collect(() -> new EnumMap<>(Field.class),
077      (r, fi) -> r.put(fi.getField(), fi.isDisplayByDefault()), (r1, r2) -> {
078      });
079
080    fieldScreenPresenter = new FieldScreenPresenter(fieldScreenView, sortField, fields,
081      fieldDisplayMap, resultListener, topScreenView);
082
083    for (int i = 0; i < fields.size(); i++) {
084      Field field = fields.get(i);
085      if (field == sortField) {
086        sortFieldPosition = i;
087        break;
088      }
089    }
090  }
091
092  @Test
093  public void testInit() {
094    fieldScreenPresenter.init();
095
096    int modeHeaderMaxLength = "#COMPingCell".length();
097    int modeDescriptionMaxLength = "Filtered Read Request Count per second".length();
098
099    verify(fieldScreenView).showFieldScreen(eq("#REQ/S"), eq(fields), eq(fieldDisplayMap),
100      eq(sortFieldPosition), eq(modeHeaderMaxLength), eq(modeDescriptionMaxLength), eq(false));
101  }
102
103  @Test
104  public void testChangeSortField() {
105    fieldScreenPresenter.arrowUp();
106    fieldScreenPresenter.setSortField();
107
108    fieldScreenPresenter.arrowDown();
109    fieldScreenPresenter.arrowDown();
110    fieldScreenPresenter.setSortField();
111
112    fieldScreenPresenter.pageUp();
113    fieldScreenPresenter.setSortField();
114
115    fieldScreenPresenter.pageDown();
116    fieldScreenPresenter.setSortField();
117
118    InOrder inOrder = inOrder(fieldScreenView);
119    inOrder.verify(fieldScreenView).showScreenDescription(eq("LRS"));
120    inOrder.verify(fieldScreenView).showScreenDescription(eq("#READ/S"));
121    inOrder.verify(fieldScreenView).showScreenDescription(eq(fields.get(0).getHeader()));
122    inOrder.verify(fieldScreenView)
123      .showScreenDescription(eq(fields.get(fields.size() - 1).getHeader()));
124  }
125
126  @Test
127  public void testSwitchFieldDisplay() {
128    fieldScreenPresenter.switchFieldDisplay();
129    fieldScreenPresenter.switchFieldDisplay();
130
131    InOrder inOrder = inOrder(fieldScreenView);
132    inOrder.verify(fieldScreenView).showField(anyInt(), any(), eq(false), anyBoolean(), anyInt(),
133      anyInt(), anyBoolean());
134    inOrder.verify(fieldScreenView).showField(anyInt(), any(), eq(true), anyBoolean(), anyInt(),
135      anyInt(), anyBoolean());
136  }
137
138  @Test
139  public void testChangeFieldsOrder() {
140    fieldScreenPresenter.turnOnMoveMode();
141    fieldScreenPresenter.arrowUp();
142    fieldScreenPresenter.turnOffMoveMode();
143
144    Field removed = fields.remove(sortFieldPosition);
145    fields.add(sortFieldPosition - 1, removed);
146
147    assertThat(fieldScreenPresenter.transitionToNextScreen(), is(topScreenView));
148    verify(resultListener).accept(any(), eq(fields), any());
149  }
150}