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 GetMock() { var mock = new Mock(); 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( Task.FromResult(putResponse), Task.FromResult(metadata), () => new Grpc.Core.Status(StatusCode.OK, string.Empty), () => metadata, () => { }); mock.Setup(x => x.PutAsync( It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) .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(), It.IsAny(), It.IsAny(), It.IsAny())) .Returns((GetRequest r, Metadata m, DateTime? dt, CancellationToken ct) => { Verifier.CheckRequest(r); if (ReturnContainerRemoved) { return new AsyncUnaryCall( Task.FromResult(getNoContainerResponse), Task.FromResult(ResponseMetaData), () => new Grpc.Core.Status(StatusCode.NotFound, string.Empty), () => ResponseMetaData, () => { }); } return new AsyncUnaryCall( 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(), It.IsAny(), It.IsAny(), It.IsAny())) .Returns((ListRequest r, Metadata m, DateTime? dt, CancellationToken ct) => { Verifier.CheckRequest(r); return new AsyncUnaryCall( Task.FromResult(listResponse), Task.FromResult(ResponseMetaData), () => new Grpc.Core.Status(StatusCode.OK, string.Empty), () => ResponseMetaData, () => { }); }); var v = mock.Setup(x => x.DeleteAsync( It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) .Returns((DeleteRequest r, Metadata m, DateTime? dt, CancellationToken ct) => { Requests.Add(new RequestData(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( Task.FromResult(response), Task.FromResult(metadata), () => new Grpc.Core.Status(StatusCode.OK, string.Empty), () => metadata, () => { }); }); return mock; } public bool ReturnContainerRemoved { get; set; } public Collection ContainerIds { get; } = []; public Collection> Requests { get; } = []; }