package com.iqser.red.service.peristence.v1.server.integration.tests;

import java.io.FileOutputStream;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.iqser.red.service.peristence.v1.server.integration.utils.AbstractPersistenceServerServiceTest;
import com.iqser.red.service.persistence.management.v1.processor.migration.migrations.ReduceTextFileSizeMigration10;
import com.iqser.red.service.persistence.management.v1.processor.utils.FileUtils;
import com.iqser.red.service.persistence.management.v1.processor.utils.StorageIdUtils;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.dossier.file.FileType;
import com.knecon.fforesight.tenantcommons.TenantContext;

import lombok.SneakyThrows;

public class ReduceFileSizeMigrationTest extends AbstractPersistenceServerServiceTest {

    @Autowired
    private ReduceTextFileSizeMigration10 reduceTextFileSizeMigration10;

    @Autowired
    private ObjectMapper objectMapper;


    @Test
    @SneakyThrows
    public void testMapping() {

        ClassPathResource responseJson = new ClassPathResource("files/migration/Text.json");

        storageService.storeObject(TenantContext.getTenantId(), StorageIdUtils.getStorageId("dossierId", "fileId", FileType.TEXT), responseJson.getInputStream());

        reduceTextFileSizeMigration10.migrateFile("dossierId", "fileId");

        // readJSONObject will not work here.
        var text = fileManagementStorageService.getObject(TenantContext.getTenantId(), StorageIdUtils.getStorageId("dossierId", "fileId", FileType.TEXT));

        try (FileOutputStream fileOutputStream = new FileOutputStream(FileUtils.getTemporaryDirectory() + "/MigratedText.json")) {
            fileOutputStream.write(text.readAllBytes());
        }

    }

}
