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.mode;
019
020import static org.hamcrest.CoreMatchers.is;
021import static org.junit.Assert.assertThat;
022import static org.mockito.ArgumentMatchers.any;
023import static org.mockito.ArgumentMatchers.eq;
024import static org.mockito.Mockito.never;
025import static org.mockito.Mockito.verify;
026
027import java.util.Arrays;
028import java.util.function.Consumer;
029import org.apache.hadoop.hbase.HBaseClassTestRule;
030import org.apache.hadoop.hbase.hbtop.mode.Mode;
031import org.apache.hadoop.hbase.hbtop.screen.top.TopScreenView;
032import org.apache.hadoop.hbase.testclassification.SmallTests;
033import org.junit.ClassRule;
034import org.junit.Test;
035import org.junit.experimental.categories.Category;
036import org.junit.runner.RunWith;
037import org.mockito.Mock;
038import org.mockito.runners.MockitoJUnitRunner;
039
040
041@Category(SmallTests.class)
042@RunWith(MockitoJUnitRunner.class)
043public class TestModeScreenPresenter {
044
045  @ClassRule
046  public static final HBaseClassTestRule CLASS_RULE =
047    HBaseClassTestRule.forClass(TestModeScreenPresenter.class);
048
049  @Mock
050  private ModeScreenView modeScreenView;
051
052  @Mock
053  private TopScreenView topScreenView;
054
055  @Mock
056  private Consumer<Mode> resultListener;
057
058  private ModeScreenPresenter createModeScreenPresenter(Mode currentMode) {
059    return new ModeScreenPresenter(modeScreenView, currentMode, resultListener, topScreenView);
060  }
061
062  @Test
063  public void testInit() {
064    ModeScreenPresenter modeScreenPresenter = createModeScreenPresenter(Mode.REGION);
065
066    modeScreenPresenter.init();
067
068    int modeHeaderMaxLength = Mode.REGION_SERVER.getHeader().length();
069    int modeDescriptionMaxLength = Mode.REGION_SERVER.getDescription().length();
070
071    verify(modeScreenView).showModeScreen(eq(Mode.REGION), eq(Arrays.asList(Mode.values())),
072      eq(Mode.REGION.ordinal()) , eq(modeHeaderMaxLength), eq(modeDescriptionMaxLength));
073  }
074
075  @Test
076  public void testSelectNamespaceMode() {
077    ModeScreenPresenter modeScreenPresenter = createModeScreenPresenter(Mode.REGION);
078
079    modeScreenPresenter.arrowUp();
080    modeScreenPresenter.arrowUp();
081
082    assertThat(modeScreenPresenter.transitionToNextScreen(true), is(topScreenView));
083    verify(resultListener).accept(eq(Mode.NAMESPACE));
084  }
085
086  @Test
087  public void testSelectTableMode() {
088    ModeScreenPresenter modeScreenPresenter = createModeScreenPresenter(Mode.REGION);
089
090    modeScreenPresenter.arrowUp();
091    assertThat(modeScreenPresenter.transitionToNextScreen(true), is(topScreenView));
092    verify(resultListener).accept(eq(Mode.TABLE));
093  }
094
095  @Test
096  public void testSelectRegionMode() {
097    ModeScreenPresenter modeScreenPresenter = createModeScreenPresenter(Mode.NAMESPACE);
098
099    modeScreenPresenter.arrowDown();
100    modeScreenPresenter.arrowDown();
101
102    assertThat(modeScreenPresenter.transitionToNextScreen(true), is(topScreenView));
103    verify(resultListener).accept(eq(Mode.REGION));
104  }
105
106  @Test
107  public void testSelectRegionServerMode() {
108    ModeScreenPresenter modeScreenPresenter = createModeScreenPresenter(Mode.REGION);
109
110    modeScreenPresenter.arrowDown();
111
112    assertThat(modeScreenPresenter.transitionToNextScreen(true), is(topScreenView));
113    verify(resultListener).accept(eq(Mode.REGION_SERVER));
114  }
115
116  @Test
117  public void testCancelSelectingMode() {
118    ModeScreenPresenter modeScreenPresenter = createModeScreenPresenter(Mode.REGION);
119
120    modeScreenPresenter.arrowDown();
121    modeScreenPresenter.arrowDown();
122
123    assertThat(modeScreenPresenter.transitionToNextScreen(false), is(topScreenView));
124    verify(resultListener, never()).accept(any());
125  }
126
127  @Test
128  public void testPageUp() {
129    ModeScreenPresenter modeScreenPresenter = createModeScreenPresenter(Mode.REGION);
130
131    modeScreenPresenter.pageUp();
132
133    assertThat(modeScreenPresenter.transitionToNextScreen(true), is(topScreenView));
134    verify(resultListener).accept(eq(Mode.values()[0]));
135  }
136
137  @Test
138  public void testPageDown() {
139    ModeScreenPresenter modeScreenPresenter = createModeScreenPresenter(Mode.REGION);
140
141    modeScreenPresenter.pageDown();
142
143    assertThat(modeScreenPresenter.transitionToNextScreen(true), is(topScreenView));
144    Mode[] modes = Mode.values();
145    verify(resultListener).accept(eq(modes[modes.length - 1]));
146  }
147}