public async Task WhenFeatureToggleEnabled_CallsNewContactDetailsApi()
        {
            // Arrange
            var assetGatewayResponse = _fixture.Create <AssetResponseObject>();

            _assetGatewayMock
            .Setup(x => x.GetById(It.IsAny <string>()))
            .ReturnsAsync(assetGatewayResponse);

            var householdMembers = _fixture.CreateMany <HouseholdMember>(3);

            var tenureGatewayResponse = _fixture.Build <TenureInformation>()
                                        .With(x => x.HouseholdMembers, householdMembers)
                                        .Create();

            _tenureInformationGatewayMock
            .Setup(x => x.GetById(It.IsAny <string>()))
            .ReturnsAsync(tenureGatewayResponse);

            var contactDetailsGatewayResponse = _fixture.CreateMany <ResidentContactDetails>(3);

            _contactDetailsGatewayMock
            .Setup(x => x.GetContactDetailsAsync(It.IsAny <IEnumerable <HouseholdMember> >()))
            .ReturnsAsync(contactDetailsGatewayResponse);

            var expectedLocationAlerts = StubPropertyAlertList(assetGatewayResponse.AssetId, 5);
            var expectedPersonAlerts   = new PersonAlertList()
            {
                Alerts = StubAlerts().Generate(5)
            };

            _alertGatewayMock.Setup(gm => gm.GetLocationAlertsAsync(It.IsAny <string>())).ReturnsAsync(expectedLocationAlerts);
            _alertGatewayMock.Setup(gm => gm.GetPersonAlertsAsync(It.IsAny <string>())).ReturnsAsync(expectedPersonAlerts);

            _featureManagerMock
            .Setup(x => x.IsEnabledAsync(FeatureFlags.EnableNewContactDetailsApi))
            .ReturnsAsync(true);

            // Act
            var result = await _classUnderTest.ExecuteAsync(assetGatewayResponse.AssetId);

            // Assert
            result.ContactDetails.Should().BeEquivalentTo(contactDetailsGatewayResponse);

            _contactDetailsGatewayMock.Verify(x => x.GetContactDetailsAsync(It.IsAny <IEnumerable <HouseholdMember> >()), Times.Once);
        }
        public async Task WhenPropertyFound_ReturnsProperty()
        {
            // Arrange
            var assetGatewayResponse = _fixture.Create <AssetResponseObject>();

            var expectedLocationAlerts = StubPropertyAlertList(assetGatewayResponse.AssetId, 5);
            var expectedPersonAlerts   = new PersonAlertList()
            {
                Alerts = StubAlerts().Generate(5)
            };

            _assetGatewayMock
            .Setup(x => x.GetById(It.IsAny <string>()))
            .ReturnsAsync(assetGatewayResponse);

            var tenureGatewayResponse = _fixture.Create <TenureInformation>();

            _tenureInformationGatewayMock
            .Setup(x => x.GetById(It.IsAny <string>()))
            .ReturnsAsync(tenureGatewayResponse);

            _alertGatewayMock.Setup(gm => gm.GetLocationAlertsAsync(It.IsAny <string>())).ReturnsAsync(expectedLocationAlerts);
            _alertGatewayMock.Setup(gm => gm.GetPersonAlertsAsync(It.IsAny <string>())).ReturnsAsync(expectedPersonAlerts);

            var contactDetailsGatewayResponse = _fixture.CreateMany <ResidentContactDetails>(3);

            _contactDetailsGatewayMock
            .Setup(x => x.GetContactDetailsAsync(It.IsAny <IEnumerable <HouseholdMember> >()))
            .ReturnsAsync(contactDetailsGatewayResponse);

            _featureManagerMock
            .Setup(x => x.IsEnabledAsync(FeatureFlags.EnableNewContactDetailsApi))
            .ReturnsAsync(true);

            // Act
            var result = await _classUnderTest.ExecuteAsync(assetGatewayResponse.AssetId);

            // Assert
            result.PropertyModel.Should().BeEquivalentTo(assetGatewayResponse.ToDomain());
            result.Tenure.Should().BeEquivalentTo(tenureGatewayResponse);
            result.LocationAlerts.Should().BeEquivalentTo(expectedLocationAlerts.Alerts);
            result.PersonAlerts.Should().BeEquivalentTo(expectedPersonAlerts.Alerts);
            result.ContactDetails.Should().BeEquivalentTo(contactDetailsGatewayResponse);
        }
        public void Setup()
        {
            _fixture = new Fixture();
            _fixture.Behaviors.Remove(new ThrowingRecursionBehavior());
            _fixture.Behaviors.Add(new OmitOnRecursionBehavior());
            _workOrderGenerator = new Helpers.StubGeneration.Generator <WorkOrder>()
                                  .AddInfrastructureWorkOrderGenerators();

            _sessionId         = "sessionId";
            _sorGatewayMock    = new Mock <IScheduleOfRatesGateway>();
            _alertsGatewayMock = new Mock <IAlertsGateway>();

            _tenureInformationUseCaseMock = new Mock <ITenureInformationUseCase>();

            _sorPriorityGatewayMock = new Mock <ISorPriorityGateway>();


            _classUnderTest = new DrsMapping(
                _sorGatewayMock.Object,
                _alertsGatewayMock.Object,
                _tenureInformationUseCaseMock.Object,
                _sorPriorityGatewayMock.Object,
                new NullLogger <DrsMapping>()
                );

            _locationAlerts = new Helpers.StubGeneration.Generator <PropertyAlertList>().AddDefaultGenerators().Generate();
            _personAlerts   = new Helpers.StubGeneration.Generator <PersonAlertList>().AddDefaultGenerators().Generate();
            var commonAlerts = new Helpers.StubGeneration.Generator <Alert>().AddDefaultGenerators().GenerateList(5);

            _locationAlerts.Alerts = _locationAlerts.Alerts.Union(commonAlerts);
            _personAlerts.Alerts   = _personAlerts.Alerts.Union(commonAlerts);

            _tenureInformation = new Helpers.StubGeneration.Generator <TenureInformation>().AddDefaultGenerators().Generate();

            _alertsGatewayMock.Setup(x => x.GetLocationAlertsAsync(It.IsAny <string>()))
            .ReturnsAsync(_locationAlerts);
            _alertsGatewayMock.Setup(x => x.GetPersonAlertsAsync(It.IsAny <string>()))
            .ReturnsAsync(_personAlerts);

            _tenureInformationUseCaseMock
            .Setup(x => x.GetTenureInformationWithPropertyReference(It.IsAny <string>()))
            .ReturnsAsync(_tenureInformation);
        }
        public async Task TruncatesOrderComments()
        {
            //arrange
            var longAlert = new PersonAlertList
            {
                Alerts = new List <Alert>
                {
                    new Alert
                    {
                        AlertCode = "AL0001", Description = "SUPER LONG DESCRIPTION 1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
                    },
                    new Alert
                    {
                        AlertCode = "AL0002", Description = "SUPER LONG DESCRIPTION 2 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
                    },
                }
            };

            _alertsGatewayMock.Setup(x => x.GetPersonAlertsAsync(It.IsAny <string>()))
            .ReturnsAsync(longAlert);

            _sorGatewayMock.Setup(x => x.GetCode(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()))
            .ReturnsAsync(new ScheduleOfRatesModel
            {
                Code                = "TEST",
                LongDescription     = "DESCRIPTION",
                TradeCode           = "trade 001",
                StandardMinuteValue = 12
            });

            _sorPriorityGatewayMock.Setup(x => x.GetLegacyPriorityCode(It.IsAny <int>()))
            .ReturnsAsync("A");

            var workOrder = _workOrderGenerator.Generate();

            //act
            var result = await _classUnderTest.BuildCreateOrderRequest(_sessionId, workOrder);

            //assert
            result.createOrder1.theOrder.orderCommentsExtended.Length.Should().BeLessThan(255);
        }
        public async Task WhenPropertyNotFound_ReturnsNull()
        {
            // Arrange
            var expectedProperty = StubProperties().Generate();
            var tmo = expectedProperty.TmoName;
            var expectedLocationAlerts = StubPropertyAlertList(expectedProperty.PropertyReference, 5);
            var expectedPersonAlerts   = new PersonAlertList()
            {
                Alerts = StubAlerts().Generate(5)
            };

            var assetId = Guid.NewGuid().ToString();

            _assetGatewayMock
            .Setup(x => x.GetById(assetId))
            .ReturnsAsync((AssetResponseObject)null);

            // Act
            var result = await _classUnderTest.ExecuteAsync(expectedProperty.PropertyReference);

            // Assert
            result.Should().BeNull();
        }
        public async Task ReturnsAlerts()
        {
            // Arrange
            const int expectedPropertyAlertCount = 5;
            const int expectedPersonAlertCount   = 5;

            string expectedPropertyReference = new Faker().Random.Number().ToString();
            var    expectedPropertyAlertList = StubPropertyAlertList(expectedPropertyReference, expectedPropertyAlertCount);
            var    expectedPersonAlertList   = new PersonAlertList {
                Alerts = StubAlerts().Generate(expectedPersonAlertCount)
            };

            _alertGatewayMock.Setup(gm => gm.GetLocationAlertsAsync(It.IsAny <string>())).ReturnsAsync(expectedPropertyAlertList);
            _alertGatewayMock.Setup(gm => gm.GetPersonAlertsAsync(It.IsAny <string>())).ReturnsAsync(expectedPersonAlertList);

            // Act
            var result = await _classUnderTest.ExecuteAsync(expectedPropertyReference);

            // Assert
            result.PropertyAlerts.PropertyReference.Should().Be(expectedPropertyReference);
            result.PropertyAlerts.Alerts.Should().HaveCount(expectedPropertyAlertCount);
            result.PersonAlerts.Alerts.Should().HaveCount(expectedPersonAlertCount);
        }
        private static void ValidateOrder(WorkOrder workOrder, order order, PropertyAlertList locationAlerts, PersonAlertList personAlertList, orderStatus?expectedStatus = null)
        {
            order.primaryOrderNumber.Should().Be(workOrder.Id.ToString(CultureInfo.InvariantCulture));
            order.status.Should().Be(expectedStatus ?? orderStatus.PLANNED);

            var uniqueCodes = locationAlerts?.Alerts.Union(personAlertList?.Alerts);

            order.orderComments.Should().Be(@$ "{uniqueCodes.ToCodeString()} {workOrder.DescriptionOfWork}".Truncate(250));

            order.contract.Should().Be(workOrder.AssignedToPrimary.ContractorReference);
            order.locationID.Should().Be(workOrder.Site.PropertyClass.FirstOrDefault()?.PropertyReference);
            order.userId.Should().Be(workOrder.AgentEmail);
            order.contactName.Should().Be(workOrder.Customer.Name);
            order.phone.Should().Be(workOrder.Customer.Person.Communication.GetPhoneNumber());

            order.orderCommentsExtended.Should().Contain(locationAlerts.Alerts.ToCommentsExtendedString().Truncate(220));
            order.orderCommentsExtended.Should()
            .Contain(personAlertList.Alerts.ToCommentsExtendedString().Truncate(Math.Max(220 - locationAlerts.Alerts.ToCommentsExtendedString().Length, 1)));

            ValidateLocation(workOrder, order.theLocation);
            ValidateTargetDate(workOrder.WorkPriority.RequiredCompletionDateTime !.Value, order.targetDate);
        }