Example #1
0
        public async Task KinesisCRUD()
        {
            var streamName = UtilityMethods.GenerateName("KinesisCRUD");

            // Create a stream.
            await Client.CreateStreamAsync(new CreateStreamRequest
            {
                ShardCount = 1,
                StreamName = streamName
            });

            _streamNames.Add(streamName);

            // Describe the stream.
            var stream = (await Client.DescribeStreamAsync(new DescribeStreamRequest
            {
                StreamName = streamName
            })).StreamDescription;

            Assert.Equal(stream.HasMoreShards, false);
            Assert.False(string.IsNullOrEmpty(stream.StreamARN));
            Assert.Equal(stream.StreamName, streamName);
            Assert.True(stream.StreamStatus == StreamStatus.CREATING || stream.StreamStatus == StreamStatus.ACTIVE);

            // List streams.
            var streamNames = (await Client.ListStreamsAsync()).StreamNames;

            Assert.True(streamNames.Count > 0);
            Assert.True(streamNames.Contains(streamName));
        }
Example #2
0
        public async void TestMultipleQueueSubscription()
        {
            var topicName1 = UtilityMethods.GenerateName("TestMultipleQueueSubscription");
            var topic      = await Client.CreateTopicAsync(topicName1);

            _topicArns.Add(topic.TopicArn);

            var topicName2 = UtilityMethods.GenerateName("TestMultipleQueueSubscription2");
            var response   = await Client.CreateTopicAsync(topicName2);

            _topicArns.Add(response.TopicArn);

            var queueName = UtilityMethods.GenerateName("TestMultipleQueueSubscription");
            var queueUrl  = (await sqsClient.CreateQueueAsync(queueName)).QueueUrl;

            _queueUrl.Add(queueUrl);

            ICoreAmazonSQS coreSqs = sqsClient as ICoreAmazonSQS;
            var            topics  = await Client.SubscribeQueueToTopicsAsync(_topicArns, coreSqs, queueUrl);

            var subscriptionArns = topics.Values;

            Assert.Equal(2, subscriptionArns.Count);

            Thread.Sleep(TimeSpan.FromSeconds(5));

            var attributes = (await sqsClient.GetQueueAttributesAsync(queueUrl, new List <string> {
                "All"
            })).Attributes;
            var policy     = Policy.FromJson(attributes["Policy"]);

            Assert.Equal(2, policy.Statements.Count);
        }
        public async Task SetTopicConfigurationTests()
        {
            using (var snsClient = new AmazonSimpleNotificationServiceClient())
            {
                string topicName         = UtilityMethods.GenerateName("events-test");
                var    snsCreateResponse = await snsClient.CreateTopicAsync(topicName);

                var bucketName = await UtilityMethods.CreateBucketAsync(Client, "SetTopicConfigurationTests");

                try
                {
                    await snsClient.AuthorizeS3ToPublishAsync(snsCreateResponse.TopicArn, bucketName);

                    PutBucketNotificationRequest putRequest = new PutBucketNotificationRequest
                    {
                        BucketName          = bucketName,
                        TopicConfigurations = new List <TopicConfiguration>
                        {
                            new TopicConfiguration
                            {
                                Id     = "the-topic-test",
                                Topic  = snsCreateResponse.TopicArn,
                                Events = new List <EventType> {
                                    EventType.ObjectCreatedPut
                                }
                            }
                        }
                    };
                    await Client.PutBucketNotificationAsync(putRequest);

                    var getResponse = WaitUtils.WaitForComplete(
                        () =>
                    {
                        return(Client.GetBucketNotificationAsync(bucketName).Result);
                    },
                        (r) =>
                    {
                        return(r.TopicConfigurations.Count > 0);
                    });

                    Assert.Equal(1, getResponse.TopicConfigurations.Count);
                    Assert.Equal(1, getResponse.TopicConfigurations[0].Events.Count);
                    Assert.Equal(EventType.ObjectCreatedPut, getResponse.TopicConfigurations[0].Events[0]);

#pragma warning disable 618
                    Assert.Equal("s3:ObjectCreated:Put", getResponse.TopicConfigurations[0].Event);
#pragma warning restore 618
                    Assert.Equal("the-topic-test", getResponse.TopicConfigurations[0].Id);
                    Assert.Equal(snsCreateResponse.TopicArn, getResponse.TopicConfigurations[0].Topic);
                }
                finally
                {
                    await snsClient.DeleteTopicAsync(snsCreateResponse.TopicArn);

                    await UtilityMethods.DeleteBucketWithObjectsAsync(Client, bucketName);
                }
            }
        }
