public void DuplicateTitle_ThrowArgumentException() { // Arrange var userId = "first"; var title = "FeedCollectionTestTitle"; var feedCollections = new List <FeedCollection>(); NewsRepoMock .Setup(repo => repo.Add(It.IsAny <FeedCollection>())) .Callback <FeedCollection>(feedCollections.Add); NewsRepoMock .Setup(repo => repo.Contains(title, userId)) .Returns(true); var request = new CreateFeedCollectionRequest { UserId = userId, Title = title }; // Act var fcID = Service.CreateFeedCollection(request); // Assert Assert.Fail(); }
public IActionResult Post([FromBody] CreateFeedCollectionRequest request) { try { request.UserId = _requestUserProvider.GetUserId(); _logger.LogInformation("User " + request.UserId + " called method NewsController.Post(string)"); var id = _newsServices.CreateFeedCollection(request); return(Ok(id)); } catch (Exception e) { return(BadRequest(e.Message)); } }
public void InvalidTitle_ThrowArgumentException() { // Arrange var userId = "first"; var feedCollections = new List <FeedCollection>(); NewsRepoMock .Setup(repo => repo.Add(It.IsAny <FeedCollection>())) .Callback <FeedCollection>(feedCollections.Add); var request = new CreateFeedCollectionRequest { UserId = userId }; // Act var fcID = Service.CreateFeedCollection(request); // Assert Assert.Fail(); }
public void ValidTitle_ReturnFeedCollectionId() { // Arrange var userId = "first"; var title = "FeedCollectionTestTitle"; var feedCollections = new List <FeedCollection>(); NewsRepoMock .Setup(repo => repo.Add(It.IsAny <FeedCollection>())) .Callback <FeedCollection>(feedCollections.Add); var request = new CreateFeedCollectionRequest { UserId = userId, Title = title }; // Act var fcID = Service.CreateFeedCollection(request); // Assert Assert.AreEqual(fcID, feedCollections[0].ID); NewsRepoMock.Verify(repo => repo.Add(feedCollections[0])); }
public int CreateFeedCollection(CreateFeedCollectionRequest request) { _logger.LogInformation("User " + request.UserId + " called method CreateFeedCollection(string)"); if (request.Title == "" || request.Title == null) { _logger.LogWarning("CreateFeedCollection threw ArgumentException: User #" + request.UserId + " gave improper title for Feed Collection."); throw new ArgumentException("Improper title for Feed Collection."); } if (_newsRepository.Contains(request.Title, request.UserId)) { _logger.LogError("CreateFeedCollection threw ArgumentException: User #" + request.UserId + " has already created Feed Collection with title \"" + request.Title + "\""); throw new DuplicateWaitObjectException("This user has already created Feed Collection with this title."); } var feedCollection = new FeedCollection { Title = request.Title, UserID = request.UserId }; _newsRepository.Add(feedCollection); var n = _newsRepository.SaveChanges(); if (n > 0) { _cache.Set("fc_" + feedCollection.ID, feedCollection, new MemoryCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30) }); } return(feedCollection.ID); }