frostfs-sdk-csharp/src/FrostFS.SDK.Tests/Mocks/ContainerServiceMocks/GetContainerMock.cs
Pavel Gross c9a75ea025
All checks were successful
DCO / DCO (pull_request) Successful in 46s
[#24] Client: Implement pool part1
first iteration - base classes and methods

Signed-off-by: Pavel Gross <p.gross@yadro.com>
2024-10-21 10:48:00 +03:00

188 lines
6.2 KiB
C#

using System.Collections.ObjectModel;
using FrostFS.Container;
using FrostFS.Refs;
using FrostFS.SDK.ClientV2;
using FrostFS.SDK.ClientV2.Mappers.GRPC;
using FrostFS.SDK.Cryptography;
using FrostFS.Session;
using Google.Protobuf;
using Grpc.Core;
using Moq;
namespace FrostFS.SDK.Tests;
public class ContainerMocker(string key) : ContainerServiceBase(key)
{
public override Mock<ContainerService.ContainerServiceClient> GetMock()
{
var mock = new Mock<ContainerService.ContainerServiceClient>();
var grpcVersion = Version.ToMessage();
PutResponse putResponse = new()
{
Body = new PutResponse.Types.Body
{
ContainerId = new ContainerID
{
Value = ByteString.CopyFrom(ContainerGuid.ToBytes())
}
},
MetaHeader = new ResponseMetaHeader
{
Version = (Version is null ? DefaultVersion : Version).ToMessage(),
Epoch = 100,
Ttl = 1
}
};
putResponse.VerifyHeader = GetResponseVerificationHeader(putResponse);
var metadata = new Metadata();
using var putContainerResponse = new AsyncUnaryCall<PutResponse>(
Task.FromResult(putResponse),
Task.FromResult(metadata),
() => new Grpc.Core.Status(StatusCode.OK, string.Empty),
() => metadata,
() => { });
mock.Setup(x => x.PutAsync(
It.IsAny<PutRequest>(),
It.IsAny<Metadata>(),
It.IsAny<DateTime?>(),
It.IsAny<CancellationToken>()))
.Returns((PutRequest r, Metadata m, DateTime? dt, CancellationToken ct) =>
{
Verifier.CheckRequest(r);
return putContainerResponse;
});
var getResponse = new GetResponse
{
Body = new GetResponse.Types.Body
{
Container = new Container.Container
{
Version = grpcVersion,
Nonce = ByteString.CopyFrom(ContainerGuid.ToBytes()),
PlacementPolicy = PlacementPolicy.GetPolicy()
}
},
MetaHeader = ResponseMetaHeader
};
getResponse.VerifyHeader = GetResponseVerificationHeader(getResponse);
var getNoContainerResponse = new GetResponse
{
Body = new(),
MetaHeader = new ResponseMetaHeader
{
Status = new Status.Status
{
Code = 3072,
Message = "container not found"
}
}
};
getNoContainerResponse.VerifyHeader = GetResponseVerificationHeader(getNoContainerResponse);
mock.Setup(x => x.GetAsync(
It.IsAny<GetRequest>(),
It.IsAny<Metadata>(),
It.IsAny<DateTime?>(),
It.IsAny<CancellationToken>()))
.Returns((GetRequest r, Metadata m, DateTime? dt, CancellationToken ct) =>
{
Verifier.CheckRequest(r);
if (ReturnContainerRemoved)
{
return new AsyncUnaryCall<GetResponse>(
Task.FromResult(getNoContainerResponse),
Task.FromResult(ResponseMetaData),
() => new Grpc.Core.Status(StatusCode.NotFound, string.Empty),
() => ResponseMetaData,
() => { });
}
return new AsyncUnaryCall<GetResponse>(
Task.FromResult(getResponse),
Task.FromResult(ResponseMetaData),
() => new Grpc.Core.Status(StatusCode.OK, string.Empty),
() => ResponseMetaData,
() => { });
});
var listResponse = new ListResponse
{
Body = new ListResponse.Types.Body(),
MetaHeader = ResponseMetaHeader
};
foreach (var item in ContainerIds)
{
listResponse.Body.ContainerIds.Add(new ContainerID { Value = ByteString.CopyFrom(item) });
}
listResponse.VerifyHeader = GetResponseVerificationHeader(listResponse);
mock.Setup(x => x.ListAsync(
It.IsAny<ListRequest>(),
It.IsAny<Metadata>(),
It.IsAny<DateTime?>(),
It.IsAny<CancellationToken>()))
.Returns((ListRequest r, Metadata m, DateTime? dt, CancellationToken ct) =>
{
Verifier.CheckRequest(r);
return new AsyncUnaryCall<ListResponse>(
Task.FromResult(listResponse),
Task.FromResult(ResponseMetaData),
() => new Grpc.Core.Status(StatusCode.OK, string.Empty),
() => ResponseMetaData,
() => { });
});
var v = mock.Setup(x => x.DeleteAsync(
It.IsAny<DeleteRequest>(),
It.IsAny<Metadata>(),
It.IsAny<DateTime?>(),
It.IsAny<CancellationToken>()))
.Returns((DeleteRequest r, Metadata m, DateTime? dt, CancellationToken ct) =>
{
Requests.Add(new RequestData<DeleteRequest>(r, m, dt, ct));
var response = new DeleteResponse
{
Body = new DeleteResponse.Types.Body(),
MetaHeader = new ResponseMetaHeader()
};
var metadata = new Metadata();
response.VerifyHeader = GetResponseVerificationHeader(response);
return new AsyncUnaryCall<DeleteResponse>(
Task.FromResult(response),
Task.FromResult(metadata),
() => new Grpc.Core.Status(StatusCode.OK, string.Empty),
() => metadata,
() => { });
});
return mock;
}
public bool ReturnContainerRemoved { get; set; }
public Collection<byte[]> ContainerIds { get; } = [];
public Collection<RequestData<DeleteRequest>> Requests { get; } = [];
}