public async Task <IActionResult> ExecuteAsync(string toggleId, string serviceId, CancellationToken cancellationToken) { ServiceToggle toggle = await _serviceToggleRepository.GetById(toggleId, serviceId, cancellationToken); if (toggle == null) { return(new NotFoundResult()); } HttpContext httpContext = _actionContextAccessor.ActionContext.HttpContext; if (httpContext.Request.Headers.TryGetValue(HeaderNames.IfModifiedSince, out Microsoft.Extensions.Primitives.StringValues stringValues)) { if (DateTimeOffset.TryParse(stringValues, out DateTimeOffset modifiedSince) && (modifiedSince >= toggle.Modified)) { return(new StatusCodeResult(StatusCodes.Status304NotModified)); } } ServiceToggleVM toggleViewModel = _mapper.Map <ServiceToggle, ServiceToggleVM>(toggle); httpContext.Response.Headers.Add(HeaderNames.LastModified, toggle.Modified.ToString("R", CultureInfo.InvariantCulture)); return(new OkObjectResult(toggleViewModel)); }
public DataResponse SetDefaultService(ServiceToggle entity) { var response = new DataResponse <bool?>(); try { base.DBInit(); var lookupEnrolledServices = DBEntity.LookupEnrolledServices.Where(a => a.BusinessId == entity.BusinessId).ToList(); var model = lookupEnrolledServices.FirstOrDefault(a => a.Id == entity.ServiceId); if (entity.Status == true) { lookupEnrolledServices.ForEach(a => a.Status = false); } model.Status = entity.Status; DBEntity.SaveChanges(); } catch (Exception ex) { ex.Log(); } finally { base.DBClose(); } return(response); }
public async Task Delete(ServiceToggle toggle, CancellationToken cancellationToken) { if (await Context.GlobalToggles.AnyAsync(t => t.Id == t.Id, cancellationToken)) { Context.ServiceToggles.Remove(toggle); await Save(cancellationToken); } }
public async Task WhenServiceToggle_Type_BLUE_Or_GREEN_And_IsServiceExcluded_True_PostThrows_HttpBadRequestException() { var serviceToggleRepository = new Mock <IRepository <ServiceToggle> >(); var toggleRepository = new Mock <IRepository <Toggle> >(); var sut = new ServiceTogglesController(toggleRepository.Object, serviceToggleRepository.Object); var toggleTypeBlue = new Toggle { Name = "T1", Description = string.Empty, Type = Constants.WellKnownToggleType.Blue }; var toggleTypeGreen = new Toggle { Name = "T2", Description = string.Empty, Type = Constants.WellKnownToggleType.Green }; var service = new Service() { Name = "S1", Version = "1.0", Description = string.Empty, }; toggleRepository.Setup(r => r.GetAsync("T1")).ReturnsAsync(toggleTypeBlue); toggleRepository.Setup(r => r.GetAsync("T2")).ReturnsAsync(toggleTypeGreen); var serviceToggleTypeBlue = new ServiceToggle() { UniqueId = "1", IsEnabled = true, Toggle = toggleTypeBlue, IsServiceExcluded = true }; var exception1 = await Assert.ThrowsAsync <HttpBadRequestException>( async() => await sut.Post(serviceToggleTypeBlue)); Assert.Equal("For BLUE Toggle type IsServiceExcluded value should be FALSE as it is not applicable.", exception1.Message); var serviceToggleTypeGreen = new ServiceToggle() { UniqueId = "1", IsEnabled = true, Service = service, Toggle = toggleTypeGreen, IsServiceExcluded = true }; var exception2 = await Assert.ThrowsAsync <HttpBadRequestException>( async() => await sut.Post(serviceToggleTypeGreen)); Assert.Equal("For GREEN Toggle type IsServiceExcluded value should be FALSE as it is not applicable.", exception2.Message); }
public IHttpActionResult SetDefaultService(ServiceModel model) { ServiceToggle entity = new ServiceToggle(); entity.ServiceId = model.ServiceId; entity.Status = model.Status; entity.BusinessId = CurrentBusinessId.Value; var response = repository.SetDefaultService(entity); return(Ok <DataResponse>(response)); }
public async Task <ServiceToggle> Add(ServiceToggle toggle, CancellationToken cancellationToken) { DateTimeOffset now = _clockService.UtcNow; toggle.Created = now; toggle.Modified = now; Context.ServiceToggles.Add(toggle); await Save(); return(toggle); }
public async Task WhenServiceToggle_Type_Green_With_True_And_Already_Exclusive_For_A_Service_PostThrows_HttpBadRequestException() { var toggleRepository = new Mock <IRepository <Toggle> >(); var serviceToggleRepository = new Mock <IRepository <ServiceToggle> >(); var sut = new ServiceTogglesController(toggleRepository.Object, serviceToggleRepository.Object); var toggleTypeBlueNamedT1 = new Toggle { Name = "T1", Description = string.Empty, Type = Constants.WellKnownToggleType.Green }; toggleRepository.Setup(r => r.GetAsync("T1")).ReturnsAsync(toggleTypeBlueNamedT1); var service1 = new Service() { Name = "S1", Version = "1.0", Description = string.Empty }; var serviceToggle = new ServiceToggle() { UniqueId = "1", IsEnabled = true, Service = service1, Toggle = toggleTypeBlueNamedT1, IsServiceExcluded = false }; var serviceToggle2 = new ServiceToggle() { UniqueId = "2", IsEnabled = true, Service = service1, Toggle = toggleTypeBlueNamedT1, IsServiceExcluded = false }; var list = new List <ServiceToggle> { serviceToggle }; serviceToggleRepository.Setup(r => r.GetAllAsync()).ReturnsAsync(list); var exception = await Assert.ThrowsAsync <HttpBadRequestException>( async() => await sut.Post(serviceToggle2)); Assert.Equal("The requested GREEN type service toggle T1 is exclusive for S1.", exception.Message); }
public async Task <ServiceToggle> Update(ServiceToggle toggle, CancellationToken cancellationToken) { ServiceToggle existingToggle = await Context.ServiceToggles.SingleOrDefaultAsync(t => t.Id == toggle.Id, cancellationToken); existingToggle.Value = toggle.Value; existingToggle.VersionRange = toggle.VersionRange; existingToggle.Modified = _clockService.UtcNow; Context.Update(existingToggle); await Save(cancellationToken); return(existingToggle); }
public async Task WhenServiceToggle_Type_Blue_With_False_Make_Exclusive_For_Requested_Service() { var toggleRepository = new Mock <IRepository <Toggle> >(); var serviceToggleRepository = new Mock <IRepository <ServiceToggle> >(); var sut = new ServiceTogglesController(toggleRepository.Object, serviceToggleRepository.Object); var toggleTypeBlueNamedT1 = new Toggle { Name = "T1", Description = string.Empty, Type = Constants.WellKnownToggleType.Blue }; toggleRepository.Setup(r => r.GetAsync("T1")).ReturnsAsync(toggleTypeBlueNamedT1); var service = new Service() { Name = "S1", Version = "1.0", Description = string.Empty }; var serviceToggle = new ServiceToggle() { UniqueId = "1", IsEnabled = true, Service = service, Toggle = toggleTypeBlueNamedT1, IsServiceExcluded = false }; var serviceToggleWithFalse = new ServiceToggle() { UniqueId = "2", IsEnabled = false, Service = service, Toggle = toggleTypeBlueNamedT1, IsServiceExcluded = false }; var list = new List <ServiceToggle> { serviceToggle }; serviceToggleRepository.Setup(r => r.GetAllAsync()).ReturnsAsync(list); serviceToggleRepository.Setup(r => r.UpdateAsync(serviceToggle.UniqueId, serviceToggle)).ReturnsAsync(serviceToggleWithFalse); var result = await sut.Post(serviceToggleWithFalse); Assert.Equal(serviceToggleWithFalse.IsEnabled, result.IsEnabled); }
public async Task <IActionResult> ExecuteAsync(SaveServiceToggleVM createToggle, CancellationToken cancellationToken) { ServiceToggle toggle = _mapper.Map <SaveServiceToggleVM, ServiceToggle>(createToggle); toggle = await _serviceToggleRepository.Add(toggle, cancellationToken); ServiceToggleVM toggleViewModel = _mapper.Map <ServiceToggle, ServiceToggleVM>(toggle); return(new CreatedAtRouteResult( ServiceControllerRoute.GetServiceToggle, new { toggleId = toggleViewModel.Id }, toggleViewModel)); }
public async Task <IActionResult> ExecuteAsync(string toggleId, string serviceId, CancellationToken cancellationToken) { ServiceToggle toggle = await _serviceToggleRepository.GetById(toggleId, serviceId, cancellationToken); if (toggle == null) { return(new NotFoundResult()); } await _serviceToggleRepository.Delete(toggle, cancellationToken); return(new NoContentResult()); }
public async Task WhenServiceToggle_Type_Blue_With_True_Is_All_Ready_Registered_Or_Duplicate_For_A_Service__PostThrows_HttpBadRequestException() { var toggleRepository = new Mock <IRepository <Toggle> >(); var serviceToggleRepository = new Mock <IRepository <ServiceToggle> >(); var sut = new ServiceTogglesController(toggleRepository.Object, serviceToggleRepository.Object); var toggle = new Toggle { Name = "T1", Description = string.Empty, Type = Constants.WellKnownToggleType.Blue }; var service = new Service() { Name = "S1", Version = "1.0", Description = string.Empty }; toggleRepository.Setup(r => r.GetAsync("T1")).ReturnsAsync(toggle); var list = new List <ServiceToggle> { new ServiceToggle() { UniqueId = "1", IsEnabled = true, Service = service, Toggle = toggle, IsServiceExcluded = false } }; serviceToggleRepository.Setup(r => r.GetAllAsync()).ReturnsAsync(list); var serviceToggleTypeBlueDuplicate = new ServiceToggle() { UniqueId = "2", IsEnabled = true, Service = service, Toggle = toggle, IsServiceExcluded = false }; var exception1 = await Assert.ThrowsAsync <HttpBadRequestException>( async() => await sut.Post(serviceToggleTypeBlueDuplicate)); Assert.Equal("The requested BLUE type service toggle with TRUE value is already registered.", exception1.Message); }
public async Task <IActionResult> ExecuteAsync(string toggleId, string serviceId, SaveServiceToggleVM saveToggle, CancellationToken cancellationToken) { ServiceToggle toggle = await _serviceToggleRepository.GetById(toggleId, serviceId, cancellationToken); if (toggle == null) { return(new NotFoundResult()); } _mapper.Map(saveToggle, toggle); toggle = await _serviceToggleRepository.Update(toggle, cancellationToken); ServiceToggleVM toggleViewModel = _mapper.Map <ServiceToggle, ServiceToggleVM>(toggle); return(new OkObjectResult(toggleViewModel)); }
public void SaveServiceToggleVM_should_mapped_to_ServiceToggle_model() { SaveServiceToggleVM toggle = new SaveServiceToggleVM { Id = "isButtonBlue", Value = true, ServiceId = "ABC", VersionRange = "*", }; ServiceToggle result = _fixture.Mapper.Map <ServiceToggle>(toggle); result.Id.Should().Be(toggle.Id); result.Value.Should().Be(toggle.Value); result.ServiceId.Should().Be(toggle.ServiceId); result.VersionRange.Should().Be(toggle.VersionRange); }
public async Task WhenServiceToggle_Has_InValid_Toggle__PostThrows_HttpBadRequestException() { var toggleRepository = new Mock <IRepository <Toggle> >(); var serviceToggleRepository = new Mock <IRepository <ServiceToggle> >(); var sut = new ServiceTogglesController(toggleRepository.Object, serviceToggleRepository.Object); var toggle1 = new Toggle { Name = "T1", Description = string.Empty, Type = Constants.WellKnownToggleType.Blue }; var toggle2 = new Toggle { Name = "T2", Description = string.Empty, Type = Constants.WellKnownToggleType.Blue }; var service = new Service() { Name = "S1", Version = "1.0", Description = string.Empty }; toggleRepository.Setup(r => r.GetAsync("T1")).ReturnsAsync(toggle1); var serviceToggle = new ServiceToggle() { UniqueId = "2", IsEnabled = true, Service = service, Toggle = toggle2, IsServiceExcluded = false }; var exception1 = await Assert.ThrowsAsync <HttpResourceNotFoundException>( async() => await sut.Post(serviceToggle)); Assert.Equal("Toggle 'T2' doesn't exist", exception1.Message); }
public void ServiceToggleVM_should_mapped_to_ServiceToggle_model() { ServiceToggleVM toggle = new ServiceToggleVM { Id = "isButtonBlue", Value = true, ServiceId = "ABC", VersionRange = "*", Created = DateTime.UtcNow, Modified = DateTime.UtcNow }; ServiceToggle result = _fixture.Mapper.Map <ServiceToggle>(toggle); result.Id.Should().Be(toggle.Id); result.Value.Should().Be(toggle.Value); result.ServiceId.Should().Be(toggle.ServiceId); result.VersionRange.Should().Be(toggle.VersionRange); result.Created.Should().Be(toggle.Created); result.Modified.Should().Be(toggle.Modified); }
public async Task WhenServiceToggle_Type_Red_With_True_And_IsExclusive_True_Allow_Mapping_To__Requested_Service() { var toggleRepository = new Mock <IRepository <Toggle> >(); var serviceToggleRepository = new Mock <IRepository <ServiceToggle> >(); var sut = new ServiceTogglesController(toggleRepository.Object, serviceToggleRepository.Object); var toggleTypeBlueNamedT1 = new Toggle { Name = "T1", Description = string.Empty, Type = Constants.WellKnownToggleType.Red }; toggleRepository.Setup(r => r.GetAsync("T1")).ReturnsAsync(toggleTypeBlueNamedT1); var service = new Service() { Name = "S1", Version = "1.0", Description = string.Empty }; var serviceToggle = new ServiceToggle() { UniqueId = "2", IsEnabled = false, Service = service, Toggle = toggleTypeBlueNamedT1, IsServiceExcluded = true }; serviceToggleRepository.Setup(r => r.CreateAsync(serviceToggle)).ReturnsAsync(serviceToggle); var result = await sut.Post(serviceToggle); Assert.Same(serviceToggle, result); }
public async Task Get_Toggles_Of_A_Service() { var toggleRepository = new Mock <IRepository <Toggle> >(); var serviceToggleRepository = new Mock <IRepository <ServiceToggle> >(); var sut = new ServiceTogglesController(toggleRepository.Object, serviceToggleRepository.Object); var toggleTypeBlue = new Toggle { Name = "T1", Description = string.Empty, Type = Constants.WellKnownToggleType.Blue }; var toggleTypeGreen = new Toggle { Name = "T2", Description = string.Empty, Type = Constants.WellKnownToggleType.Green }; var toggleTypeRed = new Toggle { Name = "T3", Description = string.Empty, Type = Constants.WellKnownToggleType.Red }; toggleRepository.Setup(r => r.GetAsync("T1")).ReturnsAsync(toggleTypeBlue); toggleRepository.Setup(r => r.GetAsync("T2")).ReturnsAsync(toggleTypeGreen); toggleRepository.Setup(r => r.GetAsync("T3")).ReturnsAsync(toggleTypeRed); var serviceS1 = new Service() { Name = "S1", Version = "1.0", Description = string.Empty }; var serviceS2 = new Service() { Name = "S2", Version = "1.0", Description = string.Empty }; #region Toggle Type BLUE var serviceToggleWithBlueTypeTrueS1 = new ServiceToggle() { UniqueId = "1", IsEnabled = true, Service = serviceS1, Toggle = toggleTypeBlue, IsServiceExcluded = false }; var serviceToggleWithBlueTypeFalseForS1 = new ServiceToggle { UniqueId = "1", IsEnabled = false, Service = serviceS1, Toggle = toggleTypeBlue, IsServiceExcluded = false }; var serviceToggleWithBlueTypeFalseForS2 = new ServiceToggle { UniqueId = "1", IsEnabled = false, Service = serviceS2, Toggle = toggleTypeBlue, IsServiceExcluded = false }; #endregion #region Toggle Type Green var serviceToggleWithGreenTypeTrueS1 = new ServiceToggle() { UniqueId = "1", IsEnabled = true, Service = serviceS1, Toggle = toggleTypeGreen, IsServiceExcluded = false }; var serviceToggleWithGreenTypeFalseForS1 = new ServiceToggle { UniqueId = "1", IsEnabled = true, Service = serviceS2, Toggle = toggleTypeGreen, IsServiceExcluded = false }; var serviceToggleWithGreenTypeFalseForS2 = new ServiceToggle { UniqueId = "1", IsEnabled = false, Service = serviceS1, Toggle = toggleTypeGreen, IsServiceExcluded = false }; #endregion #region Toggle Type Red var serviceToggleWithRedTypeTrueRedExcludeS1 = new ServiceToggle() { UniqueId = "1", IsEnabled = true, Service = serviceS1, Toggle = toggleTypeRed, IsServiceExcluded = true }; #endregion var list = new List <ServiceToggle> { serviceToggleWithBlueTypeTrueS1, serviceToggleWithBlueTypeFalseForS1, serviceToggleWithBlueTypeFalseForS2, serviceToggleWithGreenTypeTrueS1, serviceToggleWithGreenTypeFalseForS1, serviceToggleWithGreenTypeFalseForS2, serviceToggleWithRedTypeTrueRedExcludeS1 }; serviceToggleRepository.Setup(r => r.GetAllAsync()).ReturnsAsync(list); var result = (await sut.Get(serviceS1.Name, serviceS1.Version)).ToList(); Assert.Equal(4, result.Count); }
public async Task <ServiceToggle> Post([FromBody] ServiceToggle serviceToggle) { var allServiceToggleRecords = (await _serviceToggleRepository.GetAllAsync()).ToList(); // Validate Toggle existence var toggle = await _toggleRepository.GetAsync(serviceToggle.Toggle.Name); if (toggle == null) { throw new HttpResourceNotFoundException($"Toggle '{serviceToggle.Toggle.Name}' doesn't exist"); } // Validate uniqueId for mapping if (allServiceToggleRecords.Any(s => s.UniqueId.Equals(serviceToggle.UniqueId))) { throw new HttpResourceNotFoundException($"Service Toggle with unique id '{serviceToggle.UniqueId}' already exists"); } switch (toggle.Type) { case Constants.WellKnownToggleType.Blue: // TODO: Make it more modular #region " BLUE Type Toggle " // Always isServiceExcuded = false for BLUE type toggle // ToggleName ToggleValue ServiceName Version : Result // T1 True S1 1.0 : CREATE // T1 False S1 1.0 : UPDATE and make it exclusive now for S1 // T1 False S2 1.0 : Prevent as now T1 with false can be used by S1 only // T1 True S2 1.0 : CREATE // // T2 True S1 1.0 : CREATE // T2 False S1 1.0 : UPDATE and exclusive now for S1 // T2 False S2 1.0 : Prevent as now T2 with false can be used by S1 only // T2 True S2 1.0 : CREATE // // T2 True S1 1.0 : Alreday exists Skip // T2 False S1 1.0 : T2 is now already exclusive for S1, Skip // T2 False S2 1.0 : T2 is now already exclusive for S1, Skip // T2 True S2 1.0 : Already exists // Check the well know type toggle if (serviceToggle.IsServiceExcluded) { throw new HttpBadRequestException("For BLUE Toggle type IsServiceExcluded value should be FALSE as it is not applicable."); } var serviceToggleTypeBlueRecords = allServiceToggleRecords.Where(s => s.Toggle.Type == Constants.WellKnownToggleType.Blue).ToList(); if (serviceToggle.IsEnabled) { var existingBlueRecord = serviceToggleTypeBlueRecords.FirstOrDefault(s => s.Service.Name.Equals(serviceToggle.Service.Name) && s.Toggle.Name.Equals(serviceToggle.Toggle.Name) && s.IsEnabled); if (existingBlueRecord == null) { return(await _serviceToggleRepository.CreateAsync(serviceToggle)); } else { throw new HttpBadRequestException("The requested BLUE type service toggle with TRUE value is already registered."); } } else { var exclusiveBlueRecord = serviceToggleTypeBlueRecords.FirstOrDefault(s => s.Service.Name.Equals(serviceToggle.Service.Name) && s.Service.Version.Equals(serviceToggle.Service.Version) && s.Toggle.Name.Equals(serviceToggle.Toggle.Name) && s.IsEnabled); if (exclusiveBlueRecord != null) { exclusiveBlueRecord.IsEnabled = false; return(await _serviceToggleRepository.UpdateAsync(exclusiveBlueRecord.UniqueId, exclusiveBlueRecord)); } else { var existingBlueRecord = serviceToggleTypeBlueRecords.FirstOrDefault(s => s.Toggle.Name.Equals(serviceToggle.Toggle.Name) && s.IsEnabled == false); // Make toggle exclusive on first request itself for a service if (existingBlueRecord == null) { return(await _serviceToggleRepository.CreateAsync(serviceToggle)); } else { throw new HttpBadRequestException($"The requested BLUE type service toggle {serviceToggle.Toggle.Name} is exclusive for {existingBlueRecord.Service.Name}."); } } } #endregion case Constants.WellKnownToggleType.Green: // TODO: Make it more modular #region " GREEN Type toggle " // Always isServiceExcuded = false for GREEN type toggle // ToggleName ToggleValue ServiceName Version : Result // T1 True S1 1.0 : CREATE // T1 True S2 1.0 : Prevent, exclusive for S1 // T1 False S1 1.0 : CREATE // T1 False S2 1.0 : CREATE // // T2 True S1 1.0 : CREATE // T2 True S2 1.0 : Prevent, Exclusive for S1 // T2 False S1 1.0 : CREATE // T2 False S1 1.0 : Already exists // Check the well know type toggle if (serviceToggle.IsServiceExcluded) { throw new HttpBadRequestException("For GREEN Toggle type IsServiceExcluded value should be FALSE as it is not applicable."); } var serviceToggleTypeGreenRecords = allServiceToggleRecords.Where(s => s.Toggle.Type == Constants.WellKnownToggleType.Green).ToList(); if (serviceToggle.IsEnabled) { var exclusiveRecord = serviceToggleTypeGreenRecords.FirstOrDefault(s => s.Toggle.Name.Equals(serviceToggle.Toggle.Name) && s.IsEnabled); if (exclusiveRecord == null) { return(await _serviceToggleRepository.CreateAsync(serviceToggle)); } else { throw new HttpBadRequestException($"The requested GREEN type service toggle {serviceToggle.Toggle.Name} is exclusive for {exclusiveRecord.Service.Name}."); } } else { var existingBlueRecord = serviceToggleTypeGreenRecords.FirstOrDefault(s => s.Service.Name.Equals(serviceToggle.Service.Name) && s.Service.Version.Equals(serviceToggle.Service.Version) && s.Toggle.Name.Equals(serviceToggle.Toggle.Name) && s.IsEnabled == false); if (existingBlueRecord == null) { return(await _serviceToggleRepository.CreateAsync(serviceToggle)); } else { throw new HttpBadRequestException("The requested GREEN type service toggle with FALSE value is already registered."); } } #endregion case Constants.WellKnownToggleType.Red: // TODO: Make it more modular #region " RED Type toggle " var serviceToggleTypeRedRecords = allServiceToggleRecords.Where(s => s.Toggle.Type == Constants.WellKnownToggleType.Red).ToList(); var existingRedRecord = serviceToggleTypeRedRecords.FirstOrDefault(s => s.Service.Name.Equals(serviceToggle.Service.Name) && s.Toggle.Name.Equals(serviceToggle.Toggle.Name) && s.IsServiceExcluded == serviceToggle.IsServiceExcluded && s.IsEnabled == serviceToggle.IsEnabled); var existingAlreadyExcludedRedRecord = serviceToggleTypeRedRecords.FirstOrDefault(s => s.Service.Name.Equals(serviceToggle.Service.Name) && s.Toggle.Name.Equals(serviceToggle.Toggle.Name) && s.IsServiceExcluded); // If a service is already excluded, dont do anything if (existingAlreadyExcludedRedRecord != null) { return(null); } if (existingRedRecord == null) { return(await _serviceToggleRepository.CreateAsync(serviceToggle)); } else { throw new HttpBadRequestException("The requested RED type service toggle is already registered."); } #endregion default: throw new ArgumentOutOfRangeException(); } }