Esempio n. 1
0
        public IActionResult Edit(int id, int discoveryId, StarFormServiceModel model)
        {
            string oldName = this.starService.GetName(id);

            if (oldName == null)
            {
                return(BadRequest());
            }

            string newName = model.Name;

            if (this.starService.Exists(newName) &&
                oldName != newName)
            {
                TempData.AddErrorMessage(string.Format(WebConstants.EntryExists, Star));
                return(View(model));
            }

            bool success = this.starService.Edit(id, model.Name, model.Temperature);

            if (!success)
            {
                return(BadRequest());
            }

            TempData.AddSuccessMessage(string.Format(WebConstants.SuccessfullEntityOperation, Star, WebConstants.Edited));

            return(RedirectToAction(nameof(DiscoveriesController.Details), Discoveries, new { id = discoveryId, area = WebConstants.AstronomerArea }));
        }
Esempio n. 2
0
        public IActionResult Create(int id, StarFormServiceModel model)
        {
            if (this.starService.Exists(model.Name))
            {
                TempData.AddErrorMessage(string.Format(WebConstants.EntryExists, Star));
                return(View(model));
            }

            if (this.discoveryService.TotalStars(id) >= DataConstants.DiscoveryConstants.MaxStarsPerDiscovery)
            {
                TempData.AddErrorMessage(string.Format(
                                             DataConstants.DiscoveryConstants.MaxStarsPerDiscoveryErrorMessage,
                                             DataConstants.DiscoveryConstants.MaxStarsPerDiscovery));

                return(View(model));
            }

            bool success = this.starService.Create(id, model.Name, model.Temperature);

            if (!success)
            {
                return(BadRequest());
            }

            TempData.AddSuccessMessage(string.Format(WebConstants.SuccessfullEntityOperation, Star, WebConstants.Added));

            return(RedirectToAction(nameof(DiscoveriesController.Details), Discoveries, new { id, area = WebConstants.AstronomerArea }));
        }
        public void EditPost_WithNotSuccessfullEdit_ShouldReturnView()
        {
            // Arranges
            Mock <IStarService> starService = new Mock <IStarService>();

            StarFormServiceModel model = this.GetStarFormModel();

            starService
            .Setup(s => s.Exists(It.IsAny <string>()))
            .Returns(false);

            starService
            .Setup(s => s.GetName(It.IsAny <int>()))
            .Returns(model.Name);

            starService
            .Setup(s => s.Edit(It.IsAny <int>(), It.IsAny <string>(), It.IsAny <int>()))
            .Returns(false);

            StarsController starsController = new StarsController(starService.Object, null);

            // Act
            IActionResult result = starsController.Edit(1, 1, model);

            // Assert
            Assert.IsType <BadRequestResult>(result);
        }
        public void CreatePost_WithExistingStarName_ShouldReturnView()
        {
            // Arrange
            Mock <IStarService> starService = new Mock <IStarService>();

            starService
            .Setup(s => s.Exists(It.IsAny <string>()))
            .Returns(true);

            Mock <ITempDataDictionary> tempData = new Mock <ITempDataDictionary>();

            string errorMessage = null;

            tempData
            .SetupSet(t => t[WebConstants.TempDataErrorMessage]    = It.IsAny <string>())
            .Callback((string key, object message) => errorMessage = message as string);

            StarFormServiceModel formModel       = this.GetStarFormModel();
            StarsController      starsController = new StarsController(starService.Object, null);

            starsController.TempData = tempData.Object;

            // Act
            IActionResult result = starsController.Create(1, formModel);

            // Assert
            Assert.IsType <ViewResult>(result);
            object model = (result as ViewResult).Model;

            Assert.IsType <StarFormServiceModel>(model);
            StarFormServiceModel returnModel = model as StarFormServiceModel;

            this.AssertStars(formModel, returnModel);
            Assert.Equal(string.Format(WebConstants.EntryExists, Star), errorMessage);
        }
        public void CreatePost_WithNotSuccessfullyCreatedStar_ShouldReturnBadRequest()
        {
            // Arrange
            Mock <IStarService>      starService      = new Mock <IStarService>();
            Mock <IDiscoveryService> discoveryService = new Mock <IDiscoveryService>();

            starService
            .Setup(s => s.Exists(It.IsAny <string>()))
            .Returns(false);

            discoveryService
            .Setup(d => d.TotalStars(It.IsAny <int>()))
            .Returns(1);

            starService
            .Setup(s => s.Create(It.IsAny <int>(), It.IsAny <string>(), It.IsAny <int>()))
            .Returns(false);

            StarsController starsController = new StarsController(starService.Object, discoveryService.Object);

            starsController.TempData = Mock.Of <ITempDataDictionary>();
            StarFormServiceModel formModel = this.GetStarFormModel();

            // Act
            IActionResult result = starsController.Create(1, formModel);

            // Assert
            Assert.IsType <BadRequestResult>(result);
        }
Esempio n. 6
0
        public IActionResult Edit(int id, int discoveryId)
        {
            StarFormServiceModel model = this.starService.GetForm(id);

            if (model == null)
            {
                return(BadRequest());
            }

            return(View(model));
        }
