Monday, 21 October 2019

Automapper unit test cases

using System;
using System.Collections;
using System.Threading.Tasks;
using PersonEntityMatchService.Business;
using PersonEntityMatchService.Business.Mappers;
using PersonEntityMatchService.Domain.Requests;
using PersonEntityMatchService.Domain.Responses;

namespace PersonEntityMatchService.Unit.Tests.Business
{
    using AutoMoqCore;
    using NUnit.Framework;
    using System.Collections.Generic;
    using Moq;
    using Clients;
    using Clients.Requests;
    using Clients.Responses;

    [TestFixture(Category = "UnitTest")]
    public class PersonMatchHandlerTests
    {
        private AutoMoqer _mocker;

        [SetUp]
        public void Setup()
        {
            _mocker = new AutoMoqer();
        }

        [Test]
        public void GetPersonMathcScoreTest()
        {
            var expectedMatches = new[]
            {
                new MatchEntity
                {
                    matchGroups = new[]
                    {
                        new Domain.Responses.MatchGroup
                        {
                            matchDescription = "test",
                            scoreStandalone = 50,
                            scoreIncremental = 10,
                            type = "suspect"
                        }
                    },
                    matchScore = 50
                }
            };
            var returnList = new List<MdaMatchEntity>();
            _mocker.GetMock<IMdaClient>()
                .Setup(x => x.GetMatchResponse(It.IsAny<MdaRequest>()))
                .Returns(Task.FromResult<IEnumerable<MdaMatchEntity>>(returnList));
            _mocker.GetMock<IMdaRequestMapper>()
                .Setup(x => x.MapToMdaRequest(It.IsAny<PersonEntity>()))
                .Returns(new MdaRequest());
            _mocker.GetMock<IMatchEntityMapper>()
                .Setup(x => x.MapToMatchEntities(It.IsAny<IEnumerable<MdaMatchEntity>>()))
                .Returns(expectedMatches);
            var target = _mocker.Resolve<PersonMatchHandler>();
            var actualMatches = target.GetPersonMatchScore(new PersonEntity());
            Assert.AreEqual(expectedMatches, actualMatches.Result);
        }

        [Test]
        public void GetPiiMathcScoreTest()
        {
            var expectedMatches = new[]
            {
                new PiiMatchEntity
                {
                    Ssn=new Ssn()
                    {
                        value="SSnABC"
                    },
                    DoB=new Domain.Responses.DoB
                    {
                        value="DOB123"
                    },
                    matchGroups = new[]
                    {
                        new Domain.Responses.MatchGroup
                        {
                            matchDescription = "test",
                            scoreStandalone = 50,
                            scoreIncremental = 10,
                            type = "suspect"
                        }
                    },
                    matchScore = 50
                }
            };
            var returnList = new List<MdaMatchEntity>();
            _mocker.GetMock<IMdaClient>()
                .Setup(x => x.GetMatchResponse(It.IsAny<MdaRequest>()))
                .Returns(Task.FromResult<IEnumerable<MdaMatchEntity>>(returnList));
            _mocker.GetMock<IMdaRequestMapper>()
                .Setup(x => x.MapToMdaRequest(It.IsAny<PersonEntity>()))
                .Returns(new MdaRequest());
            _mocker.GetMock<IMatchEntityMapper>()
                .Setup(x => x.MapToMatchPiiEntities(It.IsAny<IEnumerable<MdaMatchEntity>>()))
                .Returns(expectedMatches);
            var target = _mocker.Resolve<PersonMatchHandler>();
            var actualMatches = target.GetPiiMatchScore(new PersonEntity());
            Assert.AreEqual(expectedMatches, actualMatches.Result);
        }

    }
}


namespace PersonEntityMatchService.Unit.Tests
{
    using System.Collections.Generic;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    using NUnit.Framework;
    using Domain.Responses;

    [TestFixture(Category = "UnitTest")]
    public class PiiMatchEntityContractTests : TestBase
    {
        [Test]
        public void Should_serialize_Entity()
        {
            var entities = ResponseEntity();

            var expected = GetJToken("POST_PiiMatch_Response.json");

            var actual = JToken.Parse(JsonConvert.SerializeObject(entities));

            Assert.True(JToken.DeepEquals(actual, expected), "Expected should match actual when serializing.");
        }

        [Test]
        public void Should_deserialize_Entity()
        {
            var entities = GetJToken("POST_PiiMatch_Response.json");

            var expected = ResponseEntity();

            var actual = JsonConvert.DeserializeObject<List<PiiMatchEntity>>(entities.ToString());

            var comparer = new ObjectsComparer.Comparer<List<PiiMatchEntity>>();

            Assert.True(comparer.Compare(actual, expected), "Expected should match actual when deserializing.");
        }

