public async Task TestFulfillOrderSimple()
        {
            // The default mock inventory service behavior is to always complete an order.

            MockInventoryService inventoryService = new MockInventoryService();

            MockServiceProxyFactory serviceProxyFactory = new MockServiceProxyFactory();

            serviceProxyFactory.AssociateMockServiceAndName(new Uri("fabric:/someapp/" + InventoryServiceName), inventoryService);

            CustomerOrderActor target = await CreateCustomerOrderActor(serviceProxyFactory);

            await target.StateManager.SetStateAsync <CustomerOrderStatus>(RequestIdPropertyName, CustomerOrderStatus.Submitted);

            await target.StateManager.SetStateAsync <long>(RequestIdPropertyName, 0);

            await target.StateManager.SetStateAsync <List <CustomerOrderItem> >(OrderItemListPropertyName, new List <CustomerOrderItem>()
            {
                new CustomerOrderItem(new InventoryItemId(), 4)
            });

            await target.FulfillOrderAsync();

            Assert.AreEqual <CustomerOrderStatus>(CustomerOrderStatus.Shipped, await target.StateManager.GetStateAsync <CustomerOrderStatus>(OrderStatusPropertyName));
        }
Exemple #2
0
        public static TestContext WithHappyPath()
        {
            var testContext = new TestContext();

            testContext.TelemetryClient = new TelemetryClient(new TelemetryConfiguration("", testContext.MockTelemetryChannel));
            testContext.Actors          = Enumerable.Range(0, 10).Select(x => Guid.NewGuid()).Select(x =>
            {
                var actorProxyFactory       = new MockActorProxyFactory();
                var mockServiceProxyFactory = new MockServiceProxyFactory();
                var actorServiceForActor    = MockActorServiceFactory.CreateActorServiceForActor <Instance>();
                var instance = new Instance(actorServiceForActor, new ActorId(x), testContext.ClusterClient.Object,
                                            testContext.TelemetryClient);
                return(instance);
            }).ToList();

            var setupSequentialResult = testContext.GuidGetter.SetupSequence(x => x.GetAGuid());

            foreach (var actor in testContext.Actors)
            {
                setupSequentialResult.Returns(actor.Id.GetGuidId());
                testContext.MockActorProxyFactory.RegisterActor(actor);
            }
            testContext.PoolActorService = CreatePoolActorService(testContext.TelemetryClient, testContext.MockActorProxyFactory,
                                                                  testContext.GuidGetter.Object);
            testContext.Pool = testContext.PoolActorService.Activate(new ActorId("fabric:/myapplicationname/myservicetypename"));
            return(testContext);
        }
Exemple #3
0
        public async Task validate_average_on_events_and_activities()
        {
            MockServiceProxyFactory serviceProxyFactory = new MockServiceProxyFactory();
            ParticipantActor        target = await CreateParticipantActor(ActorId.CreateRandom());

            var currentDate = DateTimeOffset.UtcNow;

            var current = await target.StateManager.GetStateAsync <Participations>(ParticipationsKeyName);

            var record = current.Records.FirstOrDefault();

            current.StartDate = DateTimeOffset.UtcNow.AddYears(-1);
            record.Events.Add(new ParticipantEvent()
            {
                Id = Guid.NewGuid().ToString(), Points = 1, Date = currentDate.AddMonths(-1)
            });
            record.Events.Add(new ParticipantEvent()
            {
                Id = Guid.NewGuid().ToString(), Points = 1, Date = currentDate
            });
            record.Activities.Add(new ParticipantActivity()
            {
                Points = 11, Date = currentDate
            });

            await target.StateManager.SetStateAsync(ParticipationsKeyName, current);

            var participations = await target.GetMonthParticipations(currentDate.Month, currentDate.Year);

            Assert.AreEqual(1, Math.Floor(participations.AveragePointsPerMonth));
        }
        public async Task TestFulfillOrderWithBackorder()
        {
            // instruct the mock inventory service to always return less quantity than requested
            // so that FulfillOrder always ends up in backordered status.

            MockInventoryService inventoryService = new MockInventoryService()
            {
                RemoveStockAsyncFunc = (itemId, quantity, cmid) => Task.FromResult(quantity - 1)
            };

            MockServiceProxy serviceProxy = new MockServiceProxy();

            serviceProxy.Supports <IInventoryService>(serviceUri => inventoryService);

            MockServiceProxyFactory serviceProxyFactory = new MockServiceProxyFactory();

            serviceProxyFactory.AssociateMockServiceAndName(new Uri("fabric:/someapp/" + InventoryServiceName), inventoryService);

            CustomerOrderActor target = await CreateCustomerOrderActor(serviceProxyFactory);

            await target.StateManager.SetStateAsync <CustomerOrderStatus>(RequestIdPropertyName, CustomerOrderStatus.Submitted);

            await target.StateManager.SetStateAsync <long>(RequestIdPropertyName, 0);

            await target.StateManager.SetStateAsync <List <CustomerOrderItem> >(OrderItemListPropertyName, new List <CustomerOrderItem>()
            {
                new CustomerOrderItem(new InventoryItemId(), 4)
            });

            await target.FulfillOrderAsync();

            Assert.AreEqual <CustomerOrderStatus>(CustomerOrderStatus.Backordered, await target.StateManager.GetStateAsync <CustomerOrderStatus>(OrderStatusPropertyName));
        }
