Skip to content

Commit

Permalink
Add a number of unit tests to test the public endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmed-Ghanam committed Oct 8, 2024
1 parent 104b446 commit 30f5cea
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 13 deletions.
38 changes: 34 additions & 4 deletions src/Altinn.Profile/Controllers/ContactDetailsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,40 @@ public ContactDetailsController(IContactDetailsRetriever contactDetailsRetriever
[ProducesResponseType(typeof(ContactDetailsLookupResult), StatusCodes.Status200OK)]
public async Task<ActionResult<ContactDetailsLookupResult>> PostLookup([FromBody] UserContactPointLookup request)
{
var result = await _contactDetailsRetriever.RetrieveAsync(request);
try
{
if (request == null)
{
return BadRequest();
}

return result.Match<ActionResult<ContactDetailsLookupResult>>(
success => Ok(success),
failure => Problem("Unable to retrieve contact details."));
if (request.NationalIdentityNumbers == null)
{
return BadRequest();
}

if (request.NationalIdentityNumbers.Count == 0)
{
return BadRequest();
}

var result = await _contactDetailsRetriever.RetrieveAsync(request);

return result.Match<ActionResult<ContactDetailsLookupResult>>(
success =>
{
if (success.MatchedContactDetails.Count != 0)
{
return Ok(success);
}
return NotFound();
},
noMatch => NotFound());
}
catch
{
return Problem("An unexpected error occurred.");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading.Tasks;

using Altinn.Profile.Controllers;
using Altinn.Profile.Models;
using Altinn.Profile.UseCases;

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

using Moq;
Expand All @@ -14,8 +17,8 @@ namespace Altinn.Profile.Tests.IntegrationTests.API.Controllers;

public class ContactDetailsControllerTests
{
private readonly Mock<IContactDetailsRetriever> _mockContactDetailsRetriever;
private readonly ContactDetailsController _controller;
private readonly Mock<IContactDetailsRetriever> _mockContactDetailsRetriever;

public ContactDetailsControllerTests()
{
Expand All @@ -24,7 +27,106 @@ public ContactDetailsControllerTests()
}

[Fact]
public async Task PostLookup_ReturnsOkResult_WhenSuccessful()
public async Task PostLookup_ReturnsBadRequestResult_WhenRequestIsInvalid()
{
// Arrange
var invalidRequest = new UserContactPointLookup
{
NationalIdentityNumbers = []
};

// Act
var response = await _controller.PostLookup(invalidRequest);

// Assert
var badRequestResult = Assert.IsType<BadRequestResult>(response.Result);
Assert.Equal(StatusCodes.Status400BadRequest, badRequestResult.StatusCode);
}

[Fact]
public async Task PostLookup_ReturnsInternalServerErrorResult_WhenExceptionOccurs()
{
// Arrange
var request = new UserContactPointLookup
{
NationalIdentityNumbers = ["27038893837"]
};

_mockContactDetailsRetriever.Setup(x => x.RetrieveAsync(request))
.ThrowsAsync(new System.Exception("Test exception"));

// Act
var response = await _controller.PostLookup(request);

// Assert
var problemResult = Assert.IsType<ObjectResult>(response.Result);
Assert.Equal(StatusCodes.Status500InternalServerError, problemResult.StatusCode);
}

[Fact]
public async Task PostLookup_ReturnsMixedResults_WhenOneNumberMatchesAndOneDoesNot()
{
// Arrange
var request = new UserContactPointLookup
{
NationalIdentityNumbers = ["10060339738", "16051327393"]
};

var contactDetails = new ContactDetails
{
LanguageCode = "nb",
Reservation = false,
MobilePhoneNumber = "12345678",
EmailAddress = "test@example.com",
NationalIdentityNumber = "10060339738"
};

var lookupResult = new ContactDetailsLookupResult(
matchedContactDetails: [contactDetails],
unmatchedNationalIdentityNumbers: ["16051327393"]);

_mockContactDetailsRetriever.Setup(x => x.RetrieveAsync(request))
.ReturnsAsync(lookupResult);

// Act
var response = await _controller.PostLookup(request);

// Assert
var result = Assert.IsType<OkObjectResult>(response.Result);
Assert.Equal(StatusCodes.Status200OK, result.StatusCode);

var returnValue = Assert.IsType<ContactDetailsLookupResult>(result.Value);
Assert.Equal(lookupResult, returnValue);
Assert.Single(returnValue.MatchedContactDetails);
Assert.Single(returnValue.UnmatchedNationalIdentityNumbers);
}

[Fact]
public async Task PostLookup_ReturnsNotFoundResult_WhenNoContactDetailsFound()
{
// Arrange
var request = new UserContactPointLookup
{
NationalIdentityNumbers = ["30083542175"]
};

var lookupResult = new ContactDetailsLookupResult(
matchedContactDetails: [],
unmatchedNationalIdentityNumbers: [request.NationalIdentityNumbers[0]]);

_mockContactDetailsRetriever.Setup(x => x.RetrieveAsync(request))
.ReturnsAsync(lookupResult);

// Act
var response = await _controller.PostLookup(request);

// Assert
var notFoundResult = Assert.IsType<NotFoundResult>(response.Result);
Assert.Equal(StatusCodes.Status404NotFound, notFoundResult.StatusCode);
}

[Fact]
public async Task PostLookup_ReturnsOkResult_WhenRequestIsValid()
{
// Arrange
var request = new UserContactPointLookup
Expand All @@ -38,19 +140,24 @@ public async Task PostLookup_ReturnsOkResult_WhenSuccessful()
Reservation = false,
MobilePhoneNumber = "12345678",
EmailAddress = "test@example.com",
NationalIdentityNumber = "27038893837",
NationalIdentityNumber = "27038893837"
};

var result = new ContactDetailsLookupResult(matchedContactDetails: [contactDetails], unmatchedNationalIdentityNumbers: null);
var lookupResult = new ContactDetailsLookupResult(
matchedContactDetails: [contactDetails],
unmatchedNationalIdentityNumbers: []);

_mockContactDetailsRetriever.Setup(x => x.RetrieveAsync(request)).ReturnsAsync(result);
_mockContactDetailsRetriever.Setup(x => x.RetrieveAsync(request))
.ReturnsAsync(lookupResult);

// Act
var response = await _controller.PostLookup(request);

// Assert
var resultOk = Assert.IsType<OkObjectResult>(response.Result);
var returnValue = Assert.IsType<ContactDetailsLookupResult>(resultOk.Value);
Assert.Equal(result, returnValue);
var result = Assert.IsType<OkObjectResult>(response.Result);
Assert.Equal(StatusCodes.Status200OK, result.StatusCode);

var returnValue = Assert.IsType<ContactDetailsLookupResult>(result.Value);
Assert.Equal(lookupResult, returnValue);
}
}

0 comments on commit 30f5cea

Please sign in to comment.