diff --git a/.ci/unit-tests/File_Toolkit_Tests/Adapter/PushPullTests.cs b/.ci/unit-tests/File_Toolkit_Tests/Adapter/PushPullTests.cs new file mode 100644 index 0000000..0fe8950 --- /dev/null +++ b/.ci/unit-tests/File_Toolkit_Tests/Adapter/PushPullTests.cs @@ -0,0 +1,99 @@ +/* + * This file is part of the Buildings and Habitats object Model (BHoM) + * Copyright (c) 2015 - 2023, the respective contributors. All rights reserved. + * + * Each contributor holds copyright over their respective contributions. + * The project versioning (Git) records all such contribution source information. + * + * + * The BHoM is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3.0 of the License, or + * (at your option) any later version. + * + * The BHoM is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this code. If not, see . + */ + +using NUnit.Framework; +using BH.Adapter.File; +using BH.oM.Structure.Elements; +using System.Reflection; +using FluentAssertions; +using Newtonsoft.Json; + +namespace BH.Tests.Adapter +{ + public class PushPullTests + { + string randomTestFilePath = ""; + + [SetUp] + public void SetupRandomTestFilePath() + { + randomTestFilePath = Path.Combine(Directory.GetCurrentDirectory(), Guid.NewGuid().ToString() + ".json"); + } + + [TearDown] + public void DeleteRandomTestFile() + { + if (File.Exists(randomTestFilePath)) + File.Delete(randomTestFilePath); + } + + [Test] + public void Bar() + { + Bar bar = (Bar)BH.Engine.Base.Create.RandomObject(typeof(Bar)); + + FileAdapter fa = new FileAdapter(randomTestFilePath); + var objectsToPush = new List() { bar }; + fa.Push(objectsToPush, "", BH.oM.Adapter.PushType.DeleteThenCreate); + + if (!File.Exists(randomTestFilePath)) + Assert.Fail("File was not created."); + + var fileContent = fa.Pull(new BH.oM.Adapters.File.FileContentRequest() { File = randomTestFilePath }); + + fileContent.Should().BeEquivalentTo(objectsToPush); + } + + [Test] + public void Bar_FormattedJson() + { + Bar bar = (Bar)BH.Engine.Base.Create.RandomObject(typeof(Bar)); + + FileAdapter fa = new FileAdapter(randomTestFilePath); + var objectsToPush = new List() { bar }; + fa.Push(objectsToPush, "", BH.oM.Adapter.PushType.DeleteThenCreate); + + if (!File.Exists(randomTestFilePath)) + Assert.Fail("File was not created."); + + // Format the json. + OverwriteWithFormattedJson(randomTestFilePath); + + var fileContent = fa.Pull(new BH.oM.Adapters.File.FileContentRequest() { File = randomTestFilePath }); + + fileContent.Should().BeEquivalentTo(objectsToPush); + } + + private static string FormatJson(string json) + { + dynamic parsedJson = JsonConvert.DeserializeObject(json); + return JsonConvert.SerializeObject(parsedJson, Formatting.Indented); + } + + private static void OverwriteWithFormattedJson(string filepath) + { + string json = File.ReadAllText(filepath); + string formattedJson = FormatJson(json); + File.WriteAllText(filepath, formattedJson); + } + } +} \ No newline at end of file diff --git a/.ci/unit-tests/File_Toolkit_Tests/Engine/Compute/ReadFromJsonFile.cs b/.ci/unit-tests/File_Toolkit_Tests/Engine/Compute/ReadFromJsonFile.cs new file mode 100644 index 0000000..99287d8 --- /dev/null +++ b/.ci/unit-tests/File_Toolkit_Tests/Engine/Compute/ReadFromJsonFile.cs @@ -0,0 +1,99 @@ +/* + * This file is part of the Buildings and Habitats object Model (BHoM) + * Copyright (c) 2015 - 2023, the respective contributors. All rights reserved. + * + * Each contributor holds copyright over their respective contributions. + * The project versioning (Git) records all such contribution source information. + * + * + * The BHoM is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3.0 of the License, or + * (at your option) any later version. + * + * The BHoM is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this code. If not, see . + */ + +using NUnit.Framework; +using BH.Adapter.File; +using BH.oM.Structure.Elements; +using System.Reflection; +using FluentAssertions; +using Newtonsoft.Json; + +namespace BH.Tests.Engine.Compute +{ + public class Compute + { + string randomTestFilePath = ""; + + [SetUp] + public void SetupRandomTestFilePath() + { + randomTestFilePath = Path.Combine(Directory.GetCurrentDirectory(), Guid.NewGuid().ToString() + ".json"); + } + + [TearDown] + public void DeleteRandomTestFile() + { + if (File.Exists(randomTestFilePath)) + File.Delete(randomTestFilePath); + } + + [Test] + public void Bar() + { + Bar bar = (Bar)BH.Engine.Base.Create.RandomObject(typeof(Bar)); + + FileAdapter fa = new FileAdapter(randomTestFilePath); + var objectsToPush = new List() { bar }; + fa.Push(objectsToPush, "", BH.oM.Adapter.PushType.DeleteThenCreate); + + if (!File.Exists(randomTestFilePath)) + Assert.Fail("File was not created."); + + var fileContent = BH.Engine.Adapters.File.Compute.ReadFromJsonFile(randomTestFilePath, true); + + fileContent.Should().BeEquivalentTo(objectsToPush); + } + + [Test] + public void Bar_FormattedJson() + { + Bar bar = (Bar)BH.Engine.Base.Create.RandomObject(typeof(Bar)); + + FileAdapter fa = new FileAdapter(randomTestFilePath); + var objectsToPush = new List() { bar }; + fa.Push(objectsToPush, "", BH.oM.Adapter.PushType.DeleteThenCreate); + + if (!File.Exists(randomTestFilePath)) + Assert.Fail("File was not created."); + + // Format the json. + OverwriteWithFormattedJson(randomTestFilePath); + + var fileContent = BH.Engine.Adapters.File.Compute.ReadFromJsonFile(randomTestFilePath, true); + + fileContent.Should().BeEquivalentTo(objectsToPush); + } + + private static string FormatJson(string json) + { + dynamic parsedJson = JsonConvert.DeserializeObject(json); + return JsonConvert.SerializeObject(parsedJson, Formatting.Indented); + } + + private static void OverwriteWithFormattedJson(string filepath) + { + string json = File.ReadAllText(filepath); + string formattedJson = FormatJson(json); + File.WriteAllText(filepath, formattedJson); + } + } +} \ No newline at end of file diff --git a/.ci/unit-tests/File_Toolkit_Tests/File_Toolkit_Tests.csproj b/.ci/unit-tests/File_Toolkit_Tests/File_Toolkit_Tests.csproj new file mode 100644 index 0000000..49f4b75 --- /dev/null +++ b/.ci/unit-tests/File_Toolkit_Tests/File_Toolkit_Tests.csproj @@ -0,0 +1,93 @@ + + + + net6.0 + enable + enable + + false + https://github.com/BHoM/File_Toolkit + 6.3.0.0 + 6.0.0.0 + Debug;Release;Test + + + + + + + + + + + + + + + + + + + + $(ProgramData)\BHoM\Assemblies\Adapter_oM.dll + false + + + $(ProgramData)\BHoM\Assemblies\Analytical_Engine.dll + false + + + $(ProgramData)\BHoM\Assemblies\Analytical_oM.dll + false + + + $(ProgramData)\BHoM\Assemblies\BHoM.dll + false + + + $(ProgramData)\BHoM\Assemblies\BHoM_Adapter.dll + false + + + $(ProgramData)\BHoM\Assemblies\BHoM_Engine.dll + false + + + $(ProgramData)\BHoM\Assemblies\Data_oM.dll + false + + + $(ProgramData)\BHoM\Assemblies\Dimensional_oM.dll + false + + + $(ProgramData)\BHoM\Assemblies\Geometry_Engine.dll + false + + + $(ProgramData)\BHoM\Assemblies\Geometry_oM.dll + false + + + $(ProgramData)\BHoM\Assemblies\Physical_oM.dll + false + + + $(ProgramData)\BHoM\Assemblies\Spatial_Engine.dll + false + + + $(ProgramData)\BHoM\Assemblies\Spatial_oM.dll + false + + + $(ProgramData)\BHoM\Assemblies\Structure_Engine.dll + false + + + $(ProgramData)\BHoM\Assemblies\Structure_oM.dll + false + + + + diff --git a/.ci/unit-tests/File_Toolkit_Tests/File_Toolkit_Tests.sln b/.ci/unit-tests/File_Toolkit_Tests/File_Toolkit_Tests.sln new file mode 100644 index 0000000..6749f4b --- /dev/null +++ b/.ci/unit-tests/File_Toolkit_Tests/File_Toolkit_Tests.sln @@ -0,0 +1,43 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.33424.131 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "File_Toolkit_Tests", "File_Toolkit_Tests.csproj", "{DABCC8EE-CA63-4A69-BA89-AB6E58E0E499}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "File_Adapter", "..\..\..\File_Adapter\File_Adapter.csproj", "{A5D81731-370E-4741-997B-14EC448756FC}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "File_Engine", "..\..\..\File_Engine\File_Engine.csproj", "{1213A0FB-F189-4998-9A28-CAAFE8C66826}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "File_oM", "..\..\..\File_oM\File_oM.csproj", "{727D28D4-A36D-4CB3-8AD5-77498E2A81A6}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {DABCC8EE-CA63-4A69-BA89-AB6E58E0E499}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DABCC8EE-CA63-4A69-BA89-AB6E58E0E499}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DABCC8EE-CA63-4A69-BA89-AB6E58E0E499}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DABCC8EE-CA63-4A69-BA89-AB6E58E0E499}.Release|Any CPU.Build.0 = Release|Any CPU + {A5D81731-370E-4741-997B-14EC448756FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A5D81731-370E-4741-997B-14EC448756FC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A5D81731-370E-4741-997B-14EC448756FC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A5D81731-370E-4741-997B-14EC448756FC}.Release|Any CPU.Build.0 = Release|Any CPU + {1213A0FB-F189-4998-9A28-CAAFE8C66826}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1213A0FB-F189-4998-9A28-CAAFE8C66826}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1213A0FB-F189-4998-9A28-CAAFE8C66826}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1213A0FB-F189-4998-9A28-CAAFE8C66826}.Release|Any CPU.Build.0 = Release|Any CPU + {727D28D4-A36D-4CB3-8AD5-77498E2A81A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {727D28D4-A36D-4CB3-8AD5-77498E2A81A6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {727D28D4-A36D-4CB3-8AD5-77498E2A81A6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {727D28D4-A36D-4CB3-8AD5-77498E2A81A6}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {AEBBD38D-19BB-40B5-A805-E1AC6E36279E} + EndGlobalSection +EndGlobal