Skip to content

Commit

Permalink
Fixed empty task creations throwing
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrasseur-aneo committed Oct 27, 2023
1 parent 0cfa50b commit 64e8a0d
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions Common/src/Injection/ServiceCollectionExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,6 @@ public static IServiceCollection ValidateGrpcRequests(this IServiceCollection se
.AddValidator<ListApplicationsRequestValidator>()
.AddValidator<ListPartitionsRequestValidator>()
.AddValidator<EventSubscriptionRequestValidator>()
.AddValidator<SubmitTasksRequestValidator>()
.AddGrpcValidation();
}
37 changes: 37 additions & 0 deletions Common/src/gRPC/Validators/SubmitTaskRequestValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// This file is part of the ArmoniK project
//
// Copyright (C) ANEO, 2021-2023. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY, without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

using ArmoniK.Api.gRPC.V1.Tasks;

using FluentValidation;

namespace ArmoniK.Core.Common.gRPC.Validators;

/// <summary>
/// Validator for <see cref="SubmitTasksRequest" />
/// </summary>
public class SubmitTasksRequestValidator : AbstractValidator<SubmitTasksRequest>
{
/// <summary>
/// Initializes a validator for <see cref="SubmitTasksRequest" />
/// </summary>
public SubmitTasksRequestValidator()
=> RuleFor(request => request.TaskCreations)
.NotNull()
.NotEmpty()
.WithName(nameof(SubmitTasksRequest.TaskCreations));
}
60 changes: 60 additions & 0 deletions Common/tests/Validators/SubmitTasksRequestValidatorTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// This file is part of the ArmoniK project
//
// Copyright (C) ANEO, 2021-2023. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY, without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

using ArmoniK.Api.gRPC.V1.Tasks;
using ArmoniK.Core.Common.gRPC.Validators;

using NUnit.Framework;

namespace ArmoniK.Core.Common.Tests.Validators;

[TestFixture(TestOf = typeof(SubmitTasksRequestValidator))]
public class SubmitTasksRequestValidatorTest
{
private readonly SubmitTasksRequestValidator validator_ = new();

[Test]
public void EmptyTaskCreationShouldFail()
{
var sf = new SubmitTasksRequest
{
SessionId = "session_id",
};

Assert.IsFalse(validator_.Validate(sf)
.IsValid);
}

[Test]
public void NonEmptyTaskCreationShouldSucceed()
{
var sf = new SubmitTasksRequest
{
SessionId = "session_id",
TaskCreations =
{
new SubmitTasksRequest.Types.TaskCreation
{
PayloadId = "payload_id",
},
},
};

Assert.IsTrue(validator_.Validate(sf)
.IsValid);
}
}

0 comments on commit 64e8a0d

Please sign in to comment.