Example #4
0
        public async Task TestDeleteApplication()
        {
            var appName = UtilityMethods.GenerateName();
            await Client.CreateApplicationAsync(new CreateApplicationRequest { ApplicationName = appName });

            Assert.True(Client.ListApplicationsAsync().Result.Applications.Contains(appName));
            await Client.DeleteApplicationAsync(new DeleteApplicationRequest { ApplicationName = appName });

            Assert.False(Client.ListApplicationsAsync().Result.Applications.Contains(appName));
        }
Example #5
0
        private async Task <string> CreateQueue()
        {
            var queueName = UtilityMethods.GenerateName("TestQueueSubscription");
            var queueUrl  = (await sqsClient.CreateQueueAsync(new CreateQueueRequest
            {
                QueueName = queueName
            })).QueueUrl;

            _queueUrl.Add(queueUrl);
            return(queueUrl);
        }
Example #6
0
        private async Task <string> CreateTopic()
        {
            // create new topic
            var topicName          = UtilityMethods.GenerateName("TestQueueSubscription");
            var createTopicRequest = new CreateTopicRequest
            {
                Name = topicName
            };
            var createTopicResult = await Client.CreateTopicAsync(createTopicRequest);

            var topicArn = createTopicResult.TopicArn;

            _topicArns.Add(topicArn);
            return(topicArn);
        }
Example #7
0
        public async Task TestGetQueueUrl()
        {
            string queueName = UtilityMethods.GenerateName("TestGetQueueUrl");
            CreateQueueResponse createResponse = await Client.CreateQueueAsync(new CreateQueueRequest()
            {
                QueueName = queueName
            });

            _queueUrls.Add(createResponse.QueueUrl);

            GetQueueUrlRequest request = new GetQueueUrlRequest()
            {
                QueueName = queueName
            };
            GetQueueUrlResponse response = await Client.GetQueueUrlAsync(request);

            Assert.Equal(createResponse.QueueUrl, response.QueueUrl);
        }
Example #8
0
        public async Task SimpleSend()
        {
            int    maxMessageLength = 20 * 1024;
            string queueName        = UtilityMethods.GenerateName("SimpleSend");
            string queueURL         = await createQueueTest(queueName);

            StringBuilder sb          = new StringBuilder("The quick brown fox jumped over the lazy dog");
            string        messageBody = sb.ToString();

            if (messageBody.Length > maxMessageLength)
            {
                messageBody = messageBody.Substring(0, maxMessageLength);
            }

            await TestSendMessage(Client, queueURL, messageBody);
            await TestSendMessageBatch(Client, queueURL, messageBody);
            await TestReceiveMessage(Client, queueURL);
        }
Example #9
0
        public async Task FindTopic()
        {
            // create new topic
            var name = UtilityMethods.GenerateName("FindTopic");
            var createTopicRequest = new CreateTopicRequest
            {
                Name = name
            };
            var createTopicResult = await Client.CreateTopicAsync(createTopicRequest);

            var topicArn = createTopicResult.TopicArn;

            _topicArns.Add(topicArn);

            var foundTopic = await Client.FindTopicAsync(name);

            // verify that the topic was fund
            Assert.NotNull(foundTopic);
        }
