Skip to content

Commit

Permalink
applied the suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
dhiren-singh-007 committed Aug 13, 2024
1 parent a906833 commit 2a4e2f9
Show file tree
Hide file tree
Showing 11 changed files with 16 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public async Task CreateCompanyCertificate(CompanyCertificateCreationData data,
throw new ControllerArgumentException("ValidTill date should be greater than current date");
}

if (!string.IsNullOrEmpty(data.Issuer) && !ValidationExpressionsValidator.IsValidCompanyName(data.Issuer))
if (data.Issuer != null && !data.Issuer.IsValidCompanyName())
{
throw ControllerArgumentException.Create(ValidationExpressionErrors.INCORRECT_COMPANY_NAME, [new("name", nameof(data.Issuer))]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,7 @@ public Task ExecuteInvitation(CompanyInvitationData invitationData)
throw new ControllerArgumentException("email must not be empty", "email");
}

if (string.IsNullOrWhiteSpace(invitationData.OrganisationName))
{
throw new ControllerArgumentException("organisationName must not be empty", "organisationName");
}

if (!string.IsNullOrEmpty(invitationData.OrganisationName) && !ValidationExpressionsValidator.IsValidCompanyName(invitationData.OrganisationName))
if (invitationData.OrganisationName == null || !invitationData.OrganisationName.IsValidCompanyName())
{
throw ControllerArgumentException.Create(ValidationExpressionErrors.INCORRECT_COMPANY_NAME, [new("name", nameof(invitationData.OrganisationName))]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class NetworkBusinessLogic(

public async Task HandlePartnerRegistration(PartnerRegistrationData data)
{
if (!ValidationExpressionsValidator.IsValidCompanyName(data.Name))
if (!data.Name.IsValidCompanyName())
{
throw ControllerArgumentException.Create(ValidationExpressionErrors.INCORRECT_COMPANY_NAME, [new("name", "OrganisationName")]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private async Task<CompanyWithAddressData> GetCompanyWithAddressAsyncInternal(Gu
{
throw NotFoundException.Create(AdministrationRegistrationErrors.APPLICATION_NOT_FOUND, [new("applicationId", applicationId.ToString())]);
}
if (!ValidationExpressionsValidator.IsValidCompanyName(companyWithAddress.Name))
if (!companyWithAddress.Name.IsValidCompanyName())
{
throw ControllerArgumentException.Create(ValidationExpressionErrors.INCORRECT_COMPANY_NAME, [new("name", "OrganisationName")]);
}
Expand Down Expand Up @@ -123,7 +123,7 @@ private async Task<CompanyWithAddressData> GetCompanyWithAddressAsyncInternal(Gu

public Task<Pagination.Response<CompanyApplicationDetails>> GetCompanyApplicationDetailsAsync(int page, int size, CompanyApplicationStatusFilter? companyApplicationStatusFilter, string? companyName)
{
if (!string.IsNullOrEmpty(companyName) && !ValidationExpressionsValidator.IsValidCompanyName(companyName))
if (companyName != null && !companyName.IsValidCompanyName())
{
throw ControllerArgumentException.Create(ValidationExpressionErrors.INCORRECT_COMPANY_NAME, [new("name", "CompanyName")]);
}
Expand Down Expand Up @@ -163,7 +163,7 @@ private async Task<CompanyWithAddressData> GetCompanyWithAddressAsyncInternal(Gu

public Task<Pagination.Response<CompanyDetailsOspOnboarding>> GetOspCompanyDetailsAsync(int page, int size, CompanyApplicationStatusFilter? companyApplicationStatusFilter, string? companyName)
{
if (!string.IsNullOrEmpty(companyName) && !ValidationExpressionsValidator.IsValidCompanyName(companyName))
if (companyName != null && !companyName.IsValidCompanyName())
{
throw ControllerArgumentException.Create(ValidationExpressionErrors.INCORRECT_COMPANY_NAME, [new("name", "CompanyName")]);
}
Expand Down Expand Up @@ -198,7 +198,7 @@ private async Task<CompanyWithAddressData> GetCompanyWithAddressAsyncInternal(Gu
}
public Task<Pagination.Response<CompanyApplicationWithCompanyUserDetails>> GetAllCompanyApplicationsDetailsAsync(int page, int size, string? companyName)
{
if (!ValidationExpressionsValidator.IsValidCompanyName(companyName))
if (companyName != null && !companyName.IsValidCompanyName())
{
throw ControllerArgumentException.Create(ValidationExpressionErrors.INCORRECT_COMPANY_NAME, [new("name", "CompanyName")]);
}
Expand Down
1 change: 0 additions & 1 deletion src/framework/Framework.Models/ValidationExpressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,4 @@ public static class ValidationExpressions
public const string Bpns = @"^(BPNS|bpns)[\w|\d]{12}$";
public const string Company = @"^(?!.*\s$)([\wÀ-ÿ£$€¥¢@%*+\-/\\,.:;=<>!?&^#'\x22()[\]]\s?){1,160}$";
public const string ExternalCertificateNumber = @"^[a-zA-Z0-9]{0,36}$";
public static readonly Regex CompanyRegex = new(ValidationExpressions.Company, RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture, TimeSpan.FromSeconds(1));
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ namespace Org.Eclipse.TractusX.Portal.Backend.Framework.Models;

public static class ValidationExpressionsValidator
{
public static bool IsValidCompanyName(string companyName)
private static readonly Regex CompanyRegex = new(ValidationExpressions.Company, RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture, TimeSpan.FromSeconds(1));
public static bool IsValidCompanyName(this string companyName)
{
return ValidationExpressions.CompanyRegex.IsMatch(companyName);
return CompanyRegex.IsMatch(companyName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public Task<Guid> AddAppAsync(AppRequestModel appRequestModel)
throw new ControllerArgumentException("Use Case Ids must not be null or empty", nameof(appRequestModel.UseCaseIds));
}

if (!string.IsNullOrEmpty(appRequestModel.Provider) && !ValidationExpressionsValidator.IsValidCompanyName(appRequestModel.Provider))
if (appRequestModel.Provider != null && !appRequestModel.Provider.IsValidCompanyName())
{
throw ControllerArgumentException.Create(ValidationExpressionErrors.INCORRECT_COMPANY_NAME, [new("name", nameof(appRequestModel.Provider))]);
}
Expand Down Expand Up @@ -267,7 +267,7 @@ public async Task UpdateAppReleaseAsync(Guid appId, AppRequestModel appRequestMo
throw new ForbiddenException($"Company {companyId} is not the app provider.");
}

if (!string.IsNullOrEmpty(appRequestModel.Provider) && !ValidationExpressionsValidator.IsValidCompanyName(appRequestModel.Provider))
if (appRequestModel.Provider != null && !appRequestModel.Provider.IsValidCompanyName())
{
throw ControllerArgumentException.Create(ValidationExpressionErrors.INCORRECT_COMPANY_NAME, [new("name", nameof(appRequestModel.Provider))]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public async Task AddFavouriteAppForUserAsync(Guid appId)
/// <inheritdoc/>
public async Task<Pagination.Response<OfferCompanySubscriptionStatusResponse>> GetCompanyProvidedAppSubscriptionStatusesForUserAsync(int page, int size, SubscriptionStatusSorting? sorting, OfferSubscriptionStatusId? statusId, Guid? offerId, string? companyName = null)
{
if (!string.IsNullOrEmpty(companyName) && !ValidationExpressionsValidator.IsValidCompanyName(companyName))
if (companyName != null && !companyName.IsValidCompanyName())
{
throw ControllerArgumentException.Create(ValidationExpressionErrors.INCORRECT_COMPANY_NAME, [new("name", "CompanyName")]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public Task<OfferAutoSetupResponseData> AutoSetupServiceAsync(OfferAutoSetupData
/// <inheritdoc/>
public async Task<Pagination.Response<OfferCompanySubscriptionStatusResponse>> GetCompanyProvidedServiceSubscriptionStatusesForUserAsync(int page, int size, SubscriptionStatusSorting? sorting, OfferSubscriptionStatusId? statusId, Guid? offerId, string? companyName = null)
{
if (!string.IsNullOrEmpty(companyName) && !ValidationExpressionsValidator.IsValidCompanyName(companyName))
if (companyName != null && !companyName.IsValidCompanyName())
{
throw ControllerArgumentException.Create(ValidationExpressionErrors.INCORRECT_COMPANY_NAME, [new("name", "CompanyName")]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,6 @@ public async Task ExecuteInvitation_WithoutEmail_ThrowsControllerArgumentExcepti
ex.ParamName.Should().Be("email");
}

[Fact]
public async Task ExecuteInvitation_WithoutOrganisationName_ThrowsControllerArgumentException()
{
var invitationData = _fixture.Build<CompanyInvitationData>()
.With(x => x.OrganisationName, (string?)null)
.WithNamePattern(x => x.FirstName)
.WithNamePattern(x => x.LastName)
.WithEmailPattern(x => x.Email)
.Create();

async Task Act() => await _sut.ExecuteInvitation(invitationData);

var ex = await Assert.ThrowsAsync<ControllerArgumentException>(Act);
ex.Message.Should().Be("organisationName must not be empty (Parameter 'organisationName')");
ex.ParamName.Should().Be("organisationName");
}

[Fact]
public async Task ExecuteInvitation_WithValidData_CreatesExpected()
{
Expand Down Expand Up @@ -140,6 +123,7 @@ public async Task ExecuteInvitation_WithValidOrganisationName_DoesNotThrowExcept
}

[Theory]
[InlineData((string?)null)] // null value

Check warning on line 126 in tests/administration/Administration.Service.Tests/BusinessLogic/InvitationBusinessLogicTests.cs

View workflow job for this annotation

GitHub Actions / Build, check and test services (8.0)

Null should not be used for type parameter 'invalidName' of type 'string'. Use a non-null value, or convert the parameter to a nullable type.

Check warning

Code scanning / CodeQL

Useless upcast Warning test

There is no need to upcast from
null
to
String
- the conversion can be done implicitly.
[InlineData("Organisation Name ")] // Ends with whitespace
[InlineData("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWX")] // Exceeds 160 characters
public async Task ExecuteInvitation_WithInvalidOrganisationName_ThrowsControllerArgumentException(string invalidName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class ValidationExpressionsTests
[InlineData(" ", false)] // Multiple spaces
public void TestCompanyNameRegex(string companyName, bool expectedResult)
{
var result = ValidationExpressionsValidator.IsValidCompanyName(companyName);
var result = companyName.IsValidCompanyName();
Assert.Equal(expectedResult, result);
}
}

0 comments on commit 2a4e2f9

Please sign in to comment.