public void WhenGivenAListOfTenancyRefs_GetTenanciesByRefs_ShouldReturnAllUniqueTenancies()
        {
            TenancyListItem firstTenancy  = GenerateTenancyListItem();
            TenancyListItem secondTenancy = GenerateTenancyListItem();

            DateTime firstTenancyLatestActionDate = firstTenancy.LastActionDate.AddDays(1);

            InsertArrearsActions(firstTenancy.TenancyRef, "ABC", firstTenancyLatestActionDate);

            DateTime secondTenancyLatestAgreementStartDate = secondTenancy.ArrearsAgreementStartDate.AddDays(1);

            InsertAgreement(secondTenancy.TenancyRef, "characters", secondTenancyLatestAgreementStartDate);

            var tenancies = GetTenanciesByRef(new List <string> {
                firstTenancy.TenancyRef, secondTenancy.TenancyRef
            });

            var receivedFirst = tenancies.Find(e => e.TenancyRef == firstTenancy.TenancyRef);

            Assert.Equal(firstTenancyLatestActionDate, receivedFirst.LastActionDate);
            Assert.Equal("ABC", receivedFirst.LastActionCode);

            var receivedSecond = tenancies.Find(e => e.TenancyRef == secondTenancy.TenancyRef);

            Assert.Equal(secondTenancyLatestAgreementStartDate, receivedSecond.ArrearsAgreementStartDate);
            Assert.Equal("characters", receivedSecond.ArrearsAgreementStatus);
        }
        private TenancyListItem InsertRandomisedTenancyListItem()
        {
            TenancyListItem tenancy = CreateRandomTenancyListItem();

            InsertTenancyAttributes(tenancy);

            return(tenancy);
        }
        public void WhenGivenTenancyRef_GetTenanciesByRefs_ShouldReturnTenancyObjectForThatRef()
        {
            TenancyListItem expectedTenancy = InsertRandomisedTenancyListItem();

            var tenancies = GetTenanciesByRef(new List <string> {
                expectedTenancy.TenancyRef
            });

            Assert.Single(tenancies);
            Assert.Contains(expectedTenancy, tenancies);
        }
        public void WhenGivenATenancyRefWithNoAddress_GetTenanciesByRefs_ShouldReturnNull()
        {
            TenancyListItem expectedTenancy = CreateRandomTenancyListItem();

            expectedTenancy.PrimaryContactShortAddress = null;
            InsertTenancyAttributes(expectedTenancy);

            var tenancies = GetTenanciesByRef(new List <string> {
                expectedTenancy.TenancyRef
            });

            Assert.Equal(expectedTenancy.PrimaryContactShortAddress, tenancies[0].PrimaryContactShortAddress);
        }
        public void WhenGivenTenancyRef_GetTenanciesByRefs_ShouldReturnTheLatestAgreement()
        {
            TenancyListItem expectedTenancy = GenerateTenancyListItem();

            DateTime latestAragDate = expectedTenancy.ArrearsAgreementStartDate.AddDays(1);

            InsertAgreement(expectedTenancy.TenancyRef, "Inactive", expectedTenancy.ArrearsAgreementStartDate.Subtract(DAY_IN_TIMESPAN));
            InsertAgreement(expectedTenancy.TenancyRef, "Active", latestAragDate);

            var tenancies = GetTenanciesByRef(new List <string> {
                expectedTenancy.TenancyRef
            });

            Assert.Equal(tenancies[0].ArrearsAgreementStartDate, latestAragDate);
        }
        public void WhenGivenTenancyRef_GetTenanciesByRefs_ShouldReturnTheLatestArrearsAction()
        {
            TenancyListItem expectedTenancy = GenerateTenancyListItem();

            DateTime latestActionDate = expectedTenancy.LastActionDate.AddDays(1);

            InsertArrearsActions(expectedTenancy.TenancyRef, "ABC",
                                 expectedTenancy.LastActionDate.Subtract(DAY_IN_TIMESPAN));
            InsertArrearsActions(expectedTenancy.TenancyRef, "XYZ", latestActionDate);

            var tenancies = GetTenanciesByRef(new List <string> {
                expectedTenancy.TenancyRef
            });

            Assert.Equal(tenancies[0].LastActionDate, latestActionDate);
        }
        public void WhenGivenSomeTenancyRefs_GetTenanciesByRefs_ShouldReturnTenancyObjectForEachValidRef()
        {
            TenancyListItem expectedTenancy1 = GenerateTenancyListItem();
            TenancyListItem expectedTenancy2 = GenerateTenancyListItem();

            var tenancies = GetTenanciesByRef(new List <string>
            {
                expectedTenancy1.TenancyRef,
                "NotValid",
                expectedTenancy2.TenancyRef,
                "NotPresent"
            });

            Assert.Equal(2, tenancies.Count);
            Assert.Contains(expectedTenancy1, tenancies);
            Assert.Contains(expectedTenancy2, tenancies);
        }
        private void InsertTenancyAttributes(TenancyListItem tenancyAttributes)
        {
            string     commandText = InsertQueries();
            SqlCommand command     = new SqlCommand(commandText, db);

            command.Parameters.Add("@tenancyRef", SqlDbType.Char);
            command.Parameters["@tenancyRef"].Value = tenancyAttributes.TenancyRef;
            command.Parameters.Add("@propRef", SqlDbType.Char);
            command.Parameters["@propRef"].Value = tenancyAttributes.PropertyRef;
            command.Parameters.Add("@tenure", SqlDbType.Char);
            command.Parameters["@tenure"].Value = tenancyAttributes.Tenure;
            command.Parameters.Add("@rent", SqlDbType.Decimal);
            command.Parameters["@rent"].Value = DBNull.Value;
            command.Parameters.Add("@service", SqlDbType.Decimal);
            command.Parameters["@service"].Value = DBNull.Value;
            command.Parameters.Add("@otherCharge", SqlDbType.Decimal);
            command.Parameters["@otherCharge"].Value = DBNull.Value;
            command.Parameters.Add("@currentBalance", SqlDbType.Decimal);
            command.Parameters["@currentBalance"].Value = tenancyAttributes.CurrentBalance;
            command.Parameters.Add("@primaryContactName", SqlDbType.Char);
            command.Parameters["@primaryContactName"].Value = tenancyAttributes.PrimaryContactName;
            command.Parameters.Add("@primaryContactAddress", SqlDbType.Char);
            command.Parameters["@primaryContactAddress"].Value =
                tenancyAttributes.PrimaryContactShortAddress == null
                    ? DBNull.Value.ToString()
                    : tenancyAttributes.PrimaryContactShortAddress + "\n";

            command.Parameters.Add("@primaryContactPostcode", SqlDbType.Char);
            command.Parameters["@primaryContactPostcode"].Value = tenancyAttributes.PrimaryContactPostcode;
            command.Parameters.Add("@primaryContactPhone", SqlDbType.Char);
            command.Parameters["@primaryContactPhone"].Value = DBNull.Value.ToString();
            command.ExecuteNonQuery();

            InsertAgreement(tenancyAttributes.TenancyRef, tenancyAttributes.ArrearsAgreementStatus, tenancyAttributes.ArrearsAgreementStartDate);
            InsertArrearsActions(tenancyAttributes.TenancyRef, tenancyAttributes.LastActionCode, tenancyAttributes.LastActionDate);
        }
        public void WhenGivenATenancyRef_GetTenanciesByRefs_ShouldReturnOnlyTheShortAddress()
        {
            var random = new Randomizer();

            TenancyListItem expectedTenancy = GenerateTenancyListItem();

            string longAddress = $"{expectedTenancy.PrimaryContactShortAddress}\n" +
                                 $"{random.Words()}\n{random.Words()}\n{random.Words()}";

            // make sure there's a long string in the db
            string commandText =
                $"UPDATE contacts SET con_address = '{longAddress}' WHERE contacts.tag_ref = '{expectedTenancy.TenancyRef}'";
            SqlCommand command = new SqlCommand(commandText, db);

            command.ExecuteNonQuery();

            string actualShortAddressExpected = longAddress.Split("\n")[0];
            var    tenancies = GetTenanciesByRef(new List <string> {
                expectedTenancy.TenancyRef
            });

            Assert.Equal(actualShortAddressExpected, tenancies[0].PrimaryContactShortAddress);
            Assert.NotEqual(longAddress, tenancies[0].PrimaryContactShortAddress);
        }
        public async Task GivenValidedInput__WhenExecuteAsync_ThenShouldReturnListOfTenancySummaries()
        {
            //arrange
            var tenancy1 = new TenancyListItem
            {
                PrimaryContactName         = "test",
                TenancyRef                 = "tRef",
                ArrearsAgreementStartDate  = DateTime.Now,
                ArrearsAgreementStatus     = "Active",
                CurrentBalance             = (decimal)1000.12,
                LastActionCode             = "ACC",
                LastActionDate             = DateTime.Now.AddDays(-1),
                PrimaryContactPostcode     = "test",
                PrimaryContactShortAddress = "123DreryLane",
                PropertyRef                = "2",
                Tenure = "LongLease"
            };
            var tenancy2 = new TenancyListItem
            {
                PrimaryContactName         = "test2",
                TenancyRef                 = "tRef2",
                ArrearsAgreementStartDate  = DateTime.Now,
                ArrearsAgreementStatus     = "Active2",
                CurrentBalance             = (decimal)2000.34,
                LastActionCode             = "ACC2",
                LastActionDate             = DateTime.Now.AddDays(-2),
                PrimaryContactPostcode     = "test2",
                PrimaryContactShortAddress = "123DreryLane2",
                PropertyRef                = "22",
                Tenure = "LongLease2"
            };
            var tenancyAgreementRef = "Test";

            _fakeGateway.Setup(s => s.SearchTenanciesAsync(It.Is <SearchTenancyRequest>(i => i.SearchTerm.Equals("Test")), CancellationToken.None))
            .ReturnsAsync(new PagedResults <TenancyListItem>
            {
                Results = new List <TenancyListItem>
                {
                    tenancy1,
                    tenancy2
                }
            });

            var request = new SearchTenancyRequest
            {
                SearchTerm = tenancyAgreementRef
            };
            //act
            var response = await _classUnderTest.ExecuteAsync(request, CancellationToken.None);

            //assert
            response.Should().NotBeNull();
            response.Tenancies.Should().NotBeNullOrEmpty();
            response.Tenancies[0].PropertyRef.Should().BeEquivalentTo(tenancy1.PropertyRef);
            response.Tenancies[0].TenancyRef.Should().BeEquivalentTo(tenancy1.TenancyRef);
            response.Tenancies[0].Tenure.Should().BeEquivalentTo(tenancy1.Tenure);

            response.Tenancies[0].CurrentBalance.Should().NotBeNull();
            response.Tenancies[0].CurrentBalance.Value.Should().Be(tenancy1.CurrentBalance);
            response.Tenancies[0].CurrentBalance.CurrencyCode.Should().BeEquivalentTo("GBP");

            response.Tenancies[0].PrimaryContact.Name.Should().BeEquivalentTo(tenancy1.PrimaryContactName);
            response.Tenancies[0].PrimaryContact.Postcode.Should().BeEquivalentTo(tenancy1.PrimaryContactPostcode);
            response.Tenancies[0].PrimaryContact.ShortAddress.Should().BeEquivalentTo(tenancy1.PrimaryContactShortAddress);

            response.Tenancies[1].PropertyRef.Should().BeEquivalentTo(tenancy2.PropertyRef);
            response.Tenancies[1].TenancyRef.Should().BeEquivalentTo(tenancy2.TenancyRef);
            response.Tenancies[1].Tenure.Should().BeEquivalentTo(tenancy2.Tenure);

            response.Tenancies[1].CurrentBalance.Should().NotBeNull();
            response.Tenancies[1].CurrentBalance.Value.Should().Be(tenancy2.CurrentBalance);
            response.Tenancies[1].CurrentBalance.CurrencyCode.Should().BeEquivalentTo("GBP");

            response.Tenancies[1].PrimaryContact.Name.Should().BeEquivalentTo(tenancy2.PrimaryContactName);
            response.Tenancies[1].PrimaryContact.Postcode.Should().BeEquivalentTo(tenancy2.PrimaryContactPostcode);
            response.Tenancies[1].PrimaryContact.ShortAddress.Should().BeEquivalentTo(tenancy2.PrimaryContactShortAddress);
        }
        public void WhenGivenAListOfTenancyRefs_GetTenanciesByRefs_ShouldTrimCharacterFields()
        {
            string commandText =
                "INSERT INTO tenagree (tag_ref, prop_ref) VALUES (@tenancyRef, @propRef);" +
                "INSERT INTO araction (tag_ref, action_code) VALUES (@tenancyRef, @actionCode)" +
                "INSERT INTO arag (tag_ref, arag_status) VALUES (@tenancyRef, @aragStatus)" +
                "INSERT INTO contacts (tag_ref, con_phone1) VALUES (@tenancyRef, @phone)" +
                "INSERT INTO property (prop_ref, short_address, post_code) VALUES (@propRef, @shortAddress, @postcode)";

            SqlCommand command = new SqlCommand(commandText, db);

            command.Parameters.Add("@tenancyRef", SqlDbType.Char);
            command.Parameters["@tenancyRef"].Value = "not11chars";
            command.Parameters.Add("@actionCode", SqlDbType.Char);
            command.Parameters["@actionCode"].Value = "ee";
            command.Parameters.Add("@aragStatus", SqlDbType.Char);
            command.Parameters["@aragStatus"].Value = "status";
            command.Parameters.Add("@postcode", SqlDbType.Char);
            command.Parameters["@postcode"].Value = "pcode";
            command.Parameters.Add("@phone", SqlDbType.Char);
            command.Parameters["@phone"].Value = "phone";
            command.Parameters.Add("@propRef", SqlDbType.Char);
            command.Parameters["@propRef"].Value = "pref";
            command.Parameters.Add("@shortAddress", SqlDbType.Char);
            command.Parameters["@shortAddress"].Value = "short addr";

            command.ExecuteNonQuery();

            string retrieved_value = db.Query <string>("SELECT TOP 1 tag_ref FROM tenagree WHERE tag_ref = 'not11chars '").First();

            Assert.Contains("not11chars ", retrieved_value);

            retrieved_value = db.Query <string>("SELECT TOP 1 action_code FROM araction WHERE tag_ref = 'not11chars '").First();
            Assert.Contains("ee ", retrieved_value);

            retrieved_value = db.Query <string>("SELECT TOP 1 arag_status FROM arag WHERE tag_ref = 'not11chars '").First();
            Assert.Contains("status    ", retrieved_value);

            List <dynamic> retrieved_values  = db.Query("SELECT tag_ref, con_phone1 FROM contacts WHERE contacts.tag_ref = 'not11chars '").ToList();
            IDictionary <string, object> row = retrieved_values[0];

            Assert.Contains("phone                ", row.Values);

            retrieved_values = db.Query("SELECT prop_ref, short_address, address1, post_code FROM property WHERE property.prop_ref = 'pref        '").ToList();
            row = retrieved_values[0];

            Assert.Contains("pref        ", row.Values);
            Assert.Contains("pcode     ", row.Values);

            row.TryGetValue("short_address", out var saved_address);
            Assert.Equal(200, saved_address.ToString().Length);

            TenancyListItem trimmedTenancy = GetTenanciesByRef(new List <string> {
                "not11chars"
            }).First();

            Assert.Equal("not11chars", trimmedTenancy.TenancyRef);
            Assert.Equal("pref", trimmedTenancy.PropertyRef);
            Assert.Equal("ee", trimmedTenancy.LastActionCode);
            Assert.Equal("status", trimmedTenancy.ArrearsAgreementStatus);
            Assert.Equal("pcode", trimmedTenancy.PrimaryContactPostcode);
            Assert.Equal("short addr", trimmedTenancy.PrimaryContactShortAddress);
            Assert.Equal("pcode", trimmedTenancy.PrimaryContactPostcode);
        }
 public void SetTenancyListItem(string tenancyRef, TenancyListItem tenancyListItem)
 {
     StoredTenancyListItems[tenancyRef] = tenancyListItem;
 }