Exemplo n.º 1
0
        public void Get_ReturnsOk()
        {
            // Act
            var okResult = _controller.Get();

            // Assert
            Assert.IsType <OkObjectResult>(okResult.Result);
        }
Exemplo n.º 2
0
        public void GetAllSuccess()
        {
            Setup();

            AlbumsRequest request = new AlbumsRequest();

            albumAppServiceMock.Setup(p => p.GetAllAlbumsByFilter(It.IsAny <GetAllAlbumsByFilterRequest>())).Returns(getAllAlbumsByFilterResponse);


            var result = controller.Get(request);

            Assert.AreEqual(result.ListAlbums.Count, 2);
        }
Exemplo n.º 3
0
        public void Get_ReturnsAlbumsAndPhotosCombined()
        {
            // setup
            var mockClient = new Mock <ITypicodeClient>();

            mockClient.Setup(client => client.GetAlbumsAsync()).Returns(Task.FromResult(GetMockAlbums()));
            mockClient.Setup(client => client.GetPhotosAsync()).Returns(Task.FromResult(GetMockPhotos()));

            var controller = new AlbumsController(mockClient.Object);

            // act
            var actionResult  = controller.Get().GetAwaiter().GetResult();
            var contentResult = actionResult as OkNegotiatedContentResult <List <Album> >;

            // assert
            contentResult.Should().NotBeNull();
            var albums = contentResult.Content as List <Album>;

            albums.Should().NotBeNull();
            albums.Count.Should().Be(2);
            albums[0].Photos.Should().NotBeNull();
            albums[0].Photos.Count().Should().Be(2);
            albums[1].Photos.Should().NotBeNull();
            albums[1].Photos.Count().Should().Be(3);
        }
Exemplo n.º 4
0
        public void Get_ReturnsAlbumsAndPhotosAllAttributesPopulated()
        {
            // setup
            var mockClient = new Mock <ITypicodeClient>();

            mockClient.Setup(client => client.GetAlbumsAsync()).Returns(Task.FromResult(GetMockAlbums()));
            mockClient.Setup(client => client.GetPhotosAsync()).Returns(Task.FromResult(GetMockPhotos()));
            var controller = new AlbumsController(mockClient.Object);

            // act
            var actionResult  = controller.Get().GetAwaiter().GetResult();
            var contentResult = actionResult as OkNegotiatedContentResult <List <Album> >;

            // assert
            contentResult.Should().NotBeNull();
            var albums = contentResult.Content as List <Album>;

            albums.Should().NotBeNull();
            albums.Count.Should().Be(2);
            albums[0].Id.Should().Be(1);
            albums[0].Title.Should().Be("Album1");
            albums[0].UserId.Should().Be(1);
            albums[0].Photos.Count().Should().Be(2);
            albums[0].Photos[0].Id.Should().Be(1);
            //albums[0].Photos[0].AlbumId.Should().Be(1); // get accessor is internal - property won't be serialized
            albums[0].Photos[0].Title.Should().Be("Photo1");
            albums[0].Photos[0].Url.Should().Be("http://photo1");
            albums[0].Photos[0].ThumbnailUrl.Should().Be("http://thumb1");
        }
        public void Get_ShouldReturnTheCompleteListOfAlbums()
        {
            var albumList = new List <Album>
            {
                new Album()
                {
                    Title         = "t1",
                    CoverHttpPath = "c1"
                },

                new Album()
                {
                    Title         = "t2",
                    CoverHttpPath = "c2"
                }
            };


            _mockRepository.Setup(x => x.GetAll()).Returns(albumList);

            var controller = new AlbumsController(_mockRepository.Object);

            var albumsResult = controller.Get();

            Assert.AreEqual(albumList, albumsResult.Value);
        }
        public void GetShouldReturnAllAlbumsAndPhotos()
        {
            //ARRANGE
            _albumProcessor.Setup(x => x.GetAllAlbums()).Returns(new List <Album>
            {
                new Album
                {
                    Id     = 1,
                    UserId = 1,
                    Title  = "lorem ipsum",
                    Photos = new List <Photo>
                    {
                        new Photo
                        {
                            Id           = 1,
                            AlbumId      = 1,
                            Title        = "illum velit nesciunt soluta",
                            Url          = "https://via.placeholder.com/600/512ff0",
                            ThumbnailUrl = "https://via.placeholder.com/150/512ff0"
                        },
                        new Photo
                        {
                            Id           = 2,
                            AlbumId      = 1,
                            Title        = "eos eum nam quasi atque suscipit",
                            Url          = "https://via.placeholder.com/600/9902f3",
                            ThumbnailUrl = "https://via.placeholder.com/150/9902f3"
                        }
                    }
                },
                new Album
                {
                    Id     = 2,
                    UserId = 2,
                    Title  = "lorem ipsum"
                }
            });

            //ACT
            var result           = controller.Get();
            var statusCodeResult = result as OkObjectResult;
            var objectResult     = statusCodeResult.Value as List <Album>;

            //ASSERT
            statusCodeResult.StatusCode.ShouldBe(200);
            objectResult.Count.ShouldBe(2);
        }
