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