public async Task <Boolean> Handle(UpdateDHCPv4ScopeCommand request, CancellationToken cancellationToken) { _logger.LogDebug("Handle started"); var scope = _rootScope.GetScopeById(request.ScopeId); if (scope == DHCPv4Scope.NotFound) { return(false); } Guid?parentId = scope.HasParentScope() == false ? new Guid?() : scope.ParentScope.Id; var properties = GetScopeProperties(request); var addressProperties = GetScopeAddressProperties(request); if (request.Name != scope.Name) { _rootScope.UpdateScopeName(request.ScopeId, ScopeName.FromString(request.Name)); } if (request.Description != scope.Description) { _rootScope.UpdateScopeDescription(request.ScopeId, ScopeDescription.FromString(request.Description)); } if (request.ParentId != parentId) { _rootScope.UpdateParent(request.ScopeId, request.ParentId); } _rootScope.UpdateScopeResolver(request.ScopeId, GetResolverInformation(request)); if (addressProperties != scope.AddressRelatedProperties) { _rootScope.UpdateAddressProperties(request.ScopeId, addressProperties); } if (properties != scope.Properties) { _rootScope.UpdateScopeProperties(request.ScopeId, properties); } Boolean result = await _store.Save(_rootScope); if (result == true) { var triggers = _rootScope.GetTriggers(); if (triggers.Any() == true) { await _serviceBus.Publish(new NewTriggerHappendMessage(triggers)); _rootScope.ClearTriggers(); } } return(result); }
public async Task Handle(Boolean storeResult, Boolean useDynamicTime) { Random random = new Random(); Guid scopeId = random.NextGuid(); Guid leaseId = random.NextGuid(); String name = random.GetAlphanumericString(); String description = random.GetAlphanumericString(); IPv4Address start = random.GetIPv4Address(); IPv4Address end = start + 100; String resolverName = random.GetAlphanumericString(); Mock <IScopeResolver <DHCPv4Packet, IPv4Address> > resolverMock = new Mock <IScopeResolver <DHCPv4Packet, IPv4Address> >(MockBehavior.Strict); resolverMock.Setup(x => x.GetValues()).Returns(new Dictionary <String, String>()); Mock <IScopeResolverManager <DHCPv4Packet, IPv4Address> > scopeResolverMock = new Mock <IScopeResolverManager <DHCPv4Packet, IPv4Address> >(); scopeResolverMock.Setup(x => x.IsResolverInformationValid(It.Is <CreateScopeResolverInformation>(y => y.Typename == resolverName ))).Returns(true).Verifiable(); scopeResolverMock.Setup(x => x.InitializeResolver(It.Is <CreateScopeResolverInformation>(y => y.Typename == resolverName ))).Returns(resolverMock.Object).Verifiable(); Mock <ILoggerFactory> factoryMock = new Mock <ILoggerFactory>(MockBehavior.Strict); factoryMock.Setup(x => x.CreateLogger(It.IsAny <String>())).Returns(Mock.Of <ILogger <DHCPv4RootScope> >()); DHCPv4RootScope rootScope = new DHCPv4RootScope(random.NextGuid(), scopeResolverMock.Object, factoryMock.Object); rootScope.Load(new DomainEvent[] { new DHCPv4ScopeAddedEvent { Instructions = new DHCPv4ScopeCreateInstruction { Name = random.GetAlphanumericString(), Description = random.GetAlphanumericString(), Id = scopeId, ParentId = null, ResolverInformation = new CreateScopeResolverInformation { Typename = resolverName, }, ScopeProperties = new DHCPv4ScopeProperties(), AddressProperties = DHCPv4ScopeAddressProperties.Empty, } }, new DHCPv4LeaseCreatedEvent { EntityId = leaseId, Address = start - 100, ScopeId = scopeId, ClientIdenfier = DHCPv4ClientIdentifier.FromDuid(1, new UUIDDUID(random.NextGuid()), random.NextBytes(6)).GetBytes(), }, new DHCPv4LeaseActivatedEvent { EntityId = leaseId, ScopeId = scopeId, } }); Mock <IDHCPv4StorageEngine> storageMock = new Mock <IDHCPv4StorageEngine>(MockBehavior.Strict); storageMock.Setup(x => x.Save(rootScope)).ReturnsAsync(storeResult).Verifiable(); var command = new UpdateDHCPv4ScopeCommand(scopeId, name, description, null, useDynamicTime == false ? new DHCPv4ScopeAddressPropertyReqest { Start = start.ToString(), End = end.ToString(), ExcludedAddresses = Array.Empty <String>(), AcceptDecline = random.NextBoolean(), AddressAllocationStrategy = DHCPv4ScopeAddressPropertyReqest.AddressAllocationStrategies.Next, InformsAreAllowd = random.NextBoolean(), ReuseAddressIfPossible = random.NextBoolean(), SupportDirectUnicast = random.NextBoolean(), PreferredLifetime = TimeSpan.FromDays(0.5), RenewalTime = TimeSpan.FromDays(0.25), LeaseTime = TimeSpan.FromDays(1), MaskLength = (Byte)random.Next(10, 29), } : new DHCPv4ScopeAddressPropertyReqest { Start = start.ToString(), End = end.ToString(), ExcludedAddresses = Array.Empty <String>(), AcceptDecline = random.NextBoolean(), AddressAllocationStrategy = DHCPv4ScopeAddressPropertyReqest.AddressAllocationStrategies.Next, InformsAreAllowd = random.NextBoolean(), ReuseAddressIfPossible = random.NextBoolean(), SupportDirectUnicast = random.NextBoolean(), DynamicRenewTime = new DHCPv4DynamicRenewTimeRequest { Hours = 5, Minutes = 10, MinutesToEndOfLife = 45, MinutesToRebound = 35, }, MaskLength = (Byte)random.Next(10, 29), } , new CreateScopeResolverRequest { PropertiesAndValues = new Dictionary <String, String>(), Typename = resolverName, }, new DHCPv4ScopePropertyRequest[] { new DHCPv4AddressListScopePropertyRequest { OptionCode = 24, Type = DHCPv4ScopePropertyType.AddressList, Addresses = random.GetIPv4Addresses().Select(x => x.ToString()).ToArray(), }, new DHCPv4AddressScopePropertyRequest { OptionCode = 25, Type = DHCPv4ScopePropertyType.Address, Address = random.GetIPv4Address().ToString() }, new DHCPv4BooleanScopePropertyRequest { OptionCode = 26, Type = DHCPv4ScopePropertyType.Boolean, Value = random.NextBoolean() }, new DHCPv4NumericScopePropertyRequest { OptionCode = 27, Type = DHCPv4ScopePropertyType.UInt16, NumericType = DHCPv4NumericValueTypes.UInt16, Value = (UInt16)random.NextUInt16() }, new DHCPv4TextScopePropertyRequest { OptionCode = 28, Type = DHCPv4ScopePropertyType.Text, Value = random.GetAlphanumericString() }, new DHCPv4TimeScopePropertyRequest { OptionCode = 29, Type = DHCPv4ScopePropertyType.Time, Value = TimeSpan.FromSeconds(random.Next(10, 20)) }, new DHCPv4AddressListScopePropertyRequest { OptionCode = 64, MarkAsRemovedInInheritance = true, } } ); var serviceBusMock = new Mock <IServiceBus>(MockBehavior.Strict); serviceBusMock.Setup(x => x.Publish(It.Is <NewTriggerHappendMessage>(y => y.Triggers.Count() == 1 && ((PrefixEdgeRouterBindingUpdatedTrigger)y.Triggers.First()).OldBinding.Prefix == IPv4Address.FromString("fc70::0") ))).Returns(Task.CompletedTask); var handler = new UpdateDHCPv4ScopeCommandHandler(storageMock.Object, serviceBusMock.Object, rootScope, Mock.Of <ILogger <UpdateDHCPv4ScopeCommandHandler> >()); Boolean result = await handler.Handle(command, CancellationToken.None); Assert.Equal(storeResult, result); var scope = rootScope.GetRootScopes().First(); Assert.Equal(name, scope.Name); Assert.Equal(description, scope.Description); Assert.NotNull(scope.Resolver); Assert.Equal(start, scope.AddressRelatedProperties.Start); Assert.True(scope.Properties.IsMarkedAsRemovedFromInheritance(64)); Assert.False(scope.Properties.IsMarkedAsRemovedFromInheritance(24)); if (useDynamicTime == true) { Assert.True(scope.AddressRelatedProperties.UseDynamicRewnewTime); Assert.Equal(5, scope.AddressRelatedProperties.DynamicRenewTime.Hour); } else { Assert.False(scope.AddressRelatedProperties.UseDynamicRewnewTime); } scopeResolverMock.Verify(); storageMock.Verify(); serviceBusMock.Verify(); Assert.Empty(rootScope.GetTriggers()); }