Exemplo n.º 1
0
        public async Task AddMemeToCollectionAsync_Should_Pass()
        {
            // Arrange
            MemeDto expectedMemeDto = new MemeDto
            {
                Id       = "a0Q558q",
                ImageUrl = "https://images-cdn.9gag.com/photo/a0Q558q_700b.jpg",
                VideoUrl = "http://img-9gag-fun.9cache.com/photo/a0Q558q_460sv.mp4",
                PageUrl  = "http://9gag.com/gag/a0Q558q",
                Title    = "Old but Gold"
            };
            Meme entity = DtoToEntityConverter.Convert <Meme, MemeDto>(expectedMemeDto);

            Assert.IsTrue(await MemeRepository.CreateAsync(entity));
            AddMemeToCollectionDto addMemeToCollectionDto = new AddMemeToCollectionDto
            {
                MemeId       = "a0Q558q",
                UserId       = 1,
                CollectionId = 1
            };

            // Act
            ServiceResponseDto addMemeToCollectionResultDto = await CollectionItemDetailService.AddMemeToCollectionAsync(addMemeToCollectionDto);

            // Assert
            Assert.IsTrue(addMemeToCollectionResultDto.Success);
            Assert.AreEqual(addMemeToCollectionResultDto.Message, "Successfully added meme to the collection.");
            CollectionItemDetail actualCollectionItemDetail = (await CollectionItemDetailRepository.GetAllAsync()).First();

            Assert.IsTrue(
                actualCollectionItemDetail.MemeId.Equals("a0Q558q") &&
                actualCollectionItemDetail.AddedByUserId.Equals(1) &&
                actualCollectionItemDetail.CollectionId.Equals(1));
        }
Exemplo n.º 2
0
        public async Task AddMemeToCollectionAsync_Should_Fail_With_Message_Meme_Does_Not_Exist()
        {
            // Arrange
            AddMemeToCollectionDto addMemeToCollectionDto = new AddMemeToCollectionDto
            {
                MemeId       = "a0Q558q",
                UserId       = 1,
                CollectionId = 1
            };

            // Act
            ServiceResponseDto serviceResultDto = await CollectionItemDetailService.AddMemeToCollectionAsync(addMemeToCollectionDto);

            // Assert
            Assert.IsFalse(serviceResultDto.Success);
            Assert.AreEqual(serviceResultDto.Message, "Meme does not exist.");
        }
Exemplo n.º 3
0
        public async Task RemoveMemeFromCollectionAsync_Should_Pass()
        {
            // Arrange
            MemeDto expectedMemeDto = new MemeDto
            {
                Id       = "a0Q558q",
                ImageUrl = "https://images-cdn.9gag.com/photo/a0Q558q_700b.jpg",
                VideoUrl = "http://img-9gag-fun.9cache.com/photo/a0Q558q_460sv.mp4",
                PageUrl  = "http://9gag.com/gag/a0Q558q",
                Title    = "Old but Gold"
            };
            Meme entity = DtoToEntityConverter.Convert <Meme, MemeDto>(expectedMemeDto);

            Assert.IsTrue(await MemeRepository.CreateAsync(entity));
            AddMemeToCollectionDto addMemeToCollectionDto = new AddMemeToCollectionDto
            {
                MemeId       = "a0Q558q",
                UserId       = 1,
                CollectionId = 1
            };
            ServiceResponseDto addMemeToCollectionResultDto = await CollectionItemDetailService.AddMemeToCollectionAsync(addMemeToCollectionDto);

            Assert.IsTrue(addMemeToCollectionResultDto.Success);
            RemoveMemeFromCollectionDto removeMemeFromCollectionDto = new RemoveMemeFromCollectionDto
            {
                CollectionId           = 1,
                CollectionItemDetailId = 1,
                UserId = 1
            };

            // Act
            ServiceResponseDto serviceResponseDto = await CollectionItemDetailService.RemoveMemeFromCollectionAsync(removeMemeFromCollectionDto);

            // Assert
            Assert.IsTrue(serviceResponseDto.Success);
            List <CollectionItemDetail> collectionItemDetails = (await CollectionItemDetailRepository.GetAllAsync()).ToList();

            Assert.AreEqual(collectionItemDetails.Count, 0);
        }
Exemplo n.º 4
0
        public async Task RemoveMemeFromCollectionAsync_Should_Throw_ApplicationUserIsNotAuthorizedException()
        {
            // Arrange
            MemeDto expectedMemeDto = new MemeDto
            {
                Id       = "a0Q558q",
                ImageUrl = "https://images-cdn.9gag.com/photo/a0Q558q_700b.jpg",
                VideoUrl = "http://img-9gag-fun.9cache.com/photo/a0Q558q_460sv.mp4",
                PageUrl  = "http://9gag.com/gag/a0Q558q",
                Title    = "Old but Gold"
            };
            Meme entity = DtoToEntityConverter.Convert <Meme, MemeDto>(expectedMemeDto);

            Assert.IsTrue(await MemeRepository.CreateAsync(entity));
            AddMemeToCollectionDto addMemeToCollectionDto = new AddMemeToCollectionDto
            {
                MemeId       = "a0Q558q",
                UserId       = 1,
                CollectionId = 1
            };
            ServiceResponseDto addMemeToCollectionResultDto = await CollectionItemDetailService.AddMemeToCollectionAsync(addMemeToCollectionDto);

            Assert.IsTrue(addMemeToCollectionResultDto.Success);


            RemoveMemeFromCollectionDto removeMemeFromCollectionDto = new RemoveMemeFromCollectionDto
            {
                CollectionId           = 1,
                CollectionItemDetailId = 1,
                UserId = 2
            };

            // Act & Assert
            ServiceResponseDto serviceResponseDto =
                await CollectionItemDetailService.RemoveMemeFromCollectionAsync(removeMemeFromCollectionDto);

            Assert.IsFalse(serviceResponseDto.Success);
            Assert.AreEqual(serviceResponseDto.Message, "Failed to remove meme because user is not authorized.");
        }
        public async Task <ServiceResponseDto> AddMemeToCollectionAsync(AddMemeToCollectionDto addMemeToCollectionDto)
        {
            ServiceResponseDto serviceResultDto = new ServiceResponseDto();

            if (!await _memeRepository.ExistsAsync(meme => meme.Id.Equals(addMemeToCollectionDto.MemeId)))
            {
                serviceResultDto.Message = "Meme does not exist.";
                return(serviceResultDto);
            }

            if (await _collectionItemDetailRepository.ExistsAsync(collectionItemDetail =>
                                                                  collectionItemDetail.MemeId.Equals(addMemeToCollectionDto.MemeId) &&
                                                                  collectionItemDetail.CollectionId.Equals(addMemeToCollectionDto.CollectionId)))
            {
                serviceResultDto.Message = "Meme already exists in the collection.";
                return(serviceResultDto);
            }

            CollectionItemDetail actualCollectionItemDetail = new CollectionItemDetail
            {
                AddedByUserId = addMemeToCollectionDto.UserId,
                CollectionId  = addMemeToCollectionDto.CollectionId,
                MemeId        = addMemeToCollectionDto.MemeId
            };

            if (await _collectionItemDetailRepository.CreateAsync(actualCollectionItemDetail))
            {
                serviceResultDto.Success = true;
                serviceResultDto.Message = "Successfully added meme to the collection.";
            }
            else
            {
                serviceResultDto.Message = "Failed to add meme to the collection.";
            }
            return(serviceResultDto);
        }