public void TestInfoRegistrationWithoutSending()
        {
            const string action1Descr = "action1Descr";

            var dateService = _container.Resolve <IDateTimeService>();

            // Arrange
            var registratorMock = new Mock <IUserActionsRegistratorClient>(MockBehavior.Strict);

            SetupCloseAsync(registratorMock);

            using (var wrapper = DisposableWcfClientWrapper.Create(registratorMock.Object))
            {
                var manager = CreateUAManager(wrapper, dateService);
                manager.SendReportOnEveryAction = false;

                // Action
                manager.RegisterInfo(action1Descr);

                // Assert
                Assert.AreEqual(UserActionsManager.StartingScore, manager.Score);
            }

            registratorMock.Verify();
        }
        public void TestScoreChanged()
        {
            const string descr    = "TestScoreChanged";
            const int    penalty  = 10;
            const int    newScore = UserActionsManager.StartingScore - penalty;

            var dateService     = _container.Resolve <IDateTimeService>();
            var registratorMock = new Mock <IUserActionsRegistratorClient>(MockBehavior.Loose);

            registratorMock.Setup(reg => reg.RegisterUserActionsAsync(
                                      It.IsAny <long>(),
                                      It.IsAny <Guid>(),
                                      It.IsAny <ActionDescription[]>(),
                                      It.IsAny <bool>()))
            .Callback(() => registratorMock.Raise(mock => mock.RegisterUserActionsCompleted += null,
                                                  new RegisterUserActionsCompletedEventArgs(new object[] { newScore }, null, false, false)));

            var flag = false;

            using (var wrapper = DisposableWcfClientWrapper.Create(registratorMock.Object))
            {
                var manager = CreateUAManager(wrapper, dateService);
                manager.PropertyChanged               += (sender, args) =>
                                                  flag = args.PropertyName == ExpressionUtility.NameForMember((UserActionsManager m) => m.Score);
                manager.RegisterMistake(descr, penalty);

                Assert.That(manager.Score == newScore);
            }
        }
示例#3
0
        public MainPage(IContainer container)
        {
            InitializeComponent();

            Container = container;

            var registratorMock = new Mock <IUserActionsRegistratorClient>(MockBehavior.Loose);

            registratorMock.Setup(reg => reg.RegisterUserActionsAsync(
                                      It.IsAny <long>(),
                                      It.IsAny <Guid>(),
                                      It.Is <ActionDescription[]>(d => d.Count() == 1 && d[0].Penalty == 0),
                                      It.IsAny <bool>()))
            .Callback(() => registratorMock.Raise(mock => mock.RegisterUserActionsCompleted += null,
                                                  new RegisterUserActionsCompletedEventArgs(new object[] { _currentScore }, null, false, null)));
            registratorMock.Setup(reg => reg.RegisterUserActionsAsync(
                                      It.IsAny <long>(),
                                      It.IsAny <Guid>(),
                                      It.Is <ActionDescription[]>(d => d.Count() == 1 && d[0].Penalty != 0),
                                      It.IsAny <bool>()))
            .Callback <long, Guid, ActionDescription[], bool>((l, g, d, b) => registratorMock.Raise(mock => mock.RegisterUserActionsCompleted += null,
                                                                                                    new RegisterUserActionsCompletedEventArgs(new object[] { _currentScore = _currentScore - d[0].Penalty }, null, false, null)));

            _registratorWrapper = DisposableWcfClientWrapper.Create(registratorMock.Object);
            UserActionsManager  = new UserActionsManager(0, new Guid(), _registratorWrapper, Container.Resolve <IDateTimeService>())
            {
                SendReportOnEveryAction = true
            };
        }
        public void TestMistakeAfterInfoWithSending()
        {
            const string action1Descr = "action1Descr";
            const string action2Descr = "action2Descr";
            const int    penalty      = 10;
            const int    newScore     = UserActionsManager.StartingScore - penalty;

            var dateService = _container.Resolve <IDateTimeService>();

            // Arrange
            var registratorMock = new Mock <IUserActionsRegistratorClient>(MockBehavior.Strict);

            registratorMock
            .Setup(reg => reg.RegisterUserActionsAsync(
                       It.Is <long>(i => i == TaskId),
                       It.Is <Guid>(i => i == _sessionGuid),
                       It.Is <ActionDescription[]>(d =>
                                                   d.Single().Description == action1Descr &&
                                                   d.Single().Penalty == 0 &&
                                                   d.Single().TimeStamp == dateService.Now()),
                       It.Is <bool>(i => i == false)))
            .Callback(() => registratorMock.Raise(mock => mock.RegisterUserActionsCompleted += null,
                                                  new RegisterUserActionsCompletedEventArgs(new object[] { UserActionsManager.StartingScore }, null, false, null)))
            .Verifiable();

            registratorMock
            .Setup(reg => reg.RegisterUserActionsAsync(
                       It.Is <long>(i => i == TaskId),
                       It.Is <Guid>(i => i == _sessionGuid),
                       It.Is <ActionDescription[]>(d =>
                                                   d.Single().Description == action2Descr &&
                                                   d.Single().Penalty == penalty &&
                                                   d.Single().TimeStamp == dateService.Now()),
                       It.Is <bool>(i => i == false)))
            .Callback(() => registratorMock.Raise(mock => mock.RegisterUserActionsCompleted += null,
                                                  new RegisterUserActionsCompletedEventArgs(new object[] { newScore }, null, false, null)))
            .Verifiable();

            SetupCloseAsync(registratorMock);

            using (var wrapper = DisposableWcfClientWrapper.Create(registratorMock.Object))
            {
                var manager = CreateUAManager(wrapper, dateService);
                manager.SendReportOnEveryAction = true;

                // Action
                manager.RegisterInfo(action1Descr);
                manager.RegisterMistake(action2Descr, penalty);

                // Assert
                Assert.AreEqual(newScore, manager.Score);
            }

            registratorMock.Verify();
        }
