public async Task EvaluateAsync_DoesResetAllMetConditions()
        {
            // Arrange
            var ratingViewMock = new Mock <IRatingView>();
            var gateway        = new RatingGateway
            {
                RatingView = ratingViewMock.Object
            };
            var condition1 = new CountRatingCondition(0, 1, ConditionType.Requirement)
            {
                CacheCurrentValue = false
            };
            var condition2 = new BooleanRatingCondition(ConditionType.Requirement);

            gateway.AddCondition("test", condition1);
            gateway.AddCondition("test2", condition2);

            // Act
            await gateway.EvaluateAsync();

            // Assert
            ratingViewMock.Verify(view => view.TryOpenRatingPage(), Times.Never);
            ratingViewMock.Verify(view => view.TryOpenRatingPageAsync(), Times.Once);
            Assert.Equal(0, condition1.CurrentState);
            Assert.False(condition2.CurrentState);
        }
        public async Task EvaluateAsync_FailsWhen_NoConditionMet()
        {
            // Arrange
            var ratingViewMock = new Mock <IRatingView>();
            var gateway        = new RatingGateway
            {
                RatingView = ratingViewMock.Object
            };

            gateway.AddCondition("test", new CountRatingCondition(0, 5)
            {
                CacheCurrentValue = false
            });
            gateway.AddCondition("test2", new CountRatingCondition(0, 10)
            {
                CacheCurrentValue = false
            });

            // Act
            await gateway.EvaluateAsync();

            // Assert
            ratingViewMock.Verify(view => view.TryOpenRatingPage(), Times.Never);
            ratingViewMock.Verify(view => view.TryOpenRatingPageAsync(), Times.Never);
        }
        public void Evaluate_DoesResetAllMetConditions_ExceptStrictlyForbiddenOnes()
        {
            // Arrange
            var ratingViewMock = new Mock <IRatingView>();
            var gateway        = new RatingGateway
            {
                RatingView = ratingViewMock.Object
            };
            var condition1 = new CountRatingCondition(0, 1, ConditionType.Requirement)
            {
                CacheCurrentValue = false
            };
            var condition2 = new BooleanRatingCondition(ConditionType.Requirement)
            {
                ResetAfterConditionMet = false
            };

            gateway.AddCondition("test", condition1);
            gateway.AddCondition("test2", condition2);

            // Act
            gateway.Evaluate();

            // Assert
            ratingViewMock.Verify(view => view.TryOpenRatingPage(), Times.Once);
            Assert.Equal(0, condition1.CurrentState);
            Assert.True(condition2.CurrentState);
        }
        public async Task EvaluateAsync_ManipulateShould_SaveStateOfCachables()
        {
            // Arrange
            var cacheStub = new Mock <IRatingConditionCache>();

            cacheStub.Setup(cache => cache.Load(It.IsAny <string>(), It.IsAny <ICachableCondition>())).Returns(true);
            var ratingGateway = new RatingGateway()
            {
                RatingConditionCache = cacheStub.Object
            };

            ratingGateway.AddCondition("test", new BooleanRatingCondition());
            ratingGateway.AddCondition("test2", new CountRatingCondition(0, 3));

            // Act
            await ratingGateway.EvaluateAsync(new Dictionary <string, object?>()
            {
                { "test", null },
                { "test2", null }
            });

            await ratingGateway.EvaluateAsync(new Dictionary <string, object?>()
            {
                { "test", false },
                { "test2", 1 }
            });

            // Assert
            cacheStub.Verify(cache => cache.Save(It.IsAny <string>(), It.IsAny <ICachableCondition>()), Times.Exactly(2));
        }
        public void Evaluate_ResetOnlyWhen_EvaluationSuccess()
        {
            // Arrange
            var ratingViewMock = new Mock <IRatingView>();
            var gateway        = new RatingGateway
            {
                RatingView = ratingViewMock.Object
            };
            var condition1 = new CountRatingCondition(0, 2, ConditionType.Requirement)
            {
                CacheCurrentValue = false
            };
            var condition2 = new CountRatingCondition(0, 1, ConditionType.Requirement)
            {
                CacheCurrentValue            = false,
                ResetOnlyOnEvaluationSuccess = false
            };
            var condition3 = new CountRatingCondition(0, 1, ConditionType.Requirement)
            {
                CacheCurrentValue = false
            };

            gateway.AddCondition("test", condition1);
            gateway.AddCondition("test2", condition2);
            gateway.AddCondition("test3", condition3);

            // Act
            gateway.Evaluate();

            // Assert
            ratingViewMock.Verify(view => view.TryOpenRatingPage(), Times.Never);
            Assert.Equal(1, condition1.CurrentState);
            Assert.Equal(0, condition2.CurrentState);
            Assert.Equal(1, condition3.CurrentState);
        }
        public void Evaluate_FailsWhen_NotAllPrioritysAreMet()
        {
            // Arrange
            var ratingViewMock = new Mock <IRatingView>();
            var gateway        = new RatingGateway
            {
                RatingView = ratingViewMock.Object
            };

            gateway.AddCondition("test", new BooleanRatingCondition());
            gateway.AddCondition("test2", new CountRatingCondition(0, 5)
            {
                CacheCurrentValue = false
            });
            gateway.AddCondition("test3", new BooleanRatingCondition());

            // Act
            gateway.Evaluate(new Dictionary <string, object?>()
            {
                { "test", null },
                { "test2", null }
            });

            // Assert
            ratingViewMock.Verify(view => view.TryOpenRatingPage(), Times.Never);
        }
        public void Evaluate_ResetAllMetConditionsShould_SaveStateOfCachables()
        {
            // Arrange
            var ratingViewStub = new Mock <IRatingView>();
            var cacheStub      = new Mock <IRatingConditionCache>();

            cacheStub.Setup(cache => cache.Load(It.IsAny <string>(), It.IsAny <ICachableCondition>())).Returns(true);
            var ratingGateway = new RatingGateway()
            {
                RatingView           = ratingViewStub.Object,
                RatingConditionCache = cacheStub.Object
            };

            ratingGateway.AddCondition("test", new CountRatingCondition(0, 1)
            {
                CacheCurrentValue = false
            });
            ratingGateway.AddCondition("test2", new CountRatingCondition(0, 1));

            // Act
            ratingGateway.Evaluate(new Dictionary <string, object?>()
            {
                { "test", null },
                { "test2", null }
            });

            // Assert
            ratingViewStub.Verify(rating => rating.TryOpenRatingPage(), Times.Once);
            cacheStub.Verify(cache => cache.Save(It.IsAny <string>(), It.IsAny <ICachableCondition>()), Times.Exactly(2));
        }
        public void ResetAllConditions_IsSuccessful()
        {
            // Arrange
            var condition1 = new CountRatingCondition(0, 10)
            {
                CacheCurrentValue = false
            };
            var condition2 = new CountRatingCondition(1, 7)
            {
                CacheCurrentValue = false
            };
            var conditions = new Dictionary <string, IRatingCondition>()
            {
                { "Test1", condition1 },
                { "Test2", condition2 }
            };
            var gateway = new RatingGateway();

            gateway.AddCondition(conditions);
            condition1.ManipulateState(10);
            condition2.ManipulateState(7);

            // Act
            gateway.ResetAllConditions();

            // Assert
            Assert.False(condition1.IsConditionMet);
            Assert.False(condition2.IsConditionMet);
        }
        public async Task EvaluateAsync_DoesManipulate_AsExpected(string?key, int?param, bool explicitOnly, bool disallowParameterless, int expected)
        {
            // Arrange
            var condition = new CountRatingCondition(0, 10)
            {
                ExplicitManipulationOnly          = explicitOnly,
                DisallowParamaterlessManipulation = disallowParameterless,
                CacheCurrentValue = false
            };
            var gateway = new RatingGateway();

            gateway.AddCondition(key ?? nameof(CountRatingCondition), condition);

            // Act
            if (key == null)
            {
                await gateway.EvaluateAsync();
            }
            else
            {
                await gateway.EvaluateAsync(new Dictionary <string, object?>()
                {
                    { key, param }
                });
            }

            // Assert
            Assert.Equal(expected, condition.CurrentState);
        }
        public void AddCondition_ShouldSave_WhenLoad_ReturnsFalse()
        {
            // Arrange
            var cacheStub = new Mock <IRatingConditionCache>();

            cacheStub.Setup(cache => cache.Load("test2", It.IsAny <ICachableCondition>())).Returns(false);
            cacheStub.Setup(cache => cache.Load("test4", It.IsAny <ICachableCondition>())).Returns(true);
            var ratingGateway = new RatingGateway()
            {
                RatingConditionCache = cacheStub.Object
            };

            // Act
            ratingGateway.AddCondition("test", new BooleanRatingCondition());
            ratingGateway.AddCondition("test2", new CountRatingCondition(0, 3));
            ratingGateway.AddCondition(new Dictionary <string, IRatingCondition>()
            {
                { "test3", new BooleanRatingCondition() },
                { "test4", new CountRatingCondition(0, 3) }
            });

            // Assert
            Assert.Equal(4, ratingGateway.RatingConditions.Count());
            cacheStub.Verify(cache => cache.Load(It.IsAny <string>(), It.IsAny <ICachableCondition>()), Times.Exactly(2));
            cacheStub.Verify(cache => cache.Save(It.IsAny <string>(), It.IsAny <ICachableCondition>()), Times.Once);
        }
        public void Evaluate_OpensRatingPage()
        {
            // Arrange
            var ratingViewMock = new Mock <IRatingView>();
            var gateway        = new RatingGateway
            {
                RatingView = ratingViewMock.Object
            };
            var condition1 = new CountRatingCondition(0, 1)
            {
                CacheCurrentValue = false
            };

            gateway.AddCondition("test", condition1);

            // Act
            gateway.Evaluate();
            gateway.Evaluate("test");
            gateway.Evaluate(new Dictionary <string, object?>()
            {
                { "test", null }
            });

            // Assert
            ratingViewMock.Verify(view => view.TryOpenRatingPage(), Times.Exactly(3));
            ratingViewMock.Verify(view => view.TryOpenRatingPageAsync(), Times.Never);
        }
        public void Initialize_Should_Construct()
        {
            // Arrange


            // Act
            RatingGateway.Initialize("Test", new BooleanRatingCondition());

            // Assert
            Assert.NotNull(RatingGateway.Current.RatingView);
            Assert.NotNull(RatingGateway.Current.RatingConditionCache);
            Assert.Single(RatingGateway.Current.RatingConditions);
        }
        public void RemoveCondition_IsSuccessful()
        {
            // Arrange
            var gateway = new RatingGateway();

            gateway.AddCondition("Test", new BooleanRatingCondition());

            // Act
            gateway.RemoveCondition("Test", false);

            // Assert
            Assert.Empty(gateway.RatingConditions);
        }
        public void RemoveCondition_DoesntRemove_CachedValue()
        {
            // Arrange
            var cacheStub     = new Mock <IRatingConditionCache>();
            var ratingGateway = new RatingGateway()
            {
                RatingConditionCache = cacheStub.Object
            };

            // Act
            ratingGateway.RemoveCondition("test", false);

            // Assert
            cacheStub.Verify(cache => cache.Delete(It.IsAny <string>()), Times.Never);
        }
        public void HasRequiredConditions_Returns_ExpectedResults()
        {
            // Arrange
            var gateway1 = new RatingGateway();
            var gateway2 = new RatingGateway();

            gateway1.AddCondition("Test", new BooleanRatingCondition(ConditionType.Requirement));
            gateway2.AddCondition("Test", new BooleanRatingCondition());

            // Act


            // Assert
            Assert.True(gateway1.HasRequiredConditions);
            Assert.False(gateway2.HasRequiredConditions);
        }
        public void Evaluate_FailsWhen_HasOnlyPrerequisites()
        {
            // Arrange
            var ratingViewMock = new Mock <IRatingView>();
            var gateway        = new RatingGateway
            {
                RatingView = ratingViewMock.Object
            };

            gateway.AddCondition("test", new BooleanRatingCondition(ConditionType.Prerequisite));

            // Act
            gateway.Evaluate();

            // Assert
            ratingViewMock.Verify(view => view.TryOpenRatingPage(), Times.Never);
        }
        public void Initialize_Returns_OnSecondCall()
        {
            // Arrange
            RatingGateway.Initialize("Test", new BooleanRatingCondition());

            // Act
            RatingGateway.Initialize("Test", new BooleanRatingCondition());


            // Assert
            Assert.NotNull(RatingGateway.Current.RatingView);
            Assert.NotNull(RatingGateway.Current.RatingConditionCache);
            Assert.Single(RatingGateway.Current.RatingConditions);

            // By calling initialize twice, if the second call would not return it would throw a ArgumentExecption,
            // because adding a condition with the same key is not possible.
        }
        public void Evaluate_SucceedsWhen_PrerequisitesAndStandardConditionsMet()
        {
            // Arrange
            var ratingViewMock = new Mock <IRatingView>();
            var gateway        = new RatingGateway
            {
                RatingView = ratingViewMock.Object
            };

            gateway.AddCondition("test", new BooleanRatingCondition(ConditionType.Prerequisite));
            gateway.AddCondition("test2", new BooleanRatingCondition());

            // Act
            gateway.Evaluate();

            // Assert
            ratingViewMock.Verify(view => view.TryOpenRatingPage(), Times.Once);
        }
        public void HasOnlyPrerequisiteConditions_Returns_ExpectedResuts()
        {
            // Arrange
            var gateway1 = new RatingGateway();
            var gateway2 = new RatingGateway();

            gateway1.AddCondition("Test", new BooleanRatingCondition(ConditionType.Prerequisite));
            gateway1.AddCondition("Test2", new BooleanRatingCondition(ConditionType.Prerequisite));
            gateway2.AddCondition("Test", new BooleanRatingCondition());
            gateway2.AddCondition("Test2", new BooleanRatingCondition(ConditionType.Prerequisite));

            // Act


            // Assert
            Assert.True(gateway1.HasOnlyPrerequisiteConditions);
            Assert.False(gateway2.HasOnlyPrerequisiteConditions);
        }
        public void AddCondition_ThrowsOn_DuplicateKey()
        {
            // Arrange
            var conditions = new Dictionary <string, IRatingCondition>()
            {
                { "Tester", new BooleanRatingCondition() },
                { "Tester2", new BooleanRatingCondition() }
            };
            var gateway = new RatingGateway();

            gateway.AddCondition(conditions);

            // Act


            // Assert
            Assert.Throws <ArgumentException>(() => gateway.AddCondition(conditions));
            Assert.Throws <ArgumentException>(() => gateway.AddCondition("Tester", new BooleanRatingCondition()));
        }
        public void Evaluate_ManipulationOnlyDoes_EvaluateWithoutPriorityCondition()
        {
            // Arrange
            var ratingViewMock = new Mock <IRatingView>();
            var gateway        = new RatingGateway
            {
                RatingView = ratingViewMock.Object
            };

            gateway.AddCondition("test", new CountRatingCondition(0, 5)
            {
                CacheCurrentValue = false
            });
            gateway.AddCondition("test2", new BooleanRatingCondition());

            // Act
            gateway.Evaluate("test", 3, true);

            // Assert
            ratingViewMock.Verify(view => view.TryOpenRatingPage(), Times.Once);
        }
        public void Evaluate_FailsWhen_RequiredMetAndPrioritySpecified()
        {
            // Arrange
            var ratingViewMock = new Mock <IRatingView>();
            var gateway        = new RatingGateway
            {
                RatingView = ratingViewMock.Object
            };

            gateway.AddCondition("test", new BooleanRatingCondition(ConditionType.Requirement));
            gateway.AddCondition("test2", new CountRatingCondition(0, 5)
            {
                CacheCurrentValue = false
            });

            // Act
            gateway.Evaluate("test2");

            // Assert
            ratingViewMock.Verify(view => view.TryOpenRatingPage(), Times.Never);
        }
        public void Evaluate_SucceedsWhen_AnyConditionIsMet()
        {
            // Arrange
            var ratingViewMock = new Mock <IRatingView>();
            var gateway        = new RatingGateway
            {
                RatingView = ratingViewMock.Object
            };

            gateway.AddCondition("test", new CountRatingCondition(0, 5)
            {
                CacheCurrentValue = false
            });
            gateway.AddCondition("test2", new BooleanRatingCondition());

            // Act
            gateway.Evaluate();

            // Assert
            ratingViewMock.Verify(view => view.TryOpenRatingPage(), Times.Once);
        }
        public void Evaluate_FailsWhen_PrerequisitesMetButStandardConditionNot()
        {
            // Arrange
            var ratingViewMock = new Mock <IRatingView>();
            var gateway        = new RatingGateway
            {
                RatingView = ratingViewMock.Object
            };

            gateway.AddCondition("test", new BooleanRatingCondition(ConditionType.Prerequisite));
            gateway.AddCondition("test2", new CountRatingCondition(0, 5)
            {
                CacheCurrentValue = false
            });

            // Act
            gateway.Evaluate();

            // Assert
            ratingViewMock.Verify(view => view.TryOpenRatingPage(), Times.Never);
        }
        public void AddCondition_ShouldLoad_CachedStateOfCachables()
        {
            // Arrange
            var cacheStub     = new Mock <IRatingConditionCache>();
            var ratingGateway = new RatingGateway()
            {
                RatingConditionCache = cacheStub.Object
            };

            // Act
            ratingGateway.AddCondition("test", new BooleanRatingCondition());
            ratingGateway.AddCondition("test2", new CountRatingCondition(0, 3));
            ratingGateway.AddCondition(new Dictionary <string, IRatingCondition>()
            {
                { "test3", new BooleanRatingCondition() },
                { "test4", new CountRatingCondition(0, 3) }
            });

            // Assert
            Assert.Equal(4, ratingGateway.RatingConditions.Count());
            cacheStub.Verify(cache => cache.Load(It.IsAny <string>(), It.IsAny <ICachableCondition>()), Times.Exactly(2));
        }
        public void Evaluate_FailsWhen_PriorityDoesNotExist()
        {
            // Arrange
            var ratingViewMock = new Mock <IRatingView>();
            var gateway        = new RatingGateway
            {
                RatingView = ratingViewMock.Object
            };

            gateway.AddCondition("test", new BooleanRatingCondition());
            gateway.AddCondition("test2", new CountRatingCondition(0, 5)
            {
                CacheCurrentValue = false
            });
            gateway.AddCondition("test3", new BooleanRatingCondition());

            // Act
            gateway.Evaluate("random");

            // Assert
            ratingViewMock.Verify(view => view.TryOpenRatingPage(), Times.Never);
        }
        public void ResetAllConditions_ShouldSave_StateOfCachables()
        {
            // Arrange
            var cacheStub = new Mock <IRatingConditionCache>();

            cacheStub.Setup(cache => cache.Load(It.IsAny <string>(), It.IsAny <ICachableCondition>())).Returns(true);
            var ratingGateway = new RatingGateway()
            {
                RatingConditionCache = cacheStub.Object
            };

            ratingGateway.AddCondition("test", new CountRatingCondition(1, 4)
            {
                CacheCurrentValue = false
            });
            ratingGateway.AddCondition("test2", new CountRatingCondition(0, 3));

            // Act
            ratingGateway.ResetAllConditions();

            // Assert
            cacheStub.Verify(cache => cache.Save(It.IsAny <string>(), It.IsAny <ICachableCondition>()), Times.Once);
        }
 private void InitRatingGateway()
 {
     RatingGateway.Initialize(RatingGatewaySetup.RatingConditions, new SampleRatingView(FindViewById <RelativeLayout>(Resource.Id.content_view)));
 }