Beispiel #1
0
        public void InvalidFeed_ThrowArgumentException()
        {
            // Arrange
            var userId = "first";
            var feedCollectionTitle = "First_Testing_FC";

            var feedCollections = new List <FeedCollection>
            {
                new FeedCollection {
                    ID = 1, Title = feedCollectionTitle, UserID = userId
                }
            };

            NewsRepoMock
            .Setup(repo => repo.GetUserFeedCollections(userId))
            .Returns(feedCollections);

            NewsRepoMock
            .Setup(repo => repo.GetFeed(1))
            .Returns(new Feed());

            var request = new AddFeedToCollectionRequest
            {
                UserId           = userId,
                FeedCollectionId = feedCollections[0].ID,
                FeedId           = 1
            };

            // Act
            Service.AddFeedToCollection(request);

            // Assert
            Assert.Fail();
        }
Beispiel #2
0
        public void InvalidFeedCollection_ThrowArgumentException()
        {
            // Arrange
            var userId   = "first";
            var feedLink = "testing link 1 for feed 1";

            var feeds = new List <Feed>
            {
                new Feed {
                    ID = 1, Link = feedLink, FeedType = FeedType.Atom
                }
            };

            NewsRepoMock
            .Setup(repo => repo.GetUserFeedCollections(userId))
            .Returns(new List <FeedCollection>());

            NewsRepoMock
            .Setup(repo => repo.GetFeed(1))
            .Returns(feeds[0]);

            var request = new AddFeedToCollectionRequest
            {
                UserId           = userId,
                FeedCollectionId = 1,
                FeedId           = feeds[0].ID
            };

            // Act
            Service.AddFeedToCollection(request);

            // Assert
            Assert.Fail();
        }
Beispiel #3
0
        public IActionResult AddFeedToCollection([FromBody] AddFeedToCollectionRequest request)
        {
            _restClient.BaseUrl = new System.Uri("http://localhost:50312/api/news");
            var restRequst = new RestRequest(Method.PUT);

            restRequst.AddHeader("Cache-Control", "no-cache");
            restRequst.AddHeader("Content-Type", "application/json");
            restRequst.AddHeader("Authorization", "Bearer " + AuthToken);
            restRequst.AddParameter("undefined", "{\n\t\"feedCollectionId\": " + request.FeedCollectionId + ",\n\t\"feedId\": " + request.FeedId + "\n}", ParameterType.RequestBody);
            IRestResponse response = _restClient.Execute(restRequst);

            return(Ok());
        }
Beispiel #4
0
        public void InvalidFeedId_ThrowArgumentException()
        {
            // Arrange
            var userId  = "first";
            var request = new AddFeedToCollectionRequest
            {
                UserId           = userId,
                FeedCollectionId = 1,
            };

            // Act
            Service.AddFeedToCollection(request);

            // Assert
            Assert.Fail();
        }
Beispiel #5
0
        public IActionResult Put([FromBody] AddFeedToCollectionRequest request)
        {
            try
            {
                request.UserId = _requestUserProvider.GetUserId();
                _logger.LogInformation("User " + request.UserId + " called method NewsController.Put(int, int)");

                _newsServices.AddFeedToCollection(request);

                return(Ok());
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }
Beispiel #6
0
        public void FeedCollectionContainsFeed_ThrowArgumentException()
        {
            // Arrange
            var userId = "first";
            var feedCollectionTitle = "First_Testing_FC";
            var feedLink            = "testing link 1 for feed 1";

            var feedCollections = new List <FeedCollection>
            {
                new FeedCollection {
                    ID = 1, Title = feedCollectionTitle, UserID = userId
                }
            };

            var feeds = new List <Feed>
            {
                new Feed {
                    ID = 1, Link = feedLink, FeedType = FeedType.Atom
                }
            };

            NewsRepoMock
            .Setup(repo => repo.Contains(1, 1))
            .Returns(true);

            NewsRepoMock
            .Setup(repo => repo.GetFeedCollection(feedCollections[0].ID))
            .Returns(feedCollections[0]);

            NewsRepoMock
            .Setup(repo => repo.GetFeed(feeds[0].ID))
            .Returns(feeds[0]);

            var request = new AddFeedToCollectionRequest
            {
                UserId           = userId,
                FeedCollectionId = feeds[0].ID,
                FeedId           = feeds[0].ID
            };

            // Act
            Service.AddFeedToCollection(request);

            // Assert
            Assert.Fail();
        }
Beispiel #7
0
        public void ValidFeed_ValidFeedCollection_Success()
        {
            // Arrange
            var userId = "first";
            var feedCollectionTitle = "First_Testing_FC";
            var feedLink            = "testing link 1 for feed 1";

            var feedCollections = new List <FeedCollection>
            {
                new FeedCollection {
                    ID = 1, Title = feedCollectionTitle, UserID = userId
                }
            };

            var feed = new Feed {
                ID = 1, Link = feedLink, FeedType = FeedType.Atom
            };

            NewsRepoMock
            .Setup(repo => repo.GetUserFeedCollections(userId))
            .Returns(feedCollections);

            NewsRepoMock
            .Setup(repo => repo.GetFeedCollection(1))
            .Returns(feedCollections[0]);

            NewsRepoMock
            .Setup(repo => repo.GetFeed(1))
            .Returns(feed);

            var request = new AddFeedToCollectionRequest
            {
                UserId           = userId,
                FeedCollectionId = feedCollections[0].ID,
                FeedId           = feed.ID
            };

            // Act
            Service.AddFeedToCollection(request);

            // Assert
            NewsRepoMock.Verify(repo => repo.Add(feed.ID, feedCollections[0].ID));
        }
Beispiel #8
0
        public void AddFeedToCollection(AddFeedToCollectionRequest request)
        {
            try
            {
                _logger.LogInformation("User " + request.UserId + " called method AddFeedToCollection(int, int)");

                if (request.FeedId == 0 || _newsRepository.GetFeed(request.FeedId) == null)
                {
                    _logger.LogWarning("AddFeedToCollection threw ArgumentException: User #" + request.UserId +
                                       " gave improper feedId.");
                    throw new ArgumentException("This feed doesn't exist.");
                }
                if (request.FeedCollectionId == 0 || _newsRepository.GetFeedCollection(request.FeedCollectionId) == null)
                {
                    _logger.LogWarning("AddFeedToCollection threw ArgumentException: User #" + request.UserId +
                                       " gave improper feedCollectionId.");
                    throw new ArgumentException("This feed collection doesn't exist.");
                }
                if (_newsRepository.Contains(request.FeedCollectionId, request.FeedId))
                {
                    _logger.LogWarning("AddFeedToCollection threw ArgumentException: User #" + request.UserId +
                                       " tried to add Feed to Feed Collection 2nd time.");
                    throw new DuplicateWaitObjectException("This feed collection already contains this feed.");
                }
            }
            catch (InvalidOperationException e)
            {
                _logger.LogWarning("AddFeedToCollection threw InvalidOperationException: User #" + request.UserId +
                                   " tried to work with non-existing Feed or Feed Collection.");
                throw e;
            }

            _newsRepository.Add(request.FeedCollectionId, request.FeedId);
            _newsRepository.SaveChanges();

            _cache.Remove("fc_" + request.FeedCollectionId);
        }