// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using Amazon.DynamoDBv2; using Amazon.DynamoDBv2.Model; using Amazon.S3; using Amazon.S3.Model; using Amazon.SimpleNotificationService; using Amazon.SimpleNotificationService.Model; using Amazon.SQS; using Amazon.SQS.Model; using AWS.Deploy.CLI.Commands.TypeHints; using AWS.Deploy.CLI.UnitTests.Utilities; using AWS.Deploy.Common; using AWS.Deploy.Common.Recipes; using AWS.Deploy.Common.Recipes.Validation; using AWS.Deploy.Orchestration; using AWS.Deploy.Orchestration.Data; using Moq; using Xunit; using AWS.Deploy.Common.Data; namespace AWS.Deploy.CLI.UnitTests { public class TypeHintTests { private readonly IOptionSettingHandler _optionSettingHandler; private readonly Mock _awsResourceQueryer; private readonly Mock _serviceProvider; public TypeHintTests() { _awsResourceQueryer = new Mock(); _serviceProvider = new Mock(); _serviceProvider .Setup(x => x.GetService(typeof(IAWSResourceQueryer))) .Returns(_awsResourceQueryer.Object); _optionSettingHandler = new OptionSettingHandler(new ValidatorFactory(_serviceProvider.Object)); } [Fact] public async Task TestDynamoDBTableNameTypeHint() { var listPaginator = new Mock(); listPaginator.Setup(x => x.TableNames) .Returns(new MockPaginatedEnumerable(new string[] { "Table1", "Table2" })); var ddbPaginators = new Mock(); ddbPaginators.Setup(x => x.ListTables(It.IsAny())) .Returns(listPaginator.Object); var ddbClient = new Mock(); ddbClient.Setup(x => x.Paginators) .Returns(ddbPaginators.Object); var awsClientFactory = new Mock(); awsClientFactory.Setup(x => x.GetAWSClient(It.IsAny())) .Returns(ddbClient.Object); var awsResourceQueryer = new AWSResourceQueryer(awsClientFactory.Object); var typeHintCommand = new DynamoDBTableCommand(awsResourceQueryer, null, _optionSettingHandler); var resources = await typeHintCommand.GetResources(null, null); Assert.Equal(2, resources.Rows.Count); Assert.Equal("Table1", resources.Rows[0].DisplayName); Assert.Equal("Table1", resources.Rows[0].SystemName); Assert.Equal("Table2", resources.Rows[1].DisplayName); Assert.Equal("Table2", resources.Rows[1].SystemName); } [Fact] public async Task TestSQSQueueUrlTypeHint() { var listPaginator = new Mock(); listPaginator.Setup(x => x.QueueUrls) .Returns(new MockPaginatedEnumerable(new string[] { "https://sqs.us-west-2.amazonaws.com/123412341234/queue1", "https://sqs.us-west-2.amazonaws.com/123412341234/queue2" })); var sqsPaginators = new Mock(); sqsPaginators.Setup(x => x.ListQueues(It.IsAny())) .Returns(listPaginator.Object); var sqsClient = new Mock(); sqsClient.Setup(x => x.Paginators) .Returns(sqsPaginators.Object); var awsClientFactory = new Mock(); awsClientFactory.Setup(x => x.GetAWSClient(It.IsAny())) .Returns(sqsClient.Object); var awsResourceQueryer = new AWSResourceQueryer(awsClientFactory.Object); var typeHintCommand = new SQSQueueUrlCommand(awsResourceQueryer, null, _optionSettingHandler); var resources = await typeHintCommand.GetResources(null, null); Assert.Equal(2, resources.Rows.Count); Assert.Equal("queue1", resources.Rows[0].DisplayName); Assert.Equal("https://sqs.us-west-2.amazonaws.com/123412341234/queue1", resources.Rows[0].SystemName); Assert.Equal("queue2", resources.Rows[1].DisplayName); Assert.Equal("https://sqs.us-west-2.amazonaws.com/123412341234/queue2", resources.Rows[1].SystemName); } [Fact] public async Task TestSNSTopicArnTypeHint() { var listPaginator = new Mock(); listPaginator.Setup(x => x.Topics) .Returns(new MockPaginatedEnumerable(new Topic[] { new Topic { TopicArn = "arn:aws:sns:us-west-2:123412341234:Topic1" }, new Topic { TopicArn = "arn:aws:sns:us-west-2:123412341234:Topic2" } })); var snsPaginators = new Mock(); snsPaginators.Setup(x => x.ListTopics(It.IsAny())) .Returns(listPaginator.Object); var snsClient = new Mock(); snsClient.Setup(x => x.Paginators) .Returns(snsPaginators.Object); var awsClientFactory = new Mock(); awsClientFactory.Setup(x => x.GetAWSClient(It.IsAny())) .Returns(snsClient.Object); var awsResourceQueryer = new AWSResourceQueryer(awsClientFactory.Object); var typeHintCommand = new SNSTopicArnsCommand(awsResourceQueryer, null, _optionSettingHandler); var resources = await typeHintCommand.GetResources(null, null); Assert.Equal(2, resources.Rows.Count); Assert.Equal("Topic1", resources.Rows[0].DisplayName); Assert.Equal("arn:aws:sns:us-west-2:123412341234:Topic1", resources.Rows[0].SystemName); Assert.Equal("Topic2", resources.Rows[1].DisplayName); Assert.Equal("arn:aws:sns:us-west-2:123412341234:Topic2", resources.Rows[1].SystemName); } [Fact] public async Task TestS3BucketNameTypeHint() { var s3Client = new Mock(); s3Client.Setup(x => x.ListBucketsAsync(It.IsAny())) .Returns(Task.FromResult(new ListBucketsResponse { Buckets = new List { new S3Bucket {BucketName = "Bucket1" }, new S3Bucket { BucketName = "Bucket2" } } })); var awsClientFactory = new Mock(); awsClientFactory.Setup(x => x.GetAWSClient(It.IsAny())) .Returns(s3Client.Object); var awsResourceQueryer = new AWSResourceQueryer(awsClientFactory.Object); var typeHintCommand = new S3BucketNameCommand(awsResourceQueryer, null, _optionSettingHandler); var resources = await typeHintCommand.GetResources(null, null); Assert.Equal(2, resources.Rows.Count); Assert.Equal("Bucket1", resources.Rows[0].DisplayName); Assert.Equal("Bucket1", resources.Rows[0].SystemName); Assert.Equal("Bucket2", resources.Rows[1].DisplayName); Assert.Equal("Bucket2", resources.Rows[1].SystemName); } } }