-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
125 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/CarbonAware.WebApi/src/Controllers/LocationsController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using CarbonAware.Interfaces; | ||
using CarbonAware.Model; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace CarbonAware.WebApi.Controllers; | ||
|
||
[ApiController] | ||
[Route("locations")] | ||
public class LocationsController : ControllerBase | ||
{ | ||
private readonly ILogger<CarbonAwareController> _logger; | ||
|
||
private readonly ILocationSource _locationSource; | ||
|
||
|
||
public LocationsController(ILogger<CarbonAwareController> logger, ILocationSource locationSouce) | ||
{ | ||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
_locationSource = locationSouce ?? throw new ArgumentNullException(nameof(locationSouce)); | ||
} | ||
|
||
/// <summary> | ||
/// Get all locations instances | ||
/// </summary> | ||
/// <returns>Dictionary with <see cref="Location"/> instances</returns> | ||
[Produces("application/json")] | ||
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(IDictionary<string, Location>))] | ||
[ProducesResponseType(StatusCodes.Status204NoContent)] | ||
[HttpGet()] | ||
public async Task<IActionResult> GetAllLocations() | ||
{ | ||
var response = await _locationSource.GetGeopositionLocationsAsync(); | ||
return response.Any() ? Ok(response) : NoContent(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/CarbonAware.WebApi/test/integrationTests/LocationsControllerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using CarbonAware.DataSources.Configuration; | ||
using CarbonAware.WebApi.IntegrationTests; | ||
using NUnit.Framework; | ||
using System.Net; | ||
using System.Text.Json; | ||
|
||
namespace CarbonAware.WepApi.IntegrationTests; | ||
|
||
/// <summary> | ||
/// Tests that the Web API controller handles locations instances | ||
/// </summary> | ||
[TestFixture(DataSourceType.JSON)] | ||
[TestFixture(DataSourceType.WattTime)] | ||
[TestFixture(DataSourceType.ElectricityMaps)] | ||
public class LocationsControllerTests : IntegrationTestingBase | ||
{ | ||
private readonly string locationsURI = "/locations"; | ||
|
||
public LocationsControllerTests(DataSourceType dataSource) : base(dataSource) { } | ||
|
||
|
||
[Test] | ||
public async Task GetLocations_ReturnsOk() | ||
{ | ||
var result = await _client.GetAsync(locationsURI); | ||
Assert.That(result, Is.Not.Null); | ||
Assert.That(result.StatusCode, Is.EqualTo(HttpStatusCode.OK)); | ||
Assert.That(result.Content, Is.Not.Null); | ||
using var stream = await result.Content.ReadAsStreamAsync(); | ||
Assert.That(stream, Is.Not.Null); | ||
var data = await JsonSerializer.DeserializeAsync<IDictionary<string, dynamic>>(stream); | ||
Assert.That(data, Is.Not.Null); | ||
Assert.That(data!.ContainsKey("eastus"), Is.True); | ||
Assert.That(data!.ContainsKey("northeurope"), Is.True); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/CarbonAware.WebApi/test/unitTests/LocationsControllerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using CarbonAware.WebApi.Controllers; | ||
using Microsoft.AspNetCore.Mvc; | ||
using NUnit.Framework; | ||
using System.Net; | ||
|
||
namespace CarbonAware.WepApi.UnitTests; | ||
|
||
/// <summary> | ||
/// Tests that the Web API controller handles request for Location instances | ||
/// </summary> | ||
[TestFixture] | ||
public class LocationsControllerTests : TestsBase | ||
{ | ||
/// <summary> | ||
/// Test result with content | ||
/// </summary> | ||
[Test] | ||
public async Task GetLocations_ResultsOk() | ||
{ | ||
var controller = new LocationsController(this.MockCarbonAwareLogger.Object, CreateLocations().Object); | ||
|
||
IActionResult result = await controller.GetAllLocations(); | ||
|
||
//Assert | ||
TestHelpers.AssertStatusCode(result, HttpStatusCode.OK); | ||
} | ||
|
||
/// <summary> | ||
/// Test result without content | ||
/// </summary> | ||
[Test] | ||
public async Task GetLocations_ResultsNoContent() | ||
{ | ||
var controller = new LocationsController(this.MockCarbonAwareLogger.Object, CreateLocations(false).Object); | ||
|
||
IActionResult result = await controller.GetAllLocations(); | ||
|
||
//Assert | ||
TestHelpers.AssertStatusCode(result, HttpStatusCode.NoContent); | ||
} | ||
} |