Exemple #5
0
        public async Task validate_new_actor_has_no_participations()
        {
            MockServiceProxyFactory serviceProxyFactory = new MockServiceProxyFactory();
            ParticipantActor        target = await CreateParticipantActor(ActorId.CreateRandom());

            var participations = await target.GetMonthParticipations(1, 2017);

            Assert.IsFalse(participations.Activities.Any());
            Assert.IsFalse(participations.Events.Any());
        }
        public void TestNonIServiceProxyFactory()
        {
            //mock out the called service
            var mockProxyFactory = new MockServiceProxyFactory();
            var mockService      = new MockTestStatefulServiceWithoutIService();

            mockProxyFactory.RegisterService(ServiceCallerActor.CalledServiceName, mockService);

            //act:
            var instance = mockProxyFactory.CreateNonIServiceProxy <MockTestStatefulServiceWithoutIService>(ServiceCallerActor.CalledServiceName);

            //assert:
            Assert.AreEqual(mockService, instance);
        }
Exemple #7
0
        public MockFabricRuntime(string applicationName)
        {
            ApplicationName = applicationName;
            _stateManager   = new MockReliableStateManager();

            var serviceProxyFactory = new MockServiceProxyFactory(this);
            var actorProxyFactory   = new MockActorProxyFactory(this);

            _serviceProxyFactory = serviceProxyFactory;
            _actorProxyFactory   = actorProxyFactory;

            FG.ServiceFabric.Services.Remoting.Runtime.Client.ServiceProxyFactory.SetInnerFactory((factory, serviceType) => _serviceProxyFactory);;
            FG.ServiceFabric.Actors.Client.ActorProxyFactory.SetInnerFactory((factory, serviceType, actorType) => _actorProxyFactory);;
        }
        private static async Task <CustomerOrderActor> CreateCustomerOrderActor(MockServiceProxyFactory serviceProxyFactory)
        {
            CustomerOrderActor target = new CustomerOrderActor();

            PropertyInfo idProperty = typeof(ActorBase).GetProperty("Id");

            idProperty.SetValue(target, new ActorId(Guid.NewGuid()));

            PropertyInfo stateManagerProperty = typeof(MockableActor).GetProperty("StateManager");

            stateManagerProperty.SetValue(target, new MockActorStateManager());

            await target.InternalActivateAsync(codePackageContext, serviceProxyFactory);

            return(target);
        }
Exemple #9
0
        static FixtureBase()
        {
            _messageDictionary = new Dictionary <string, string>();

            _actorProxyFactory   = new MockActorProxyFactory();
            _serviceProxyFactory = new MockServiceProxyFactory();

            var repositoryFactoryMock = new Mock <IRepositoryFactories>();

            var repositoryMock = new Mock <IMessageRepository>();

            repositoryMock.Setup(i => i.Send(It.IsAny <string>(), It.IsAny <string>())).Returns((string s, string y) => MockSendMessage(s, y));

            repositoryFactoryMock.Setup(f => f.CreateMessageRepository()).Returns(repositoryMock.Object);

            // setup repositories when you can
            _factories = repositoryFactoryMock.Object;
        }
