public async Task TwoResponses_ErrorOnFirstGet_ReturnsError() { // Arrange var expected = new object(); const int payload2 = 5000; const string payload3 = "test-2"; const string message1 = "test-error-1"; const string message2 = "test-error-2"; var results = new List <object>(); var response1 = ResponseStub <decimal> .Error(message1); var response2 = ResponseStub <int> .Success(payload2); var transformed1 = ResponseStub <string> .Success(payload3); var errorResponse2 = ResponseStub <string> .Error(message2); var responseComposer = GetMock <IResponseComposer>(); responseComposer.Setup(x => x.From <decimal, string>(response1)).Returns(errorResponse2); responseComposer.Setup(x => x.Success(payload3)).Returns(transformed1); var logger = new LoggerStub(); var page = GetMock <IPageModelBase <object> >(); page.Setup(x => x.Logger).Returns(logger); page.Setup(x => x.Return(errorResponse2)).Returns(expected); // Act var result = await ResponsePipeline <object> .Get(() => Task.FromResult(response1)) .Get(r => { results.Add(r); return(Task.FromResult(response2)); }) .Combine((p, r) => { results.Add(p); results.Add(r); return(payload3); }) .EvaluateAsync(page, responseComposer); // Assert result.Should().Be(expected); results.Should().BeEmpty(); logger.Trace.Should().BeEmpty(); logger.Error.Should() .Contain(response1) .And.HaveCount(1); logger.Debug.Should().BeEmpty(); logger.Information.Should().BeEmpty(); logger.Warning.Should().BeEmpty(); logger.Critical.Should().BeEmpty(); }
public async Task SingleResponse_WithFailedTry_ReturnsResult() { // Arrange const decimal payload = 1000m; var response = GetMock <IResponse <decimal> >(); response.Setup(x => x.Succeeded).Returns(true); response.Setup(x => x.Payload).Returns(payload); var results = new List <object>(); var expected = new object(); const string expectedError = "E"; var negativeTry = ResponseStub.Error(expectedError); var errorResult = ResponseStub <decimal> .Error("E2"); var responseComposer = GetMock <IResponseComposer>(); responseComposer .Setup(x => x.From <decimal>(negativeTry)) .Returns(errorResult); var logger = new LoggerStub(); var page = GetMock <IPageModelBase <object> >(); page.Setup(x => x.Logger).Returns(logger); page.Setup(x => x.Return(errorResult)).Returns(expected); // Act var result = await ResponsePipeline <object> .Get(() => Task.FromResult(response)) .Try(r => { results.Add(r); return(Task.FromResult(negativeTry)); }) .EvaluateAsync(page, responseComposer, null, null); // Assert result.Should().Be(expected); results.Should().Contain(payload).And.HaveCount(1); logger.Trace.Should().Contain(response).And.HaveCount(1); logger.Error.Should().Contain(negativeTry) .And.HaveCount(1); logger.Debug.Should().BeEmpty(); logger.Information.Should().BeEmpty(); logger.Warning.Should().BeEmpty(); logger.Critical.Should().BeEmpty(); }
public async Task SingleResponse_WithTransform_WithCustomHandler_ReturnsResult() { // Arrange const string expectedError = "error"; var response = ResponseStub <decimal> .Error(expectedError); var responseComposer = GetMock <IResponseComposer>(); const string expected = "expected"; var transformed = new ResponseStub <string>() { Succeeded = true, Payload = expected }; var logger = new LoggerStub(); var page = GetMock <IPageModelBase <object> >(); page.Setup(x => x.Logger).Returns(logger); page.Setup(x => x.Return(response)).Returns(new object()); string receivedValue = null; // Act var result = await ResponsePipeline <object> .Get(() => Task.FromResult(response)) .Transform(r => Task.FromResult((IResponse <string>)transformed)) .Evaluate(page, responseComposer, ((value, p) => { receivedValue = value; return(p); }), null); // Assert result.Should().Be(page); receivedValue.Should().Be(expected); logger.Error.Should().Contain(x => x is IResponse <decimal> && !((IResponse <decimal>)x).Succeeded && ((IResponse <decimal>)x).Message == expectedError).And.HaveCount(1); logger.Trace.Should().Contain(transformed).And.HaveCount(1); logger.Debug.Should().BeEmpty(); logger.Information.Should().BeEmpty(); logger.Warning.Should().BeEmpty(); logger.Critical.Should().BeEmpty(); }