package dev.dinauer.maven.maven.core;

import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.security.TestSecurity;
import io.restassured.RestAssured;
import jakarta.inject.Inject;
import org.flywaydb.core.Flyway;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.platform.commons.util.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

@QuarkusTest
public class ResourceTest
{
    @Inject
    Flyway flyway;

    @BeforeEach
    void before()
    {
        flyway.clean();
        flyway.migrate();
    }

    @Test
    @TestSecurity(user = "user")
    void test_snapshot_01() throws IOException
    {
        String uploadPom = new String(readFile("/jar/postgresql-42.7.9.pom").readAllBytes(), StandardCharsets.UTF_8);
        RestAssured.given().body(uploadPom).put("/maven2/org/postgresql/postgresql/42.7.9-SNAPSHOT/postgresql-42.7.9-20250419.123456-1.pom").then().statusCode(204);
        byte[] uploadJar = readFile("/jar/postgresql-42.7.9.jar").readAllBytes();
        RestAssured.given().body(uploadJar).put("/maven2/org/postgresql/postgresql/42.7.9-SNAPSHOT/postgresql-42.7.9-20250419.123456-1.jar").then().statusCode(204);


        String pom = RestAssured.given().get("/maven2/org/postgresql/postgresql/42.7.9-SNAPSHOT/postgresql-42.7.9-20250419.123456-1.pom").body().asString();
        byte[] jar = RestAssured.given().get("/maven2/org/postgresql/postgresql/42.7.9-SNAPSHOT/postgresql-42.7.9-20250419.123456-1.jar").body().asByteArray();

        String jarMd5 = RestAssured.given().get("/maven2/org/postgresql/postgresql/42.7.9-SNAPSHOT/postgresql-42.7.9-20250419.123456-1.jar.md5").body().asString();
        String pomMd5 = RestAssured.given().get("/maven2/org/postgresql/postgresql/42.7.9-SNAPSHOT/postgresql-42.7.9-20250419.123456-1.pom.md5").body().asString();

        Assertions.assertFalse(StringUtils.isBlank(jarMd5));
        Assertions.assertFalse(StringUtils.isBlank(pomMd5));

        Assertions.assertArrayEquals(uploadJar, jar);
        Assertions.assertEquals(uploadPom, pom);
    }

    @Test
    @TestSecurity(user = "user")
    void test_release_01() throws IOException
    {
        String uploadPom = new String(readFile("/jar/postgresql-42.7.9.pom").readAllBytes(), StandardCharsets.UTF_8);
        RestAssured.given().body(uploadPom).put("/maven2/org/postgresql/postgresql/42.7.9/postgresql-42.7.9.pom").then().statusCode(204);
        byte[] uploadJar = readFile("/jar/postgresql-42.7.9.jar").readAllBytes();
        RestAssured.given().body(uploadJar).put("/maven2/org/postgresql/postgresql/42.7.9/postgresql-42.7.9.jar").then().statusCode(204);


        String pom = RestAssured.given().get("/maven2/org/postgresql/postgresql/42.7.9/postgresql-42.7.9.pom").body().asString();
        byte[] jar = RestAssured.given().get("/maven2/org/postgresql/postgresql/42.7.9/postgresql-42.7.9.jar").body().asByteArray();

        String jarMd5 = RestAssured.given().get("/maven2/org/postgresql/postgresql/42.7.9/postgresql-42.7.9.jar.md5").body().asString();
        String pomMd5 = RestAssured.given().get("/maven2/org/postgresql/postgresql/42.7.9/postgresql-42.7.9.pom.md5").body().asString();

        Assertions.assertFalse(StringUtils.isBlank(jarMd5));
        Assertions.assertFalse(StringUtils.isBlank(pomMd5));

        Assertions.assertArrayEquals(uploadJar, jar);
        Assertions.assertEquals(uploadPom, pom);
    }

    private InputStream readFile(String path)
    {
        return getClass().getResourceAsStream(path);
    }
}
