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.backup.impl;
019
020import static org.mockito.ArgumentMatchers.any;
021import static org.mockito.Mockito.mock;
022import static org.mockito.Mockito.mockConstruction;
023import static org.mockito.Mockito.when;
024
025import java.io.IOException;
026import java.util.ArrayList;
027import java.util.Arrays;
028import java.util.List;
029import java.util.function.Predicate;
030import java.util.stream.Collectors;
031import org.apache.hadoop.conf.Configuration;
032import org.apache.hadoop.hbase.backup.BackupInfo;
033import org.apache.hadoop.hbase.backup.BackupInfo.BackupState;
034import org.apache.hadoop.hbase.backup.PointInTimeRestoreRequest;
035import org.apache.hadoop.hbase.client.Connection;
036import org.apache.hadoop.hbase.testclassification.SmallTests;
037import org.junit.jupiter.api.BeforeEach;
038import org.junit.jupiter.api.Tag;
039import org.mockito.MockedConstruction;
040
041@Tag(SmallTests.TAG)
042public class TestDefaultPitrRestoreHandler extends TestAbstractPitrRestoreHandlerGetBackupMetadata {
043
044  @BeforeEach
045  void setUp() {
046    mockConn = mock(Connection.class);
047    when(mockConn.getConfiguration()).thenReturn(new Configuration());
048    request = new PointInTimeRestoreRequest.Builder().build();
049  }
050
051  @Override
052  protected List<PitrBackupMetadata> callGetBackupMetadataWithBackupStates(BackupState... states)
053    throws IOException {
054    List<BackupInfo> allInfos = new ArrayList<>();
055    for (int i = 0; i < states.length; i++) {
056      BackupInfo info = getBackupInfo(states[i]);
057      info.setBackupId("backup_" + i);
058      allInfos.add(info);
059    }
060
061    try (MockedConstruction<BackupSystemTable> mockedConstruction =
062      mockConstruction(BackupSystemTable.class, (mockSysTable, context) -> {
063        when(mockSysTable.getBackupHistory(any())).thenAnswer(invocation -> {
064          Predicate<BackupInfo> combined = Arrays.stream(invocation.getArguments())
065            .map(arg -> (Predicate<BackupInfo>) arg).reduce(Predicate::and).orElse(x -> true);
066          return allInfos.stream().filter(combined).collect(Collectors.toList());
067        });
068      })) {
069      DefaultPitrRestoreHandler handler = new DefaultPitrRestoreHandler(mockConn, request);
070      return handler.getBackupMetadata(request);
071    }
072  }
073}