package com.cloudhandson.vpdbackoffice.service;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.cloudhandson.vpdbackoffice.config.SecuritySqlScriptProperties;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;

class SecuritySqlScriptServiceTest {

  private final SecuritySqlScriptService service = new SecuritySqlScriptService(
      new SecuritySqlScriptProperties("""
          [{"scriptId":"tool-users","category":"사용자","fileName":"legacy/smilegate/70_tool_user.sql",
            "title":"도구 사용자","description":"도구 사용자 초기 데이터"},
           {"scriptId":"identity-administration","category":"권한","fileName":"legacy/smilegate/71_identity_administration.sql",
            "title":"권한 관리","description":"권한 메타데이터"}]
          """),
      new ObjectMapper()
  );

  @Test
  void exposesOnlyTheCuratedGitTrackedSecurityScripts() {
    assertThat(service.list())
        .extracting(item -> item.fileName())
        .containsExactly(
            "legacy/smilegate/70_tool_user.sql", "legacy/smilegate/71_identity_administration.sql"
        );

    assertThat(service.find("identity-administration").source())
        .contains("create table sg_app_user")
        .contains("DATA_AI_TF");
  }

  @Test
  void rejectsUnknownScriptIdsInsteadOfResolvingAPathFromRequestInput() {
    assertThatThrownBy(() -> service.find("../../etc/passwd"))
        .isInstanceOf(AppException.class)
        .hasMessageContaining("조회할 수 없는");
  }
}