Exemplo n.º 7
0
        public void Get_ReturnsNonNullResponse()
        {
            var mockClient = new Mock <ITypicodeClient>();
            var controller = new AlbumsController(mockClient.Object);

            var result = controller.Get().GetAwaiter().GetResult();

            result.Should().NotBeNull();
        }
Exemplo n.º 8
0
        public void GetByUserId_UserIdNotValid_ReturnsBadRequest()
        {
            var mockClient = new Mock <ITypicodeClient>();
            var controller = new AlbumsController(mockClient.Object);

            var actionResult  = controller.Get(0).GetAwaiter().GetResult();
            var contentResult = actionResult as BadRequestErrorMessageResult;

            contentResult.Should().NotBeNull();
        }
Exemplo n.º 9
0
        public void GetByUserId_ClientThrowsException_ReturnsInternalServerError()
        {
            var mockClient = new Mock <ITypicodeClient>();

            mockClient.Setup(client => client.GetAlbumsByUserIdAsync(It.IsAny <int>())).Throws(new Exception("Some error"));
            var controller = new AlbumsController(mockClient.Object);

            var actionResult  = controller.Get(1).GetAwaiter().GetResult();
            var contentResult = actionResult as ExceptionResult;

            contentResult.Should().NotBeNull();
        }
Exemplo n.º 10
0
        public async Task ShouldReturnSuccessAndCorrectData(int userid, int countOfAlbums, int countOfPhotos)
        {
            var mockHttp       = new MockHttpMessageHandler();
            var albumsTestData = new List <Album>()
            {
                new Album {
                    Id = 1, UserId = 123
                }
                , new Album {
                    Id = 2, UserId = 124
                }
                , new Album {
                    Id = 3, UserId = 124
                }
            };
            var photosTestData = new List <Photo>()
            {
                new Photo {
                    Id = 10, AlbumId = 1
                }
                , new Photo {
                    Id = 11, AlbumId = 2
                }
                , new Photo {
                    Id = 12, AlbumId = 2
                }
                , new Photo {
                    Id = 13, AlbumId = 3
                }
            };

            mockHttp.When("http://jsonplaceholder.typicode.com/albums")
            .Respond("application/json", JsonConvert.SerializeObject(albumsTestData));         // Respond with JSON

            mockHttp.When("http://jsonplaceholder.typicode.com/photos")
            .Respond("application/json", JsonConvert.SerializeObject(photosTestData));         // Respond with JSON

            var client = mockHttp.ToHttpClient();

            client.BaseAddress = new Uri("http://jsonplaceholder.typicode.com");

            var controller = new AlbumsController(new AlbumApiClient(client), new PhotoApiClient(client));

            var result = await controller.Get(userid);

            var response = ((OkObjectResult)result.Result).Value as IList <dynamic>;

            Assert.Equal(countOfAlbums, response.Count);

            var data = JsonConvert.DeserializeObject <List <TestResponse> >(JsonConvert.SerializeObject(response));

            Assert.Equal(countOfPhotos, data.Sum(album => album.Photos.Count));
        }
Exemplo n.º 11
0
        public void Get_NoAlbumsFound_ReturnsInternalServerError()
        {
            var mockClient = new Mock <ITypicodeClient>();

            mockClient.Setup(client => client.GetAlbumsAsync()).Returns(Task.FromResult <List <Album> >(null));
            var controller = new AlbumsController(mockClient.Object);

            var actionResult  = controller.Get().GetAwaiter().GetResult();
            var contentResult = actionResult as ExceptionResult;

            contentResult.Should().NotBeNull();
        }
Exemplo n.º 12
0
        public void Get_AlbumsFound_ReturnsSuccessResponse()
        {
            var mockClient = new Mock <ITypicodeClient>();

            mockClient.Setup(client => client.GetAlbumsAsync()).Returns(Task.FromResult(new List <Album>()));
            var controller = new AlbumsController(mockClient.Object);

            var actionResult  = controller.Get().GetAwaiter().GetResult();
            var contentResult = actionResult as OkNegotiatedContentResult <List <Album> >;

            contentResult.Should().NotBeNull();
        }
Exemplo n.º 13
0
        public void GetNotExistingAlbumTest()
        {
            //Setup
            Mock <IDataContext> mock = new Mock <IDataContext>();

            mock.Setup(context => context.Albums).Returns(GetAlbumsList());
            AlbumsController albumsController = new AlbumsController(mock.Object);

            //Action
            IActionResult result = albumsController.Get(10);

            //Assert
            var status = Assert.IsType <NotFoundResult>(result);
        }
