Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Refactor and Review #34

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
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
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using DFC.Common.Standard.GuidHelper;
using DFC.HTTP.Standard;
using DFC.HTTP.Standard;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Moq;
using NCS.DSS.Subscriptions.Cosmos.Helper;
using NCS.DSS.Subscriptions.GetSubscriptionsForTouchpointHttpTrigger.Service;
using System.Net;
using GetSubscriptionsForTouchpointHttpTriggerrRun = NCS.DSS.Subscriptions.GetSubscriptionsForTouchpointHttpTrigger.Function.GetSubscriptionsForTouchpointHttpTrigger;
Expand All @@ -19,27 +17,24 @@ public class GetSubscriptionsForTouchpointHttpTriggerTests
private readonly Guid _customerId = Guid.Parse("1dd4d206-131a-44fd-8e2d-18b88b383f72");
private const string _touchPointId = "0000000001";
private HttpRequest _request;
private Mock<IResourceHelper> _resourceHelper;
private Mock<IGetSubscriptionsForTouchpointHttpTriggerService> _getSubscriptionsForTouchpointHttpTriggerService;
private Mock<IHttpRequestHelper> _httpRequestHelper;
private List<Models.Subscriptions> _subscriptions;
private IGuidHelper _guidHelper;
private GetSubscriptionsForTouchpointHttpTriggerrRun _getSubscriptionsForTouchpointHttpTrigger;
[SetUp]
public void Setup()
{
_subscriptions = new List<Models.Subscriptions>();
_subscriptions = [
new Models.Subscriptions()
];
_request = (new DefaultHttpContext()).Request;
_resourceHelper = new Mock<IResourceHelper>();
_httpRequestHelper = new Mock<IHttpRequestHelper>();
var loggerHelper = new Mock<ILogger<GetSubscriptionsForTouchpointHttpTriggerrRun>>();
_guidHelper = new GuidHelper();
var logger = new Mock<ILogger<GetSubscriptionsForTouchpointHttpTriggerrRun>>();
_getSubscriptionsForTouchpointHttpTriggerService = new Mock<IGetSubscriptionsForTouchpointHttpTriggerService>();
_getSubscriptionsForTouchpointHttpTrigger = new GetSubscriptionsForTouchpointHttpTriggerrRun(
_resourceHelper.Object,
_httpRequestHelper.Object,
_getSubscriptionsForTouchpointHttpTriggerService.Object,
loggerHelper.Object
logger.Object
);

}
Expand All @@ -54,7 +49,7 @@ public async Task GetSubscriptionsForTouchpointHttpTrigger_ReturnsStatusCodeBadR
var result = await RunFunction(ValidCustomerId);

// Assert
Assert.That(result, Is.InstanceOf<BadRequestResult>());
Assert.That(result, Is.InstanceOf<BadRequestObjectResult>());
}

