public async Task GetOrAddAsync_ReturnsComplexItemFromCacheIfPresent() { Mock <IDistributedCache> cacheMock = new Mock <IDistributedCache>(); CancellationTokenSource cts = new CancellationTokenSource(1000); var service = new ServiceDTO { Id = Guid.NewGuid(), Code = "dummyservice" }; byte[] valueBytes = service.ToByteArray(); cacheMock.Setup(x => x.GetAsync("dummy", It.IsAny <CancellationToken>()) ).ReturnsAsync(valueBytes); var result = await cacheMock.Object.GetOrAddAsync <ServiceDTO>("dummy", (options) => { return(Task.FromResult(new ServiceDTO { Id = Guid.NewGuid(), Code = "otherservice" })); }, cts.Token); result .Should() .NotBeNull() .And .BeEquivalentTo(service); }
public async Task GetByCodeAsync_ReturnDtoWhenServiceCodeIsPresentInCache() { var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict); handlerMock .Protected() .Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>() ) .ReturnsAsync(new HttpResponseMessage() { StatusCode = HttpStatusCode.OK, Content = new StringContent("[{'id':1,'value':'1'}]"), // return random array }) .Verifiable(); var service = new ServiceDTO { Code = "dummy", Name = "dummy", Id = Guid.NewGuid(), Address = new Uri("http://address.to"), UnkownRoutesPassthrough = true, KnownRoutes = new List <KnownRouteDTO> { new KnownRouteDTO { Id = Guid.NewGuid(), MethodName = "GET", RelativeUrl = "/path" } } }; cacheMock.Setup(x => x.GetAsync(It.IsAny <string>(), It.IsAny <CancellationToken>()) ).ReturnsAsync(service.ToByteArray()); var httpClient = new HttpClient(handlerMock.Object) { BaseAddress = new Uri("http://api.address/"), }; AnonymousServiceHttpClient client = new AnonymousServiceHttpClient(httpClient, loggerMock.Object, cacheMock.Object); CancellationTokenSource cts = new CancellationTokenSource(1000); var result = await client.GetByCodeAsync("dummy", cts.Token); result .Should() .NotBeNull() .And .BeOfType <ServiceDTO>() .And .BeEquivalentTo <ServiceDTO>(service); }
public void Serialization_SupportsComplexObject() { ServiceDTO dto = new ServiceDTO { Id = Guid.NewGuid(), Code = "dummy" }; byte[] serialized = dto.ToByteArray(); serialized .Should() .NotBeNullOrEmpty(); var result = serialized.FromByteArray <ServiceDTO>(); result .Should() .NotBeNull() .And .BeEquivalentTo <ServiceDTO>(dto); }