Exemplo n.º 14
0
        public void GetByUserId_NoAlbumsFound_ReturnsEmptyList()
        {
            var mockClient = new Mock <ITypicodeClient>();

            mockClient.Setup(client => client.GetAlbumsByUserIdAsync(It.IsAny <int>())).Returns(Task.FromResult(new List <Album>()));
            var controller = new AlbumsController(mockClient.Object);

            var actionResult  = controller.Get(1).GetAwaiter().GetResult();
            var contentResult = actionResult as OkNegotiatedContentResult <List <Album> >;

            contentResult.Should().NotBeNull();
            var albums = contentResult.Content as List <Album>;

            albums.Should().NotBeNull();
            albums.Count.Should().Be(0);
        }
Exemplo n.º 15
0
        public void GetOneAlbumTest()
        {
            //Setup
            Mock <IDataContext> mock = new Mock <IDataContext>();

            mock.Setup(context => context.Albums).Returns(GetAlbumsList());
            AlbumsController albumsController = new AlbumsController(mock.Object);

            //Action
            IActionResult result = albumsController.Get(1);

            //Assert
            var status = Assert.IsType <OkObjectResult>(result);
            var model  = Assert.IsAssignableFrom <AlbumModel>(status.Value);

            Assert.Equal(1, model.Id);
            Assert.Equal("TestName1", model.AlbumName);
        }
Exemplo n.º 16
0
        public void Get_WhenNoPhotos_ReturnsEmptyAlbums()
        {
            var mockClient = new Mock <ITypicodeClient>();

            mockClient.Setup(client => client.GetAlbumsAsync()).Returns(Task.FromResult(GetMockAlbums()));
            mockClient.Setup(client => client.GetPhotosAsync()).Returns(Task.FromResult <List <Photo> >(null));
            var controller = new AlbumsController(mockClient.Object);

            var actionResult  = controller.Get().GetAwaiter().GetResult();
            var contentResult = actionResult as OkNegotiatedContentResult <List <Album> >;

            contentResult.Should().NotBeNull();
            var albums = contentResult.Content as List <Album>;

            albums.Should().NotBeNull();
            albums.Count.Should().Be(2);
            albums[0].Photos.Should().BeNull();
            albums[1].Photos.Should().BeNull();
        }
        public async Task Get_ShouldReturnList()
        {
            // Arrange
            var controller = new AlbumsController();

            // Act
            var actionResult = await controller.Get();

            var contentResult = actionResult as OkNegotiatedContentResult <List <Album> >;
            var actual        = contentResult?.Content;

            // Assert
            Assert.IsNotNull(actionResult);
            Assert.IsNotNull(contentResult);
            Assert.IsNotNull(actual);
            Assert.AreEqual(100, actual.Count());
            Assert.IsTrue(actual.All(m => m.Id > 0));
            Assert.IsTrue(actual.All(m => m.UserId > 0));
            Assert.IsFalse(actual.All(m => string.IsNullOrWhiteSpace(m.Title)));
            Assert.AreNotEqual(actual.First().Id, actual.Last().Id);
        }
Exemplo n.º 18
0
        public void GetByUserId_ReturnsAlbumWithPhotos()
        {
            // setup
            var mockClient = new Mock <ITypicodeClient>();

            mockClient.Setup(client => client.GetAlbumsByUserIdAsync(1)).Returns(Task.FromResult(GetMockAlbums().FindAll(a => a.UserId == 1)));
            mockClient.Setup(client => client.GetPhotosByAlbumIdAsync(1)).Returns(Task.FromResult(GetMockPhotos().FindAll(p => p.Id == 1 || p.Id == 2)));

            var controller = new AlbumsController(mockClient.Object);

            // act
            var actionResult  = controller.Get(1).GetAwaiter().GetResult();
            var contentResult = actionResult as OkNegotiatedContentResult <List <Album> >;

            // assert
            contentResult.Should().NotBeNull();
            var albums = contentResult.Content as List <Album>;

            albums.Should().NotBeNull();
            albums.Count.Should().Be(1);
            albums[0].Photos.Should().NotBeNull();
            albums[0].Photos.Count().Should().Be(2);
        }
Exemplo n.º 19
0
        public async Task TestCase_ReadSingle(String usrid, int albumid)
        {
            var context = fixture.GetCurrentDataContext();

            this.fixture.InitTestData(context);

            var mockHttpContextAccessor = new Mock <IHttpContextAccessor>();

            mockHttpContextAccessor.Setup(req => req.HttpContext.User.Identity.Name).Returns(
                //It.IsAny<string>(usrid)
                usrid
                );

            var control = new AlbumsController(context, mockHttpContextAccessor.Object);

            var getrst = control.Get(albumid);

            Assert.NotNull(getrst);
            //var getokrst = Assert.IsType<OkObjectResult>(getrst);
            //var allalbums = Assert.IsAssignableFrom<IQueryable<Album>>(getokrst.Value);

            //if (String.IsNullOrEmpty(usrid))
            //{
            //    Assert.Equal(1, allalbums.Count());
            //}
            //else if (usrid == DataSetupUtility.UserA)
            //{
            //    Assert.Equal(2, allalbums.Count());
            //}
            //else if (usrid == DataSetupUtility.UserB)
            //{
            //    Assert.Equal(2, allalbums.Count());
            //}

            await context.DisposeAsync();
        }