package com.mesosphere.dcos.cassandra.executor.backup;

import com.amazonaws.services.s3.internal.Constants;
import com.mesosphere.dcos.cassandra.common.tasks.backup.BackupContext;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.net.URISyntaxException;

/**
 * This class tests the S3StorageDriver class.
 */
public class S3StorageDriverTest {
    private S3StorageDriver s3StorageDriver;

    @Before
    public void beforeEach() {
        s3StorageDriver = new S3StorageDriver();
    }

    @Test
    public void testGetBucketNameS3Protocol() throws URISyntaxException {
        String bucketName = "cassandraBackup";
        BackupContext backupContext = BackupContext.create(
                "node-id",
                "name",
                "s3://" + bucketName,
                "local-location",
                "account-id",
                "secret-key");
        Assert.assertEquals(bucketName, s3StorageDriver.getBucketName(backupContext));
    }

    @Test
    public void testGetBucketNameHTTPSProtocol() throws URISyntaxException {
        String bucketName = "cassandraBackup";
        BackupContext backupContext = BackupContext.create(
                "node-id",
                "name",
                "https://s3-us-west-2.amazonaws.com/" + bucketName,
                "local-location",
                "account-id",
                "secret-key");
        Assert.assertEquals(bucketName, s3StorageDriver.getBucketName(backupContext));
    }

    @Test
    public void testGetEmptyPrefixKeyS3Protocol() throws URISyntaxException {
        String backupName = "backup-name";
        BackupContext backupContext = BackupContext.create(
                "node-id",
                backupName,
                "s3://cassandrabackup",
                "local-location",
                "account-id",
                "secret-key");
        Assert.assertEquals(backupName, s3StorageDriver.getPrefixKey(backupContext));
    }

    @Test
    public void testGetNestedPrefixKeyS3Protocol() throws URISyntaxException {
        String backupName = "backup-name";
        String nestedPath = "nested-path";
        BackupContext backupContext = BackupContext.create(
                "node-id",
                backupName,
                "s3://cassandrabackup/" + nestedPath,
                "local-location",
                "account-id",
                "secret-key");
        Assert.assertEquals(nestedPath + "/" + backupName, s3StorageDriver.getPrefixKey(backupContext));
    }

    @Test
    public void testGetEmptyPrefixKeyHTTPSProtocol() throws URISyntaxException {
        String backupName = "backup-name";
        BackupContext backupContext = BackupContext.create(
                "node-id",
                backupName,
                "https://s3-us-west-2.amazonaws.com/cassandrabackup",
                "local-location",
                "account-id",
                "secret-key");
        Assert.assertEquals(backupName, s3StorageDriver.getPrefixKey(backupContext));
    }

    @Test
    public void testGetNestedPrefixKeyHTTPSProtocol() throws URISyntaxException {
        String backupName = "backup-name";
        String nestedPath = "nested-path";
        BackupContext backupContext = BackupContext.create(
                "node-id",
                backupName,
                "https://s3-us-west-2.amazonaws.com/cassandrabackup/" + nestedPath,
                "local-location",
                "account-id",
                "secret-key");
        Assert.assertEquals(nestedPath + "/" + backupName, s3StorageDriver.getPrefixKey(backupContext));
    }

    @Test
    public void testGetEndpointS3Protocol() throws URISyntaxException {
        BackupContext backupContext = BackupContext.create(
                "node-id",
                "name",
                "s3://cassandrabackup",
                "local-location",
                "account-id",
                "secret-key");
        Assert.assertEquals(Constants.S3_HOSTNAME, s3StorageDriver.getEndpoint(backupContext));
    }

    @Test
    public void testGetEndpointHTTPSProtocol() throws URISyntaxException {
        String endpoint = "s3-us-west-2.amazonaws.com";
        BackupContext backupContext = BackupContext.create(
                "node-id",
                "name",
                "https://" + endpoint + "/cassandrabackup",
                "local-location",
                "account-id",
                "secret-key");
        Assert.assertEquals(endpoint, s3StorageDriver.getEndpoint(backupContext));
    }
}

@@ -4150,16 +4150,24 @@
 oint = %22
+https://
 s3-us-we
@@ -4318,20 +4318,960 @@
     
-%22https://%22 +
+endpoint + %22/cassandrabackup%22,%0A                %22local-location%22,%0A                %22account-id%22,%0A                %22secret-key%22);%0A        Assert.assertEquals(endpoint, s3StorageDriver.getEndpoint(backupContext));%0A    %7D%0A%0A    @Test%0A    public void testGetEndpointHTTPProtocolHostPort() throws URISyntaxException %7B%0A        String endpoint = %22http://host:1000%22;%0A        BackupContext backupContext = BackupContext.create(%0A                %22node-id%22,%0A                %22name%22,%0A                endpoint + %22/cassandrabackup%22,%0A                %22local-location%22,%0A                %22account-id%22,%0A                %22secret-key%22);%0A        Assert.assertEquals(endpoint, s3StorageDriver.getEndpoint(backupContext));%0A    %7D%0A%0A    @Test%0A    public void testGetEndpointHTTPProtocolHost() throws URISyntaxException %7B%0A        String endpoint = %22http://host%22;%0A        BackupContext backupContext = BackupContext.create(%0A                %22node-id%22,%0A                %22name%22,%0A               
  end

Add endpoint tests