EDSM: Logs of tests (lol)

This commit is contained in:
alterNERDtive 2022-05-29 17:33:16 +02:00
parent c6585ccff2
commit bdf1dae0dd
24 changed files with 299 additions and 66 deletions

View file

@ -17,8 +17,6 @@
// along with EDNA. If not, see <https://www.gnu.org/licenses/>.
// </copyright>
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
@ -32,26 +30,77 @@ namespace alterNERDtive.Edna
/// </summary>
public class Commander : Locatable
{
/// <summary>
/// Initializes a new instance of the <see cref="Commander"/> class.
/// </summary>
/// <param name="name">The CMDRs name.</param>
public Commander(string name)
{
this.Name = name;
}
/// <param name="apiKey">The CMDRs EDSM API key.</param>
private Commander(string name, string? edsmProfileUrl, DateTime? lastActiveAt, StarSystem? starsystem, Coordinates? coordinates)
=> (this.Name, this.EdsmProfileUrl, this.LastActiveAt, this.StarSystem, this.Coordinates) = (name, edsmProfileUrl, lastActiveAt, starsystem, coordinates);
/// <summary>
/// Gets the CMDRs name.
/// </summary>
public string Name { get; }
public string EdsmProfileUrl { get; private set; }
/// <summary>
/// Gets the CMDRs EDSM profile URL.
/// </summary>
public string? EdsmProfileUrl { get; }
public DateTime LastActiveAt { get; private set; }
/// <summary>
/// Gets the CMDRs date of last activity.
/// </summary>
public DateTime? LastActiveAt { get; }
public StarSystem StarSystem { get; private set; }
/// <summary>
/// Gets the CMDRs current star system.
/// </summary>
public StarSystem? StarSystem { get; }
public new Coordinates Coordinates { get; private set; }
/// <summary>
/// Gets the CMDRs current coordinates.
/// </summary>
public new Coordinates? Coordinates { get; }
/// <summary>
/// Finds a CMDR by name. Optionally takes an EDSM API key to access a
/// private profile.
/// </summary>
/// <param name="name">The CMDRs name.</param>
/// <param name="apiKey">The CMDRs EDSM API key.</param>
/// <returns>The CMDR.</returns>
public static Commander Find(string name, string? apiKey = null)
{
return FindAsync(name, apiKey).GetAwaiter().GetResult();
}
/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <param name="apiKey"></param>
/// <returns></returns>
public static async Task<Commander> FindAsync(string name, string? apiKey = null)
{
if (apiKey != null)
{
throw new NotImplementedException();
}
try
{
Edsm.ApiCmdr cmdr = await Edsm.LogsApi.FindCmdr(name);
return new Commander(name, cmdr.Url, DateTime.Parse(cmdr.DateLastActivity), StarSystem.Find(cmdr.SystemId64!.Value), new Coordinates(cmdr.Coordinates!.Value));
}
catch (ArgumentException e)
{
throw new CommanderNotFoundException(e.Message, e);
}
catch (AccessViolationException e)
{
throw new CommanderHiddenException(e.Message, e);
}
}
}
}

View file

@ -1,7 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net48;net5.0</TargetFrameworks>
<TargetFrameworks>net48;net6.0</TargetFrameworks>
<nullable>enable</nullable>
<PackageId>alterNERDtive.EDNA</PackageId>
<Version>0.0.1</Version>
<Company>alterNERDtive</Company>

79
EDNA/Exceptions.cs Normal file
View file

@ -0,0 +1,79 @@
// <copyright file="Exceptions.cs" company="alterNERDtive">
// Copyright 20212022 alterNERDtive.
//
// This file is part of EDNA.
//
// EDNA is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// EDNA 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with EDNA. If not, see &lt;https://www.gnu.org/licenses/&gt;.
// </copyright>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace alterNERDtive.Edna
{
public class SystemNotFoundException : Exception
{
public SystemNotFoundException(string message)
: base(message)
{
}
}
public class CommanderNotFoundException : Exception
{
public string? Name { get; private set; }
public CommanderNotFoundException()
: base()
{
}
public CommanderNotFoundException(string message)
: base(message)
{
}
public CommanderNotFoundException(string message, Exception innerException)
:base(message, innerException)
{
}
public CommanderNotFoundException(string message, ArgumentException innerException)
: base(message, innerException)
{
this.Name = innerException.ParamName;
}
}
public class CommanderHiddenException : Exception
{
public CommanderHiddenException()
: base()
{
}
public CommanderHiddenException(string message)
: base(message)
{
}
public CommanderHiddenException(string message, Exception innerException)
: base(message, innerException)
{
}
}
}