Esempio n. 7
0
        public void GetForm_WithNotExistingId_ShouldReturnNull()
        {
            // Arrange
            StarStuffDbContext db          = this.Database;
            StarService        starService = new StarService(db);

            // Act
            StarFormServiceModel result = starService.GetForm(1);

            // Assert
            Assert.Null(result);
        }
Esempio n. 8
0
        public void GetForm_WithExistingId_ShouldReturnStar()
        {
            // Arrange
            StarStuffDbContext db          = this.Database;
            StarService        starService = new StarService(db);

            this.SeedDatabase(db);

            const int starId = 1;

            Star expected = this.GetFakeStars().First();

            // Act
            StarFormServiceModel actual = starService.GetForm(starId);

            // Assert
            this.CompareStars(expected, actual);
        }
        public void EditGet_WithNotExistingId_ShouldReturnBadRequest()
        {
            // Arrange
            Mock <IStarService>  starService = new Mock <IStarService>();
            StarFormServiceModel model       = null;

            starService
            .Setup(s => s.GetForm(It.IsAny <int>()))
            .Returns(model);

            StarsController starsController = new StarsController(starService.Object, null);

            // Act
            IActionResult result = starsController.Edit(1, 1);

            // Assert
            Assert.IsType <BadRequestResult>(result);
        }
        public void CreatePost_WithSuccessfullyCreatedStar_ShouldReturnRedirectResult()
        {
            // Arrange
            Mock <IStarService>      starService      = new Mock <IStarService>();
            Mock <IDiscoveryService> discoveryService = new Mock <IDiscoveryService>();

            const int discoveryId = 1;

            starService
            .Setup(s => s.Exists(It.IsAny <string>()))
            .Returns(false);

            discoveryService
            .Setup(d => d.TotalStars(It.IsAny <int>()))
            .Returns(1);

            starService
            .Setup(s => s.Create(It.IsAny <int>(), It.IsAny <string>(), It.IsAny <int>()))
            .Returns(true);

            Mock <ITempDataDictionary> tempData = new Mock <ITempDataDictionary>();

            string successmessage = null;

            tempData
            .SetupSet(t => t[WebConstants.TempDataSuccessMessage]    = It.IsAny <string>())
            .Callback((string key, object message) => successmessage = message as string);

            StarFormServiceModel formModel       = this.GetStarFormModel();
            StarsController      starsController = new StarsController(starService.Object, discoveryService.Object);

            starsController.TempData = tempData.Object;

            // Act
            IActionResult result = starsController.Create(discoveryId, formModel);

            // Assert
            Assert.IsType <RedirectToActionResult>(result);
            RedirectToActionResult redirectResult = result as RedirectToActionResult;

            this.AssertRedirect(discoveryId, redirectResult);
            Assert.Equal(string.Format(WebConstants.SuccessfullEntityOperation, Star, WebConstants.Added), successmessage);
        }
        public void CreatePost_WithDiscoveryWithMoreThanThreeStars_ShouldReturnView()
        {
            // Arrange
            Mock <IStarService>      starService      = new Mock <IStarService>();
            Mock <IDiscoveryService> discoveryService = new Mock <IDiscoveryService>();

            starService
            .Setup(s => s.Exists(It.IsAny <string>()))
            .Returns(false);

            discoveryService
            .Setup(d => d.TotalStars(It.IsAny <int>()))
            .Returns(3);

            Mock <ITempDataDictionary> tempData = new Mock <ITempDataDictionary>();

            string errorMessage = null;

            tempData
            .SetupSet(t => t[WebConstants.TempDataErrorMessage]    = It.IsAny <string>())
            .Callback((string key, object message) => errorMessage = message as string);

            StarFormServiceModel formModel       = this.GetStarFormModel();
            StarsController      starsController = new StarsController(starService.Object, discoveryService.Object);

            starsController.TempData = tempData.Object;

            // Act
            IActionResult result = starsController.Create(1, formModel);

            // Assert
            Assert.IsType <ViewResult>(result);
            object model = (result as ViewResult).Model;

            Assert.IsType <StarFormServiceModel>(model);
            StarFormServiceModel returnModel = model as StarFormServiceModel;

            this.AssertStars(formModel, returnModel);
            Assert.Equal(string.Format(
                             DataConstants.DiscoveryConstants.MaxStarsPerDiscoveryErrorMessage,
                             DataConstants.DiscoveryConstants.MaxStarsPerDiscovery),
                         errorMessage);
        }
        public void EditGet_WithExistingId_ShouldReturnView()
        {
            // Arrange
            Mock <IStarService>  starService = new Mock <IStarService>();
            StarFormServiceModel formModel   = this.GetStarFormModel();

            starService
            .Setup(s => s.GetForm(It.IsAny <int>()))
            .Returns(formModel);

            StarsController starsController = new StarsController(starService.Object, null);

            // Act
            IActionResult result = starsController.Edit(1, 1);

            // Assert
            Assert.IsType <ViewResult>(result);
            object model = (result as ViewResult).Model;

            Assert.IsType <StarFormServiceModel>(model);
            StarFormServiceModel returnModel = model as StarFormServiceModel;

            this.AssertStars(formModel, returnModel);
        }
Esempio n. 13
0
 private void CompareStars(Star expected, StarFormServiceModel actual)
 {
     Assert.Equal(expected.Name, actual.Name);
     Assert.Equal(expected.Temperature, actual.Temperature);
 }