using System; using System.Collections.Generic; using System.IO; using System.Reflection; using LCU.API.Collections.CollectionsApiLib.Models; using LCU.Shared.Collections.RepoApiLib.Models; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Xunit.Sdk; namespace LCU.API.Collections.RepoApiTests { public class RepoUpdateTestRow { public TemenosAccount CollectionAccount { get; set; } public TemenosCase CollectionCase { get; set; } public RepoAccount ExistingRepoAccount { get; set; } public RepoAccount NewRepoAccount { get; set; } } public class CollectionsJsonFileDataAttribute : DataAttribute { private readonly string _filePath; private readonly string _propertyName; /// /// Load data from a JSON file as the data source for a theory /// /// The absolute or relative path to the JSON file to load public CollectionsJsonFileDataAttribute(string filePath) : this(filePath, null) { } /// /// Load data from a JSON file as the data source for a theory /// /// The absolute or relative path to the JSON file to load /// The name of the property on the JSON file that contains the data for the test public CollectionsJsonFileDataAttribute(string filePath, string propertyName) { _filePath = filePath; _propertyName = propertyName; } /// public override IEnumerable GetData(MethodInfo testMethod) { if (testMethod == null) { throw new ArgumentNullException(nameof(testMethod)); } // Get the absolute path to the JSON file var path = Path.IsPathRooted(_filePath) ? _filePath : Path.GetRelativePath(Directory.GetCurrentDirectory(), _filePath); if (!File.Exists(path)) { throw new ArgumentException($"Could not find file at path: {path}"); } // Load the file var fileData = File.ReadAllText(_filePath); if (string.IsNullOrEmpty(_propertyName)) { //whole file is the data return JsonConvert.DeserializeObject>(fileData); } // Only use the specified property as the data var allData = JObject.Parse(fileData); var data = allData[_propertyName]; return data.ToObject>(); } } }