Exemplo n.º 1
0
        public void SearchIndexTest_GivenSearchSchemasAndModels_EnsureFieldsMatch()
        {
            // Arrange
            IList <string>         ErrorLog = new List <string>();
            DatasetDefinitionIndex datasetDefinitionIndex = new DatasetDefinitionIndex();
            ProvidersIndex         providersIndex         = new ProvidersIndex();
            PublishedFundingIndex  publishedfundingindex  = new PublishedFundingIndex();
            PublishedProviderIndex publishedProviderIndex = new PublishedProviderIndex();
            SpecificationIndex     specificationindex     = new SpecificationIndex();
            TemplateIndex          templateIndex          = new TemplateIndex();

            IEnumerable <Type> searchIndexTypes = GetTypesWithSearchIndexAttribute();

            IEnumerable <string> indexNames = Directory
                                              .GetDirectories(searchIndexDirectoryPath, "*index", SearchOption.TopDirectoryOnly)
                                              .Select(m => new DirectoryInfo(m).Name);

            //Act
            foreach (string indexName in indexNames)
            {
                try
                {
                    string jsonFilePath = $@"{searchIndexDirectoryPath}\{indexName}\{indexName}.json";

                    string            jsonText          = File.ReadAllText(jsonFilePath, Encoding.UTF8);
                    SearchIndexSchema searchIndexSchema = JsonConvert.DeserializeObject <SearchIndexSchema>(jsonText);
                    if (searchIndexSchema?.Name == null)
                    {
                        ErrorLog.Add(string.IsNullOrWhiteSpace(jsonText)
                            ? $"{indexName} json is blank"
                            : $"{indexName} json name is not available");
                    }
                    else if (searchIndexSchema.Name != indexName)
                    {
                        ErrorLog.Add($"Expected to find index { indexName }, but found { searchIndexSchema.Name }");
                    }
                    else
                    {
                        Type searchIndexType = searchIndexTypes
                                               .FirstOrDefault(m => m.CustomAttributes
                                                               .FirstOrDefault(p => p.NamedArguments
                                                                               .Any(n => n.TypedValue.Value.ToString() == searchIndexSchema.Name)) != null);

                        IEnumerable <string> searchIndexProperties = searchIndexType.GetProperties()
                                                                     .Select(m => m.CustomAttributes
                                                                             .FirstOrDefault(a => a.AttributeType.Name == "JsonPropertyAttribute")
                                                                             ?.ConstructorArguments[0].Value.ToString().ToLower())
                                                                     .Where(p => p != null);

                        IEnumerable <string> searchIndexJsonProperties = searchIndexSchema.Fields
                                                                         .Select(m => m.Name.ToLower())
                                                                         .Where(p => p != null);

                        if (!searchIndexProperties.Any())
                        {
                            ErrorLog.Add($"Index {indexName}: The model contains no properties");
                        }
                        else if (!searchIndexJsonProperties.Any())
                        {
                            ErrorLog.Add($"Index {indexName}: The json contains no properties");
                        }
                        else
                        {
                            IEnumerable <string> notInJson = searchIndexProperties.Except(searchIndexJsonProperties);
                            if (notInJson.Any())
                            {
                                string properties = string.Join(",", notInJson);
                                ErrorLog.Add($"Index {indexName}: The model contains the following properties not found in the json schema ({properties})");
                            }

                            IEnumerable <string> notInModel = searchIndexJsonProperties.Except(searchIndexProperties);
                            if (notInModel.Any())
                            {
                                string properties = string.Join(",", notInModel);
                                ErrorLog.Add($"Index {indexName}: The json schema contains the following properties not found in the model ({properties})");
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    ErrorLog.Add($"Unexpected error checking '{indexName}': {e.Message}{Environment.NewLine}{ e.StackTrace }");
                }
            }

            //Assert
            if (ErrorLog.Any())
            {
                Assert.Fail(string.Join(Environment.NewLine, ErrorLog));
            }
        }
Exemplo n.º 2
0
        public void SearchIndexTest_GivenSearchSchemasAndModels_EnsuresAttributesMatch()
        {
            //Arrange
            IList <string>         ErrorLog = new List <string>();
            DatasetDefinitionIndex datasetDefinitionIndex = new DatasetDefinitionIndex();
            ProvidersIndex         providersIndex         = new ProvidersIndex();
            PublishedFundingIndex  publishedfundingindex  = new PublishedFundingIndex();
            PublishedProviderIndex publishedProviderIndex = new PublishedProviderIndex();
            SpecificationIndex     specificationindex     = new SpecificationIndex();
            TemplateIndex          templateIndex          = new TemplateIndex();

            IEnumerable <Type> searchIndexTypes = GetTypesWithSearchIndexAttribute();

            IEnumerable <string> indexNames = Directory
                                              .GetDirectories(searchIndexDirectoryPath, "*index", SearchOption.TopDirectoryOnly)
                                              .Select(m => new DirectoryInfo(m).Name);

            //Act
            foreach (string indexName in indexNames)
            {
                try
                {
                    string jsonFilePath = $@"{searchIndexDirectoryPath}\{indexName}\{indexName}.json";

                    string jsonText = File.ReadAllText(jsonFilePath, Encoding.UTF8);

                    SearchIndexSchema searchIndexSchema = JsonConvert.DeserializeObject <SearchIndexSchema>(jsonText);
                    if (searchIndexSchema?.Name == null)
                    {
                        ErrorLog.Add(string.IsNullOrWhiteSpace(jsonText)
                            ? $"{indexName} json is blank"
                            : $"{indexName} json name is not available");
                    }
                    else if (searchIndexSchema.Name != indexName)
                    {
                        ErrorLog.Add($"Expected to find index {indexName}, but found {searchIndexSchema.Name}");
                    }
                    else
                    {
                        Type searchIndexType = searchIndexTypes.FirstOrDefault(m =>
                                                                               m.CustomAttributes.FirstOrDefault(p =>
                                                                                                                 p.NamedArguments.Any(n =>
                                                                                                                                      n.TypedValue.Value.ToString() == searchIndexSchema.Name)) != null);

                        IEnumerable <PropertyInfo> searchIndexProperties = searchIndexType.GetProperties();

                        foreach (SearchIndexField searchIndexField in searchIndexSchema.Fields)
                        {
                            PropertyInfo matchedProperty = searchIndexProperties.FirstOrDefault(m =>
                                                                                                string.Equals(m.CustomAttributes
                                                                                                              .SingleOrDefault(x => x.AttributeType.Name == "JsonPropertyAttribute")
                                                                                                              ?.ConstructorArguments[0].ToString().Replace("\"", "")
                                                                                                              ?? m.Name,
                                                                                                              searchIndexField.Name,
                                                                                                              StringComparison.InvariantCultureIgnoreCase));

                            if (matchedProperty == null)
                            {
                                ErrorLog.Add($"{indexName}: {searchIndexField.Name} did not match any properties");
                            }
                            else
                            {
                                CheckIndexFieldTypes(matchedProperty, searchIndexField, ErrorLog, indexName);
                                CheckIndexAttributes(matchedProperty, searchIndexField, ErrorLog, indexName);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    ErrorLog.Add($"Unexpected error checking '{indexName}': {e.Message}{Environment.NewLine}{e.StackTrace}");
                }
            }

            //Assert
            if (ErrorLog.Any())
            {
                Assert.Fail(string.Join(Environment.NewLine, ErrorLog));
            }
        }