Exemplo n.º 1
0
        public void Post_ShouldReturnSuccess()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            NewsRelease post = TestData.CreateDbPost();

            context.NewsRelease.Add(post);
            context.SaveChanges();

            var releaseLogToCreate = new Models.PostLog
            {
                Description = "toto",
                DateTime    = DateTime.Now,
                PostKey     = post.Key
            };

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            var result = controller.AddPostLog(releaseLogToCreate) as ObjectResult;

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            result.Should().BeOfType <CreatedAtRouteResult>("because the create operation should go smoothly");
            result.StatusCode.Should().Be(201, "because HTTP Status 201 should be returned upon creation of new entry");
            var model = result.Value as Models.PostLog;

            model.Description.Should().Be("toto");
        }
Exemplo n.º 2
0
        public static NewsRelease CreateNewsRelease()
        {
            var release = new NewsRelease
            {
                Id              = Guid.NewGuid(),
                Key             = NextId().ToString(),
                Keywords        = string.Join(", ", f.Lorem.Words(3)),
                Year            = f.PickRandom(new[] { 2018, 2010, 2001, 1995 }),
                Timestamp       = f.Date.Past(),
                ReleaseDateTime = f.Date.Past(),
                PublishDateTime = f.Date.Past(),
                IsPublished     = f.PickRandom(new bool[] { true, true, false }),
                IsActive        = f.PickRandom(new bool[] { true, true, false }),
                IsCommitted     = f.PickRandom(new bool[] { true, true, false }),
                Logs            = CreateNewsReleaseLogCollection(f.Random.Number(1, 5))
            };

            // Link children to parent object
            foreach (var x in release.Logs)
            {
                x.ReleaseId = release.Id;
            }

            return(release);
        }
        public void GetPostLogs_ShouldReturnSuccess()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            NewsRelease post = TestData.CreateDbPost();

            context.NewsRelease.Add(post);
            post.NewsReleaseLog = TestData.CreateDbPostLogs(post);
            var expectedLogEntry = post.NewsReleaseLog.FirstOrDefault();
            var expectedCount    = post.NewsReleaseLog.Count;

            context.SaveChanges();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            var result = controller.GetPostLogs(post.Key) as ObjectResult;

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            result.Should().BeOfType(typeof(OkObjectResult), "because the read operation should go smoothly");
            var actual         = result.Value as IEnumerable <Models.PostLog>;
            var actualLogEntry = actual.FirstOrDefault();

            actual.Count().Should().Be(expectedCount);
            actualLogEntry.PostKey.Should().Be(expectedLogEntry.Release.Key);
            actualLogEntry.Description.Should().Be(expectedLogEntry.Description);
        }
 public NewsReleaseLogsControllerTests()
 {
     //-----------------------------------------------------------------------------------------------------------
     // Arrange
     //-----------------------------------------------------------------------------------------------------------
     _expectedModelReturn = TestData.TestNewsRelease;
     _logger = new Mock <ILogger <NewsReleaseLogsController> >();
     _mapper = CreateMapper();
 }
        public NewsRelease Update(string id, NewsRelease updatedRelease)
        {
            var release = NewsReleases.FirstOrDefault(r => r.Key == id);

            release.Keywords = updatedRelease.Keywords;

            NewsReleases.Remove(release);
            NewsReleases.Add(release);

            return(updatedRelease);
        }
Exemplo n.º 6
0
        public IActionResult Put(string id, [FromBody] NewsRelease model)
        {
            try
            {
                var oldRelease = _repository.GetReleaseByKey(id);
                if (oldRelease == null)
                {
                    return(NotFound($"Could not find a release with an id of {id}"));
                }
                _repository.Update(id, model);
                return(Ok(model));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
            }

            return(BadRequest("Couldn't update release"));
        }
Exemplo n.º 7
0
 public static ICollection <NewsReleaseLog> CreateDbPostLogs(NewsRelease release, string author = "Jane Doe")
 {
     return(new List <NewsReleaseLog>
     {
         new NewsReleaseLog
         {
             Id = 1,
             Description = "Created by " + author,
             DateTime = DateTime.Now.AddDays(-1),
             Release = release
         },
         new NewsReleaseLog {
             Id = 2,
             Description = "Edited by " + author,
             DateTime = DateTime.Now,
             Release = release
         }
     });
 }
Exemplo n.º 8
0
 public IActionResult AddPost(Models.Post post)
 {
     try
     {
         if (post == null)
         {
             throw new ValidationException();
         }
         var dbPost = new NewsRelease {
             Id = Guid.NewGuid(), IsPublished = true
         };
         dbPost.UpdateFromModel(post, dbContext);
         dbContext.NewsRelease.Add(dbPost);
         dbContext.SaveChanges();
         return(CreatedAtRoute("GetPost", new { key = dbPost.Key }, dbPost.ToModel(mapper)));
     }
     catch (Exception ex)
     {
         return(BadRequest("Failed to save a new post", ex));
     }
 }