Example #10
0
        public async Task CRUDTopics()
        {
            // list all topics
            var allTopics = await GetAllTopics();

            // create new topic
            var name = UtilityMethods.GenerateName("CrudTopics");
            var createTopicRequest = new CreateTopicRequest
            {
                Name = name
            };
            var createTopicResult = await Client.CreateTopicAsync(createTopicRequest);

            var topicArn = createTopicResult.TopicArn;

            _topicArns.Add(topicArn);

            // verify there is a new topic
            allTopics = await GetAllTopics();

            Assert.True(allTopics.Exists(t => t.TopicArn.Contains(name)));

            // set topic attribute
            var setTopicAttributesRequest = new SetTopicAttributesRequest
            {
                TopicArn       = topicArn,
                AttributeName  = "DisplayName",
                AttributeValue = "Test topic"
            };
            await Client.SetTopicAttributesAsync(setTopicAttributesRequest);

            // verify topic attributes
            var getTopicAttributesRequest = new GetTopicAttributesRequest
            {
                TopicArn = topicArn
            };
            var topicAttributes =
                (await Client.GetTopicAttributesAsync(getTopicAttributesRequest)).Attributes;

            Assert.Equal(setTopicAttributesRequest.AttributeValue,
                         topicAttributes[setTopicAttributesRequest.AttributeName]);
        }
        public async Task ListGlacierCalls()
        {
            string vaultName = UtilityMethods.GenerateName("glacier-test");

            Client.CreateVaultAsync(new CreateVaultRequest
            {
                VaultName = vaultName
            }).Wait();
            try
            {
                Assert.NotNull(await Client.ListJobsAsync(new ListJobsRequest {
                    VaultName = vaultName
                }).ConfigureAwait(false));
                Assert.NotNull(await Client.ListMultipartUploadsAsync(new Glacier.Model.ListMultipartUploadsRequest {
                    VaultName = vaultName
                }).ConfigureAwait(false));
                Assert.NotNull(await Client.ListVaultsAsync(new ListVaultsRequest()).ConfigureAwait(false));
            }
            finally
            {
                await Client.DeleteVaultAsync(new DeleteVaultRequest { VaultName = vaultName });
            }
        }
Example #12
0
        public async Task AuthorizeS3ToSendMessageAsyncTest()
        {
            string bucketName    = UtilityMethods.GenerateName("AuthorizeS3ToSendMessageAsync");
            string mainQueueName = UtilityMethods.GenerateName("AuthorizeS3ToSendMessageAsync");
            string mainQueueURL  = await createQueueTest(mainQueueName);

            string s3ObjectKey = UtilityMethods.GenerateName("key");

            Assert.False(String.Compare(mainQueueURL, "fail") == 0, "Failed to create an SQS queue");

            using (var s3Client = new AmazonS3Client(s3ClientConfig))
            {
                var bucketResponse = await s3Client.PutBucketAsync(new PutBucketRequest { BucketName = bucketName });

                _bucketNames.Add(bucketName);

                Assert.True(await hasS3Bucket(s3Client, bucketName), "Failed to create an S3 bucket");

                var arn = await Client.AuthorizeS3ToSendMessageAsync(mainQueueURL, bucketName);

                await s3Client.PutBucketNotificationAsync(new PutBucketNotificationRequest {
                    BucketName          = bucketName,
                    QueueConfigurations = new List <QueueConfiguration> {
                        new QueueConfiguration {
                            Queue = arn, Events = new List <EventType> {
                                EventType.ObjectCreatedAll
                            }
                        }
                    },
                });

                var count = (await Client.ReceiveMessageAsync(mainQueueURL)).Messages.Count;

                await s3Client.PutObjectAsync(new PutObjectRequest {
                    ContentBody = "Hello World",
                    BucketName  = bucketName,
                    Key         = s3ObjectKey,
                });

                Assert.True(await hasS3Object(s3Client, bucketName, s3ObjectKey), "Failed to put an S3 object");

                bool objectCreaedMessageReceived = false;
                for (int i = 0; i < 5; i++)
                {
                    var messages = (await Client.ReceiveMessageAsync(mainQueueURL)).Messages;

                    foreach (var message in messages)
                    {
                        objectCreaedMessageReceived = message.Body.Contains("ObjectCreated:Put");
                        if (objectCreaedMessageReceived)
                        {
                            return;
                        }
                    }

                    UtilityMethods.Sleep(TimeSpan.FromSeconds(2));
                }

                Assert.True(objectCreaedMessageReceived, "Failed to receive object creaed message.");
            }
        }
