001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.regionserver;
020
021import static org.mockito.ArgumentMatchers.anyInt;
022import static org.mockito.Matchers.any;
023import static org.mockito.Mockito.doAnswer;
024import static org.mockito.Mockito.mock;
025import static org.mockito.Mockito.when;
026
027import java.util.Optional;
028
029import org.apache.hadoop.hbase.regionserver.compactions.CompactionContext;
030import org.mockito.invocation.InvocationOnMock;
031import org.mockito.stubbing.Answer;
032
033/**
034 * This class is a helper that allows to create a partially-implemented, stateful mocks of
035 * Store. It contains a bunch of blank methods, and answers redirecting to these.
036 */
037public class StatefulStoreMockMaker {
038  // Add and expand the methods and answers as needed.
039  public Optional<CompactionContext> selectCompaction() {
040    return Optional.empty();
041  }
042
043  public void cancelCompaction(Object originalContext) {}
044
045  public int getPriority() {
046    return 0;
047  }
048  private class CancelAnswer implements Answer<Object> {
049    @Override
050    public CompactionContext answer(InvocationOnMock invocation) throws Throwable {
051      cancelCompaction(invocation.getArgument(0));
052      return null;
053    }
054  }
055
056  public HStore createStoreMock(String name) throws Exception {
057    HStore store = mock(HStore.class, name);
058    when(store.requestCompaction(anyInt(), any(), any())).then(inv -> selectCompaction());
059    when(store.getCompactPriority()).then(inv -> getPriority());
060    doAnswer(new CancelAnswer()).when(store).cancelRequestedCompaction(any());
061    return store;
062  }
063}