示例#5
0
        public void TestGetVariant()
        {
            const string variantData    = "Пусть это будут данные варианта. То, что это на самом деле строка, не принципиально.";
            var          allowedVersion = new Version(1, 0, 234, 999);

            var dataServiceMock = new Mock <IVariantProviderServiceClient>(MockBehavior.Strict);
            var taskVariantInfo = new TaskVariantDto
            {
                Data             = Encoding.UTF8.GetBytes(variantData),
                GeneratorVersion = allowedVersion.ToString(),
                Id      = 7,
                Number  = "test",
                Version = 1
            };

            dataServiceMock
            .Setup(srv => srv.GetVariantAsync(It.Is <long>(l => l == TaskId), It.Is <Guid>(g => g == _sessionGuid)))
            .Callback(() => dataServiceMock.Raise(mock => mock.GetVariantCompleted += null,
                                                  new GetVariantCompletedEventArgs(new object[] { taskVariantInfo }, null, false, null)))
            .Verifiable();
            SetupCloseAsync(dataServiceMock);

            using (var wrapper = DisposableWcfClientWrapper.Create(dataServiceMock.Object))
            {
                var variantProvider = new VariantProvider(
                    TaskId,
                    _sessionGuid,
                    new[] { new Version(allowedVersion.Major, allowedVersion.Minor + 1) },
                    wrapper);

                using (var flag = new AutoResetEvent(false))
                {
                    var handled = false;
                    EventHandler <VariantDownloadedEventArgs> handler = (sender, args) =>
                    {
                        Assert.AreSame(sender, variantProvider);
                        Assert.NotNull(args);
                        Assert.That(args.Data.SequenceEqual(taskVariantInfo.Data));
                        Assert.AreEqual(taskVariantInfo.Number, args.Number);
                        Assert.AreEqual(taskVariantInfo.Number, args.Number);
                        handled = true;
                        flag.Set();
                    };

                    variantProvider.VariantDownloaded += handler;
                    variantProvider.DownloadVariantAsync();
                    flag.WaitOne(1000);
                    variantProvider.VariantDownloaded -= handler;

                    Assert.IsTrue(handled);
                }
            }

            dataServiceMock.Verify();
        }
        /// <summary> Выполнить конфигурацию </summary>
        public void Configure(ContainerBuilder builder)
        {
            var actionRegistratorClient =
                DisposableWcfClientWrapper.Create(GetActionRegistratorClient());
            var dateServiceClient =
                DisposableWcfClientWrapper.Create(GetDataServiceClient());

            builder.RegisterInstance(actionRegistratorClient)
            .As <DisposableWcfClientWrapper <IUserActionsRegistratorClient> >();
            builder.RegisterInstance(dateServiceClient)
            .As <DisposableWcfClientWrapper <IVariantProviderServiceClient> >();
        }
示例#7
0
        public void TestInvalidVersion()
        {
            const string variantData    = "Пусть это будут данные варианта. То, что это на самом деле строка, не принципиально.";
            var          allowedVersion = new Version(1, 0);

            var dataServiceMock = new Mock <IVariantProviderServiceClient>(MockBehavior.Strict);
            var taskVariantInfo = new TaskVariantDto
            {
                Data             = Encoding.UTF8.GetBytes(variantData),
                GeneratorVersion = allowedVersion.ToString(),
                Id      = 7,
                Number  = "test",
                Version = 1
            };

            Assert.Throws <Exception>(() =>
            {
                dataServiceMock
                .Setup(
                    srv => srv.GetVariantAsync(It.Is <long>(l => l == TaskId), It.Is <Guid>(g => g == _sessionGuid)))
                .Callback(() => dataServiceMock.Raise(mock => mock.GetVariantCompleted += null,
                                                      new GetVariantCompletedEventArgs(new object[] { taskVariantInfo }, null, false, null)))
                .Verifiable();
                SetupCloseAsync(dataServiceMock);

                using (var wrapper = DisposableWcfClientWrapper.Create(dataServiceMock.Object))
                {
                    var variantProvider = new VariantProvider(
                        TaskId,
                        _sessionGuid,
                        new[] { new Version(allowedVersion.Major + 1, allowedVersion.Minor) },
                        wrapper);
                    variantProvider.DownloadVariantAsync();
                }
            });
        }
 private UserActionsManager CreateUAManager(DisposableWcfClientWrapper <IUserActionsRegistratorClient> registratorMock, IDateTimeService dateService)
 {
     return(new UserActionsManager(TaskId, _sessionGuid, registratorMock, dateService));
 }