require_relative "../support/spec_helper"

module Dalmatian
  RSpec.describe Aurora do
    let(:aurora_reference) do
      {
        "identifier" => "testaurora",
        "in_use_by" => [
          "test-service"
        ],
        "minimum_size" => {
          "production" => 2,
          "staging" => 1
        },
        "maximum_size" => {
          "production" => 2,
          "staging" => 1
        },
        "engine" => "aurora-postgresql",
        "engine_version" => "11.9",
        "db_name" => "testapp",
        "port" => 5432,
        "maintenance_window" => "mon:19:00-mon:19:30",
        "backup_window" => "09:00-10:00",
        "backup_retention_period" => 31,
        "force_ssl" => true,
        "parameter_store_path_db_url_name" => "DATABASE_URL",
        "sql_backup_scheduled_task_environment_variables" => [],
        "sync_sql_backup_to_azure" => false,
        "replication_bucket_destination_arn" => "arn:aws:s3:::dest-bucket",
        "replication_kms_key_id" => "key-id"
      }
    end

    let(:cluster) do
      instance_double(
        Cluster,
        id: "new-dedicated-cluster",
        name: "new-dedicated-cluster",
        environments: {"staging" => {}, "production" => {}},
        services: [double(name: "test-service", domain_names: {"staging" => ["example-domain-name.co.uk"]}, launch_on_cluster: "test"),
          double(name: "test-service", domain_names: {"staging" => ["example-domain-name.co.uk"]}, launch_on_cluster: "")],
        account_id: 123456789012,
        auroras: [double(reference: {identifier: "someotheraurora"}),
          double(reference: aurora_reference)]
      )
    end
    let(:aurora) { Aurora.new(cluster: cluster, reference: aurora_reference) }

    describe "#identifier" do
      it "uses aurora identifier" do
        expect(aurora.identifier).to eq("testaurora")
      end
    end

    describe "#in_use_by" do
      it "uses aurora in_use_by list" do
        expect(aurora.in_use_by).to eq(["test-service"])
      end
    end

    describe "#clusters_in_use" do
      it "uses aurora clusters_in_use list" do
        expect(aurora.clusters_in_use).to eq({"staging" => ["test", "default_dalmatian_ecs_cluster"], "production" => ["test", "default_dalmatian_ecs_cluster"]})
      end
    end

    describe "#minimum_size" do
      it "uses aurora minimum_size" do
        expect(aurora.minimum_size).to eq({"production" => 2, "staging" => 1})
      end
    end

    describe "#maximum_size" do
      it "uses aurora maximum_size" do
        expect(aurora.maximum_size).to eq({"production" => 2, "staging" => 1})
      end
    end

    describe "#engine" do
      it "uses aurora engine" do
        expect(aurora.engine).to eq("aurora-postgresql")
      end
    end

    describe "#engine_version" do
      it "uses the aurora engine_version" do
        expect(aurora.engine_version).to eq("11.9")
      end
    end

    describe "#db_name" do
      it "uses the aurora db_name" do
        expect(aurora.db_name).to eq("testapp")
      end
    end

    describe "#port" do
      it "uses the aurora port" do
        expect(aurora.port).to eq(5432)
      end
    end

    describe "#maintenance_window" do
      it "uses the aurora maintenance_window" do
        expect(aurora.maintenance_window).to eq("mon:19:00-mon:19:30")
      end
    end

    describe "#backup_window" do
      it "uses the aurora backup_window" do
        expect(aurora.backup_window).to eq("09:00-10:00")
      end
    end

    describe "#backup_retention_period" do
      it "uses the aurora backup_retention_period" do
        expect(aurora.backup_retention_period).to eq(31)
      end
    end

    describe "#force_ssl" do
      it "uses the aurora force_ssl bool" do
        expect(aurora.force_ssl).to eq(true)
      end
    end

    describe "#parameter_store_path_db_url_name" do
      it "uses the aurora parameter_store_path_db_url_name" do
        expect(aurora.parameter_store_path_db_url_name).to eq("DATABASE_URL")
      end
    end

    describe "#sql_backup_scheduled_task_environment_variables" do
      it "uses the aurora sql_backup_scheduled_task_environment_variables" do
        expect(aurora.sql_backup_scheduled_task_environment_variables).to eq([])
      end
    end

    describe "#sync_sql_backup_to_azure" do
      it "will have offsite backups disabled by default" do
        expect(aurora.sync_sql_backup_to_azure).to eq(false)
      end
    end

    describe "#replication_bucket_destination_arn" do
      it "uses the aurora replication_bucket_destination_arn" do
        expect(aurora.replication_bucket_destination_arn).to eq("arn:aws:s3:::dest-bucket")
      end
    end

    describe "#replication_kms_key_id" do
      it "uses the aurora replication_kms_key_id" do
        expect(aurora.replication_kms_key_id).to eq("key-id")
      end
    end

    describe "#to_params" do
      it "provides a hash of attributes for use in deployment" do
        expected_params = {
          "identifier" => "testaurora",
          "in_use_by" => [
            "test-service"
          ],
          "clusters_in_use" => {
            "production" => [
              "test",
              "default_dalmatian_ecs_cluster"
            ],
            "staging" => [
              "test",
              "default_dalmatian_ecs_cluster"
            ]
          },
          "minimum_size" => {
            "production" => 2,
            "staging" => 1
          },
          "maximum_size" => {
            "production" => 2,
            "staging" => 1
          },
          "engine" => "aurora-postgresql",
          "engine_version" => "11.9",
          "db_name" => "testapp",
          "port" => 5432,
          "force_ssl" => true,
          "maintenance_window" => "mon:19:00-mon:19:30",
          "backup_window" => "09:00-10:00",
          "backup_retention_period" => 31,
          "parameter_store_path_db_url_name" => "DATABASE_URL",
          "sql_backup_scheduled_task_environment_variables" => [],
          "sync_sql_backup_to_azure" => false,
          "replication_bucket_destination_arn" => "arn:aws:s3:::dest-bucket",
          "replication_kms_key_id" => "key-id"
        }

        expect(aurora.to_params).to eq(expected_params)
      end
    end
  end
end