Exemple #10
0
        public async Task TestServiceProxyFactory()
        {
            //mock out the called service
            var mockProxyFactory = new MockServiceProxyFactory();
            var mockService      = new MockTestStatefulService();

            mockProxyFactory.RegisterService(ServiceCallerActor.CalledServiceName, mockService);

            //prepare the actor:
            Func <ActorService, ActorId, ActorBase> actorFactory = (service, actorId) => new ServiceCallerActor(service, actorId, mockProxyFactory);
            var svc   = MockActorServiceFactory.CreateActorServiceForActor <ServiceCallerActor>(actorFactory);
            var actor = svc.Activate(ActorId.CreateRandom());

            //act:
            await actor.InsertAsync("test", new Payload("some other value"));

            //assert:
            Assert.IsTrue(mockService.InsertAsyncCalled);
        }
        private static async Task <CustomerOrderActor> CreateCustomerOrderActor(MockServiceProxyFactory serviceProxyFactory)
        {
            try
            {
                CustomerOrderActor target = new CustomerOrderActor(
                    new ActorService(
                        context: statefulServiceContext,
                        actorTypeInfo: ActorTypeInformation.Get(typeof(CustomerOrderActor)),
                        stateManagerFactory: (actorBase, stateProvider) => new MockActorStateManager()),
                    new ActorId(Guid.NewGuid()));

                await target.InternalActivateAsync(codePackageContext, serviceProxyFactory);

                return(target);
            }
            catch (Exception e)
            {
                throw;
            }
        }
        public async Task TestFulfillOrderToCompletion()
        {
            int itemCount = 5;

            // instruct the mock inventory service to only fulfill one item each time
            // so that FulfillOrder has to make multiple iterations to complete an order
            MockInventoryService inventoryService = new MockInventoryService()
            {
                RemoveStockAsyncFunc = (itemId, quantity, cmid) => Task.FromResult(1)
            };

            MockServiceProxy serviceProxy = new MockServiceProxy();

            serviceProxy.Supports <IInventoryService>(serviceUri => inventoryService);

            MockServiceProxyFactory serviceProxyFactory = new MockServiceProxyFactory();

            serviceProxyFactory.AssociateMockServiceAndName(new Uri("fabric:/someapp/" + InventoryServiceName), inventoryService);

            CustomerOrderActor target = await CreateCustomerOrderActor(serviceProxyFactory);

            await target.StateManager.SetStateAsync <CustomerOrderStatus>(RequestIdPropertyName, CustomerOrderStatus.Submitted);

            await target.StateManager.SetStateAsync <long>(RequestIdPropertyName, 0);

            await target.StateManager.SetStateAsync <List <CustomerOrderItem> >(OrderItemListPropertyName, new List <CustomerOrderItem>()
            {
                new CustomerOrderItem(new InventoryItemId(), 5)
            });

            for (int i = 0; i < itemCount - 1; ++i)
            {
                await target.FulfillOrderAsync();

                Assert.AreEqual <CustomerOrderStatus>(CustomerOrderStatus.Backordered, await target.StateManager.GetStateAsync <CustomerOrderStatus>(OrderStatusPropertyName));
            }

            await target.FulfillOrderAsync();

            Assert.AreEqual <CustomerOrderStatus>(CustomerOrderStatus.Shipped, await target.StateManager.GetStateAsync <CustomerOrderStatus>(OrderStatusPropertyName));
        }
        public async Task TestFulfillOrderSimple()
        {
            // The default mock inventory service behavior is to always complete an order.

            MockInventoryService inventoryService = new MockInventoryService();

            MockServiceProxyFactory serviceProxyFactory = new MockServiceProxyFactory();
            serviceProxyFactory.AssociateMockServiceAndName(new Uri("fabric:/someapp/" + InventoryServiceName), inventoryService);

            CustomerOrderActor target = await CreateCustomerOrderActor(serviceProxyFactory);

            await target.StateManager.SetStateAsync<CustomerOrderStatus>(RequestIdPropertyName, CustomerOrderStatus.Submitted);
            await target.StateManager.SetStateAsync<long>(RequestIdPropertyName, 0);
            await target.StateManager.SetStateAsync<List<CustomerOrderItem>>(OrderItemListPropertyName, new List<CustomerOrderItem>()
            {
                new CustomerOrderItem(new InventoryItemId(), 4)
            });

            await target.FulfillOrderAsync();

            Assert.AreEqual<CustomerOrderStatus>(CustomerOrderStatus.Shipped, await target.StateManager.GetStateAsync<CustomerOrderStatus>(OrderStatusPropertyName));
        }