View file

@ -17,8 +17,6 @@
// along with EDNA. If not, see &lt;https://www.gnu.org/licenses/&gt;.
// </copyright>
#nullable enable
using System;
namespace alterNERDtive.Edna
@ -65,6 +63,14 @@ namespace alterNERDtive.Edna
=> (this.X, this.Y, this.Z, this.Precision)
= (edsmSystem.Coords.Value.X, edsmSystem.Coords.Value.Y, edsmSystem.Coords.Value.Z, 0);
/// <summary>
/// Initializes a new instance of the <see cref="Coordinates"/> struct.
/// </summary>
/// <param name="edsmCoords">A set of EDSM coordinates to convert.</param>
public Coordinates(Edsm.Coordinates edsmCoords)
=> (this.X, this.Y, this.Z, this.Precision)
= (edsmCoords.X, edsmCoords.Y, edsmCoords.Z, 0);
/// <summary>
/// Gets the x coordinate.
/// </summary>

View file

@ -17,8 +17,6 @@
// along with EDNA. If not, see &lt;https://www.gnu.org/licenses/&gt;.
// </copyright>
#nullable enable
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

View file

@ -17,8 +17,6 @@
// along with EDNA. If not, see &lt;https://www.gnu.org/licenses/&gt;.
// </copyright>
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;

View file

@ -17,8 +17,6 @@
// along with EDNA. If not, see &lt;https://www.gnu.org/licenses/&gt;.
// </copyright>
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;

View file

@ -17,8 +17,6 @@
// along with EDNA. If not, see &lt;https://www.gnu.org/licenses/&gt;.
// </copyright>
#nullable enable
using System;
using System.Collections.Generic;

View file

@ -1,11 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net48;net5.0</TargetFrameworks>
<TargetFrameworks>net48;net6.0</TargetFrameworks>
<nullable>enable</nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.8" />
<PackageReference Include="RestSharp" Version="107.3.0" />
</ItemGroup>

View file

