package ru.nbch.credit_tracker.utils;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Optional;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

@SpringBootTest
public class ZipUtilsTest {

    private static Stream<Arguments> entryTestCases() {
        return Stream.of(
                Arguments.of(
                        "/monitoring_request/valid_request.zip",
                        true
                ),
                Arguments.of(
                        "/monitoring_request/directory_request.zip",
                        false
                )
                ,
                Arguments.of(
                        "/monitoring_request/empty_request.zip",
                        false
                )
                ,
                Arguments.of(
                        "/monitoring_request/many_files_request.zip",
                        false
                )
                ,
                Arguments.of(
                        "/monitoring_request/txt_request.zip",
                        false
                )
        );
    }

    @ParameterizedTest(name = "[{index}] PATH: [ {0} ] EXPECTED: [ {1} ]")
    @MethodSource("entryTestCases")
    void testExtractEntryFromZip(String path, boolean expected) {
        ZipInputStream zipInputStream;
        Optional<ZipEntry> xmlFileEntry;
        try {
            zipInputStream = new ZipInputStream(getClass().getResourceAsStream(path));
            xmlFileEntry = ZipUtils.extractValidEntry(zipInputStream);
            Assertions.assertEquals(expected, xmlFileEntry.isPresent());
            zipInputStream.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


}
