public async Task <ActionResult <string> > UpdateTagStepAndRequirements( [FromHeader(Name = CurrentPlantMiddleware.PlantHeader)] [Required] string plant, [FromRoute] int id, [FromBody] UpdateTagStepAndRequirementsDto dto) { var newRequirements = dto.NewRequirements?. Select(r => new RequirementForCommand(r.RequirementDefinitionId, r.IntervalWeeks)).ToList(); var updatedRequirements = dto.UpdatedRequirements?.Select(r => new UpdateRequirementForCommand(r.RequirementId, r.IntervalWeeks, r.IsVoided, r.RowVersion)).ToList(); var deletedRequirements = dto.DeletedRequirements?.Select(r => new DeleteRequirementForCommand(r.RequirementId, r.RowVersion)).ToList(); var command = new UpdateTagStepAndRequirementsCommand( id, dto.Description, dto.StepId, updatedRequirements, newRequirements, deletedRequirements, dto.RowVersion); var result = await _mediator.Send(command); return(this.FromResult(result)); }
public async Task HandlingUpdateTagStepAndRequirementsCommand_ShouldBeAbleToDeleteRequirement_WhenRequirementIsAlreadyVoided() { // Arrange var tagRequirement1 = _standardTagWithTwoRequirements.Requirements.Single(r => r.RequirementDefinitionId == ReqDefId1); tagRequirement1.IsVoided = true; Assert.AreEqual(2, _standardTagWithTwoRequirements.Requirements.Count); var deleteRequirements = new List <DeleteRequirementForCommand> { new DeleteRequirementForCommand(tagRequirement1.Id, RowVersion) }; var command = new UpdateTagStepAndRequirementsCommand( StandardTagId2, null, StepId2, null, null, deleteRequirements, RowVersion); // Act var result = await _dut.Handle(command, default); // Assert Assert.AreEqual(0, result.Errors.Count); Assert.AreEqual(1, _standardTagWithTwoRequirements.Requirements.Count); }
public async Task HandlingUpdateTagStepAndRequirementsCommand_ShouldStartPreservationOnNewRequirements_WhenPreservationActive() { // Arrange Assert.IsNull(_standardTagWithOneRequirement.NextDueTimeUtc); _standardTagWithOneRequirement.StartPreservation(); Assert.IsNotNull(_standardTagWithOneRequirement.NextDueTimeUtc); var newRequirements = new List <RequirementForCommand> { new RequirementForCommand(ReqDefId2, ThreeWeekInterval) }; var command = new UpdateTagStepAndRequirementsCommand( StandardTagId1, null, StepId1, null, newRequirements, null, RowVersion); // Act var result = await _dut.Handle(command, default); // Assert Assert.AreEqual(0, result.Errors.Count); var newReq = _standardTagWithOneRequirement.Requirements.Single(r => r.RequirementDefinitionId == ReqDefId2); Assert.IsNotNull(newReq.ActivePeriod); Assert.IsNotNull(newReq.NextDueTimeUtc); }
public void Validate_ShouldFail_WhenInvalidRowVersion() { const string invalidRowVersion = "String"; var command = new UpdateTagStepAndRequirementsCommand( _tagId, null, _stepId, null, new List <RequirementForCommand> { new RequirementForCommand(_rdForSupplierId, 1), new RequirementForCommand(_rdForOtherThanSupplierId, 1) }, null, invalidRowVersion); _rowVersionValidatorMock.Setup(r => r.IsValid(invalidRowVersion)).Returns(false); var result = _dut.Validate(command); Assert.IsFalse(result.IsValid); Assert.AreEqual(1, result.Errors.Count); Assert.IsTrue(result.Errors[0].ErrorMessage.StartsWith("Not a valid row version!")); }
public async Task HandlingUpdateTagStepAndRequirementsCommand_ShouldBeAbleToVoidRequirement_WhenNonVoidedRequirementExistsAfter() { // Arrange var tagRequirement1 = _standardTagWithTwoRequirements.Requirements.Single(r => r.RequirementDefinitionId == ReqDefId1); var tagRequirement2 = _standardTagWithTwoRequirements.Requirements.Single(r => r.RequirementDefinitionId == ReqDefId2); Assert.IsFalse(tagRequirement1.IsVoided); Assert.IsFalse(tagRequirement2.IsVoided); var updatedRequirements = new List <UpdateRequirementForCommand> { new UpdateRequirementForCommand(tagRequirement1.Id, ThreeWeekInterval, true, RowVersion) }; var command = new UpdateTagStepAndRequirementsCommand( StandardTagId2, null, StepId2, updatedRequirements, null, null, RowVersion); // Act var result = await _dut.Handle(command, default); // Assert Assert.AreEqual(0, result.Errors.Count); Assert.IsTrue(tagRequirement1.IsVoided); Assert.IsFalse(tagRequirement2.IsVoided); }
public async Task HandlingUpdateTagStepAndRequirementsCommand_ShouldUpdateExistingRequirement() { // Arrange var tagRequirement = _standardTagWithOneRequirement.Requirements.First(); Assert.AreEqual(ThreeWeekInterval, tagRequirement.IntervalWeeks); var updatedRequirements = new List <UpdateRequirementForCommand> { new UpdateRequirementForCommand(tagRequirement.Id, TwoWeekInterval, false, RowVersion) }; var command = new UpdateTagStepAndRequirementsCommand( StandardTagId1, null, StepId2, updatedRequirements, null, null, RowVersion); // Act var result = await _dut.Handle(command, default); // Assert Assert.AreEqual(0, result.Errors.Count); Assert.AreEqual(StepId2, _standardTagWithOneRequirement.StepId); Assert.AreEqual(1, _standardTagWithOneRequirement.Requirements.Count); Assert.AreEqual(TwoWeekInterval, tagRequirement.IntervalWeeks); }
public void Validate_ShouldBeValid_ForOtherStep_WhenNeitherUpdateOrAdd() { // Arrange _stepValidatorMock.Setup(r => r.IsForSupplierAsync(_stepId, default)).Returns(Task.FromResult(false)); _tagValidatorMock.Setup(t => t.RequirementUsageWillCoverForOtherThanSuppliersAsync( _tagId, new List <int>(), new List <int>(), new List <int>(), default)) .Returns(Task.FromResult(true)); var command = new UpdateTagStepAndRequirementsCommand( _tagId, null, _stepId, null, null, null, RowVersion); // Act var result = _dut.Validate(command); // Assert Assert.IsTrue(result.IsValid); }
public async Task HandlingUpdateTagStepAndRequirementsCommand_ShouldKeepDescription_ForStandardTag() { // Act var command = new UpdateTagStepAndRequirementsCommand( StandardTagId1, "NewDescription", StepId1, null, null, null, RowVersion); // Act var result = await _dut.Handle(command, default); // Assert Assert.AreEqual(0, result.Errors.Count); Assert.AreEqual(Description, _areaTagWithOneRequirement.Description); }
public void Validate_ShouldBeValid_WhenSettingDescriptionOnAreaTag() { _tagValidatorMock.Setup(t => t.VerifyTagIsAreaTagAsync(_tagId, default)).Returns(Task.FromResult(true)); var command = new UpdateTagStepAndRequirementsCommand( _tagId, "Desc", _stepId, null, new List <RequirementForCommand> { new RequirementForCommand(_rdForSupplierId, 1), new RequirementForCommand(_rdForOtherThanSupplierId, 1) }, null, RowVersion); var result = _dut.Validate(command); Assert.IsTrue(result.IsValid); }
public void Validate_ShouldBeValid_WhenRequirementToDelete_WillBeVoidedInSameCommand() { // Arrange _tagValidatorMock.Setup(t => t.AllRequirementsWillBeUniqueAsync(_tagId, new List <int> { _rdForOtherThanSupplierId }, default)).Returns(Task.FromResult(true)); _tagValidatorMock.Setup(t => t.RequirementUsageWillCoverBothForSupplierAndOtherAsync( _tagId, new List <int>(), new List <int> { _tagReqForSupplierId }, new List <int> { _rdForOtherThanSupplierId }, default)) .Returns(Task.FromResult(true)); var command = new UpdateTagStepAndRequirementsCommand( _tagId, null, _stepId, new List <UpdateRequirementForCommand> { new UpdateRequirementForCommand(_tagReqForSupplierId, 1, true, RowVersion) }, new List <RequirementForCommand> { new RequirementForCommand(_rdForOtherThanSupplierId, 1) }, new List <DeleteRequirementForCommand> { new DeleteRequirementForCommand(_tagReqForSupplierId, RowVersion) }, RowVersion); // Act var result = _dut.Validate(command); // Assert Assert.IsTrue(result.IsValid); }
public void Validate_ShouldFail_WhenRequirementToUpdateNotExists() { // Arrange _tagValidatorMock.Setup(t => t.HasRequirementAsync(_tagId, _tagReqForSupplierId, default)).Returns(Task.FromResult(false)); _tagValidatorMock.Setup(t => t.AllRequirementsWillBeUniqueAsync(_tagId, new List <int> { _rdForOtherThanSupplierId }, default)).Returns(Task.FromResult(true)); _tagValidatorMock.Setup(t => t.RequirementUsageWillCoverBothForSupplierAndOtherAsync( _tagId, new List <int> { _tagReqForSupplierId }, new List <int>(), new List <int> { _rdForOtherThanSupplierId }, default)) .Returns(Task.FromResult(true)); var command = new UpdateTagStepAndRequirementsCommand( _tagId, null, _stepId, new List <UpdateRequirementForCommand> { new UpdateRequirementForCommand(_tagReqForSupplierId, 1, false, RowVersion) }, new List <RequirementForCommand> { new RequirementForCommand(_rdForOtherThanSupplierId, 1) }, null, RowVersion); // Act var result = _dut.Validate(command); // Assert Assert.IsFalse(result.IsValid); Assert.AreEqual(1, result.Errors.Count); Assert.IsTrue(result.Errors[0].ErrorMessage.StartsWith("Requirement doesn't exist!")); }
public async Task HandlingUpdateTagStepAndRequirementsCommand_ShouldUpdateNextDueOnActiveTag_WhenAddingNewRequirementWithShorterIntervalThanExisting() { // Arrange _standardTagWithOneRequirement.StartPreservation(); Assert.AreEqual(ThreeWeekInterval, _standardTagWithOneRequirement.Requirements.First().IntervalWeeks); var oldNextDueTimeOnTag = _standardTagWithOneRequirement.NextDueTimeUtc; Assert.IsNotNull(oldNextDueTimeOnTag); var newRequirements = new List <RequirementForCommand> { new RequirementForCommand(ReqDefId2, TwoWeekInterval) }; var command = new UpdateTagStepAndRequirementsCommand( StandardTagId1, null, StepId1, null, newRequirements, null, RowVersion); // Act var result = await _dut.Handle(command, default); // Assert Assert.AreEqual(0, result.Errors.Count); Assert.AreEqual(2, _standardTagWithOneRequirement.Requirements.Count); var newNextDueTimeOnTag = _standardTagWithOneRequirement.NextDueTimeUtc; Assert.IsNotNull(newNextDueTimeOnTag); Assert.IsTrue(newNextDueTimeOnTag.Value < oldNextDueTimeOnTag.Value); var nearestRequirement = _standardTagWithOneRequirement.Requirements.Single(r => r.RequirementDefinitionId == ReqDefId2); Assert.AreEqual(TwoWeekInterval, nearestRequirement.IntervalWeeks); var nextDueTimeUtcOnRequirement = nearestRequirement.NextDueTimeUtc; Assert.IsNotNull(nextDueTimeUtcOnRequirement); Assert.AreEqual(newNextDueTimeOnTag.Value, nextDueTimeUtcOnRequirement.Value); }
public async Task HandlingUpdateTagStepAndRequirementsCommand_ShouldKeepDescription_WhenDescriptionNotGiven() { // Arrange Assert.AreEqual(Description, _areaTagWithOneRequirement.Description); var command = new UpdateTagStepAndRequirementsCommand( AreaTagId, null, StepId1, null, null, null, RowVersion); // Act var result = await _dut.Handle(command, default); // Assert Assert.AreEqual(0, result.Errors.Count); Assert.AreEqual(Description, _areaTagWithOneRequirement.Description); }
public void Validate_ShouldFail_WhenSettingDescriptionOnOtherThanAreaTag() { var command = new UpdateTagStepAndRequirementsCommand( _tagId, "Desc", _stepId, null, new List <RequirementForCommand> { new RequirementForCommand(_rdForSupplierId, 1), new RequirementForCommand(_rdForOtherThanSupplierId, 1) }, null, RowVersion); var result = _dut.Validate(command); Assert.IsFalse(result.IsValid); Assert.AreEqual(1, result.Errors.Count); Assert.IsTrue(result.Errors[0].ErrorMessage.StartsWith("Tag must be an area tag to update description!")); }
public void Validate_ShouldBeValid_ForSupplierStep_WhenVoidingAllRequirementsButAddingNew() { // Arrange _rdValidatorMock.Setup(r => r.ExistsAsync(_reqDef3Id, default)).Returns(Task.FromResult(true)); _tagValidatorMock.Setup(t => t.AllRequirementsWillBeUniqueAsync(_tagId, new List <int> { _reqDef3Id }, default)).Returns(Task.FromResult(true)); _tagValidatorMock.Setup(t => t.RequirementUsageWillCoverBothForSupplierAndOtherAsync( _tagId, new List <int>(), new List <int> { _tagReqForSupplierId, _tagReqForOtherThanSupplierId }, new List <int> { _reqDef3Id }, default)) .Returns(Task.FromResult(true)); var command = new UpdateTagStepAndRequirementsCommand( _tagId, null, _stepId, new List <UpdateRequirementForCommand> { new UpdateRequirementForCommand(_tagReqForSupplierId, 1, true, RowVersion), new UpdateRequirementForCommand(_tagReqForOtherThanSupplierId, 1, true, RowVersion) }, new List <RequirementForCommand> { new RequirementForCommand(_reqDef3Id, 1) }, null, RowVersion); // Act var result = _dut.Validate(command); // Assert Assert.IsTrue(result.IsValid); }
public void Validate_ShouldFail_WhenDescriptionIsBlankForAreaTag() { _tagValidatorMock.Setup(t => t.VerifyTagIsAreaTagAsync(_tagId, default)).Returns(Task.FromResult(true)); var command = new UpdateTagStepAndRequirementsCommand( _tagId, "", _stepId, null, new List <RequirementForCommand> { new RequirementForCommand(_rdForSupplierId, 1), new RequirementForCommand(_rdForOtherThanSupplierId, 1) }, null, RowVersion); var result = _dut.Validate(command); Assert.IsFalse(result.IsValid); Assert.AreEqual(1, result.Errors.Count); Assert.IsTrue(result.Errors[0].ErrorMessage.StartsWith("Description can not be blank!")); }
public async Task HandlingUpdateTagStepAndRequirementsCommand_ShouldUpdateNextDueOnActiveTag_WhenUpdatingExistingWithShorterInterval() { // Arrange _standardTagWithOneRequirement.StartPreservation(); var tagRequirement = _standardTagWithOneRequirement.Requirements.First(); Assert.AreEqual(ThreeWeekInterval, tagRequirement.IntervalWeeks); var oldNextDueTimeOnTag = _standardTagWithOneRequirement.NextDueTimeUtc; Assert.IsNotNull(oldNextDueTimeOnTag); var updatedRequirements = new List <UpdateRequirementForCommand> { new UpdateRequirementForCommand(tagRequirement.Id, TwoWeekInterval, false, RowVersion) }; var command = new UpdateTagStepAndRequirementsCommand( StandardTagId1, null, StepId2, updatedRequirements, null, null, RowVersion); // Act var result = await _dut.Handle(command, default); // Assert Assert.AreEqual(0, result.Errors.Count); Assert.AreEqual(1, _standardTagWithOneRequirement.Requirements.Count); var newNextDueTimeOnTag = _standardTagWithOneRequirement.NextDueTimeUtc; Assert.IsNotNull(newNextDueTimeOnTag); Assert.IsTrue(newNextDueTimeOnTag.Value < oldNextDueTimeOnTag.Value); var nextDueTimeUtcOnRequirement = tagRequirement.NextDueTimeUtc; Assert.IsNotNull(nextDueTimeUtcOnRequirement); Assert.AreEqual(newNextDueTimeOnTag.Value, nextDueTimeUtcOnRequirement.Value); }
public async Task HandlingUpdateTagStepAndRequirementsCommand_ShouldAddNewRequirement_WhenAddingAddNewRequirement() { // Arrange var newRequirements = new List <RequirementForCommand> { new RequirementForCommand(ReqDefId2, ThreeWeekInterval) }; var command = new UpdateTagStepAndRequirementsCommand( StandardTagId1, null, StepId1, null, newRequirements, null, RowVersion); // Act var result = await _dut.Handle(command, default); // Assert Assert.AreEqual(0, result.Errors.Count); Assert.AreEqual(StepId1, _standardTagWithOneRequirement.StepId); Assert.AreEqual(2, _standardTagWithOneRequirement.Requirements.Count); }
public void Setup_OkState() { _tagValidatorMock = new Mock <ITagValidator>(); _tagValidatorMock.Setup(t => t.ExistsAsync(_tagId, default)).Returns(Task.FromResult(true)); _tagValidatorMock.Setup(t => t.HasRequirementAsync(_tagId, _tagReqForSupplierId, default)).Returns(Task.FromResult(true)); _tagValidatorMock.Setup(t => t.HasRequirementAsync(_tagId, _tagReqForOtherThanSupplierId, default)).Returns(Task.FromResult(true)); _tagValidatorMock.Setup(t => t.AllRequirementsWillBeUniqueAsync(_tagId, new List <int> { _rdForSupplierId, _rdForOtherThanSupplierId }, default)).Returns(Task.FromResult(true)); _tagValidatorMock.Setup(t => t.RequirementUsageWillCoverBothForSupplierAndOtherAsync( _tagId, new List <int>(), new List <int>(), new List <int> { _rdForSupplierId, _rdForOtherThanSupplierId }, default)) .Returns(Task.FromResult(true)); _tagValidatorMock.Setup(t => t.RequirementUsageWillCoverForOtherThanSuppliersAsync( _tagId, new List <int>(), new List <int>(), new List <int> { _rdForSupplierId, _rdForOtherThanSupplierId }, default)) .Returns(Task.FromResult(true)); _tagValidatorMock.Setup(t => t.RequirementUsageWillCoverForSuppliersAsync( _tagId, new List <int>(), new List <int>(), new List <int> { _rdForSupplierId, _rdForOtherThanSupplierId }, default)) .Returns(Task.FromResult(true)); _tagValidatorMock.Setup(t => t.HasStepAsync(_tagId, _stepId, default)).Returns(Task.FromResult(true)); _stepValidatorMock = new Mock <IStepValidator>(); _stepValidatorMock.Setup(r => r.ExistsAsync(_stepId, default)).Returns(Task.FromResult(true)); _stepValidatorMock.Setup(r => r.IsForSupplierAsync(_stepId, default)).Returns(Task.FromResult(true)); _projectValidatorMock = new Mock <IProjectValidator>(); _rdValidatorMock = new Mock <IRequirementDefinitionValidator>(); _rdValidatorMock.Setup(r => r.ExistsAsync(_rdForSupplierId, default)).Returns(Task.FromResult(true)); _rdValidatorMock.Setup(r => r.ExistsAsync(_rdForOtherThanSupplierId, default)).Returns(Task.FromResult(true)); _rowVersionValidatorMock = new Mock <IRowVersionValidator>(); _rowVersionValidatorMock.Setup(r => r.IsValid(RowVersion)).Returns(true); _dut = new UpdateTagStepAndRequirementsCommandValidator( _projectValidatorMock.Object, _tagValidatorMock.Object, _stepValidatorMock.Object, _rdValidatorMock.Object, _rowVersionValidatorMock.Object); _addTwoReqsCommand = new UpdateTagStepAndRequirementsCommand( _tagId, null, _stepId, null, new List <RequirementForCommand> { new RequirementForCommand(_rdForSupplierId, 1), new RequirementForCommand(_rdForOtherThanSupplierId, 1) }, null, RowVersion); }