[Test]
Expand All @@ -74,8 +69,8 @@ public async Task GetSubscriptionsForTouchpointHttpTrigger_ReturnsStatusCodeNoCo
{
// Arrange
_httpRequestHelper.Setup(x => x.GetDssTouchpointId(_request)).Returns(_touchPointId);
_resourceHelper.Setup(x => x.DoesCustomerExist(It.IsAny<Guid>())).Returns(Task.FromResult(true));
_getSubscriptionsForTouchpointHttpTriggerService.Setup(x => x.GetSubscriptionsForTouchpointAsync(_customerId, _touchPointId)).Returns(Task.FromResult<List<Models.Subscriptions>>(null));
_getSubscriptionsForTouchpointHttpTriggerService.Setup(x => x.DoesCustomerExist(It.IsAny<Guid>())).Returns(Task.FromResult(true));
_getSubscriptionsForTouchpointHttpTriggerService.Setup(x => x.GetSubscriptionsForTouchpointAsync(_customerId, _touchPointId)).Returns(Task.FromResult<List<Models.Subscriptions>>(_subscriptions));

// Act
var result = await RunFunction(ValidCustomerId);
Expand All @@ -88,7 +83,7 @@ public async Task GetSubscriptionsForTouchpointHttpTrigger_ReturnsStatusCodeOk_W
{
// Arrange
_httpRequestHelper.Setup(x => x.GetDssTouchpointId(_request)).Returns(_touchPointId);
_resourceHelper.Setup(x => x.DoesCustomerExist(It.IsAny<Guid>())).Returns(Task.FromResult(true));
_getSubscriptionsForTouchpointHttpTriggerService.Setup(x => x.DoesCustomerExist(It.IsAny<Guid>())).Returns(Task.FromResult(true));
_getSubscriptionsForTouchpointHttpTriggerService.Setup(x => x.GetSubscriptionsForTouchpointAsync(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult(_subscriptions));

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Moq;
using NCS.DSS.Subscriptions.Cosmos.Helper;
using NCS.DSS.Subscriptions.Helpers;
using NCS.DSS.Subscriptions.Models;
using NCS.DSS.Subscriptions.PatchSubscriptionsHttpTrigger.Service;
Expand All @@ -27,11 +26,10 @@ public class PatchSubscriptionsHttpTriggerTests
// local variables
private HttpRequest _request;
private Mock<IValidate> _validate;
private Mock<IResourceHelper> _resourceHelper;
private Mock<IConvertToDynamic> _convertToDynamic;
private Mock<IPatchSubscriptionsHttpTriggerService> _patchSubscriptionsHttpTriggerService;
private Mock<IHttpRequestHelper> _httpRequestHelper;
private Models.Subscriptions _subscriptions;
private Models.Subscriptions? _subscriptions;
private SubscriptionsPatch _subscriptionsPatch;
private PatchSubscriptionsHttpTriggerRun _patchSubscriptionsHttpTriggerRun;
private List<ValidationResult> _validationResults;
Expand All @@ -43,18 +41,16 @@ public void Setup()
_request = (new DefaultHttpContext()).Request;
_validate = new Mock<IValidate>();
_validationResults = new List<ValidationResult> { };
_resourceHelper = new Mock<IResourceHelper>();
_convertToDynamic = new Mock<IConvertToDynamic>();
_httpRequestHelper = new Mock<IHttpRequestHelper>();
var loggerHelper = new Mock<ILogger<PatchSubscriptionsHttpTriggerRun>>();
var logger = new Mock<ILogger<PatchSubscriptionsHttpTriggerRun>>();
_patchSubscriptionsHttpTriggerService = new Mock<IPatchSubscriptionsHttpTriggerService>();
_patchSubscriptionsHttpTriggerRun = new PatchSubscriptionsHttpTriggerRun(
_resourceHelper.Object,
_httpRequestHelper.Object,
_validate.Object,
_patchSubscriptionsHttpTriggerService.Object,
_convertToDynamic.Object,
loggerHelper.Object
logger.Object
);

}
Expand All @@ -69,7 +65,7 @@ public async Task PatchSubscriptionsHttpTrigger_ReturnsStatusCodeBadRequest_When
var result = await RunFunction(ValidCustomerId, ValidSubscriptionId);

// Assert
Assert.That(result, Is.InstanceOf<BadRequestResult>());
Assert.That(result, Is.InstanceOf<BadRequestObjectResult>());
}

[Test]
Expand Down Expand Up @@ -144,7 +140,7 @@ public async Task PatchSubscriptionsHttpTrigger_ReturnsStatusCodeNoContent_WhenC
_validationResults.Clear();
_validate.Setup(x => x.ValidateResource(It.IsAny<ISubscription>())).Returns(_validationResults);

_resourceHelper.Setup(x => x.DoesCustomerExist(It.IsAny<Guid>())).Returns(Task.FromResult(false));
_patchSubscriptionsHttpTriggerService.Setup(x => x.DoesCustomerExist(It.IsAny<Guid>())).Returns(Task.FromResult(false));

// Act
var result = await RunFunction(ValidCustomerId, ValidSubscriptionId);
Expand All @@ -165,9 +161,9 @@ public async Task PatchSubscriptionsHttpTrigger_ReturnsStatusCodeNoContent_WhenS
_validationResults.Clear();
_validate.Setup(x => x.ValidateResource(It.IsAny<ISubscription>())).Returns(_validationResults);

_resourceHelper.Setup(x => x.DoesCustomerExist(It.IsAny<Guid>())).Returns(Task.FromResult(true));

_patchSubscriptionsHttpTriggerService.Setup(x => x.GetSubscriptionsForCustomerAsync(It.IsAny<Guid>(), It.IsAny<Guid>())).Returns(Task.FromResult<Models.Subscriptions>(null));
_patchSubscriptionsHttpTriggerService.Setup(x => x.DoesCustomerExist(It.IsAny<Guid>())).Returns(Task.FromResult(true));
_subscriptions = null;
_patchSubscriptionsHttpTriggerService.Setup(x => x.GetSubscriptionsForCustomerAsync(It.IsAny<Guid>(), It.IsAny<Guid>())).Returns(Task.FromResult(_subscriptions));
// Act
var result = await RunFunction(ValidCustomerId, ValidSubscriptionId);

Expand All @@ -187,7 +183,7 @@ public async Task PatchSubscriptionsHttpTrigger_ReturnsStatusCodeOk_WhenRequestI
_validationResults.Clear();
_validate.Setup(x => x.ValidateResource(It.IsAny<ISubscription>())).Returns(_validationResults);

_resourceHelper.Setup(x => x.DoesCustomerExist(It.IsAny<Guid>())).Returns(Task.FromResult(true));
_patchSubscriptionsHttpTriggerService.Setup(x => x.DoesCustomerExist(It.IsAny<Guid>())).Returns(Task.FromResult(true));

_patchSubscriptionsHttpTriggerService.Setup(x => x.GetSubscriptionsForCustomerAsync(It.IsAny<Guid>(), It.IsAny<Guid>())).Returns(Task.FromResult(_subscriptions));
_patchSubscriptionsHttpTriggerService.Setup(x => x.UpdateAsync(_subscriptions, _subscriptionsPatch)).Returns(Task.FromResult(_subscriptions));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Moq;
using NCS.DSS.Subscriptions.Cosmos.Helper;
using NCS.DSS.Subscriptions.Helpers;
using NCS.DSS.Subscriptions.Models;
using NCS.DSS.Subscriptions.PostSubscriptionsHttpTrigger.Service;
Expand All @@ -29,7 +28,6 @@ public class PostSubscriptionsHttpTriggerTests
private HttpRequest _request;
private Mock<IValidate> _validate;
private List<ValidationResult> _validationResults;
private Mock<IResourceHelper> _resourceHelper;
private Mock<IConvertToDynamic> _convertToDynamic;
private Mock<IPostSubscriptionsHttpTriggerService> _postSubscriptionsHttpTriggerService;
private Mock<IHttpRequestHelper> _httpRequestHelper;
Expand All @@ -42,17 +40,15 @@ public void Setup()
_request = (new DefaultHttpContext()).Request;
_validate = new Mock<IValidate>();
_validationResults = new List<ValidationResult> { };
_resourceHelper = new Mock<IResourceHelper>();
_convertToDynamic = new Mock<IConvertToDynamic>();
_httpRequestHelper = new Mock<IHttpRequestHelper>();
var loggerHelper = new Mock<ILogger<PostSubscriptionsHttpTriggerRun>>();
var logger = new Mock<ILogger<PostSubscriptionsHttpTriggerRun>>();
_postSubscriptionsHttpTriggerService = new Mock<IPostSubscriptionsHttpTriggerService>();
_postSubscriptionsHttpTriggerRun = new PostSubscriptionsHttpTriggerRun(
_resourceHelper.Object,
_httpRequestHelper.Object,
_validate.Object,
_postSubscriptionsHttpTriggerService.Object,
loggerHelper.Object,
logger.Object,
_convertToDynamic.Object
);

Expand Down Expand Up @@ -130,7 +126,7 @@ public async Task PostSubscriptionsHttpTrigger_ReturnsStatusCodeNoContent_WhenCu
_validationResults.Clear();
_validate.Setup(x => x.ValidateResource(It.IsAny<ISubscription>())).Returns(_validationResults);

_resourceHelper.Setup(x => x.DoesCustomerExist(It.IsAny<Guid>())).Returns(Task.FromResult(false));
_postSubscriptionsHttpTriggerService.Setup(x => x.DoesCustomerExist(It.IsAny<Guid>())).Returns(Task.FromResult(false));

// Act
var result = await RunFunction(ValidCustomerId);
Expand All @@ -149,8 +145,8 @@ public async Task PostSubscriptionsHttpTrigger_ReturnsStatusCodeConflict_WhenSub
_validationResults.Clear();
_validate.Setup(x => x.ValidateResource(It.IsAny<ISubscription>())).Returns(_validationResults);

_resourceHelper.Setup(x => x.DoesCustomerExist(It.IsAny<Guid>())).Returns(Task.FromResult(true));
_resourceHelper.Setup(x => x.DoesSubscriptionExist(It.IsAny<Guid>(), It.IsAny<string>())).Returns((Task.FromResult(_subscriptionId)));
_postSubscriptionsHttpTriggerService.Setup(x => x.DoesCustomerExist(It.IsAny<Guid>())).Returns(Task.FromResult(true));
_postSubscriptionsHttpTriggerService.Setup(x => x.DoesSubscriptionExist(It.IsAny<Guid>(), It.IsAny<string>())).Returns((Task.FromResult(_subscriptionId)));
_validate.Setup(x => x.ValidateResultForDuplicateSubscriptionId(It.IsAny<Guid>())).Returns(await Task.FromResult(new SubscriptionError() { }));

// Act
Expand All @@ -171,8 +167,8 @@ public async Task PostSubscriptionsHttpTrigger_ReturnsStatusCodeBadRequest_WhenS
_validationResults.Clear();
_validate.Setup(x => x.ValidateResource(It.IsAny<ISubscription>())).Returns(_validationResults);

_resourceHelper.Setup(x => x.DoesCustomerExist(It.IsAny<Guid>())).Returns(Task.FromResult(true));
_resourceHelper.Setup(x => x.DoesSubscriptionExist(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult<Guid?>(null));
_postSubscriptionsHttpTriggerService.Setup(x => x.DoesCustomerExist(It.IsAny<Guid>())).Returns(Task.FromResult(true));
_postSubscriptionsHttpTriggerService.Setup(x => x.DoesSubscriptionExist(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult<Guid?>(null));

_postSubscriptionsHttpTriggerService.Setup(x => x.CreateAsync(_subscriptions)).Returns(Task.FromResult<Models.Subscriptions>(null));
// Act
Expand All @@ -194,8 +190,8 @@ public async Task PostSubscriptionsHttpTrigger_ReturnsStatusCodeCreated_WhenRequ
_validationResults.Clear();
_validate.Setup(x => x.ValidateResource(It.IsAny<ISubscription>())).Returns(_validationResults);

_resourceHelper.Setup(x => x.DoesCustomerExist(It.IsAny<Guid>())).Returns(Task.FromResult(true));
_resourceHelper.Setup(x => x.DoesSubscriptionExist(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult<Guid?>(null));
_postSubscriptionsHttpTriggerService.Setup(x => x.DoesCustomerExist(It.IsAny<Guid>())).Returns(Task.FromResult(true));
_postSubscriptionsHttpTriggerService.Setup(x => x.DoesSubscriptionExist(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult<Guid?>(null));

_postSubscriptionsHttpTriggerService.Setup(x => x.CreateAsync(_subscriptions)).Returns(Task.FromResult(_subscriptions));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class SubscriptionModelTests
private Models.Subscriptions _subscriptions;
private readonly Guid _customerId = Guid.Parse("1dd4d206-131a-44fd-8e2d-18b88b383f72");
private readonly Guid? _subscriptionId = Guid.Parse("0f0ce9cd-5f0e-41f7-84e7-6532e01691ae");
private const string _touchPointId = "0000000001";
private string _touchPointId = "0000000001";
[SetUp]
public void Setup()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ namespace NCS.DSS.Subscriptions.Tests.ServiceTests
public class GetSubscriptionsForTouchpointHttpTriggerServiceTests
{
private readonly IGetSubscriptionsForTouchpointHttpTriggerService _getSubscriptionsForTouchpointHttpTriggerService;
private readonly Mock<IDocumentDBProvider> _documentDbProvider;
private readonly Mock<ICosmosDBProvider> _cosmosDbProvider;
private readonly Guid _customerId = Guid.Parse("58b43e3f-4a50-4900-9c82-a14682ee90fa");
private const string _touchPointId = "0000000001";
public GetSubscriptionsForTouchpointHttpTriggerServiceTests()
{
_documentDbProvider = new Mock<IDocumentDBProvider>();
_getSubscriptionsForTouchpointHttpTriggerService = new GetSubscriptionsForTouchpointHttpTriggerService(_documentDbProvider.Object);
_cosmosDbProvider = new Mock<ICosmosDBProvider>();
_getSubscriptionsForTouchpointHttpTriggerService = new GetSubscriptionsForTouchpointHttpTriggerService(_cosmosDbProvider.Object);
}
[Test]
public async Task GetSubscriptionsForTouchpointHttpTriggerServiceTests_GetSubscriptionsForTouchpointAsync_ReturnsNullWhenResourceCannotBeFound()
{
// Arrange
_documentDbProvider.Setup(x => x.GetSubscriptionsForTouchpointAsync(_customerId, _touchPointId)).Returns(Task.FromResult<List<Models.Subscriptions>>(null));

// Act
_cosmosDbProvider.Setup(x => x.GetSubscriptionsForTouchpointAsync(_customerId, _touchPointId)).Returns(Task.FromResult<List<Models.Subscriptions>>(null));
// Act
var result = await _getSubscriptionsForTouchpointHttpTriggerService.GetSubscriptionsForTouchpointAsync(_customerId, _touchPointId);

// Assert
Expand All @@ -32,7 +32,7 @@ public async Task GetSubscriptionsForTouchpointHttpTriggerServiceTests_GetSubscr
public async Task GetSubscriptionsForTouchpointHttpTriggerServiceTests_GetSubscriptionsForTouchpointAsync_ReturnsResource()
{
// Arrange
_documentDbProvider.Setup(x => x.GetSubscriptionsForTouchpointAsync(_customerId, _touchPointId)).Returns(Task.FromResult(new List<Models.Subscriptions>()));
_cosmosDbProvider.Setup(x => x.GetSubscriptionsForTouchpointAsync(_customerId, _touchPointId)).Returns(Task.FromResult(new List<Models.Subscriptions>()));

// Act
var result = await _getSubscriptionsForTouchpointHttpTriggerService.GetSubscriptionsForTouchpointAsync(_customerId, _touchPointId);
Expand Down
Loading