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.regionserver;
019
020import static org.mockito.ArgumentMatchers.any;
021import static org.mockito.ArgumentMatchers.anyInt;
022import static org.mockito.Mockito.doAnswer;
023import static org.mockito.Mockito.mock;
024import static org.mockito.Mockito.when;
025
026import java.util.Optional;
027import org.apache.hadoop.hbase.regionserver.compactions.CompactionContext;
028import org.mockito.invocation.InvocationOnMock;
029import org.mockito.stubbing.Answer;
030
031/**
032 * This class is a helper that allows to create a partially-implemented, stateful mocks of Store. It
033 * contains a bunch of blank methods, and answers redirecting to these.
034 */
035public class StatefulStoreMockMaker {
036  // Add and expand the methods and answers as needed.
037  public Optional<CompactionContext> selectCompaction() {
038    return Optional.empty();
039  }
040
041  public void cancelCompaction(Object originalContext) {
042  }
043
044  public int getPriority() {
045    return 0;
046  }
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}