require_relative "../support/spec_helper"

module Dalmatian
  RSpec.describe Rds do
    let(:rds_reference) do
      {
        "identifier" => "testservice",
        "in_use_by" => [
          "test-service"
        ],
        "instance_class" => {
          "production" => "db.t2.small",
          "staging" => "db.t2.micro"
        },
        "engine" => "postgres",
        "engine_version" => "11.4",
        "allocated_storage" => 20,
        "storage_encrypted" => true,
        "storage_type" => "gp3",
        "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" => [
          {
            "name" => "foo",
            "value" => "bar"
          }
        ],
        "check_sql_backup_scheduled_task_environment_variables" => [
          {
            "name" => "foo",
            "value" => "bar"
          }
        ],
        "sync_sql_backup_to_azure" => false,
        "replication_bucket_destination_arn" => "arn:aws:s3:::dest-bucket",
        "replication_kms_key_id" => "key-id",
        "codebuild_access" => [
          "service-name"
        ]
      }
    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,
        rdss: [double(reference: {identifier: "someotherrds"}),
          double(reference: rds_reference)]
      )
    end
    let(:rds) { Rds.new(cluster: cluster, reference: rds_reference) }

    describe "#identifier" do
      it "uses rds identifier" do
        expect(rds.identifier).to eq("testservice")
      end
    end

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

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

    describe "#instance_class" do
      it "uses rds instance_class" do
        expect(rds.instance_class).to eq({"production" => "db.t2.small", "staging" => "db.t2.micro"})
      end
    end

    describe "#engine" do
      it "uses rds engine" do
        expect(rds.engine).to eq("postgres")
      end
    end

    describe "#engine_version" do
      it "uses the rds engine_version" do
        expect(rds.engine_version).to eq("11.4")
      end
    end

    describe "#allocated_storage" do
      it "uses the rds allocated_storage" do
        expect(rds.allocated_storage).to eq(20)
      end
    end

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

    describe "#storage_type" do
      it "uses the rds storage_type gp3" do
        expect(rds.storage_type).to eq("gp3")
      end
    end

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

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

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

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

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

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

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

    describe "#sql_backup_scheduled_task_environment_variables" do
      it "uses the rds sql_backup_scheduled_task_environment_variables" do
        expect(rds.sql_backup_scheduled_task_environment_variables).to eq([{"name" => "foo", "value" => "bar"}])
      end
    end

    describe "#check_sql_backup_scheduled_task_environment_variables" do
      it "uses the rds check_sql_backup_scheduled_task_environment_variables" do
        expect(rds.check_sql_backup_scheduled_task_environment_variables).to eq([{"name" => "foo", "value" => "bar"}])
      end
    end

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

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

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

    describe "#codebuild_access" do
      it "uses the rds codebuild_access" do
        expect(rds.codebuild_access).to eq(["service-name"])
      end
    end

    describe "#to_params" do
      it "provides a hash of attributes for use in deployment" do
        expected_params = {
          "identifier" => "testservice",
          "in_use_by" => [
            "test-service"
          ],
          "clusters_in_use" => {
            "production" => [
              "test",
              "default_dalmatian_ecs_cluster"
            ],
            "staging" => [
              "test",
              "default_dalmatian_ecs_cluster"
            ]
          },
          "instance_class" => {
            "production" => "db.t2.small",
            "staging" => "db.t2.micro"
          },
          "engine" => "postgres",
          "engine_version" => "11.4",
          "allocated_storage" => 20,
          "storage_encrypted" => true,
          "storage_type" => "gp3",
          "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" => [
            {
              "name" => "foo",
              "value" => "bar"
            }
          ],
          "check_sql_backup_scheduled_task_environment_variables" => [
            {
              "name" => "foo",
              "value" => "bar"
            }
          ],
          "sync_sql_backup_to_azure" => false,
          "replication_bucket_destination_arn" => "arn:aws:s3:::dest-bucket",
          "replication_kms_key_id" => "key-id",
          "codebuild_access" => [
            "service-name"
          ]
        }

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