package com.cloudhandson.ddsbackoffice.service;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.cloudhandson.ddsbackoffice.config.DdsProperties;
import java.time.Duration;
import java.util.Map;
import org.junit.jupiter.api.Test;

class DdsQueryServiceTest {

  @Test
  void reportsMissingDatabaseConfigurationWithoutOpeningAConnection() {
    var properties = new DdsProperties(
        "", Duration.ofSeconds(5), "ADMIN.V_DDS_CUSTOMERS_PG", "ADMIN.V_DDS_CUSTOMERS_MY",
        Map.of("my", new DdsProperties.User("MY", "source", "ddsuser_my", "secret"))
    );

    var result = new DdsQueryService(properties).query("my", "PG", "", 20);

    assertFalse(result.success());
    assertTrue(result.title().contains("DB 접속"));
  }

  @Test
  void rejectsUnknownSourceBeforeOpeningAConnection() {
    var properties = new DdsProperties(
        "jdbc:test", Duration.ofSeconds(5), "ADMIN.V_DDS_CUSTOMERS_PG", "ADMIN.V_DDS_CUSTOMERS_MY",
        Map.of("my", new DdsProperties.User("MY", "source", "ddsuser_my", "secret"))
    );

    assertThrows(IllegalArgumentException.class,
        () -> new DdsQueryService(properties).query("my", "RDS", "", 20));
  }
}