Example #13
0
        public async Task SetQueueConfigurationTests()
        {
            var filterRule = new FilterRule("Prefix", "test/");

            using (var sqsClient = new AmazonSQSClient())
            {
                string topicName      = UtilityMethods.GenerateName("events-test");
                var    createResponse = await sqsClient.CreateQueueAsync(topicName);

                var bucketName = await UtilityMethods.CreateBucketAsync(Client, "SetQueueConfigurationTests");

                try
                {
                    var queueArn = await sqsClient.AuthorizeS3ToSendMessageAsync(createResponse.QueueUrl, bucketName);

                    PutBucketNotificationRequest putRequest = new PutBucketNotificationRequest
                    {
                        BucketName          = bucketName,
                        QueueConfigurations = new List <QueueConfiguration>
                        {
                            new QueueConfiguration
                            {
                                Id     = "the-queue-test",
                                Queue  = queueArn,
                                Events = { EventType.ObjectCreatedPut },
                                Filter = new Filter
                                {
                                    S3KeyFilter = new S3KeyFilter
                                    {
                                        FilterRules = new List <FilterRule>
                                        {
                                            filterRule
                                        }
                                    }
                                }
                            }
                        }
                    };

                    await Client.PutBucketNotificationAsync(putRequest);

                    var getResponse = await Client.GetBucketNotificationAsync(bucketName);

                    Assert.Equal(1, getResponse.QueueConfigurations.Count);
                    Assert.Equal(1, getResponse.QueueConfigurations[0].Events.Count);
                    Assert.Equal(EventType.ObjectCreatedPut, getResponse.QueueConfigurations[0].Events[0]);

                    Assert.NotNull(getResponse.QueueConfigurations[0].Filter);
                    Assert.NotNull(getResponse.QueueConfigurations[0].Filter.S3KeyFilter);
                    Assert.NotNull(getResponse.QueueConfigurations[0].Filter.S3KeyFilter.FilterRules);
                    Assert.Equal(1, getResponse.QueueConfigurations[0].Filter.S3KeyFilter.FilterRules.Count);
                    Assert.Equal(filterRule.Name, getResponse.QueueConfigurations[0].Filter.S3KeyFilter.FilterRules[0].Name);
                    Assert.Equal(filterRule.Value, getResponse.QueueConfigurations[0].Filter.S3KeyFilter.FilterRules[0].Value);

                    Assert.Equal("the-queue-test", getResponse.QueueConfigurations[0].Id);
                    Assert.Equal(queueArn, getResponse.QueueConfigurations[0].Queue);

                    // Purge queue to remove test message sent configuration was setup.
                    await sqsClient.PurgeQueueAsync(createResponse.QueueUrl);

                    Thread.Sleep(TimeSpan.FromSeconds(1));

                    var putObjectRequest = new PutObjectRequest
                    {
                        BucketName  = bucketName,
                        Key         = "test/data.txt",
                        ContentBody = "Important Data"
                    };

                    await Client.PutObjectAsync(putObjectRequest);

                    string messageBody = null;
                    for (int i = 0; i < 5 && messageBody == null; i++)
                    {
                        var receiveResponse = await sqsClient.ReceiveMessageAsync(new ReceiveMessageRequest { QueueUrl = createResponse.QueueUrl, WaitTimeSeconds = 20 });

                        if (receiveResponse.Messages.Count != 0)
                        {
                            messageBody = receiveResponse.Messages[0].Body;
                        }
                    }


                    var evnt = S3EventNotification.ParseJson(messageBody);

                    Assert.Equal(1, evnt.Records.Count);
                    Assert.Equal(putObjectRequest.BucketName, evnt.Records[0].S3.Bucket.Name);
                    Assert.Equal(putObjectRequest.Key, evnt.Records[0].S3.Object.Key);
                    Assert.Equal(putObjectRequest.ContentBody.Length, evnt.Records[0].S3.Object.Size);
                }
                finally
                {
                    await sqsClient.DeleteQueueAsync(createResponse.QueueUrl);

                    await UtilityMethods.DeleteBucketWithObjectsAsync(Client, bucketName);
                }
            }
        }
