public void CanDeleteValidEstablishments() { // Arrange - create a establishment var establishment = new Establishment { EstablishmentId = 2, Name = "Test" }; // Arrange - create a local mock repository var localMock = new Mock <IEstablishmentRepository>(); localMock.Setup(m => m.Establishments).Returns(new[] { new Establishment { EstablishmentId = 1, Name = "P1" }, establishment, new Establishment { EstablishmentId = 3, Name = "P3" } }.AsQueryable()); // Arrange - create a controller var controller = new EstablishmentController(localMock.Object); // Action - delete the product controller.Delete(establishment.EstablishmentId); // assert - ensure that the repository Delete method was called with the correct Product localMock.Verify(m => m.DeleteEstablishment(establishment)); }
public void CannotDeleteInvalidEstablishments() { // Arrange - create a controller var controller = new EstablishmentController(_mockRepository.Object); // Action - attempt to delete using a EstablishmentId that does not exist controller.Delete(95); // assert - ensure that the repository Delete method was not called _mockRepository.Verify(m => m.DeleteEstablishment(It.IsAny <Establishment>()), Times.Never()); }
public void CannotEditNonexistentProduct() { // Arrange - create a controller var controller = new EstablishmentController(_mockRepository.Object); // Action var result = (Establishment)controller.Edit(6).ViewData.Model; // Assert Assert.IsNull(result); }
public async Task PopulateLayoutProperties(object viewModel, int?establishmentUrn, int?groupUId, IPrincipal user, Action <EstablishmentModel> processEstablishment = null, Action <GroupModel> processGroup = null) { if (establishmentUrn.HasValue && groupUId.HasValue) { throw new InvalidParameterException("Both urn and uid cannot be populated"); } if (!establishmentUrn.HasValue && !groupUId.HasValue) { throw new InvalidParameterException($"Both {nameof(establishmentUrn)} and {nameof(groupUId)} parameters are null"); } if (establishmentUrn.HasValue) { var domainModel = (await _establishmentReadService.GetAsync(establishmentUrn.Value, user)).GetResult(); var displayPolicy = await _establishmentReadService.GetDisplayPolicyAsync(domainModel, user); var permissibleGovernanceModes = await _establishmentReadService.GetPermissibleLocalGovernorsAsync(establishmentUrn.Value, user); if (!permissibleGovernanceModes.Any()) { domainModel.GovernanceModeId = null; // hack the model returned. } var vm = (IEstablishmentPageViewModel)viewModel; vm.Layout = EstabLayout; vm.Name = domainModel.Name; if (domainModel.TypeId.HasValue) { vm.TypeName = (await _cls.GetNameAsync(() => domainModel.TypeId)); } vm.SelectedTab = "governance"; vm.Urn = domainModel.Urn; vm.TabDisplayPolicy = new TabDisplayPolicy(domainModel, displayPolicy, user); vm.LegalParentGroup = EstablishmentController.GetLegalParent(vm.Urn.Value, await _groupReadService.GetAllByEstablishmentUrnAsync(vm.Urn.Value, user), user); // I agree, this shouldn't be a static. We should refector all this. We should have a base view model class. processEstablishment?.Invoke(domainModel); } else if (groupUId.HasValue) { var domainModel = (await _groupReadService.GetAsync(groupUId.Value, user)).GetResult(); var vm = (IGroupPageViewModel)viewModel; vm.Layout = GroupsLayout; vm.GroupName = domainModel.Name; vm.GroupTypeId = domainModel.GroupTypeId.Value; vm.GroupUId = groupUId; if (vm.GroupTypeId.HasValue) { vm.GroupTypeName = (await _cls.GetNameAsync(() => vm.GroupTypeId)); } vm.SelectedTabName = "governance"; vm.ListOfEstablishmentsPluralName = _nomenclatureService.GetEstablishmentsPluralName((eLookupGroupType)vm.GroupTypeId.Value); processGroup?.Invoke(domainModel); } }
public void GetEstablishmentReport_Handles_InvalidReponse() { var httpClientMock = GetHttpClientMock(false); _httpClienServiceFactory.Setup(p => p.Create(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <IEnumerable <KeyValuePair <string, string> > >())) .Returns(httpClientMock.Object); var result = new EstablishmentController(_httpClienServiceFactory.Object, new LocalCacheService(), _configurationOptions).GetEstablishmentReport(1, 1); var contentResult = result as NotFoundObjectResult; Assert.NotNull(result); Assert.NotNull(contentResult); }
public void CanEditEstablishment() { // Arrange - create a controller var controller = new EstablishmentController(_mockRepository.Object); // Action var e1 = controller.Edit(1).ViewData.Model as Establishment; var e2 = controller.Edit(2).ViewData.Model as Establishment; var e3 = controller.Edit(3).ViewData.Model as Establishment; // Assert Assert.AreEqual(1, e1.EstablishmentId); Assert.AreEqual(2, e2.EstablishmentId); Assert.AreEqual(3, e3.EstablishmentId); }
public void GetEstablishmentReport_Returns_ValidReport() { var httpClientMock = GetHttpClientMock(true); _httpClienServiceFactory.Setup(p => p.Create(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <IEnumerable <KeyValuePair <string, string> > >())) .Returns(httpClientMock.Object); var result = new EstablishmentController(_httpClienServiceFactory.Object, new LocalCacheService(), _configurationOptions).GetEstablishmentReport(1, 1); var contentResult = result as JsonResult; var dataResult = contentResult?.Value as AuthorityReport; Assert.NotNull(contentResult); Assert.NotNull(contentResult.Value); Assert.NotNull(dataResult); Assert.Equal(6, dataResult.TotalEstablishments); }
public void IndexReturnsEntireEstablishmentList() { // Arrange - create a controller var controller = new EstablishmentController(_mockRepository.Object); // Action Establishment[] result = ((IEnumerable <Establishment>)controller.Index().ViewData.Model).ToArray(); // Assert Assert.AreEqual(result.Length, 5); Assert.AreEqual("E1", result[0].Name); Assert.AreEqual("E2", result[1].Name); Assert.AreEqual("E3", result[2].Name); Assert.AreEqual("E4", result[3].Name); Assert.AreEqual("E4", result[4].Name); }
public void CanSaveValidChanges() { // Arrange - create a controller var controller = new EstablishmentController(_mockRepository.Object); // Arrange - create a product var establishment = new Establishment { Name = "Test" }; // Action - try to save the establishment ActionResult result = controller.Edit(establishment, null); // Assert - check that the repository was called _mockRepository.Verify(m => m.SaveEstablishment(establishment)); // Assert - check the method result type Assert.IsNotInstanceOfType(result, typeof(ViewResult)); }
public void CannotSaveInvalidChanges() { // Arrange - create a controller var controller = new EstablishmentController(_mockRepository.Object); // Arrange - create a product var establishment = new Establishment { Name = "Test" }; // Arrange - add an error to the model state controller.ModelState.AddModelError("error", "error"); // Action - try to save the product ActionResult result = controller.Edit(establishment, null); // Assert - check that the repository was called _mockRepository.Verify(m => m.SaveEstablishment(It.IsAny <Establishment>()), Times.Never()); // Assert - check the method result type Assert.IsInstanceOfType(result, typeof(ViewResult)); }