Skip to content

Commit

Permalink
feat(countrylist): Add new endpoint getAllCountries (#347)
Browse files Browse the repository at this point in the history
* countrylist endpoint returning internationalized names of all countries
---------
Co-authored-by: Norbert Truchsess <[email protected]>
Reviewed-by: Norbert Truchsess <[email protected]>
  • Loading branch information
AnuragNagpure authored Nov 29, 2023
1 parent 4e1c4ba commit 1989515
Show file tree
Hide file tree
Showing 12 changed files with 317 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Model for CountryLongNameData
/// </summary>]
/// <param name="Alpha2Code">Language Short Name</param>
/// <param name="CountryName">Language Long Name</param>
/// <returns></returns>
public record CountryLongNameData(string Alpha2Code, IEnumerable<CountryName> CountryName);

/// <summary>
/// Model for CountryName
/// </summary>
/// <param name="Language">language</param>
/// <param name="value">long Description</param>
/// <returns></returns>
public record CountryName(string Language, string Value);
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -40,6 +39,12 @@ public interface IStaticDataRepository
/// <returns>Returns a async enumerable of <see cref="LanguageData"/></returns>
IAsyncEnumerable<LanguageData> GetAllLanguage();

/// <summary>
/// Get all Countries.
/// </summary>
/// <returns>AsyncEnumerable of the result Counties with long names</returns>
IAsyncEnumerable<CountryLongNameData> GetAllCountries();

/// <summary>
/// Retrieve Unique Identifier Data for Country Alpha2Code
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -57,6 +56,17 @@ public IAsyncEnumerable<LanguageData> GetAllLanguage() =>
))
.AsAsyncEnumerable();

///<inheritdoc />
public IAsyncEnumerable<CountryLongNameData> GetAllCountries() =>
_dbContext.Countries
.AsNoTracking()
.Select(s => new CountryLongNameData
(
s.Alpha2Code,
s.CountryLongNames.Select(x => new CountryName(x.ShortName, x.LongName))
))
.AsAsyncEnumerable();

///<inheritdoc />
public Task<(IEnumerable<UniqueIdentifierId> IdentifierIds, bool IsValidCountryCode)> GetCompanyIdentifiers(string alpha2Code) =>
_dbContext.Countries
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Get all Countries.
/// </summary>
/// <returns>AsyncEnumerable of the result Counties with long names</returns>
IAsyncEnumerable<CountryLongNameData> GetAllCountries();
}
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Constructor
/// </summary>
/// <param name="portalRepositories"></param>
public StaticDataBusinessLogic(IPortalRepositories portalRepositories)
{
_portalRepositories = portalRepositories;
}

/// <inheritdoc/>
public IAsyncEnumerable<CountryLongNameData> GetAllCountries() =>
_portalRepositories.GetInstance<IStaticDataRepository>().GetAllCountries();
}
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Creates a new instance of <see cref="StaticDataController"/>
/// </summary>
/// <param name="staticDataBusinessLogic">Access to the business logic</param>
public StaticDataController(IStaticDataBusinessLogic staticDataBusinessLogic)
{
_staticDataBusinessLogic = staticDataBusinessLogic;
}

/// <summary>
/// Retrieve all app countries - short name (2digit) and countries long name
/// </summary>
/// <returns>AsyncEnumerable of Countries Long name Data</returns>
/// <remarks>
/// Example: GET: /api/registration/staticdata/countrylist
/// <response code="200">Returns a list of all countries long name with language code i.e german and english</response>
[HttpGet]
[Route("countrylist")]
[ProducesResponseType(typeof(IAsyncEnumerable<CountryLongNameData>), StatusCodes.Status200OK)]
public IAsyncEnumerable<CountryLongNameData> GetCountries() =>
_staticDataBusinessLogic.GetAllCountries();
}
}
2 changes: 1 addition & 1 deletion src/registration/Registration.Service/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
.AddProvisioningManager(builder.Configuration);
builder.Services.AddTransient<IUserProvisioningService, UserProvisioningService>();
builder.Services.AddTransient<IStaticDataBusinessLogic, StaticDataBusinessLogic>();
builder.Services.AddTransient<IRegistrationBusinessLogic, RegistrationBusinessLogic>()
.ConfigureRegistrationSettings(builder.Configuration.GetSection("Registration"))
.AddTransient<INetworkBusinessLogic, NetworkBusinessLogic>();
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -33,7 +32,7 @@ public StaticDataControllerTest()
{
_fixture = new Fixture();
_logic = A.Fake<IStaticDataBusinessLogic>();
this._controller = new StaticDataController(_logic);
_controller = new StaticDataController(_logic);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ThrowingRecursionBehavior>().ToList()
.ForEach(b => _fixture.Behaviors.Remove(b));
_fixture.Behaviors.Add(new OmitOnRecursionBehavior());

_companyRepository = A.Fake<ICompanyRepository>();
_staticDataRepository = A.Fake<IStaticDataRepository>();
_portalRepositories = A.Fake<IPortalRepositories>();

A.CallTo(() => _portalRepositories.GetInstance<ICompanyRepository>()).Returns(_companyRepository);
A.CallTo(() => _portalRepositories.GetInstance<IStaticDataRepository>()).Returns(_staticDataRepository);
}

[Fact]
public async Task GetAllCountries_ReturnsExpectedResult()
{
// Arrange
var data = _fixture.CreateMany<CountryLongNameData>(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);
}
}
Original file line number Diff line number Diff line change
@@ -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<IStaticDataBusinessLogic>();
_controller = new StaticDataController(_logic);
}

[Fact]
public async Task GetCountries_ReturnsExpectedResult()
{
//Arrange
var data = _fixture.CreateMany<CountryLongNameData>(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<CountryLongNameData>();
}
}

0 comments on commit 1989515

Please sign in to comment.