Exemplo n.º 1
0
        public void GetTagFile_NormalInput_ReturnsTagFile()
        {
            // Arrange
            TagFileS3Access fakeTagFileS3Access = new TagFileS3Access();

            var fakeS3Client = Substitute.For <IMyAmazonS3Client>();

            fakeTagFileS3Access.S3Client = fakeS3Client;
            var getObjectRequest = S3AccessTests.GetGetObjectRequest("test", "test");

            // we write some fake content to the stream
            string expectedTagFile = "scripture-1\ntheology-1\nfun-1,2\nbunny-2\npaul-2";

            GetObjectResponse fakeResponse = WriteContentToStream(expectedTagFile);


            fakeS3Client.GetObjectAsync(getObjectRequest, default(System.Threading.CancellationToken)).ReturnsForAnyArgs(fakeResponse);

            // Act
            var response = fakeTagFileS3Access.GetTagFile();

            response.Wait();
            TagFile result = response.Result;

            // Assert
            // I'm generally not a fan of multiple assertions, but I am drawing a blank here
            Assert.That(result != null);
            Assert.That(result.Count == 5);
            Assert.That(result.GetIdsFromTag("fun").Count == 2);
        }
        public void GetObject_NormalInput_ReturnsContent()
        {
            // Arrange
            var fakeS3Access = new FakeS3Access();
            var fakeS3Client = Substitute.For <IMyAmazonS3Client>();

            fakeS3Access.S3Client = fakeS3Client;
            var getObjectRequest = S3AccessTests.GetGetObjectRequest("test", "test");

            // we write some fake content to the stream
            string expectedContent = "This is some fake content";
            var    fakeResponse    = new GetObjectResponse();

            fakeResponse.Key           = "fakeKey";
            fakeResponse.BucketName    = "fakeBucket";
            fakeResponse.ContentLength = expectedContent.Length;
            MemoryStream stream = new MemoryStream();

            StreamWriter writer = new StreamWriter(stream);

            {
                try
                {
                    writer.Write(expectedContent);
                    stream.Seek(0, SeekOrigin.Begin);
                    stream.Position = 0;
                    writer.Flush();
                    stream.Seek(0, SeekOrigin.Begin);
                }
                catch (Exception ex)
                {
                    Assert.Fail("Writing to Stream to set test up failed!");
                }
            }

            fakeResponse.ResponseStream = stream;

            fakeS3Client.GetObjectAsync(getObjectRequest, default(System.Threading.CancellationToken)).ReturnsForAnyArgs(fakeResponse);

            // Act
            var response = fakeS3Access.GetObject(getObjectRequest);

            response.Wait();
            var actualContent = response.Result;

            // we have to do this here because if we dispose it in a using() on line 79, we can't read from
            // the stream! or at least I _think_ that's why
            writer.Dispose();

            // Assert
            Assert.That(expectedContent.Equals(actualContent));
        }
Exemplo n.º 3
0
        public void GetTagFile_MalformedTagFiles_ThrowsException(string malformedTagFile)
        {
            // Arrange
            TagFileS3Access access = new TagFileS3Access();

            var fakeS3Client = Substitute.For <IMyAmazonS3Client>();

            access.S3Client = fakeS3Client;
            var getObjectRequest = S3AccessTests.GetGetObjectRequest("test", "test");

            GetObjectResponse fakeResponse = WriteContentToStream(malformedTagFile);

            fakeS3Client.GetObjectAsync(getObjectRequest, default(System.Threading.CancellationToken)).ReturnsForAnyArgs(fakeResponse);

            // Act/Assert
            Assert.ThrowsAsync <TagFileException>(() => access.GetTagFile());
        }
        public void GetObject_NonExistentId_LogsError()
        {
            // Arrange
            var fakeS3Client = new FakeMyAmazonS3ClientThatReturnsNull(Utility.GetS3Config("test"));

            BlogPostS3Access access = new BlogPostS3Access("test", "test")
            {
                S3Client = fakeS3Client
            };

            var fakeLambdaLogger = Substitute.For <ILambdaLogger>();

            access.Logger = fakeLambdaLogger;

            // Act
            var response = access.GetObject(S3AccessTests.GetGetObjectRequest());

            response.Wait();
            var expectedResult = response.Result;

            // Assert
            fakeLambdaLogger.Received().Log(Arg.Any <string>());
        }