From 1989515c4b0e2fc0554e0787f9f2e90d47c34963 Mon Sep 17 00:00:00 2001 From: AnuragNagpure <145100366+AnuragNagpure@users.noreply.github.com> Date: Wed, 29 Nov 2023 21:09:00 +0530 Subject: [PATCH] feat(countrylist): Add new endpoint getAllCountries (#347) * countrylist endpoint returning internationalized names of all countries --------- Co-authored-by: Norbert Truchsess Reviewed-by: Norbert Truchsess --- .../Controllers/StaticDataController.cs | 1 - .../Models/CountryLongNameData.cs | 36 +++++++++ .../Repositories/IStaticDataRepository.cs | 7 +- .../Repositories/StaticDataRepository.cs | 12 ++- .../Entities/Country.cs | 1 - .../BusinessLogic/IStaticDataBusinessLogic.cs | 33 +++++++++ .../BusinessLogic/StaticDataBusinessLogic.cs | 42 +++++++++++ .../Controllers/StaticDataController.cs | 57 +++++++++++++++ .../Registration.Service/Program.cs | 2 +- .../Controllers/StaticDataControllerTest.cs | 3 +- .../StaticDataBusinessLogicTest.cs | 73 +++++++++++++++++++ .../Controller/StaticDataControllerTest.cs | 57 +++++++++++++++ 12 files changed, 317 insertions(+), 7 deletions(-) create mode 100644 src/portalbackend/PortalBackend.DBAccess/Models/CountryLongNameData.cs create mode 100644 src/registration/Registration.Service/BusinessLogic/IStaticDataBusinessLogic.cs create mode 100644 src/registration/Registration.Service/BusinessLogic/StaticDataBusinessLogic.cs create mode 100644 src/registration/Registration.Service/Controllers/StaticDataController.cs create mode 100644 tests/registration/Registration.Service.Tests/BusinessLogic/StaticDataBusinessLogicTest.cs create mode 100644 tests/registration/Registration.Service.Tests/Controller/StaticDataControllerTest.cs diff --git a/src/administration/Administration.Service/Controllers/StaticDataController.cs b/src/administration/Administration.Service/Controllers/StaticDataController.cs index 3104faa9f5..90917a22c9 100644 --- a/src/administration/Administration.Service/Controllers/StaticDataController.cs +++ b/src/administration/Administration.Service/Controllers/StaticDataController.cs @@ -1,5 +1,4 @@ /******************************************************************************** - * Copyright (c) 2021, 2023 BMW Group AG * Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional diff --git a/src/portalbackend/PortalBackend.DBAccess/Models/CountryLongNameData.cs b/src/portalbackend/PortalBackend.DBAccess/Models/CountryLongNameData.cs new file mode 100644 index 0000000000..725acf68b2 --- /dev/null +++ b/src/portalbackend/PortalBackend.DBAccess/Models/CountryLongNameData.cs @@ -0,0 +1,36 @@ +/******************************************************************************** + * Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ + +namespace Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Models; + +/// +/// Model for CountryLongNameData +/// ] +/// Language Short Name +/// Language Long Name +/// +public record CountryLongNameData(string Alpha2Code, IEnumerable CountryName); + +/// +/// Model for CountryName +/// +/// language +/// long Description +/// +public record CountryName(string Language, string Value); diff --git a/src/portalbackend/PortalBackend.DBAccess/Repositories/IStaticDataRepository.cs b/src/portalbackend/PortalBackend.DBAccess/Repositories/IStaticDataRepository.cs index 63ae3457ba..1225ce12c0 100644 --- a/src/portalbackend/PortalBackend.DBAccess/Repositories/IStaticDataRepository.cs +++ b/src/portalbackend/PortalBackend.DBAccess/Repositories/IStaticDataRepository.cs @@ -1,5 +1,4 @@ /******************************************************************************** - * Copyright (c) 2021, 2023 BMW Group AG * Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional @@ -40,6 +39,12 @@ public interface IStaticDataRepository /// Returns a async enumerable of IAsyncEnumerable GetAllLanguage(); + /// + /// Get all Countries. + /// + /// AsyncEnumerable of the result Counties with long names + IAsyncEnumerable GetAllCountries(); + /// /// Retrieve Unique Identifier Data for Country Alpha2Code /// diff --git a/src/portalbackend/PortalBackend.DBAccess/Repositories/StaticDataRepository.cs b/src/portalbackend/PortalBackend.DBAccess/Repositories/StaticDataRepository.cs index 94c271346f..d446b8689b 100644 --- a/src/portalbackend/PortalBackend.DBAccess/Repositories/StaticDataRepository.cs +++ b/src/portalbackend/PortalBackend.DBAccess/Repositories/StaticDataRepository.cs @@ -1,5 +1,4 @@ /******************************************************************************** - * Copyright (c) 2021, 2023 BMW Group AG * Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional @@ -57,6 +56,17 @@ public IAsyncEnumerable GetAllLanguage() => )) .AsAsyncEnumerable(); + /// + public IAsyncEnumerable GetAllCountries() => + _dbContext.Countries + .AsNoTracking() + .Select(s => new CountryLongNameData + ( + s.Alpha2Code, + s.CountryLongNames.Select(x => new CountryName(x.ShortName, x.LongName)) + )) + .AsAsyncEnumerable(); + /// public Task<(IEnumerable IdentifierIds, bool IsValidCountryCode)> GetCompanyIdentifiers(string alpha2Code) => _dbContext.Countries diff --git a/src/portalbackend/PortalBackend.PortalEntities/Entities/Country.cs b/src/portalbackend/PortalBackend.PortalEntities/Entities/Country.cs index 766673f236..c0af2c9294 100644 --- a/src/portalbackend/PortalBackend.PortalEntities/Entities/Country.cs +++ b/src/portalbackend/PortalBackend.PortalEntities/Entities/Country.cs @@ -1,5 +1,4 @@ /******************************************************************************** - * Copyright (c) 2021, 2023 BMW Group AG * Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional diff --git a/src/registration/Registration.Service/BusinessLogic/IStaticDataBusinessLogic.cs b/src/registration/Registration.Service/BusinessLogic/IStaticDataBusinessLogic.cs new file mode 100644 index 0000000000..f076910434 --- /dev/null +++ b/src/registration/Registration.Service/BusinessLogic/IStaticDataBusinessLogic.cs @@ -0,0 +1,33 @@ +/******************************************************************************** + * Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ + +using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Models; +using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.PortalEntities.Entities; +using Org.Eclipse.TractusX.Portal.Backend.Registration.Service.Model; + +namespace Org.Eclipse.TractusX.Portal.Backend.Registration.Service.BusinessLogic; + +public interface IStaticDataBusinessLogic +{ + /// + /// Get all Countries. + /// + /// AsyncEnumerable of the result Counties with long names + IAsyncEnumerable GetAllCountries(); +} diff --git a/src/registration/Registration.Service/BusinessLogic/StaticDataBusinessLogic.cs b/src/registration/Registration.Service/BusinessLogic/StaticDataBusinessLogic.cs new file mode 100644 index 0000000000..73e518d27c --- /dev/null +++ b/src/registration/Registration.Service/BusinessLogic/StaticDataBusinessLogic.cs @@ -0,0 +1,42 @@ +/******************************************************************************** + * Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ + +using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess; +using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Models; +using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Repositories; + +namespace Org.Eclipse.TractusX.Portal.Backend.Registration.Service.BusinessLogic; + +public class StaticDataBusinessLogic : IStaticDataBusinessLogic +{ + private readonly IPortalRepositories _portalRepositories; + + /// + /// Constructor + /// + /// + public StaticDataBusinessLogic(IPortalRepositories portalRepositories) + { + _portalRepositories = portalRepositories; + } + + /// + public IAsyncEnumerable GetAllCountries() => + _portalRepositories.GetInstance().GetAllCountries(); +} diff --git a/src/registration/Registration.Service/Controllers/StaticDataController.cs b/src/registration/Registration.Service/Controllers/StaticDataController.cs new file mode 100644 index 0000000000..19c5e5824e --- /dev/null +++ b/src/registration/Registration.Service/Controllers/StaticDataController.cs @@ -0,0 +1,57 @@ +/******************************************************************************** + * Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ + +using Microsoft.AspNetCore.Mvc; +using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Models; +using Org.Eclipse.TractusX.Portal.Backend.Registration.Service.BusinessLogic; + +namespace Org.Eclipse.TractusX.Portal.Backend.Registration.Service.Controllers +{ + + [ApiController] + [Route("api/registration/[controller]")] + [Produces("application/json")] + [Consumes("application/json")] + public class StaticDataController : ControllerBase + { + private readonly IStaticDataBusinessLogic _staticDataBusinessLogic; + + /// + /// Creates a new instance of + /// + /// Access to the business logic + public StaticDataController(IStaticDataBusinessLogic staticDataBusinessLogic) + { + _staticDataBusinessLogic = staticDataBusinessLogic; + } + + /// + /// Retrieve all app countries - short name (2digit) and countries long name + /// + /// AsyncEnumerable of Countries Long name Data + /// + /// Example: GET: /api/registration/staticdata/countrylist + /// Returns a list of all countries long name with language code i.e german and english + [HttpGet] + [Route("countrylist")] + [ProducesResponseType(typeof(IAsyncEnumerable), StatusCodes.Status200OK)] + public IAsyncEnumerable GetCountries() => + _staticDataBusinessLogic.GetAllCountries(); + } +} diff --git a/src/registration/Registration.Service/Program.cs b/src/registration/Registration.Service/Program.cs index 7469e19b6c..577cc3d67b 100644 --- a/src/registration/Registration.Service/Program.cs +++ b/src/registration/Registration.Service/Program.cs @@ -39,7 +39,7 @@ .AddProvisioningManager(builder.Configuration); builder.Services.AddTransient(); - + builder.Services.AddTransient(); builder.Services.AddTransient() .ConfigureRegistrationSettings(builder.Configuration.GetSection("Registration")) .AddTransient(); diff --git a/tests/administration/Administration.Service.Tests/Controllers/StaticDataControllerTest.cs b/tests/administration/Administration.Service.Tests/Controllers/StaticDataControllerTest.cs index 2df42691a2..86a0f6be3a 100644 --- a/tests/administration/Administration.Service.Tests/Controllers/StaticDataControllerTest.cs +++ b/tests/administration/Administration.Service.Tests/Controllers/StaticDataControllerTest.cs @@ -1,5 +1,4 @@ /******************************************************************************** - * Copyright (c) 2021, 2023 BMW Group AG * Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation * * See the NOTICE file(s) distributed with this work for additional @@ -33,7 +32,7 @@ public StaticDataControllerTest() { _fixture = new Fixture(); _logic = A.Fake(); - this._controller = new StaticDataController(_logic); + _controller = new StaticDataController(_logic); } [Fact] diff --git a/tests/registration/Registration.Service.Tests/BusinessLogic/StaticDataBusinessLogicTest.cs b/tests/registration/Registration.Service.Tests/BusinessLogic/StaticDataBusinessLogicTest.cs new file mode 100644 index 0000000000..8ef0ceded6 --- /dev/null +++ b/tests/registration/Registration.Service.Tests/BusinessLogic/StaticDataBusinessLogicTest.cs @@ -0,0 +1,73 @@ +/******************************************************************************** + * Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ +using AutoFixture; +using AutoFixture.AutoFakeItEasy; +using FakeItEasy; +using FluentAssertions; +using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess; +using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Models; +using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Repositories; +using Org.Eclipse.TractusX.Portal.Backend.Registration.Service.BusinessLogic; +using System.Collections.Immutable; +using Xunit; + +namespace Org.Eclipse.TractusX.Portal.Backend.Registration.Service.Tests; + +public class StaticDataBusinessLogicTest +{ + private readonly IFixture _fixture; + private readonly IPortalRepositories _portalRepositories; + private readonly ICompanyRepository _companyRepository; + private readonly IStaticDataRepository _staticDataRepository; + + public StaticDataBusinessLogicTest() + { + _fixture = new Fixture().Customize(new AutoFakeItEasyCustomization { ConfigureMembers = true }); + _fixture.Behaviors.OfType().ToList() + .ForEach(b => _fixture.Behaviors.Remove(b)); + _fixture.Behaviors.Add(new OmitOnRecursionBehavior()); + + _companyRepository = A.Fake(); + _staticDataRepository = A.Fake(); + _portalRepositories = A.Fake(); + + A.CallTo(() => _portalRepositories.GetInstance()).Returns(_companyRepository); + A.CallTo(() => _portalRepositories.GetInstance()).Returns(_staticDataRepository); + } + + [Fact] + public async Task GetAllCountries_ReturnsExpectedResult() + { + // Arrange + var data = _fixture.CreateMany(3).ToImmutableArray(); + + A.CallTo(() => _staticDataRepository.GetAllCountries()) + .Returns(data.ToAsyncEnumerable()); + + var sut = new StaticDataBusinessLogic(_portalRepositories); + + // Act + var result = await sut.GetAllCountries().ToListAsync().ConfigureAwait(false); + + // Assert + A.CallTo(() => _staticDataRepository.GetAllCountries()) + .MustHaveHappenedOnceExactly(); + result.Should().HaveCount(3).And.ContainInOrder(data); + } +} diff --git a/tests/registration/Registration.Service.Tests/Controller/StaticDataControllerTest.cs b/tests/registration/Registration.Service.Tests/Controller/StaticDataControllerTest.cs new file mode 100644 index 0000000000..3b91340b34 --- /dev/null +++ b/tests/registration/Registration.Service.Tests/Controller/StaticDataControllerTest.cs @@ -0,0 +1,57 @@ +/******************************************************************************** + * Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ + +using AutoFixture; +using FakeItEasy; +using FluentAssertions; +using Org.Eclipse.TractusX.Portal.Backend.PortalBackend.DBAccess.Models; +using Org.Eclipse.TractusX.Portal.Backend.Registration.Service.BusinessLogic; +using Org.Eclipse.TractusX.Portal.Backend.Registration.Service.Controllers; +using Xunit; + +namespace Org.Eclipse.TractusX.Portal.Backend.Registration.Service.Tests; + +public class StaticDataControllerTest +{ + private readonly IStaticDataBusinessLogic _logic; + private readonly StaticDataController _controller; + private readonly Fixture _fixture; + public StaticDataControllerTest() + { + _fixture = new Fixture(); + _logic = A.Fake(); + _controller = new StaticDataController(_logic); + } + + [Fact] + public async Task GetCountries_ReturnsExpectedResult() + { + //Arrange + var data = _fixture.CreateMany(5).ToAsyncEnumerable(); + A.CallTo(() => _logic.GetAllCountries()) + .Returns(data); + + //Act + var result = await _controller.GetCountries().ToListAsync().ConfigureAwait(false); + + // Assert + A.CallTo(() => _logic.GetAllCountries()).MustHaveHappenedOnceExactly(); + result.Should().HaveCount(5).And.AllBeOfType(); + } +}