Exemplo n.º 9
0
        public static NewsRelease CreateDbPost(string key = "2018PREM1234-123456", string pageTitle = "Test title")
        {
            var release = new NewsRelease
            {
                Id              = Guid.NewGuid(),
                Key             = key,
                ReleaseType     = ReleaseType.Story,
                Timestamp       = DateTime.Now,
                ReleaseDateTime = DateTime.Now,
                PublishDateTime = DateTime.Now,
                IsPublished     = true,
                IsActive        = true,
                IsCommitted     = true,
                Ministry        = new Ministry {
                    Id = Guid.NewGuid(), Key = "environment", DisplayName = "Environment"
                },
                NewsReleaseLanguage = new List <NewsReleaseLanguage> {
                    new NewsReleaseLanguage {
                        Summary = "summary", LanguageId = 4105
                    }
                }
            };

            release.NewsReleaseDocument = new List <NewsReleaseDocument>
            {
                new NewsReleaseDocument
                {
                    Id      = Guid.NewGuid(),
                    Release = release,
                    NewsReleaseDocumentLanguage = new List <NewsReleaseDocumentLanguage>
                    {
                        new NewsReleaseDocumentLanguage {
                            PageTitle = pageTitle
                        }
                    }
                }
            };
            return(release);
        }
Exemplo n.º 10
0
        public void GetPostLogs_ShouldReturnSuccess()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            NewsRelease post = TestData.CreateDbPost();

            context.NewsRelease.Add(post);
            // line 52 caused the test to fail after the update to EF core 3.0
            // post.NewsReleaseLog = TestData.CreateDbPostLogs(post);
            // lines 54 to 56 replace the problematic line 52 by saving the logs before adding them to the parent post
            var logs = TestData.CreateDbPostLogs(post);

            context.AddRange(logs);
            context.SaveChanges();
            post.NewsReleaseLog = logs;
            var expectedLogEntry = post.NewsReleaseLog.FirstOrDefault();
            var expectedCount    = post.NewsReleaseLog.Count;

            context.SaveChanges();

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            var result = controller.GetPostLogs(post.Key) as ObjectResult;

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            result.Should().BeOfType(typeof(OkObjectResult), "because the read operation should go smoothly");
            var actual         = result.Value as IEnumerable <Models.PostLog>;
            var actualLogEntry = actual.FirstOrDefault();

            actual.Count().Should().Be(expectedCount);
            actualLogEntry.PostKey.Should().Be(expectedLogEntry.Release.Key);
            actualLogEntry.Description.Should().Be(expectedLogEntry.Description);
        }