Exemple #14
0
        public async Task validate_annual_total_on_events_and_activities()
        {
            MockServiceProxyFactory serviceProxyFactory = new MockServiceProxyFactory();
            ParticipantActor        target = await CreateParticipantActor(ActorId.CreateRandom());

            var currentDate = DateTimeOffset.UtcNow;

            var current = await target.StateManager.GetStateAsync <Participations>(ParticipationsKeyName);

            int monthChanger = -1;

            if (currentDate.Month == 1)
            {
                monthChanger = 1;
            }

            var record = current.Records.FirstOrDefault();

            record.Events.Add(new ParticipantEvent()
            {
                Id = Guid.NewGuid().ToString(), Points = 1, Date = currentDate.AddMonths(monthChanger)
            });
            record.Events.Add(new ParticipantEvent()
            {
                Id = Guid.NewGuid().ToString(), Points = 1, Date = currentDate
            });
            record.Activities.Add(new ParticipantActivity()
            {
                Points = 1, Date = currentDate
            });

            await target.StateManager.SetStateAsync(ParticipationsKeyName, current);

            var participations = await target.GetMonthParticipations(currentDate.Month, currentDate.Year);

            Assert.AreEqual(3, participations.AnnualTotalPoints);
        }
        public async Task TestFulfillOrderCancelled()
        {
            // instruct the mock inventory service to return 0 for all items to simulate items that don't exist.
            // and have it return false when asked if an item exists to make sure FulfillOrder doesn't get into
            // an infinite backorder loop.
            MockInventoryService inventoryService = new MockInventoryService()
            {
                IsItemInInventoryAsyncFunc = itemId => Task.FromResult(false),
                RemoveStockAsyncFunc       = (itemId, quantity, cmid) => Task.FromResult(0)
            };

            MockServiceProxy serviceProxy = new MockServiceProxy();

            serviceProxy.Supports <IInventoryService>(serviceUri => inventoryService);

            MockServiceProxyFactory serviceProxyFactory = new MockServiceProxyFactory();

            serviceProxyFactory.AssociateMockServiceAndName(new Uri("fabric:/someapp/" + InventoryServiceName), inventoryService);

            CustomerOrderActor target = await CreateCustomerOrderActor(serviceProxyFactory);

            await target.StateManager.SetStateAsync <CustomerOrderStatus>(RequestIdPropertyName, CustomerOrderStatus.Submitted);

            await target.StateManager.SetStateAsync <long>(RequestIdPropertyName, 0);

            await target.StateManager.SetStateAsync <List <CustomerOrderItem> >(OrderItemListPropertyName, new List <CustomerOrderItem>()
            {
                new CustomerOrderItem(new InventoryItemId(), 5)
            });

            await target.FulfillOrderAsync();

            CustomerOrderStatus status = await target.StateManager.GetStateAsync <CustomerOrderStatus>(OrderStatusPropertyName);

            Assert.AreEqual <CustomerOrderStatus>(CustomerOrderStatus.Canceled, status);
        }
