public void User_Can_Update_a_Patron_Header() { UpdatePatronHeader command = new UpdatePatronHeader("Update User", TestHelper.Now) { PatronId = Guid.NewGuid(), DisplayName = "Updated Test Patron", IsAnonymous = true, PatronType = "Updated Type" }; var eventBus = new Mock <IEventBus>(); PatronHeaderChanged publishedEvent = null; eventBus.Setup(bus => bus.Publish(It.IsAny <PatronHeaderChanged>())) .Callback <PatronHeaderChanged>(evnt => publishedEvent = evnt).Verifiable(); ICommandHandler <UpdatePatronHeader> handler = new PatronCommandHandler(eventBus.Object); handler.Handle(command); Assert.NotEqual(Guid.Empty, command.Id); eventBus.VerifyPublish <PatronHeaderChanged>(Times.Once()); Assert.NotNull(publishedEvent); Assert.Equal("Updated Test Patron", publishedEvent.DisplayName); Assert.Equal(command.IsAnonymous, publishedEvent.IsAnonymous); Assert.Equal(command.PatronType, publishedEvent.PatronType); Assert.Equal(command.Id, publishedEvent.SourceId); Assert.Equal(command.PatronId, publishedEvent.PatronId); Assert.NotEqual(command.Id, publishedEvent.Id); Assert.Equal("Update User", publishedEvent.GeneratedBy); Assert.Equal(TestHelper.Now, publishedEvent.GeneratedOn); }
public void UpdateHeader_With_ValidCommand_ForFoundPatron_IsPublished_AndReturnsAcceptedResult_WithLocation() { Guid patronId = MakeTestPatron(); UpdatePatronHeader cmd = new UpdatePatronHeader("Patron Web Test", TestHelper.Now) { PatronId = patronId, DisplayName = "Updated Patron Display Name", IsAnonymous = false, PatronType = "Patron Updated" }; cmdQueueMock.Setup(c => c.Publish(It.IsAny <UpdatePatronHeader>())).Verifiable(); var mockUrlHelper = new Mock <IUrlHelper>(MockBehavior.Strict); Expression <Func <IUrlHelper, string> > urlSetup = m => m.Action(It.Is <UrlActionContext>(uac => uac.Action == "Get" && GetId(uac.Values) == patronId)); mockUrlHelper.Setup(urlSetup).Returns("a/mock/url/for/testing").Verifiable(); AcceptedResult result; using (var controller = new PatronController(cmdQueue, dbContextFactory) { Url = mockUrlHelper.Object }) { result = controller.UpdateHeader(cmd.PatronId, cmd) as AcceptedResult; } Assert.IsType <AcceptedResult>(result); Assert.Equal(patronId, cmd.PatronId); mockUrlHelper.Verify(urlSetup, Times.Once()); cmdQueueMock.Verify(c => c.Publish(It.IsAny <UpdatePatronHeader>()), Times.Once()); Assert.Equal("a/mock/url/for/testing", result.Location); }
public void Handle(UpdatePatronHeader command) { var evnt = new PatronHeaderChanged(command.GeneratedBy, command.GeneratedOn, command.Id) { PatronId = command.PatronId, DisplayName = command.DisplayName, IsAnonymous = command.IsAnonymous, PatronType = command.PatronType }; eventBus.Publish(evnt); }
public void UpdateHeader_With_ValidCommand_ButPatronNotFound_Returns_NotFound() { NotFoundObjectResult result; UpdatePatronHeader command; using (PatronController controller = new PatronController(cmdQueue, dbContextFactory)) { command = new UpdatePatronHeader("Patron Web Test", TestHelper.Now) { PatronId = Guid.NewGuid() }; result = controller.UpdateHeader(command.PatronId, command) as NotFoundObjectResult; } cmdQueueMock.Verify(c => c.Publish(It.IsAny <ICommand>()), Times.Never()); Assert.IsType <NotFoundObjectResult>(result); Assert.IsType <Guid>(result.Value); Assert.Equal(command.PatronId, (Guid)result.Value); }
public void UpdateHeader_With_ModelStateError_Returns_BadRequest() { BadRequestObjectResult result; using (PatronController controller = new PatronController(cmdQueue, dbContextFactory)) { controller.ModelState.AddModelError("DisplayName", "Display Name error is being tested."); var command = new UpdatePatronHeader("Patron Web Test", TestHelper.Now) { PatronId = Guid.NewGuid(), DisplayName = string.Join(" - ", Enumerable.Repeat("A Really Long String", 1000)) }; result = controller.UpdateHeader(command.PatronId, command) as BadRequestObjectResult; } cmdQueueMock.Verify(c => c.Publish(It.IsAny <ICommand>()), Times.Never()); Assert.IsType <BadRequestObjectResult>(result); Assert.IsType <SerializableError>(result.Value); }
public void UpdateHeader_With_ValidCommand_ButMismatchedPatronIds_Returns_BadRequest() { UpdatePatronHeader command; BadRequestObjectResult result; Guid newPatronGuid = Guid.NewGuid(); Guid somethingDifferentGuid = Guid.NewGuid(); using (PatronController controller = new PatronController(cmdQueue, dbContextFactory)) { command = new UpdatePatronHeader("Patron Web Test", TestHelper.Now) { PatronId = newPatronGuid }; result = controller.UpdateHeader(somethingDifferentGuid, command) as BadRequestObjectResult; } cmdQueueMock.Verify(c => c.Publish(It.IsAny <ICommand>()), Times.Never()); Assert.NotEqual(newPatronGuid, somethingDifferentGuid); Assert.IsType <BadRequestObjectResult>(result); Assert.IsType <SerializableError>(result.Value); }
public IActionResult UpdateHeader(Guid patronId, [FromBody] UpdatePatronHeader command) { if (command == null || !ModelState.IsValid) { return(BadRequest(ModelState)); } else if (patronId != command.PatronId) { ModelState.AddModelError(nameof(command.PatronId), "Patron Id does not match command target."); return(BadRequest(ModelState)); } using var context = ContextFactory(); if (context.Patrons.Find(command.PatronId) != null) { CommandQueue.Publish(command); return(Accepted(Url.Action(nameof(Get), new { id = command.PatronId }))); } else { return(NotFound(command.PatronId)); } }