@ -17,8 +17,6 @@
// along with EDNA. If not, see &lt;https://www.gnu.org/licenses/&gt;.
// </copyright>
#nullable enable
namespace alterNERDtive.Edna.Edsm
{
/// <summary>

View file

@ -17,8 +17,6 @@
// along with EDNA. If not, see &lt;https://www.gnu.org/licenses/&gt;.
// </copyright>
#nullable enable
using System;
using System.Threading.Tasks;
using RestSharp;
@ -42,7 +40,7 @@ namespace alterNERDtive.Edna.Edsm
/// Gets or sets the APIs status message; see
/// https://www.edsm.net/en/api-logs-v1 for details.
/// </summary>
public string Message { get; set; }
public string Msg { get; set; }
/// <summary>
/// Gets or sets the commanders current system. Will be “null” if the
@ -61,7 +59,7 @@ namespace alterNERDtive.Edna.Edsm
/// current system. Will be null if the commanders flight log
/// timestamps are hidden.
/// </summary>
public DateTime? Date { get; set; }
public string? Date { get; set; }
/// <summary>
/// Gets or sets EDSMs internal ID of the commanders current system.
@ -99,7 +97,7 @@ namespace alterNERDtive.Edna.Edsm
/// Gets or sets the date time of docking at the commanders currently
/// docked station.
/// </summary>
public DateTime? DateDocked { get; set; }
public string? DateDocked { get; set; }
/// <summary>
/// Gets or sets the ship ID of the commanders current ship. That is
@ -122,7 +120,7 @@ namespace alterNERDtive.Edna.Edsm
/// Gets or sets the date and time of the commanders last recorded
/// activity. Will be “null” if the commanders flight log is hidden.
/// </summary>
public DateTime? DateLastActivity { get; set; }
public string? DateLastActivity { get; set; }
/// <summary>
/// Gets or sets the commanders EDSM profile URL. Will be “null” if the
@ -132,7 +130,7 @@ namespace alterNERDtive.Edna.Edsm
}
/// <summary>
///
/// Pulls data about CMDRs from the EDSM Logs API.
/// </summary>
public class LogsApi
{
@ -140,16 +138,16 @@ namespace alterNERDtive.Edna.Edsm
private static readonly RestClient ApiClient = new RestClient(ApiUrl);
/// <summary>
///
/// Pulls data about a single CMDR from the EDSM Logs API.
/// </summary>
/// <param name="name"></param>
/// <param name="apiKey"></param>
/// <param name="name">The CMDRs name.</param>
/// <param name="apiKey">The CMDRs EDSM API key.</param>
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
public static async Task<ApiCmdr> FindCmdr(string name, string? apiKey = null)
{
RestRequest request = new RestRequest("get-position")
.AddQueryParameter("commanderName", name)
.AddQueryParameter("showID", 1)
.AddQueryParameter("showId", 1)
.AddQueryParameter("showCoordinates", 1);
if (apiKey != null)
@ -161,7 +159,7 @@ namespace alterNERDtive.Edna.Edsm
if (response.MsgNum == 203)
{
throw new ArgumentException($"Cmdr “{name}” not found{(apiKey == null ? string.Empty : " and/or invalid API key")}.");
throw new ArgumentException($"Cmdr not found{(apiKey == null ? string.Empty : " and/or invalid API key")}.", name);
}
else if (response.MsgNum == 100 && response.System == null && response.FirstDiscover == null && response.Date == null)
{

View file

@ -17,8 +17,6 @@
// along with EDNA. If not, see &lt;https://www.gnu.org/licenses/&gt;.
// </copyright>
#nullable enable
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

View file

@ -1,12 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net48;net5.0</TargetFrameworks>
<TargetFrameworks>net48;net6.0</TargetFrameworks>
<nullable>enable</nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
<PackageReference Include="RestSharp" Version="107.2.0" />
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.8" />
<PackageReference Include="RestSharp" Version="107.3.0" />
</ItemGroup>
<ItemGroup>

View file

@ -17,8 +17,6 @@
// along with EDNA. If not, see &lt;https://www.gnu.org/licenses/&gt;.
// </copyright>
#nullable enable
using System;
using System.Threading.Tasks;

View file

@ -1,11 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net48;net5.0</TargetFrameworks>
<TargetFrameworks>net48;net6.0</TargetFrameworks>
<nullable>enable</nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.8" />
</ItemGroup>
<ItemGroup>

View file

@ -17,8 +17,6 @@
// along with EDNA. If not, see &lt;https://www.gnu.org/licenses/&gt;.
// </copyright>
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;

View file

@ -0,0 +1,65 @@
// <copyright file="CommanderTest.cs" company="alterNERDtive">
// Copyright 20212022 alterNERDtive.
//
// This file is part of EDNA.
//
// EDNA is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// EDNA 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with EDNA. If not, see &lt;https://www.gnu.org/licenses/&gt;.
// </copyright>
using System;
using alterNERDtive.Edna;
using Xunit;
namespace Test.Edna
{
/// <summary>
/// Tests getting CMDR data correctly from the EDSM API.
/// </summary>
public class CommanderTest
{
/// <summary>
/// Tests getting data correctly for a known CMDR.
/// </summary>
[Fact]
public void KnownCmdrTest()
{
Assert.Throws<NotImplementedException>(() => Commander.Find("IHaveFuelYouDont", "sometext"));
Commander cmdr = Commander.Find("IHaveFuelYouDont");
Assert.Equal(expected: new Coordinates(x: 25.40625, y: -31.0625, z: 41.625), actual: cmdr.Coordinates);
Assert.Equal(expected: "https://www.edsm.net/en/user/profile/id/86423/cmdr/IHaveFuelYouDont", actual: cmdr.EdsmProfileUrl);
Assert.IsType<DateTime>(cmdr.LastActiveAt);
Assert.Equal(expected: "IHaveFuelYouDont", actual: cmdr.Name);
Assert.Equal(expected: "Dromi", actual: cmdr.StarSystem!.Name);
}
/// <summary>
/// Tests correctly getting a CmdrHiddenException for hidden CMDRs.
/// </summary>
[Fact]
public void HiddenCmdrTest()
{
Assert.Throws<CommanderHiddenException>(() => Commander.Find("Hojothefool"));
}
/// <summary>
/// Tests correctly getting a CmdrNotFoundException for nonexistent CMDRs.
/// </summary>
[Fact]
public void NonexistentCmdrTest()
{
Assert.Throws<CommanderNotFoundException>(() => Commander.Find("IHaveFuelYouDoButDontExistLOL"));
}
}
}

View file

@ -17,8 +17,6 @@
// along with EDNA. If not, see &lt;https://www.gnu.org/licenses/&gt;.
// </copyright>
#nullable enable
using System;
using alterNERDtive.Edna;
using Xunit;

View file

@ -17,8 +17,6 @@
// along with EDNA. If not, see &lt;https://www.gnu.org/licenses/&gt;.
// </copyright>
#nullable enable
using alterNERDtive.Edna;
using Xunit;

View file

@ -17,8 +17,6 @@
// along with EDNA. If not, see &lt;https://www.gnu.org/licenses/&gt;.
// </copyright>
#nullable enable
using alterNERDtive.Edna;
using Xunit;

View file

@ -17,17 +17,74 @@
// along with EDNA. If not, see &lt;https://www.gnu.org/licenses/&gt;.
// </copyright>
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using alterNERDtive.Edna.Edsm;
using Xunit;
namespace Test.Edsm
{
class LogsApiTest
/// <summary>
/// Tests correctly getting data from EDSMs Log API.
/// </summary>
public class LogsApiTest
{
/// <summary>
/// Tests getting data correctly for a known CMDR.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task KnownCmdrTestAsync()
{
ApiCmdr cmdr = await LogsApi.FindCmdr("IHaveFuelYouDont");
Assert.Equal(expected: 100, actual: cmdr.MsgNum);
Assert.Equal(expected: "OK", actual: cmdr.Msg);
Assert.Equal(expected: "Dromi", actual: cmdr.System);
Assert.Equal(expected: false, actual: cmdr.FirstDiscover);
_ = DateTime.Parse(cmdr.Date);
Assert.Equal(expected: 38324688UL, actual: cmdr.SystemId);
Assert.Equal(expected: 1213084977515UL, actual: cmdr.SystemId64);
Assert.Equal(expected: new Coordinates { X = 25.40625, Y = -31.0625, Z = 41.625 }, actual: cmdr.Coordinates);
Assert.IsType<bool>(cmdr.IsDocked);
Assert.IsType<int>(cmdr.ShipId);
Assert.Equal(expected: "Anaconda", actual: cmdr.ShipType);
Assert.Null(cmdr.ShipFuel);
_ = DateTime.Parse(cmdr.DateLastActivity);
Assert.Equal(expected: "https://www.edsm.net/en/user/profile/id/86423/cmdr/IHaveFuelYouDont", actual: cmdr.Url);
}
/// <summary>
/// Tests correctly getting an ArgumentException for wrong API key.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task WrongApiKeyTestAsync()
{
await Assert.ThrowsAsync<ArgumentException>(() => LogsApi.FindCmdr("IHaveFuelYouDont", "sometext"));
}
/// <summary>
/// Tests correctly getting an AccessViolationException for hidden CMDRs.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task HiddenCmdrTestAsync()
{
await Assert.ThrowsAsync<AccessViolationException>(() => LogsApi.FindCmdr("Hojothefool"));
}
/// <summary>
/// Tests correctly getting an Argumentexception for nonexistent CMDRs.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
[Fact]
public async Task NonexistentCmdrTestAsync()
{
await Assert.ThrowsAsync<ArgumentException>(() => LogsApi.FindCmdr("IHaveFuelYouDoButDontExistLOL"));
}
}
}

View file

@ -17,8 +17,6 @@
// along with EDNA. If not, see &lt;https://www.gnu.org/licenses/&gt;.
// </copyright>
#nullable enable
#pragma warning disable SA1615 // Element return value should be documented
using System;

View file

@ -20,8 +20,6 @@
#pragma warning disable SA1615 // Element return value should be documented
#pragma warning disable SA1201 // Elements should appear in the correct order
#nullable enable
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

View file

@ -1,16 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net48;net5.0</TargetFrameworks>
<TargetFrameworks>net48;net6.0</TargetFrameworks>
<nullable>enable</nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.8" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>