public void IsDataQualityChekcingDataSupportTest()
        {
            var successTestQualityCheckingRule = new StringCompareCheckingRule("test", false, "test");
            var mockQualityCheckingRule = new Mock<IDataQualityCheckingRule>();

            var mockVersionHelper = new Mock<IDataVersioningHelper>();
            var mockRepository = new Mock<IRepository<CV_RelationshipType>>();

            var testTool = new SampleMatrixTypeCheckingTool(mockVersionHelper.Object, mockRepository.Object);

            Assert.True(testTool.IsDataQualityCheckingRuleSupported(successTestQualityCheckingRule));
            Assert.False(testTool.IsDataQualityCheckingRuleSupported(mockQualityCheckingRule.Object));
        }
        public void DataNotSupportCheckTest()
        {
            var mockVersionHelper = new Mock<IDataVersioningHelper>();
            var mockRepository = new Mock<IRepository<CV_RelationshipType>>();

            var testTool = new SampleMatrixTypeCheckingTool(mockVersionHelper.Object, mockRepository.Object);
            var testQualityCheckingRule = new StringCompareCheckingRule("test", false, "test");

            var dataNotSupportCheckResult = testTool.Check(1, testQualityCheckingRule);

            Assert.NotNull(dataNotSupportCheckResult);
            Assert.AreEqual("Data is not supported by the Quality Checking Tool.", dataNotSupportCheckResult.Message);
            Assert.False(dataNotSupportCheckResult.NeedCorrection);
            Assert.AreEqual(QualityCheckingResultLevel.Error, dataNotSupportCheckResult.Level);
        }
        private IQualityCheckingResult GenerateCorrectSampleMatrixTypeDataNewVersion(Hatfield.EnviroData.Core.Action sampleActionData,
                                                                                     StringCompareCheckingRule rule)
        {
            var newVersionOfData = CorrectSampleMatrixTypeData(sampleActionData, rule);


            var relationActionOfNewVersion = new RelatedAction();
            relationActionOfNewVersion.Action = sampleActionData;
            relationActionOfNewVersion.Action1 = newVersionOfData;
            relationActionOfNewVersion.RelationshipTypeCV = QualityAssuranceConstants.SubVersionRelationCVType;
            var relationShipCV = _relationShipTypeCVRepository.GetAll()
                                    .Where(x => x.Name == QualityAssuranceConstants.SubVersionRelationCVType)
                                    .FirstOrDefault();
            relationActionOfNewVersion.CV_RelationshipType = relationShipCV;

            sampleActionData.RelatedActions.Add(relationActionOfNewVersion);

            return new QualityCheckingResult("Create new version for correction data.", false, QualityCheckingResultLevel.Info);
        }
        public void ValidCheckTest()
        {
            var testActionData = CreateTestAction();
            var testQualityCheckingRule = new StringCompareCheckingRule("Water", false, "test");

            var mockVersionHelper = new Mock<IDataVersioningHelper>();
            mockVersionHelper.Setup(x => x.GetLatestVersionActionData(It.IsAny<Hatfield.EnviroData.Core.Action>())).Returns(testActionData);
            var mockRepository = new Mock<IRepository<CV_RelationshipType>>();

            var testTool = new SampleMatrixTypeCheckingTool(mockVersionHelper.Object, mockRepository.Object);
            
            var qcResult = testTool.Check(testActionData, testQualityCheckingRule);

            var expextedMessageBuilder = new StringBuilder();
            expextedMessageBuilder.AppendLine("Sample Result: abdc02ff-4bc6-4b8c-8578-e3c8d3620cea on May-01-2015's sample matrix meets the expected value.");
            expextedMessageBuilder.AppendLine("Sample Result: abdc02ff-4bc6-4b8c-8578-e3c8d3620cea's sample matrix meets the expected value.");
            expextedMessageBuilder.AppendLine("Sample Result: abdc02ff-4bc6-4b8c-8578-e3c8d3620cea on May-01-2015's sample matrix value is test type, the expected value is Water. Need data correction.");
            expextedMessageBuilder.AppendLine("Sample Result: abdc02ff-4bc6-4b8c-8578-e3c8d3620cea on May-01-2015 does not contain sample matrix data.");

            Assert.AreEqual(expextedMessageBuilder.ToString(), qcResult.Message);
            Assert.True(qcResult.NeedCorrection);
            Assert.AreEqual(QualityCheckingResultLevel.Info, qcResult.Level);
        }
        public void DataNeedCorrectionTest()
        {
            var testActionData = CreateTestAction();
            var testQualityCheckingRule = new StringCompareCheckingRule("Water", true, "test");

            var mockVersionHelper = new Mock<IDataVersioningHelper>();
            var mockRepository = new Mock<IRepository<CV_RelationshipType>>();
            mockRepository.Setup(x => x.GetAll())
                            .Returns(() => 
                                (new List<CV_RelationshipType> { 
                                    new CV_RelationshipType{
                                        Name = "Is new version of"
                                    }
                                })
                                .AsQueryable());

            var mockDefaultValueProvider = new Mock<IWQDefaultValueProvider>();
            mockDefaultValueProvider.Setup(x => x.ActionRelationshipTypeSubVersion).Returns("is new version of");

            var testTool = new SampleMatrixTypeCheckingTool(new DataVersioningHelper(mockDefaultValueProvider.Object), mockRepository.Object);

            var correctedResult = testTool.Correct(testActionData, testQualityCheckingRule);

            Assert.NotNull(correctedResult);
            Assert.AreEqual("Create new version for correction data.", correctedResult.Message);
            Assert.AreEqual(QualityCheckingResultLevel.Info, correctedResult.Level);
            Assert.False(correctedResult.NeedCorrection);

            var relationShips = testActionData.RelatedActions;
            Assert.AreEqual(1, relationShips.Count);
            Assert.AreEqual("Is new version of", relationShips.ElementAt(0).RelationshipTypeCV);
            Assert.AreEqual("Is new version of", relationShips.ElementAt(0).CV_RelationshipType.Name);

            var newVersionAction = relationShips.ElementAt(0).Action1;

            Assert.NotNull(newVersionAction);

            foreach(var featureAction in newVersionAction.FeatureActions)
            {
                foreach(var result in featureAction.Results)
                {
                    var extension = result.ResultExtensionPropertyValues.Where(x => x.ExtensionProperty.PropertyName == "Matrix Type").FirstOrDefault();

                    if(extension != null)
                    {
                        Assert.AreEqual("test", extension.PropertyValue);
                    }
                }
            }
        }
        public void DataDoesNotNeedCorrectionTest()
        {
            var mockVersionHelper = new Mock<IDataVersioningHelper>();
            var mockRepository = new Mock<IRepository<CV_RelationshipType>>();
            
            var testQualityCheckingRule = new StringCompareCheckingRule("Water", true, "test");

            var testTool = new SampleMatrixTypeCheckingTool(mockVersionHelper.Object, mockRepository.Object);

            var correctResult = testTool.Correct(new Hatfield.EnviroData.Core.Action(), testQualityCheckingRule);

            Assert.NotNull(correctResult);
            Assert.AreEqual("Sample data meets quality checking rule. No correction is needed.", correctResult.Message);
            Assert.AreEqual(QualityCheckingResultLevel.Info, correctResult.Level);
            Assert.False(correctResult.NeedCorrection);
        }
        private IQualityCheckingResult IsSampleMatrixTypeDataMeetQualityCheckingRule(Hatfield.EnviroData.Core.Action sampleActionData, 
                                                                                     StringCompareCheckingRule rule)
        {
            if (sampleActionData == null || sampleActionData.FeatureActions == null || !sampleActionData.FeatureActions.Any())
            {
                return new QualityCheckingResult("No results found for the sample action.", false, QualityCheckingResultLevel.Info);
            }

            var sampleResults = from featureAction in sampleActionData.FeatureActions
                                from result in featureAction.Results
                                where featureAction.SamplingFeature.SamplingFeatureTypeCV == QualityAssuranceConstants.SiteSampleFeatureTypeCV
                                select result;

            if (sampleResults == null || !sampleResults.Any())
            {
                return new QualityCheckingResult("No results found for the sample action.", false, QualityCheckingResultLevel.Info);
            }

            var resultStringBuilder = new StringBuilder();
            var needCorrection = false;

            foreach(var result in sampleResults)
            {
                var sampleMatrixExtension = result.ResultExtensionPropertyValues
                                                    .Where(x => x.ExtensionProperty.PropertyName == 
                                                           ESDATSampleCollectionConstants.ResultExtensionPropertyValueKeyMatrixType)
                                                    .FirstOrDefault();

                if (sampleMatrixExtension == null)
                {
                    resultStringBuilder.AppendLine("Sample Result: " + ResultToString(result) + " does not contain sample matrix data.");
                }
                else
                {
                    var stringCompareOption = rule.IsCaseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;

                    if (string.Equals(sampleMatrixExtension.PropertyValue, rule.ExpectedValue, stringCompareOption))
                    {
                        resultStringBuilder.AppendLine(
                            string.Format(validSampleMatrixResultFormat, ResultToString(result))
                        );
                    }
                    else
                    {
                        resultStringBuilder.AppendLine(
                            string.Format(invalidSampleMatrixResultFormat, 
                                          ResultToString(result), 
                                          sampleMatrixExtension.PropertyValue, 
                                          rule.ExpectedValue)
                        );
                        needCorrection = true;
                    }
                }
            }

            var qcResult = new QualityCheckingResult(resultStringBuilder.ToString(), needCorrection, QualityCheckingResultLevel.Info);
            return qcResult;
        }
        private Hatfield.EnviroData.Core.Action CorrectSampleMatrixTypeData(Hatfield.EnviroData.Core.Action sampleActionData,
                                                                            StringCompareCheckingRule rule)
        {
            var newVersionOfData = _dataVersioningHelper.CloneActionData(sampleActionData);

            foreach (var featureAction in newVersionOfData.FeatureActions)
            {
                foreach(var result in featureAction.Results)
                {
                    foreach(var extension in result.ResultExtensionPropertyValues)
                    {
                        if (extension.ExtensionProperty.PropertyName == ESDATSampleCollectionConstants.ResultExtensionPropertyValueKeyMatrixType)
                        {
                            extension.PropertyValue = rule.CorrectionValue;
                        }
                    }
                }
            }

            return newVersionOfData;
        }