public void ConvertToJs() { maintenanceWindowModelService = new MaintenanceWindowModelService(); var startTime = DateTime.UtcNow.AddDays(3); var endTime = DateTime.UtcNow.AddDays(4); var jsWindow = new JsMaintenanceWindowInput { StartTime = startTime.ToString(FormattingConstants.DateTimeFormat), EndTime = endTime.ToString(FormattingConstants.DateTimeFormat), Comments = "Test comments", Reason = MaintenanceWindowReason.HardwareMigration }; var expectedWindow = new MaintenanceWindow { Comments = jsWindow.Comments, IsDeleted = false, Reason = jsWindow.Reason, StartTime = startTime, EndTime = endTime }; var result = maintenanceWindowModelService.ConvertFromJS(jsWindow); Assert.That(result, Is.Not.Null); Assert.That(result.Comments, Is.EqualTo(expectedWindow.Comments)); Assert.That(result.Reason, Is.EqualTo(expectedWindow.Reason)); Assert.That(result.StartTime, Is.EqualTo(expectedWindow.StartTime).Within(TimeSpan.FromHours(1))); Assert.That(result.EndTime, Is.EqualTo(expectedWindow.EndTime).Within(TimeSpan.FromHours(1))); }
public MaintenanceWindow ConvertFromJS(JsMaintenanceWindowInput input) { var result = new MaintenanceWindow { StartTime = string.IsNullOrEmpty(input.StartTime) ? DateTime.MinValue : DateTime.ParseExact(input.StartTime, FormattingConstants.DateTimeFormat, CultureInfo.InvariantCulture), EndTime = string.IsNullOrEmpty(input.EndTime) ? DateTime.MinValue : DateTime.ParseExact(input.EndTime, FormattingConstants.DateTimeFormat, CultureInfo.InvariantCulture), Comments = input.Comments, Reason = input.Reason }; return(result); }
public async Task CreateMaintenanceWindow_Valid() { var startTime = DateTime.UtcNow.AddDays(3); var endTime = DateTime.UtcNow.AddDays(4); var jsWindow = new JsMaintenanceWindowInput { StartTime = startTime.ToString(FormattingConstants.DateTimeFormat), EndTime = endTime.ToString(FormattingConstants.DateTimeFormat), }; var window = new MaintenanceWindow { StartTime = startTime, EndTime = endTime }; var validation = new ValidationResult(); var createdWindow = new MaintenanceWindow { Id = 1, IsDeleted = false, StartTime = DateTime.ParseExact(jsWindow.StartTime, FormattingConstants.DateTimeFormat, CultureInfo.InvariantCulture), EndTime = DateTime.ParseExact(jsWindow.EndTime, FormattingConstants.DateTimeFormat, CultureInfo.InvariantCulture), }; var expectedResult = new MaintenanceWindowValidationResult { Valid = true, Result = createdWindow }; maintenanceWindowModelServiceMock.Setup(mock => mock.ConvertFromJS(jsWindow)).Returns(window); maintenanceWindowValidatorMock.Setup(mock => mock.Validate(window)) .Returns(validation); maintenanceWindowSchedulingServiceMock.Setup(mock => mock.ScheduleMaintenanceWindowAsync(window)) .ReturnsAsync(createdWindow); var result = await maintenanceWindowSchedulerController.CreateMaintenanceWindow(jsWindow); Assert.That(result, Is.Not.Null); Assert.That(result.Valid, Is.EqualTo(expectedResult.Valid)); Assert.That(result.Result, Is.EqualTo(expectedResult.Result)); Assert.That(result.Errors, Is.EqualTo(expectedResult.Errors)); }
public async Task CreateMaintenanceWindow_Invalid() { var startTime = DateTime.UtcNow.AddDays(4); var endTime = DateTime.UtcNow.AddDays(3); var jsWindow = new JsMaintenanceWindowInput { StartTime = startTime.ToString(FormattingConstants.DateTimeFormat), EndTime = endTime.ToString(FormattingConstants.DateTimeFormat), }; var window = new MaintenanceWindow { StartTime = startTime, EndTime = endTime }; var validation = new ValidationResult { Errors = { new ValidationFailure("StartTime", "Start Time must be after End Time.") } }; var expectedValidation = new MaintenanceWindowValidationResult { Valid = false, Result = window, Errors = new List <string> { "Start Time must be after End Time." } }; maintenanceWindowModelServiceMock.Setup(mock => mock.ConvertFromJS(jsWindow)).Returns(window); maintenanceWindowValidatorMock.Setup(mock => mock.Validate(window)) .Returns(validation); var result = await maintenanceWindowSchedulerController.CreateMaintenanceWindow(jsWindow); Assert.That(result, Is.Not.Null); Assert.That(result.Valid, Is.EqualTo(expectedValidation.Valid)); Assert.That(result.Valid, Is.False); Assert.That(result.Result, Is.EqualTo(expectedValidation.Result)); }
public async Task <MaintenanceWindowValidationResult> CreateMaintenanceWindow(JsMaintenanceWindowInput window) { var thisWindow = maintenanceWindowModelService.ConvertFromJS(window); var oldValidationResult = new MaintenanceWindowValidationResult(); var validationResult = this.maintenanceWindowValidator.Validate(thisWindow); oldValidationResult.Valid = validationResult.IsValid; if (validationResult.IsValid) { // Create MaintenanceWindow oldValidationResult.Result = await this.maintenanceWindowSchedulingService.ScheduleMaintenanceWindowAsync(thisWindow); return(oldValidationResult); } else { // Return error details validationResult.AddToModelState(this.ModelState, null); oldValidationResult.Errors = validationResult.Errors.Select(x => x.ToString()).ToList(); oldValidationResult.Result = thisWindow; return(oldValidationResult); } }