Example #14
0
        public async void TestQueueSubscription()
        {
            // create new topic
            var topicName          = UtilityMethods.GenerateName("TestQueueSubscription");
            var createTopicRequest = new CreateTopicRequest
            {
                Name = topicName
            };
            var createTopicResult = await Client.CreateTopicAsync(createTopicRequest);

            var topicArn = createTopicResult.TopicArn;

            _topicArns.Add(topicArn);

            var queueName = UtilityMethods.GenerateName("TestQueueSubscription");
            var queueUrl  = (await sqsClient.CreateQueueAsync(new CreateQueueRequest
            {
                QueueName = queueName
            })).QueueUrl;

            _queueUrl.Add(queueUrl);

            ICoreAmazonSQS coreSqs         = sqsClient as ICoreAmazonSQS;
            var            subscriptionARN = await Client.SubscribeQueueAsync(topicArn, coreSqs, queueUrl);

            // Sleep to wait for the subscribe to complete.
            Thread.Sleep(TimeSpan.FromSeconds(5));

            var publishRequest = new PublishRequest
            {
                TopicArn          = topicArn,
                Subject           = "Test Subject",
                Message           = "Test Message",
                MessageAttributes = new Dictionary <string, SNSMessageAttributeValue>
                {
                    { "Color", new SNSMessageAttributeValue {
                          StringValue = "Red", DataType = "String"
                      } },
                    { "Binary", new SNSMessageAttributeValue {
                          DataType = "Binary", BinaryValue = new MemoryStream(Encoding.UTF8.GetBytes("Yes please"))
                      } },
                    { "Prime", new SNSMessageAttributeValue {
                          StringValue = "31", DataType = "Number"
                      } },
                }
            };
            await Client.PublishAsync(publishRequest);

            var messages = (await sqsClient.ReceiveMessageAsync(new ReceiveMessageRequest
            {
                QueueUrl = queueUrl,
                WaitTimeSeconds = 20
            })).Messages;

            Assert.Equal(1, messages.Count);
            var message = messages[0];

            string bodyJson;

            // Handle some accounts returning message body as base 64 encoded.
            if (message.Body.Trim()[0] == '{')
            {
                bodyJson = message.Body;
            }
            else
            {
                bodyJson = Encoding.UTF8.GetString(Convert.FromBase64String(message.Body));
            }

            var json           = ThirdParty.Json.LitJson.JsonMapper.ToObject(bodyJson);
            var messageText    = json["Message"];
            var messageSubject = json["Subject"];

            Assert.Equal(publishRequest.Message, messageText.ToString());
            Assert.Equal(publishRequest.Subject, messageSubject.ToString());
            var messageAttributes = json["MessageAttributes"];

            Assert.Equal(publishRequest.MessageAttributes.Count, messageAttributes.Count);
            foreach (var ma in publishRequest.MessageAttributes)
            {
                var name  = ma.Key;
                var value = ma.Value;
                Assert.True(messageAttributes.PropertyNames.Contains(name, StringComparer.Ordinal));
                var jsonAttribute = messageAttributes[name];
                var jsonType      = jsonAttribute["Type"].ToString();
                var jsonValue     = jsonAttribute["Value"].ToString();
                Assert.NotNull(jsonType);
                Assert.NotNull(jsonValue);
                Assert.Equal(value.DataType, jsonType);
                Assert.Equal(value.DataType != "Binary"
                                    ? value.StringValue
                                    : Convert.ToBase64String(value.BinaryValue.ToArray()), jsonValue);
            }

            await sqsClient.DeleteMessageAsync(new DeleteMessageRequest
            {
                QueueUrl      = queueUrl,
                ReceiptHandle = messages[0].ReceiptHandle
            });

            // This will unsubscribe but leave the policy in place.
            await Client.UnsubscribeAsync(new UnsubscribeRequest
            {
                SubscriptionArn = subscriptionARN
            });

            // Subscribe again to see if this affects the policy.
            await Client.SubscribeQueueAsync(topicArn, coreSqs, queueUrl);

            await Client.PublishAsync(new PublishRequest
            {
                TopicArn = topicArn,
                Message  = "Test Message again"
            });

            messages = (await sqsClient.ReceiveMessageAsync(new ReceiveMessageRequest
            {
                QueueUrl = queueUrl,
                WaitTimeSeconds = 20
            })).Messages;

            Assert.Equal(1, messages.Count);

            var response = WaitUtils.WaitForComplete(
                () => {
                return(sqsClient.GetQueueAttributesAsync(new GetQueueAttributesRequest
                {
                    AttributeNames = new List <string> {
                        "All"
                    },
                    QueueUrl = queueUrl
                }).Result);
            },
                (r) =>
            {
                return(!string.IsNullOrEmpty(r.Policy));
            });

            var policy = Policy.FromJson(response.Policy);

            Assert.Equal(1, policy.Statements.Count);
        }