Exemple #16
0
 public void Cleanup()
 {
     _actorProxyFactory   = new MockActorProxyFactory();
     _serviceProxyFactory = new MockServiceProxyFactory();
 }
        public async Task TestFulfillOrderWithBackorder()
        {
            // instruct the mock inventory service to always return less quantity than requested 
            // so that FulfillOrder always ends up in backordered status.            

            MockInventoryService inventoryService = new MockInventoryService()
            {
                RemoveStockAsyncFunc = (itemId, quantity, cmid) => Task.FromResult(quantity - 1)
            };

            MockServiceProxy serviceProxy = new MockServiceProxy();
            serviceProxy.Supports<IInventoryService>(serviceUri => inventoryService);

            MockServiceProxyFactory serviceProxyFactory = new MockServiceProxyFactory();
            serviceProxyFactory.AssociateMockServiceAndName(new Uri("fabric:/someapp/" + InventoryServiceName), inventoryService);

            CustomerOrderActor target = await CreateCustomerOrderActor(serviceProxyFactory);

            await target.StateManager.SetStateAsync<CustomerOrderStatus>(RequestIdPropertyName, CustomerOrderStatus.Submitted);
            await target.StateManager.SetStateAsync<long>(RequestIdPropertyName, 0);
            await target.StateManager.SetStateAsync<List<CustomerOrderItem>>(OrderItemListPropertyName, new List<CustomerOrderItem>()
            {
                new CustomerOrderItem(new InventoryItemId(), 4)
            });

            await target.FulfillOrderAsync();

            Assert.AreEqual<CustomerOrderStatus>(CustomerOrderStatus.Backordered, await target.StateManager.GetStateAsync<CustomerOrderStatus>(OrderStatusPropertyName));
        }
        private static async Task<CustomerOrderActor> CreateCustomerOrderActor(MockServiceProxyFactory serviceProxyFactory)
        {
            try
            {
                CustomerOrderActor target = new CustomerOrderActor(
                    new ActorService(
                        context: statefulServiceContext,
                        actorTypeInfo: ActorTypeInformation.Get(typeof(CustomerOrderActor)),
                        stateManagerFactory: (actorBase, stateProvider) => new MockActorStateManager()),
                    new ActorId(Guid.NewGuid()));

                await target.InternalActivateAsync(codePackageContext, serviceProxyFactory);

                return target;
            }
            catch (Exception e)
            {
                throw;
            }
        }
        public async Task TestFulfillOrderCancelled()
        {
            // instruct the mock inventory service to return 0 for all items to simulate items that don't exist.
            // and have it return false when asked if an item exists to make sure FulfillOrder doesn't get into
            // an infinite backorder loop.
            MockInventoryService inventoryService = new MockInventoryService()
            {
                IsItemInInventoryAsyncFunc = itemId => Task.FromResult(false),
                RemoveStockAsyncFunc = (itemId, quantity, cmid) => Task.FromResult(0)
            };

            MockServiceProxy serviceProxy = new MockServiceProxy();
            serviceProxy.Supports<IInventoryService>(serviceUri => inventoryService);

            MockServiceProxyFactory serviceProxyFactory = new MockServiceProxyFactory();
            serviceProxyFactory.AssociateMockServiceAndName(new Uri("fabric:/someapp/" + InventoryServiceName), inventoryService);

            CustomerOrderActor target = await CreateCustomerOrderActor(serviceProxyFactory);

            await target.StateManager.SetStateAsync<CustomerOrderStatus>(RequestIdPropertyName, CustomerOrderStatus.Submitted);
            await target.StateManager.SetStateAsync<long>(RequestIdPropertyName, 0);
            await target.StateManager.SetStateAsync<List<CustomerOrderItem>>(OrderItemListPropertyName, new List<CustomerOrderItem>()
            {
                new CustomerOrderItem(new InventoryItemId(), 5)
            });

            await target.FulfillOrderAsync();

            CustomerOrderStatus status = await target.StateManager.GetStateAsync<CustomerOrderStatus>(OrderStatusPropertyName);

            Assert.AreEqual<CustomerOrderStatus>(CustomerOrderStatus.Canceled, status);
        }
        public async Task TestFulfillOrderToCompletion()
        {
            int itemCount = 5;

            // instruct the mock inventory service to only fulfill one item each time
            // so that FulfillOrder has to make multiple iterations to complete an order
            MockInventoryService inventoryService = new MockInventoryService()
            {
                RemoveStockAsyncFunc = (itemId, quantity, cmid) => Task.FromResult(1)
            };

            MockServiceProxy serviceProxy = new MockServiceProxy();
            serviceProxy.Supports<IInventoryService>(serviceUri => inventoryService);

            MockServiceProxyFactory serviceProxyFactory = new MockServiceProxyFactory();
            serviceProxyFactory.AssociateMockServiceAndName(new Uri("fabric:/someapp/" + InventoryServiceName), inventoryService);

            CustomerOrderActor target = await CreateCustomerOrderActor(serviceProxyFactory);

            await target.StateManager.SetStateAsync<CustomerOrderStatus>(RequestIdPropertyName, CustomerOrderStatus.Submitted);
            await target.StateManager.SetStateAsync<long>(RequestIdPropertyName, 0);
            await target.StateManager.SetStateAsync<List<CustomerOrderItem>>(OrderItemListPropertyName, new List<CustomerOrderItem>()
            {
                new CustomerOrderItem(new InventoryItemId(), 5)
            });

            for (int i = 0; i < itemCount - 1; ++i)
            {
                await target.FulfillOrderAsync();
                Assert.AreEqual<CustomerOrderStatus>(CustomerOrderStatus.Backordered, await target.StateManager.GetStateAsync<CustomerOrderStatus>(OrderStatusPropertyName));
            }

            await target.FulfillOrderAsync();
            Assert.AreEqual<CustomerOrderStatus>(CustomerOrderStatus.Shipped, await target.StateManager.GetStateAsync<CustomerOrderStatus>(OrderStatusPropertyName));
        }