Exemplo n.º 11
0
        public InMemoryDataContext()
        {
            articleImages = new Dictionary <string, List <string> > {
                { "Public Safety and Solicitor General",
                  new List <string>
                  {
                      "https://images.unsplash.com/photo-1536245344390-dbf1df63c30a?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=0a5bf0efe2ef6417f062d7789d67b276&auto=format&fit=crop&w=2552&q=80",
                      "https://images.unsplash.com/photo-1528439828770-950eb52695a6?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=c75ea538c2f897da10fc750caef0c863&auto=format&fit=crop&w=2545&q=80",
                      "https://images.unsplash.com/photo-1534531105712-130df666d2a1?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=365264d02955eec6fab094e800e050f2&auto=format&fit=crop&w=2550&q=80"
                  } },
                { "Finance", new List <string>
                  {
                      "https://images.unsplash.com/photo-1531816466008-90e3a4b01450?ixlib=rb-0.3.5&s=39a4431a3ee4bc89a99bb6d9dc442a98&auto=format&fit=crop&w=2550&q=80",
                      "https://images.unsplash.com/photo-1521709986901-579827f9924a?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=087ed0470bf2e3f2afb83b62d8bc510e&auto=format&fit=crop&w=2500&q=80",
                      "https://images.unsplash.com/photo-1519162584292-56dfc9eb5db4?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=f6dec85ffce82ce1557a66a84ab088cd&auto=format&fit=crop&w=2168&q=80"
                  } },
                { "Education", new List <string>
                  {
                      "https://images.unsplash.com/photo-1496469888073-80de7e952517?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=33548b2ee7e57ecb0062b82c9f19d174&auto=format&fit=crop&w=2989&q=80",
                      "https://images.unsplash.com/photo-1488521787991-ed7bbaae773c?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=b73302ddf5fabf9285df2da9c462efbb&auto=format&fit=crop&w=2550&q=80",
                      "https://images.unsplash.com/photo-1477281765962-ef34e8bb0967?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=31e82f46b077da92e44712da8f71bc51&auto=format&fit=crop&w=2132&q=80"
                  } },
                { "Agriculture", new List <string>
                  {
                      "https://images.unsplash.com/photo-1535048637252-3a8c40fa2172?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=3a63c59c9a44ab1866557678994e9e36&auto=format&fit=crop&w=2990&q=80",
                      "https://images.unsplash.com/photo-1527847263472-aa5338d178b8?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=09296a26ab3cea9678943c1a80d1a7c4&auto=format&fit=crop&w=2553&q=80",
                      "https://images.unsplash.com/photo-1524486361537-8ad15938e1a3?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=72b0832e8eddf137d77b487bad06f280&auto=format&fit=crop&w=2549&q=80"
                  } },
                { "Health", new List <string>
                  {
                      "https://images.unsplash.com/photo-1526256262350-7da7584cf5eb?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=ca8d963483559212f18b75c07fb6302f&auto=format&fit=crop&w=2550&q=80",
                      "https://images.unsplash.com/photo-1512615199361-5c7a110a8d11?ixlib=rb-0.3.5&s=f0ff0a6ef8be3e7fb1472276b0b08ea1&auto=format&fit=crop&w=2550&q=80",
                      "https://images.unsplash.com/photo-1536052737712-5dacded6aa90?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=af4aa10e9e5bc7b32d095efddd240127&auto=format&fit=crop&w=2550&q=80"
                  } },
                { "Labour", new List <string>
                  {
                      "https://images.unsplash.com/photo-1532634882-df7054069092?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=5994f30310a545de6fd4dd6b95a73d46&auto=format&fit=crop&w=2550&q=80",
                      "https://images.unsplash.com/photo-1527289626332-8b3abcdda02b?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=1bd2deec78d4bce7123d179c6027d5a5&auto=format&fit=crop&w=2555&q=80",
                      "https://images.unsplash.com/photo-1521799022345-481a897e45ca?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=30de113284aab4977434a32b803f3e5f&auto=format&fit=crop&w=2802&q=80"
                  } },
            };

            lipsum = new LipsumGenerator();
            Random rnd = new Random();

            Articles = new List <Article>();

            for (var i = 1; i <= articlesToCreate; i++)
            {
                var ministry       = ministries[rnd.Next(ministries.Count())];
                var region         = regions[rnd.Next(regions.Count())];
                var ministryImages = articleImages[ministry];
                var article        = new Article
                {
                    Title           = string.Join("", lipsum.GenerateSentences(1)),
                    Summary         = string.Join("", lipsum.GenerateSentences(rnd.Next(1, 3))),
                    Body            = string.Join("", lipsum.GenerateParagraphs(rnd.Next(3, 6))),
                    ImageURL        = $"{ministryImages[rnd.Next(ministryImages.Count())]}",
                    Ministry        = $"{ministry}",                                          // pick a random ministry
                    Region          = $"{region}",
                    PublicationDate = DateTime.UtcNow.AddDays(-(rnd.NextDouble() * 90 - 30)), // vary the pub date between 30 and 90 days ago
                    TimeToRead      = rnd.Next(1, 10)
                };

                Articles.Add(article);
            }


            NewsReleases = new List <NewsRelease>();

            for (var i = 0; i < releasesToCreate; i++)
            {
                var releaseId = Guid.NewGuid();

                var release = new NewsRelease
                {
                    Id              = releaseId,
                    Key             = $"2018PREM{i}-{i}00000",
                    Year            = 2018,
                    Timestamp       = DateTime.Now,
                    ReleaseDateTime = DateTime.Now,
                    PublishDateTime = DateTime.Now,
                    IsPublished     = true,
                    IsActive        = true,
                    IsCommitted     = true,
                    Keywords        = "lorem, ipsum, dolor",
                    NewsReleaseLog  = new List <NewsReleaseLog>
                    {
                        new NewsReleaseLog
                        {
                            Id          = 1,
                            Description = "Created by Jane Doe",
                            DateTime    = DateTime.Now,
                            ReleaseId   = releaseId
                        },
                        new NewsReleaseLog {
                            Id          = 2,
                            Description = "Edited by John Doe",
                            DateTime    = DateTime.Now,
                            ReleaseId   = releaseId
                        }
                    }
                };

                NewsReleases.Add(release);
            }
        }
Exemplo n.º 12
0
 public void Delete(NewsRelease release)
 {
     NewsReleases.Remove(release);
 }
Exemplo n.º 13
0
 public void Delete(NewsRelease release)
 {
     _ctx.Delete(release);
 }
Exemplo n.º 14
0
 public NewsRelease Update(string id, NewsRelease release)
 {
     return(_ctx.Update(id, release));
 }