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