public async Task AddBasketItemAsync_SkuNotInBasket_ShouldBeSuccessful() { var basketGuid = Guid.NewGuid(); var mock = new Mock <IBasketRepository>(); mock.Setup(x => x.BasketGetByBasketGuidAsync(basketGuid)) .Returns(Task.FromResult(new Basket { Guid = basketGuid })); var container = ContainerFacade.GetContainer(builder => builder.RegisterInstance(mock.Object).As <IBasketRepository>()); var service = container.Resolve <IBasketService>(); var basketItemCreateModel = new BasketItemCreateModel { ProductSku = "foo", Count = 1 }; Func <Task> f = async() => { (await service.AddBasketItemAsync(basketGuid, basketItemCreateModel)).Should().NotBe(Guid.Empty); }; f.Should().NotThrow(); }
public void Start_queueWrapper_and_backplane_start_method_should_be_called_before_messageSubscriber_start() { var sequence = new MockSequence(); _mockQueueWrapper.InSequence(sequence).Setup(m => m.Start()); _mockBackplane.InSequence(sequence).Setup(x => x.Start()); _mockMessageSubscriber.InSequence(sequence).Setup(m => m.Start()); _mockScaling.InSequence(sequence).Setup(m => m.Start()); _container.Configure(x => { x.For <IStartable>().ClearAll(); x.For <IStartStoppable>().ClearAll(); x.For <IStartable>().Add(_mockMessageSubscriber.Object); x.For <IStartable>().Add(_mockQueueWrapper.Object); x.For <IStartable>().Add(_mockBackplane.Object); x.For <IStartable>().Add(_mockScaling.Object); x.For <IStartStoppable>().Add(_mockScaling.Object); x.For <IStartStoppable>().Add(_mockMessageSubscriber.Object); x.For <IStartStoppable>().Add(_mockQueueWrapper.Object); }); _containerFacade = new ContainerFacade(_container, _configurationManagerMock.Object); Sut.Start(); _mockQueueWrapper.Verify(x => x.Start(), Times.Once); _mockBackplane.Verify(x => x.Start(), Times.Once); _mockMessageSubscriber.Verify(x => x.Start(), Times.Once); _mockScaling.Verify(x => x.Start(), Times.Once); }
public void MessageIsRepublished() { ArtificialQueueWrapper qw = new ArtificialQueueWrapper(); Mock <ILogger> log = new Mock <ILogger>(); Mock <IMethodInspectorManager> mim = new Mock <IMethodInspectorManager>(); Mock <IConfigurationManager> config = new Mock <IConfigurationManager>(); config.Setup(x => x.Get("UseStructureMap", false)).Returns(true); var container = new Container(new MicroserviceRegistry(typeof(ConsumeMessageTests).Assembly)); container.Configure(x => { x.For <IQueueWrapper>().ClearAll().Use(qw); }); var containerFacade = new ContainerFacade(container, config.Object); MessageSubscriber subscriber = new MessageSubscriber(qw, log.Object, config.Object, _metrics, mim.Object, containerFacade); Guid messageGuid = Guid.NewGuid(); BasicDeliverEventArgs eventDetails = new BasicDeliverEventArgs() { Body = Encoding.ASCII.GetBytes("{uuid:\"" + messageGuid + "\", method:\"GetFish\", source:\"Nowhere\", need:\"redherring\",publishedTime:\"" + DateTime.UtcNow.ToString("s") + "\", lastModifiedBy:\"test\", lastModifiedTime:\"" + DateTime.UtcNow.ToString("s") + "\"}") }; subscriber.ConsumeMessage(null, eventDetails); Thread.Sleep(50); Assert.AreEqual(1, qw.AcknowledgedMessages.Count); Assert.IsTrue(qw.AcknowledgedMessages.Contains(eventDetails)); Assert.AreEqual(1, qw.PublishedMessages.Count); }
public async Task CreateBasket_WithEmptyGuid_ShouldThrowException() { var container = ContainerFacade.GetContainer(); var service = container.Resolve <IBasketService>(); Func <Task> f = async() => { await service.CreateBasketAsync(Guid.Empty); }; f.Should().Throw <ArgumentException>(); }
public void LoadAssembly(ContainerFacade container) { Resolver.Container = container; // Ensure that any mapping using AutoMap uses this container Mapper.Initialize(cfg => cfg.ConstructServicesUsing(container.Resolve)); RegisterAllServices(container); }
protected void Application_Start() { GlobalConfiguration.Configure(WebApiConfig.Register); var config = GlobalConfiguration.Configuration; MappingConfig.Register(); ContainerFacade.RegisterControllers(Assembly.GetExecutingAssembly()); config.DependencyResolver = new AutofacWebApiDependencyResolver(ContainerFacade.Container); }
public async Task CreateBasket_WithNotExistingUserBasket_ShouldBeSuccessful() { var userguid = Guid.NewGuid(); var container = ContainerFacade.GetContainer(); var service = container.Resolve <IBasketService>(); Func <Task> f = async() => { (await service.CreateBasketAsync(userguid)).Should().NotBe(Guid.Empty); }; f.Should().NotThrow(); }
public static ContainerFacade Create() { ContainerFacade facade = null; // Old Code. The new constructor requires the container instance! var container = GetContainer(); if (container == null) { throw new Exception("Could not found IoC Container!"); } facade = new ContainerFacade(container); return(facade); }
public async Task CreateBasket_WithExistingUserBasket_ShouldThrowException() { var userguid = Guid.NewGuid(); var mock = new Mock <IBasketRepository>(); mock.Setup(x => x.BasketGetByUserGuidAsync(userguid)) .Returns(Task.FromResult(new Basket { UserGuid = userguid })); var container = ContainerFacade.GetContainer(builder => builder.RegisterInstance(mock.Object).As <IBasketRepository>()); var service = container.Resolve <IBasketService>(); Func <Task> f = async() => { await service.CreateBasketAsync(userguid); }; f.Should().Throw <BasketException>(); }
public void SetUp() { _mockQueueWrapper = new Mock <IStartStoppable>(MockBehavior.Strict); _mockQueueWrapper.Setup(x => x.StartupPriority).Returns(StartupPriority.High); _mockBackplane = new Mock <IStartable>(MockBehavior.Strict); _mockBackplane.Setup(x => x.StartupPriority).Returns(StartupPriority.Normal); _mockMessageSubscriber = new Mock <IStartStoppable>(MockBehavior.Strict); _mockMessageSubscriber.Setup(x => x.StartupPriority).Returns(StartupPriority.Low); _mockScaling = new Mock <IStartStoppable>(MockBehavior.Strict); _mockScaling.Setup(x => x.StartupPriority).Returns(StartupPriority.Lowest); _configurationManagerMock = new Mock <IConfigurationManager>(); _configurationManagerMock.Setup(x => x.Get("UseStructureMap", false)).Returns(true); _container = new Container(new MicroserviceRegistry(typeof(ServiceTests).Assembly)); _containerFacade = new ContainerFacade(_container, _configurationManagerMock.Object); }
/// <summary> /// Registers all implementations located in the service assemblies /// of all contracts located in this assembly. /// </summary> /// <param name="container"></param> protected virtual void RegisterAllServices(ContainerFacade container) { // Load all the service contracts from this (the domain) assembly var thisAssembly = Assembly.GetExecutingAssembly(); var contracts = thisAssembly.GetTypes().Where(t => t.IsInterface && t.Name.EndsWith("Service")).ToArray(); // Load all the service assemblies in the current application domain var serviceAssemblies = AppDomain.CurrentDomain.GetAssemblies().Where(a => a.FullName.StartsWith("ReggerOnline.Services")); var concreteTypes = new List<Type>(); foreach (var serviceAssembly in serviceAssemblies) { try { concreteTypes.AddRange(serviceAssembly.GetTypes()); } catch (Exception e) { throw new ApplicationException( "Error loading types from: {0}\n{1}".FormatWith(serviceAssembly.FullName, e.Message), e); } } // Build a registration of all the service contracts and their implementations var serviceTypes = new Dictionary<Type, Type>(); foreach (var concreteType in concreteTypes) { var interfaces = concreteType.GetInterfaces().Intersect(contracts).ToArray(); if (!interfaces.Any()) { continue; } foreach (var @interface in interfaces) { // If this is already defined, we have two implementations of the same contract. // This may be desirable, and if required should be implemented here. if (serviceTypes.ContainsKey(@interface)) { System.Diagnostics.Debug.WriteLine("Multiple implementation of service {0}: {1} and {2}.", @interface.FullName, serviceTypes[@interface].FullName, concreteType.FullName); continue; } serviceTypes[@interface] = concreteType; } } // Validate that we have implementations for all our contracts and register with Unity. foreach (var contract in contracts) { if (!serviceTypes.ContainsKey(contract)) { throw new ApplicationException(string.Format("Missing implementation of service: {0}.", contract.FullName)); } System.Diagnostics.Debug.WriteLine("Registering service {0} for {1}", serviceTypes[contract].FullName, contract.Name); container.RegisterType(contract, serviceTypes[contract], LifetimeManagers.PerRequest); } }
public void LoadAssembly(ContainerFacade container) { LoadMappings(); }