/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.flink.test.checkpointing;

import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.CheckpointingOptions;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.configuration.StateRecoveryOptions;
import org.apache.flink.testutils.junit.extensions.parameterized.Parameter;
import org.apache.flink.testutils.junit.extensions.parameterized.ParameterizedTestExtension;
import org.apache.flink.testutils.junit.extensions.parameterized.Parameters;
import org.apache.flink.util.TestLoggerExtension;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import static org.apache.flink.test.checkpointing.EventTimeWindowCheckpointingITCase.StateBackendEnum;
import static org.apache.flink.test.checkpointing.EventTimeWindowCheckpointingITCase.StateBackendEnum.FILE;
import static org.apache.flink.test.checkpointing.EventTimeWindowCheckpointingITCase.StateBackendEnum.ROCKSDB_FULL;
import static org.apache.flink.test.checkpointing.EventTimeWindowCheckpointingITCase.StateBackendEnum.ROCKSDB_INCREMENTAL_ZK;

/**
 * This test delegates to instances of {@link EventTimeWindowCheckpointingITCase} that have been
 * reconfigured to use local recovery.
 *
 * <p>TODO: This class must be refactored to properly extend {@link
 * EventTimeWindowCheckpointingITCase}.
 */
@ExtendWith({TestLoggerExtension.class, ParameterizedTestExtension.class})
class LocalRecoveryITCase {

    @Parameter private StateBackendEnum backendEnum;

    @Parameter(1)
    private boolean localRecoveryEnabled;

    @Parameter(2)
    private boolean localBackupEnabled;

    private static final List<StateBackendEnum> STATE_BACKEND_ENUMS =
            Arrays.asList(ROCKSDB_FULL, ROCKSDB_INCREMENTAL_ZK, FILE);

    private static final List<Tuple2<Boolean, Boolean>> LOCAL_BACKUP_AND_RECOVERY_CONFIGS =
            Arrays.asList(Tuple2.of(true, true), Tuple2.of(true, false), Tuple2.of(false, true));

    @Parameters(
            name = "stateBackendType = {0}, localBackupEnabled = {1}, localRecoveryEnabled = {2}")
    private static Collection<Object[]> parameter() {
        List<Object[]> parameterList = new ArrayList<>();
        for (StateBackendEnum stateBackend : STATE_BACKEND_ENUMS) {
            for (Tuple2<Boolean, Boolean> backupAndRecoveryConfig :
                    LOCAL_BACKUP_AND_RECOVERY_CONFIGS) {
                parameterList.add(
                        new Object[] {
                            stateBackend, backupAndRecoveryConfig.f0, backupAndRecoveryConfig.f1
                        });
            }
        }
        return parameterList;
    }

    @TestTemplate
    final void executeTest(TestInfo testInfo) throws Exception {
        EventTimeWindowCheckpointingITCase windowChkITCase =
                new EventTimeWindowCheckpointingITCaseInstance(
                        backendEnum, localBackupEnabled, localRecoveryEnabled);

        executeTest(windowChkITCase, testInfo);
    }

    private void executeTest(EventTimeWindowCheckpointingITCase delegate, TestInfo testInfo)
            throws Exception {
        delegate.stateBackendEnum = backendEnum;
        delegate.setupTestCluster(testInfo);
        try {
            delegate.testTumblingTimeWindow();
            delegate.stopTestCluster(testInfo);
        } catch (Exception e) {
            delegate.stopTestCluster(testInfo);
            throw new RuntimeException(e);
        }

        delegate.setupTestCluster(testInfo);
        try {
            delegate.testSlidingTimeWindow();
            delegate.stopTestCluster(testInfo);
        } catch (Exception e) {
            delegate.stopTestCluster(testInfo);
            throw new RuntimeException(e);
        }
    }

    @Disabled("Prevents this class from being considered a test class by JUnit.")
    private static class EventTimeWindowCheckpointingITCaseInstance
            extends EventTimeWindowCheckpointingITCase {

        private final StateBackendEnum backendEnum;
        private final boolean localBackupEnable;
        private final boolean localRecoveryEnabled;

        private EventTimeWindowCheckpointingITCaseInstance(
                StateBackendEnum backendEnum,
                boolean localBackupEnable,
                boolean localRecoveryEnabled) {
            super();
            this.backendEnum = backendEnum;
            this.localBackupEnable = localBackupEnable;
            this.localRecoveryEnabled = localRecoveryEnabled;
        }

        @Override
        protected StateBackendEnum getStateBackend() {
            return backendEnum;
        }

        @Override
        protected Configuration createClusterConfig() throws IOException {
            Configuration config = super.createClusterConfig();
            config.set(StateRecoveryOptions.LOCAL_RECOVERY, localRecoveryEnabled);
            config.set(CheckpointingOptions.LOCAL_BACKUP_ENABLED, localBackupEnable);
            return config;
        }
    }
}
