Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions .ci/unit-tests/OneClickLCA_Tests/ApiAdapterMock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using BH.Adapter;
using BH.oM.Adapter;
using BH.oM.Adapters.HTTP;
using BH.oM.Data.Requests;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OneClickLCA_Tests
{
internal class ApiAdapterMock: BHoMAdapter, IBHoMAdapter
{
public override IEnumerable<object> Pull(IRequest request, PullType pullType = PullType.AdapterDefault, ActionConfig actionConfig = null)
{
GetRequest getRequest = (GetRequest)request;

if (getRequest.BaseUrl.Contains("materials-carbon-data"))
{
return new List<object>()
{
"{\"found\":1,\"hits\":[{\"document\":{\"_id\":\"testId\",\"nameEN\":\"Test material\",\"unitForData\":\"kg\"," +
"\"impacts\":{\"A1-A3\":{\"impactGWP100_kgCO2e_total\":1.5}},\"epdNumber\":\"EPD-1\"},\"highlight\":{},\"highlights\":[]}]," +
"\"page\":1,\"facet_counts\":[],\"request_params\":{\"collection_name\":\"c\",\"first_q\":\"*\",\"per_page\":10,\"q\":\"*\"}," +
"\"search_cutoff\":false,\"search_time_ms\":1}"
};
}
else if (getRequest.BaseUrl.Contains("projects"))
{
//TODO: get json of an expected project response
return new List<object>() { "expectedProjectsResponse" };
}
else if (getRequest.BaseUrl.Contains("calculation-results"))
{
//TODO: get jsons for dictionary and calculation results responses
if (getRequest.BaseUrl.Contains("dictionary"))
{
return new List<object>() { "expectedDictionaryResponse" };
}
return new List<object>() { "expectedCalculationResultsResponse" };
}

throw new NotImplementedException($"The requested uri was not expected: {getRequest.BaseUrl}");
}
}
}
38 changes: 38 additions & 0 deletions .ci/unit-tests/OneClickLCA_Tests/AuthAdapterMock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using BH.Adapter;
using BH.Adapter.OneClickLCA.Objects;
using BH.oM.Adapter;
using BH.oM.Adapters.SQL;
using BH.oM.Base;
using BH.oM.Data.Requests;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OneClickLCA_Tests
{
/// <summary>
/// This is a mock BHoMAdapter used for testing that the main adapter flow is working correctly,
/// it does not return an actually valid API key, nor does it need to.
/// <br />
/// This adapter is solely for testing.
/// </summary>
internal class AuthAdapterMock: BHoMAdapter, IBHoMAdapter
{
/// <summary>
/// mock pull method that directly returns a "valid API key"
/// </summary>
public override IEnumerable<object> Pull(IRequest request, PullType pullType = PullType.AdapterDefault, ActionConfig actionConfig = null)
{
TableRequest tRequest = request as TableRequest;

if (tRequest == null)
return null;

UserAPIDetails rtn = new UserAPIDetails() { ClientId = "ValidClientId", ClientSecret = "ValidAPIKey" };

return new List<object>() { rtn };
}
}
}
16 changes: 2 additions & 14 deletions .ci/unit-tests/OneClickLCA_Tests/CalculationsResultsApi_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ namespace OneClickLCA_Tests
{
public class CalculationsResultsApi_Tests
{
private const string clientId = "service_acc_buro_happold";
private string clientSecret = "";

// These can be set to whatever designId & toolId you want to fetch
private const string designId = "69b1576c2b13bd02eee02e3c";
private const string toolId = "lcaRicsV2";
Expand All @@ -23,14 +20,7 @@ public class CalculationsResultsApi_Tests
[Description("One time set up for the adapter and client secret key.")]
public void OneTimeSetUp()
{
//TODO: change to pull from database based on current user instead of token file.
string tokenFile = "$ProgramData/BHoM/Settings/OneClickLCA.token";
if (File.Exists(tokenFile))
clientSecret = File.ReadAllText(tokenFile);
else
Assert.Inconclusive("Token file was not found in settings, so these tests have been marked as inconclusive.");

adapter = new OneClickLCAAdapter();
adapter = new OneClickLCAAdapter(new AuthAdapterMock(), new ApiAdapterMock());
}

[SetUp]
Expand All @@ -39,15 +29,13 @@ public void Setup()
{
CalculationResultsApiRequest request = new CalculationResultsApiRequest
{
ClientId = clientId,
ClientSecret = clientSecret,
DesignId = designId,
ToolId = toolId,
ShowAllCategoriesForTool = false
};

IEnumerable<object> pullResult = adapter.Pull(request);
calculationResults = pullResult?.FirstOrDefault() as IEnumerable<CalculationResult> ?? Enumerable.Empty<CalculationResult>();
calculationResults = pullResult?.FirstOrDefault() as IEnumerable<CalculationResult> ?? Enumerable.Empty<CalculationResult>().ToList();
}

[Test]
Expand Down
21 changes: 11 additions & 10 deletions .ci/unit-tests/OneClickLCA_Tests/MaterialsCarbonDataApi_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,28 @@ public class MaterialsCarbonDataApi_Tests
"\"page\":1,\"facet_counts\":[],\"request_params\":{\"collection_name\":\"c\",\"first_q\":\"*\",\"per_page\":10,\"q\":\"*\"}," +
"\"search_cutoff\":false,\"search_time_ms\":1}";

private static readonly JsonSerializerOptions JsonOptions = new JsonSerializerOptions
private OneClickLCAAdapter adapter;

[OneTimeSetUp]
[Description("One time set up for the adapter and client secret key.")]
public void OneTimeSetUp()
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
adapter = new OneClickLCAAdapter(new AuthAdapterMock(), new ApiAdapterMock());
}

[Test]
[Description("Test that a materials-carbon document extracted from a search response can be converted to an EPD using the public converter.")]
public void MaterialsCarbonDocumentJson_Converts_ToEpd()
{
using JsonDocument doc = JsonDocument.Parse(MinimalSearchJson);
JsonElement documentElement = doc.RootElement.GetProperty("hits")[0].GetProperty("document");
IEnumerable<object> objs = adapter.Pull(new MaterialsCarbonDataApiRequest());

string documentJson = documentElement.GetRawText();

var epd = BH.Adapter.OneClickLCA.Convert.ToEnvironmentalProductDeclaration(documentJson);
Assert.That(objs.ToList(), Has.Count.EqualTo(1));
EnvironmentalProductDeclaration? epd = objs.Single() as EnvironmentalProductDeclaration;

Assert.That(epd, Is.Not.Null);
Assert.That(epd!.Name, Is.EqualTo("Test material"));
Assert.That(epd.EnvironmentalMetrics, Is.Not.Null);
Assert.That(epd.EnvironmentalMetrics.Count, Is.GreaterThan(0));
Assert.That(epd.EnvironmentalMetrics, Is.Not.Empty);
}

[Description("Test that the public converter returns null for invalid input and produces expected EPD for valid document JSON.")]
Expand Down
4 changes: 4 additions & 0 deletions .ci/unit-tests/OneClickLCA_Tests/OneClickLCA_Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@
<HintPath>$(ProgramData)\BHoM\Assemblies\LifeCycleAssessment_oM.dll</HintPath>
<SpecificVersion>false</SpecificVersion>
</Reference>
<Reference Include="SQL_oM">
<HintPath>$(ProgramData)\BHoM\Assemblies\SQL_oM.dll</HintPath>
<SpecificVersion>false</SpecificVersion>
</Reference>
</ItemGroup>

<ItemGroup>
Expand Down
Loading