Skip to content
This repository has been archived by the owner on May 8, 2023. It is now read-only.

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
x-platformcoder committed Nov 17, 2022
1 parent d2bd09c commit d199fd5
Show file tree
Hide file tree
Showing 14 changed files with 316 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public async Task<HttpResponseData> RunAsync(

if (AuthenticatedMatchesSenderId(inboundMessage) == false)
{
return _httpResponseBuilder.CreateBadRequestB2BSenderIsNotAuthorizedResponse(request);
var errorMessage = B2BErrorMessageFactory.CreateSenderNotAuthorizedErrorMessage().WriteAsXmlString();
return _httpResponseBuilder.CreateBadRequestB2BResponse(request, errorMessage);
}

await _chargeLinksCommandBundleHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ public async Task<HttpResponseData> RunAsync(

if (!AuthenticatedMatchesSenderId(inboundMessage))
{
return _httpResponseBuilder.CreateBadRequestB2BSenderIsNotAuthorizedResponse(request);
var errorMessage = B2BErrorMessageFactory.CreateSenderNotAuthorizedErrorMessage().WriteAsXmlString();
return _httpResponseBuilder.CreateBadRequestB2BResponse(request, errorMessage);
}

var bundle = inboundMessage.ValidatedMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,11 @@ public async Task<HttpResponseData> CreateBadRequestResponseAsync(
return httpResponse;
}

public HttpResponseData CreateBadRequestB2BSenderIsNotAuthorizedResponse(HttpRequestData request)
{
var httpResponse = request.CreateResponse(HttpStatusCode.BadRequest);
AddCorrelationIdToHeaders(httpResponse);
var errorMessage = B2BErrorMessageFactory.CreateSenderNotAuthorizedErrorMessage();
var unauthorizedRequest = errorMessage.WriteAsXmlString();
httpResponse.WriteString(unauthorizedRequest, Encoding.UTF8);
return httpResponse;
}

public HttpResponseData CreateBadRequestB2BResponse(HttpRequestData request, string errorMessageAsXml)
{
var httpResponse = request.CreateResponse(HttpStatusCode.BadRequest);
AddCorrelationIdToHeaders(httpResponse);
var unauthorizedRequest = errorMessageAsXml;
httpResponse.WriteString(unauthorizedRequest, Encoding.UTF8);
httpResponse.WriteString(errorMessageAsXml, Encoding.UTF8);
return httpResponse;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,6 @@ public interface IHttpResponseBuilder
/// <returns>A <see cref="Task{TResult}"/> with 400 Bad request response data.</returns>
Task<HttpResponseData> CreateBadRequestResponseAsync(HttpRequestData request, ErrorResponse errorResponse);

/// <summary>
/// Creates a bad request response asynchronously
/// </summary>
/// <param name="request"></param>
/// <returns>400 B2B bad request response data</returns>
HttpResponseData CreateBadRequestB2BSenderIsNotAuthorizedResponse(HttpRequestData request);

/// <summary>
/// Creates a bad request response asynchronously, with error details
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
using GreenEnergyHub.Charges.IntegrationTest.Core.TestFiles.Charges;
using GreenEnergyHub.Charges.IntegrationTest.Core.TestHelpers;
using GreenEnergyHub.Charges.IntegrationTests.Fixtures;
using GreenEnergyHub.Charges.TestCore.TestHelpers;
using Xunit;
using Xunit.Abstractions;
using Xunit.Categories;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

namespace GreenEnergyHub.Charges.IntegrationTest.Core.TestHelpers
namespace GreenEnergyHub.Charges.TestCore.TestHelpers
{
public static class ErrorMessageConstants
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ limitations under the License.
<ProjectReference Include="..\GreenEnergyHub.Charges.Domain\GreenEnergyHub.Charges.Domain.csproj" />
<ProjectReference Include="..\GreenEnergyHub.Charges.FunctionHost\GreenEnergyHub.Charges.FunctionHost.csproj" />
<ProjectReference Include="..\GreenEnergyHub.Charges.Infrastructure\GreenEnergyHub.Charges.Infrastructure.csproj" />
<ProjectReference Include="..\GreenEnergyHub.Charges.IntegrationTest.Core\GreenEnergyHub.Charges.IntegrationTest.Core.csproj" />
<ProjectReference Include="..\GreenEnergyHub.Charges.TestCore\GreenEnergyHub.Charges.TestCore.csproj" />
<ProjectReference Include="..\GreenEnergyHub.Charges.WebApi\GreenEnergyHub.Charges.WebApi.csproj" />
</ItemGroup>
Expand Down Expand Up @@ -74,9 +73,18 @@ limitations under the License.
</ItemGroup>

<ItemGroup>
<Content Include="TestFiles\Invalid_CIM_Charge_EmptyId.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestFiles\Invalid_CIM_Charge_UnsupportedBusinessReasonCode.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestFiles\TariffPriceSeriesBundle.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestFiles\Invalid_CIM_ChargePrice_UnsupportedPriceResolution.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestFiles\Valid_CIM_ChargeLink.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

using FluentAssertions;
using GreenEnergyHub.Charges.Infrastructure.Core.Function;
using GreenEnergyHub.Charges.IntegrationTest.Core.TestHelpers;
using GreenEnergyHub.Charges.TestCore.TestHelpers;
using Xunit;
using Xunit.Categories;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ public void CreateBadRequestResponseWithSenderIsNotAuthorized_WhenBadRequest_Cre
{
// Arrange
const string expectedCode = B2BErrorCodeConstants.SenderIsNotAuthorized;
const string expectedMessage = "The sender organization provided in the request body does not match the organization in the bearer token.";
var correlationContext = CorrelationContextGenerator.Create();
var sut = new HttpResponseBuilder(correlationContext);
var httpRequestData = CreateHttpRequestData(executionContext, "POST", "test", "http://localhost?Id=3");
var message = B2BErrorMessageFactory.CreateSenderNotAuthorizedErrorMessage().WriteAsXmlString();

// Act
var responseData = sut.CreateBadRequestB2BSenderIsNotAuthorizedResponse(httpRequestData);
var responseData = sut.CreateBadRequestB2BResponse(httpRequestData, message);

// Assert
const string correlationIdKey = HttpRequestHeaderConstants.CorrelationId;
Expand All @@ -103,7 +103,7 @@ public void CreateBadRequestResponseWithSenderIsNotAuthorized_WhenBadRequest_Cre
responseData.Body.Position = 0;
var xmlMessage = XDocument.Load(responseData.Body);
xmlMessage.Element("Code")?.Value.Should().Be(expectedCode);
xmlMessage.Element("Message")?.Value.Should().Be(expectedMessage);
xmlMessage.Element("Message")?.Value.Should().Be(message);
}

[Theory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using GreenEnergyHub.Charges.Domain.Dtos.ChargePriceCommands;
using GreenEnergyHub.Charges.Domain.Dtos.SharedDtos;
using GreenEnergyHub.Charges.Infrastructure.CimDeserialization.ChargeBundle;
using GreenEnergyHub.Charges.Infrastructure.CimDeserialization.MarketDocument;
using GreenEnergyHub.Charges.TestCore.Attributes;
using GreenEnergyHub.Charges.TestCore.TestHelpers;
using GreenEnergyHub.Iso8601;
Expand Down Expand Up @@ -328,6 +329,60 @@ public async Task ConvertAsync_WhenCalledWithMixedChargePriceBundle_ReturnsGroup
chargeCommandWithTwoOperations.Operations.Should().Contain(x => x.OperationId == "Operation4");
}

[Theory]
[InlineAutoMoqData]
public async Task ConvertAsync_WhenCalledWithInvalidResolution_ThrowsAnInvalidXmlException(
[Frozen] Mock<ICorrelationContext> context,
[Frozen] Mock<IIso8601Durations> iso8601Durations,
ChargeCommandBundleConverter sut)
{
// Arrange
var correlationId = Guid.NewGuid().ToString();
var expectedTime = InstantPattern.ExtendedIso.Parse("2022-10-31T23:00:00Z").Value;
SetupTest(context, iso8601Durations, correlationId, expectedTime);
using var memoryStream = new MemoryStream();
var reader = GetReader(memoryStream, "TestFiles/Invalid_CIM_ChargePrice_UnsupportedPriceResolution.xml");

// Act + Assert
await Assert.ThrowsAsync<InvalidXmlValueException>(() => sut.ConvertAsync(reader));
}

[Theory]
[InlineAutoMoqData]
public async Task ConvertAsync_WhenCalledWithInvalidBusinessReasonCode_ThrowsAnInvalidXmlException(
[Frozen] Mock<ICorrelationContext> context,
[Frozen] Mock<IIso8601Durations> iso8601Durations,
ChargeCommandBundleConverter sut)
{
// Arrange
var correlationId = Guid.NewGuid().ToString();
var expectedTime = InstantPattern.ExtendedIso.Parse("2022-10-31T23:00:00Z").Value;
SetupTest(context, iso8601Durations, correlationId, expectedTime);
using var memoryStream = new MemoryStream();
var reader = GetReader(memoryStream, "TestFiles/Invalid_CIM_Charge_UnsupportedBusinessReasonCode.xml");

// Act + Assert
await Assert.ThrowsAsync<InvalidXmlValueException>(() => sut.ConvertAsync(reader));
}

[Theory]
[InlineAutoMoqData]
public async Task ConvertAsync_WhenCalledWithEmptyId_ThrowsAnInvalidXmlException(
[Frozen] Mock<ICorrelationContext> context,
[Frozen] Mock<IIso8601Durations> iso8601Durations,
ChargeCommandBundleConverter sut)
{
// Arrange
var correlationId = Guid.NewGuid().ToString();
var expectedTime = InstantPattern.ExtendedIso.Parse("2022-10-31T23:00:00Z").Value;
SetupTest(context, iso8601Durations, correlationId, expectedTime);
using var memoryStream = new MemoryStream();
var reader = GetReader(memoryStream, "TestFiles/Invalid_CIM_Charge_EmptyId.xml");

// Act + Assert
await Assert.ThrowsAsync<InvalidXmlValueException>(() => sut.ConvertAsync(reader));
}

private static SchemaValidatingReader GetReader(Stream memoryStream, string filePath)
{
var path = GetFullFilePath(filePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using FluentAssertions;
using GreenEnergyHub.Charges.Infrastructure.CimDeserialization.MarketDocument;
using GreenEnergyHub.Charges.Infrastructure.Core.Function;
Expand All @@ -32,16 +31,12 @@ public void InvalidXmlValueException_WhenConstructorIsCalledWithEmptyContent_The
const string errorContent = "";
var expectedErrorMessage = B2BErrorMessageFactory.CreateIsEmptyOrWhitespaceErrorMessage(errorIdentifier);

// Act & Assert
try
{
throw new InvalidXmlValueException(B2BErrorCode.IsEmptyOrWhitespaceErrorMessage, errorIdentifier, errorContent);
}
catch (Exception exception)
{
exception.Should().BeOfType<InvalidXmlValueException>();
exception.Message.Should().Be(expectedErrorMessage.WriteAsXmlString());
}
// Act
var exception = new InvalidXmlValueException(B2BErrorCode.IsEmptyOrWhitespaceErrorMessage, errorIdentifier, errorContent);

// Assert
exception.Should().BeOfType<InvalidXmlValueException>();
exception.Message.Should().Be(expectedErrorMessage.WriteAsXmlString());
}

[Fact]
Expand All @@ -52,16 +47,12 @@ public void InvalidXmlValueException_WhenConstructorIsCalledWithInvalidEnum_Then
const string errorContent = "D17";
var expectedErrorMessage = B2BErrorMessageFactory.CreateCouldNotMapEnumErrorMessage(errorIdentifier, errorContent);

// Act & Assert
try
{
throw new InvalidXmlValueException(B2BErrorCode.CouldNotMapEnumErrorMessage, errorIdentifier, errorContent);
}
catch (Exception exception)
{
exception.Should().BeOfType<InvalidXmlValueException>();
exception.Message.Should().Be(expectedErrorMessage.WriteAsXmlString());
}
// Act
var exception = new InvalidXmlValueException(B2BErrorCode.CouldNotMapEnumErrorMessage, errorIdentifier, errorContent);

// Assert
exception.Should().BeOfType<InvalidXmlValueException>();
exception.Message.Should().Be(expectedErrorMessage.WriteAsXmlString());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2020 Energinet DataHub A/S
Licensed under the Apache License, Version 2.0 (the "License2");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<cim:RequestChangeOfPriceList_MarketDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cim="urn:ediel.org:structure:requestchangeofpricelist:0:1" xsi:schemaLocation="urn:ediel.org:structure:requestchangeofpricelist:0:1 urn-ediel-org-structure-requestchangeofpricelist-0-1.xsd">
<cim:mRID>12345678</cim:mRID>
<cim:type>D10</cim:type>
<cim:process.processType>D08</cim:process.processType>
<cim:businessSector.type>23</cim:businessSector.type>
<cim:sender_MarketParticipant.mRID codingScheme="A10">8100000000030</cim:sender_MarketParticipant.mRID>
<cim:sender_MarketParticipant.marketRole.type>EZ</cim:sender_MarketParticipant.marketRole.type>
<cim:receiver_MarketParticipant.mRID codingScheme="A10">5790001330552</cim:receiver_MarketParticipant.mRID>
<cim:receiver_MarketParticipant.marketRole.type>DDZ</cim:receiver_MarketParticipant.marketRole.type>
<cim:createdDateTime>2022-04-06T09:30:47Z</cim:createdDateTime>
<cim:MktActivityRecord>
<cim:mRID>Operation1</cim:mRID>
<cim:ChargeGroup>
<cim:ChargeType>
<cim:chargeTypeOwner_MarketParticipant.mRID codingScheme="A10">8100000000030</cim:chargeTypeOwner_MarketParticipant.mRID>
<cim:type>D03</cim:type>
<cim:mRID>EA-001</cim:mRID>
<cim:priceTimeFrame_Period.resolution>PT1H</cim:priceTimeFrame_Period.resolution>
<cim:effectiveDate>2022-05-31T23:00:00Z</cim:effectiveDate>
<cim:Series_Period>
<cim:resolution>P1Y</cim:resolution>
<cim:timeInterval>
<cim:start>2022-05-31T23:00Z</cim:start>
<cim:end>2022-0601T22:00Z</cim:end>
</cim:timeInterval>
<cim:Point>
<cim:position>1</cim:position>
<cim:price.amount>1.001</cim:price.amount>
</cim:Point>
<cim:Point>
<cim:position>2</cim:position>
<cim:price.amount>2.002</cim:price.amount>
</cim:Point>
<cim:Point>
<cim:position>3</cim:position>
<cim:price.amount>3.003</cim:price.amount>
</cim:Point>
<cim:Point>
<cim:position>4</cim:position>
<cim:price.amount>4.004</cim:price.amount>
</cim:Point>
<cim:Point>
<cim:position>5</cim:position>
<cim:price.amount>5.005</cim:price.amount>
</cim:Point>
<cim:Point>
<cim:position>6</cim:position>
<cim:price.amount>6.006</cim:price.amount>
</cim:Point>
<cim:Point>
<cim:position>7</cim:position>
<cim:price.amount>7.007</cim:price.amount>
</cim:Point>
<cim:Point>
<cim:position>8</cim:position>
<cim:price.amount>8.008</cim:price.amount>
</cim:Point>
<cim:Point>
<cim:position>9</cim:position>
<cim:price.amount>9.009</cim:price.amount>
</cim:Point>
<cim:Point>
<cim:position>10</cim:position>
<cim:price.amount>10.010</cim:price.amount>
</cim:Point>
<cim:Point>
<cim:position>11</cim:position>
<cim:price.amount>11.011</cim:price.amount>
</cim:Point>
<cim:Point>
<cim:position>12</cim:position>
<cim:price.amount>12.012</cim:price.amount>
</cim:Point>
<cim:Point>
<cim:position>13</cim:position>
<cim:price.amount>13.013</cim:price.amount>
</cim:Point>
<cim:Point>
<cim:position>14</cim:position>
<cim:price.amount>14.014</cim:price.amount>
</cim:Point>
<cim:Point>
<cim:position>15</cim:position>
<cim:price.amount>15.015</cim:price.amount>
</cim:Point>
<cim:Point>
<cim:position>16</cim:position>
<cim:price.amount>16.016</cim:price.amount>
</cim:Point>
<cim:Point>
<cim:position>17</cim:position>
<cim:price.amount>17.017</cim:price.amount>
</cim:Point>
<cim:Point>
<cim:position>18</cim:position>
<cim:price.amount>18.018</cim:price.amount>
</cim:Point>
<cim:Point>
<cim:position>19</cim:position>
<cim:price.amount>19.019</cim:price.amount>
</cim:Point>
<cim:Point>
<cim:position>20</cim:position>
<cim:price.amount>20.020</cim:price.amount>
</cim:Point>
<cim:Point>
<cim:position>21</cim:position>
<cim:price.amount>21.021</cim:price.amount>
</cim:Point>
<cim:Point>
<cim:position>22</cim:position>
<cim:price.amount>22.022</cim:price.amount>
</cim:Point>
<cim:Point>
<cim:position>23</cim:position>
<cim:price.amount>23.023</cim:price.amount>
</cim:Point>
<cim:Point>
<cim:position>24</cim:position>
<cim:price.amount>24.024</cim:price.amount>
</cim:Point>
</cim:Series_Period>
</cim:ChargeType>
</cim:ChargeGroup>
</cim:MktActivityRecord>
</cim:RequestChangeOfPriceList_MarketDocument>
Loading

0 comments on commit d199fd5

Please sign in to comment.