Example #15
0
        public async Task SubscribeTopic()
        {
            const string emailAddress          = "*****@*****.**";
            var          subscriptionWaitDelay = TimeSpan.FromMinutes(2);

            // create new topic
            var name = UtilityMethods.GenerateName("SubscribeTopic");
            var createTopicRequest = new CreateTopicRequest
            {
                Name = name
            };
            var createTopicResult = await Client.CreateTopicAsync(createTopicRequest);

            var topicArn = createTopicResult.TopicArn;

            _topicArns.Add(topicArn);

            // subscribe an email address to the topic
            var subscribeRequest = new SubscribeRequest
            {
                Protocol = "email",
                Endpoint = emailAddress,
                TopicArn = topicArn
            };
            await Client.SubscribeAsync(subscribeRequest);

            // wait until subscription has been confirmed, maximum wait time of two minutes
            // by default
            string subArn = null;
            var    latest = DateTime.Now + subscriptionWaitDelay;

            while (DateTime.Now < latest)
            {
                // get subscriptions for topic
                var listSubscriptionsRequest = new ListSubscriptionsByTopicRequest
                {
                    TopicArn = topicArn
                };
                var subs = (await Client.ListSubscriptionsByTopicAsync(listSubscriptionsRequest)).Subscriptions;
                Assert.Equal(1, subs.Count);

                // test whether the subscription has been confirmed
                var subscription = subs[0];
                if (!string.Equals(subscription.SubscriptionArn, "PendingConfirmation", StringComparison.Ordinal))
                {
                    subArn = subscription.SubscriptionArn;
                    break;
                }

                // wait
                UtilityMethods.Sleep(TimeSpan.FromSeconds(15));
            }

            // verify that the subscription was confirmed and the arn has been set
            Assert.NotNull(subArn);

            // publish a message to the topic
            await Client.PublishAsync(new PublishRequest
            {
                TopicArn = topicArn,
                Subject  = "Test subject",
                Message  = "Test message"
            });

            // delete the subscription
            await Client.UnsubscribeAsync(new UnsubscribeRequest
            {
                SubscriptionArn = subArn
            });
        }