diff --git a/TramsDataApi.Test/UseCases/SearchTrustsTests.cs b/TramsDataApi.Test/UseCases/SearchTrustsTests.cs index 925923cef..f7f85eae5 100644 --- a/TramsDataApi.Test/UseCases/SearchTrustsTests.cs +++ b/TramsDataApi.Test/UseCases/SearchTrustsTests.cs @@ -1,3 +1,4 @@ +using System; using System.Collections; using System.Collections.Generic; using System.Linq; @@ -32,12 +33,10 @@ public void SearchTrusts_ReturnsEmptyList_WhenNoTrustsFound() result.Should().BeEquivalentTo(new List()); } - [Fact] - public void SearchTrusts_ReturnsListOfTrustSummaryResponses_WhenTrustsFound() + private IList Test(string groupName) { - var groupName = "groupName"; - var expectedTrusts = Builder.CreateListOfSize(10) + return Builder.CreateListOfSize(10) .TheFirst(5) .With(g => g.GroupType = "Trust") .TheNext(3) @@ -47,6 +46,14 @@ public void SearchTrusts_ReturnsListOfTrustSummaryResponses_WhenTrustsFound() .All() .With(g => g.GroupName = groupName) .Build(); + } + + [Fact] + public void SearchTrusts_ReturnsListOfTrustSummaryResponses_WhenTrustsFound() + { + var groupName = "groupName"; + + var expectedTrusts = Test(groupName); var trustsGateway = new Mock(); var establishmentsGateway = new Mock(); @@ -69,6 +76,45 @@ public void SearchTrusts_ReturnsListOfTrustSummaryResponses_WhenTrustsFound() result.Should().BeEquivalentTo(expected); } + [Fact] + public void SearchTrusts_ReturnsListOfTrustSummaryResponsesWithAddresses_WhenTrustsFound() + { + var groupName = "groupName"; + + var expectedGroups = Test(groupName); + + IDictionary trusts = new Dictionary(); + + foreach (var expectedGroup in expectedGroups) + { + var expectedTrust = Builder + .CreateNew() + .With(g => g.TrustRef = expectedGroup.GroupId) + .Build(); + trusts.Add(expectedGroup.GroupId, expectedTrust); + } + + var trustsGateway = new Mock(); + var establishmentsGateway = new Mock(); + + trustsGateway.Setup(g => g.SearchGroups(1, 10, groupName, string.Empty, string.Empty)) + .Returns((expectedGroups, expectedGroups.Count)); + + trustsGateway.Setup(m => m.GetIfdTrustsByTrustRef(It.IsAny())) + .Returns(trusts.Values.ToList()); + + establishmentsGateway.Setup(g => g.GetByTrustUids(It.IsAny())) + .Returns(new List()); + + var expected = expectedGroups. + Select(e => TrustSummaryResponseFactory.Create(e, new List(), trusts[e.GroupId])) + .ToList(); + + var searchTrusts = new SearchTrusts(trustsGateway.Object, establishmentsGateway.Object); + var (result, _) = searchTrusts.Execute(1, 10, groupName); + + result.Should().BeEquivalentTo(expected); + } [Fact] public void SearchTrusts_ShouldGetTrustsWithEstablishments_WhenTrustsAndEstablishmentsAreFound() @@ -132,5 +178,6 @@ public void SearchTrusts_WithoutEstablishments() var (result, _) = searchTrusts.Execute(1, 10, ukPrn: ukprn, includeEstablishments: false); result.Should().BeEquivalentTo(expected); } + } } \ No newline at end of file diff --git a/TramsDataApi/UseCases/SearchTrusts.cs b/TramsDataApi/UseCases/SearchTrusts.cs index 5761f4cb1..012c9fd39 100644 --- a/TramsDataApi/UseCases/SearchTrusts.cs +++ b/TramsDataApi/UseCases/SearchTrusts.cs @@ -42,8 +42,8 @@ public SearchTrusts(ITrustGateway trustGateway, IEstablishmentGateway establishm return ( groups.Select(group => { + var trust = trustsForGroup.FirstOrDefault(e => e.TrustRef == group.GroupId); var establishments = establishmentsForGroup.Where(e => e.TrustsCode == group.GroupUid); - var trust = trustsForGroup.FirstOrDefault(e => e.TrustRef == group.GroupUid); return TrustSummaryResponseFactory.Create(group, establishments, trust); }).ToArray(), recordCount