public void SearchIndexTest_GivenSearchSchemasAndModels_EnsureFieldsMatch() { // Arrange IList <string> ErrorLog = new List <string>(); IEnumerable <Type> searchIndexTypes = GetTypesWithSearchIndexAttribute(); IEnumerable <string> indexNames = Directory.GetDirectories(searchIndexDirectoryPath, "*index", SearchOption.TopDirectoryOnly) .Select(m => new DirectoryInfo(m).Name); foreach (string indexName in indexNames) { string jsonFilePath = $@"{searchIndexDirectoryPath}\{indexName}\{indexName}.json"; string jsonText = File.ReadAllText(jsonFilePath, Encoding.UTF8); SearchIndexSchema searchIndexSchema = JsonConvert.DeserializeObject <SearchIndexSchema>(jsonText); 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 .First(a => a.AttributeType.Name == "JsonPropertyAttribute") .ConstructorArguments[0].Value.ToString().ToLower()); IEnumerable <string> searchIndexJsonProperties = searchIndexSchema.Fields.Select(m => m.Name.ToLower()); bool areEquivalent = !searchIndexProperties.Except(searchIndexJsonProperties).Any(); if (!areEquivalent) { ErrorLog.Add($"Index {indexName}: The model contains the following properties not found in the json Schema ({ string.Join(",", searchIndexProperties.Except(searchIndexJsonProperties))})"); } areEquivalent = !searchIndexJsonProperties.Except(searchIndexProperties).Any(); if (!areEquivalent) { ErrorLog.Add($"Index {indexName}: The json schema contains the following properties not found in the model ({ string.Join(",", searchIndexJsonProperties.Except(searchIndexProperties))})"); } } //Assert if (ErrorLog.Any()) { Assert.Fail(string.Join("\r\n", ErrorLog)); } }
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)); } }
public void SearchIndexTest_GivenSearchSchemasAndModels_EnsuresAttributesMatch() { //Arrange IList <string> ErrorLog = new List <string>(); 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) { string jsonFilePath = $@"{searchIndexDirectoryPath}\{indexName}\{indexName}.json"; string jsonText = File.ReadAllText(jsonFilePath, Encoding.UTF8); SearchIndexSchema searchIndexSchema = JsonConvert.DeserializeObject <SearchIndexSchema>(jsonText); 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.Name, searchIndexField.Name, StringComparison.InvariantCultureIgnoreCase)); if (matchedProperty != null) { IEnumerable <string> attributesEnabledInModel = matchedProperty.CustomAttributes.Where(m => m.AttributeType.Name != "JsonPropertyAttribute").Select(m => m.AttributeType.Name.Replace("Attribute", "").Replace("Is", "").ToLower()); IEnumerable <string> attributesEnabledInJson = searchIndexField.GetType().GetProperties().Where(p => p.PropertyType == typeof(bool) && (bool)p.GetValue(searchIndexField, null)) .Select(p => p.Name.ToLower()); IEnumerable <string> unsyncedAttributes = attributesEnabledInModel.Except(attributesEnabledInJson); bool areEquivalent = !unsyncedAttributes.Any(); if (!areEquivalent) { ErrorLog.Add($"Index {indexName}: The follwoing attributes in the model for {searchIndexField.Name} are not in sync with the json schema ({string.Join(",",unsyncedAttributes)})"); } unsyncedAttributes = attributesEnabledInJson.Except(attributesEnabledInModel); areEquivalent = !unsyncedAttributes.Any(); if (!areEquivalent) { ErrorLog.Add($"Index {indexName}: The following attributes in the json schema for {searchIndexField.Name} are not in sync with the model ({string.Join(",", unsyncedAttributes)})"); } } } } //Assert if (ErrorLog.Any()) { Assert.Fail(string.Join("\r\n", ErrorLog)); } }
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)); } }