        private static List<PiiMatchEntity> ResponseEntity()
        {
            return new List<PiiMatchEntity>
            {
                new PiiMatchEntity{
                    Ssn=new Ssn()
                    {
                        value="/wEBReHevtjQYqAYuGis+AX0qWELTg4SkguSAXo6iE8j2927gVZLAyfosBrTYNKrIuagGrF/WlPBgobEAgjvaJ8i5uTKmGEjmweg2DvILux/wymO9hiLjUTW/wIVKkxPpVpeMJEfRuF3ejSbbgO2SChINn/mejqITyPb3bu+yKZp4nVrUub7728TFDlgx9MuqwmXx/NqQIkmKf/I0ThiQZXRhkYYqUNUwoejrhl0hqyPoLyPuHXm9sMeEZr8GHAiW3GGYUcY"
                    },
                    DoB=new DoB()
                    {
                        value="/wEBXxoST1IkLAuhdVuJ2QZsHad3mwDhaxKXAXo6iE8j29272zuTofDocz41YlG3R6zEbC9MHYXsxOdHAgjvaJ8i5uTKcv+3xrEkchQZvDo9b4saercM38kzFM1dv+wl4X6LnQeDegOo+Fd+q3y2u7h3ZqSJejqITyPb3bts/OpIHCkZYWC7FR0wG+xalm1iY+IdkzdoJR8ferrzAeQD3y9lu47NZ86X5DAXh+bh3MVZACGpd4DtEA0rWmXy0DFz+uGL8TuG"
                    },
                    uri = "/Entity?filter=EntityId eq '2f2bfe8add3041d8abfe8add30e1d854'",
                    matchScore = 80,
                    matchGroups = new[]
                    {
                        new MatchGroup
                        {
                            type = "suspect",
                            matchDescription = "Suspect individual match by fuzzy first name, fuzzy last name, email, and postal code",
                            scoreStandalone = 60,
                            scoreIncremental = 10
                        },
                        new MatchGroup
                        {
                            type = "suspect",
                            matchDescription = "Suspect individual match by first name, last name, email and postal code",
                            scoreStandalone = 70,
                            scoreIncremental = 10
                        }
                    }
                }
            };
    }
}
}



using System;
using System.IO;
using Newtonsoft.Json.Linq;
using NUnit.Framework;

namespace PersonEntityMatchService.Unit.Tests
{
    public class TestBase
    {
        [SetUp]
        public void Setup()
        {
            // Method intentionally left empty.
        }

        [TearDown]
        public void TearDown()
        {
            // Method intentionally left empty.
        }

        protected static JToken GetJToken(string fileName)
        {
            return JToken.Parse(File.ReadAllText(Path.Combine($"{AppDomain.CurrentDomain.BaseDirectory}/TestData", fileName)));
        }
    }
}


[
  {
    "Ssn": {
      "value": "/wEBReHevtjQYqAYuGis+AX0qWELTg4SkguSAXo6iE8j2927gVZLAyfosBrTYNKrIuagGrF/WlPBgobEAgjvaJ8i5uTKmGEjmweg2DvILux/wymO9hiLjUTW/wIVKkxPpVpeMJEfRuF3ejSbbgO2SChINn/mejqITyPb3bu+yKZp4nVrUub7728TFDlgx9MuqwmXx/NqQIkmKf/I0ThiQZXRhkYYqUNUwoejrhl0hqyPoLyPuHXm9sMeEZr8GHAiW3GGYUcY"
    },
    "DoB": {
      "value": "/wEBXxoST1IkLAuhdVuJ2QZsHad3mwDhaxKXAXo6iE8j29272zuTofDocz41YlG3R6zEbC9MHYXsxOdHAgjvaJ8i5uTKcv+3xrEkchQZvDo9b4saercM38kzFM1dv+wl4X6LnQeDegOo+Fd+q3y2u7h3ZqSJejqITyPb3bts/OpIHCkZYWC7FR0wG+xalm1iY+IdkzdoJR8ferrzAeQD3y9lu47NZ86X5DAXh+bh3MVZACGpd4DtEA0rWmXy0DFz+uGL8TuG"
    },
    "uri": "/Entity?filter=EntityId eq '2f2bfe8add3041d8abfe8add30e1d854'",
    "matchGroups": [
      {
        "type": "suspect",
        "matchDescription": "Suspect individual match by fuzzy first name, fuzzy last name, email, and postal code",
        "scoreStandalone": 60,
        "scoreIncremental": 10
      },
      {
        "type": "suspect",
        "matchDescription": "Suspect individual match by first name, last name, email and postal code",
        "scoreStandalone": 70,
        "scoreIncremental": 10
      }
    ],
    "matchScore": 80
  }
]

No comments:

Post a Comment