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_TryAfterTransform_ReturnsResult() { // Arrange const decimal payload = 1000m; const string transformed = "test"; 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(); var responseComposer = GetMock <IResponseComposer>(); var positiveTry = ResponseStub.Success(); var transformedResponse = ResponseStub <string> .Success(transformed); var logger = new LoggerStub(); var page = GetMock <IPageModelBase <object> >(); page.Setup(x => x.Logger).Returns(logger); page.Setup(x => x.Return(transformedResponse)).Returns(expected); // Act var result = await ResponsePipeline <object> .Get(() => Task.FromResult(response)) .Transform(r => { results.Add(r.Payload); return(Task.FromResult(transformedResponse)); }) .Try(r => { results.Add(r); return(Task.FromResult(positiveTry)); }) .EvaluateAsync(page, responseComposer, null, null); // Assert result.Should().Be(expected); results.Should().Contain(payload).And.Contain(transformed).And.HaveCount(2); logger.Trace.Should() .Contain(response) .And.Contain(positiveTry) .And.Contain(transformedResponse) .And.HaveCount(3); logger.Error.Should().BeEmpty(); 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 TwoResponses_WithCustomHandler_ReturnsResult() { // Arrange const decimal payload1 = 1000m; const string payload2 = "test"; var results = new List <object>(); var response = GetMock <IResponse <decimal> >(); response.Setup(x => x.Succeeded).Returns(true); response.Setup(x => x.Payload).Returns(payload1); var responseComposer = GetMock <IResponseComposer>(); 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()); var response2 = ResponseStub <string> .Success(payload2); // Act var result = await ResponsePipeline <object> .Get(() => Task.FromResult(response)) .Get(t => { results.Add(t); return(Task.FromResult(response2)); }) .Evaluate(page, responseComposer, ((value, p) => { results.Add(value); return(p); }), null); // Assert result.Should().Be(page); results.Should().Contain(payload1).And.Contain(payload2).And.HaveCount(2); logger.Trace.Should().Contain(response).And.Contain(response2).And.HaveCount(2); logger.Error.Should().BeEmpty(); logger.Debug.Should().BeEmpty(); logger.Information.Should().BeEmpty(); logger.Warning.Should().BeEmpty(); logger.Critical.Should().BeEmpty(); }
public async Task TwoResponses_ReturnsResult() { // Arrange var results = new List <object>(); var response = ResponseStub <object> .Success(new object()); var expected = new object(); var responseComposer = GetMock <IResponseComposer>(); const decimal payload1 = 1000m; var logger = new LoggerStub(); var page = GetMock <IPageModelBase <object> >(); page.Setup(x => x.Logger).Returns(logger); page.Setup(x => x.Return(response)).Returns(expected); var response1 = ResponseStub <decimal> .Success(payload1); // Act var result = await ResponsePipeline <object> .Get(() => { return(Task.FromResult(response1)); }) .Get(t => { results.Add(t); return(Task.FromResult(response)); }) .EvaluateAsync(page, responseComposer, null, null); // Assert results.Should().Contain(payload1).And.HaveCount(1); result.Should().Be(expected); logger.Trace.Should() .Contain(response) .And.Contain(response1) .And.HaveCount(2); logger.Error.Should().BeEmpty(); 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(); }
public async Task TwoResponses_WithTransform_WithCustomHandler_ReturnsResult() { // Arrange const string payload2 = "test"; const bool payload3 = true; var results = new List <object>(); const string payload1 = "expected"; const decimal payload0 = 1000m; var responseComposer = GetMock <IResponseComposer>(); var response1 = ResponseStub <decimal> .Success(payload0); var response2 = ResponseStub <string> .Success(payload2); var transformed1 = ResponseStub <string> .Success(payload1); var transformed2 = ResponseStub <bool> .Success(payload3); var logger = new LoggerStub(); var page = GetMock <IPageModelBase <object> >(); page.Setup(x => x.Logger).Returns(logger); // Act var result = await ResponsePipeline <object> .Get(() => Task.FromResult(response1)) .Transform(r => { results.Add(r); return(Task.FromResult(transformed1)); }) .Get(r => { results.Add(r); return(Task.FromResult(response2)); }) .Transform((p, r) => { results.Add(p); results.Add(r); return(Task.FromResult(transformed2)); }) .Evaluate(page, responseComposer, ((value, p) => { results.Add(value); return(p); }), null); // Assert result.Should().Be(page); results[0].Should().Be(response1); results[1].Should().Be(payload1); results[2].Should().BeAssignableTo <IResponse <string> >().And.Match(x => (x as IResponse <string>).Payload == payload1); results[3].Should().BeAssignableTo <IResponse <string> >().And.Match(x => (x as IResponse <string>).Payload == payload2); results[4].Should().Be(payload3); results.Should().HaveCount(5); logger.Trace.Should() .Contain(response1) .And.Contain(response2) .And.Contain(transformed1) .And.Contain(transformed2) .And.HaveCount(4); logger.Error.Should().BeEmpty(); logger.Debug.Should().BeEmpty(); logger.Information.Should().BeEmpty(); logger.Warning.Should().BeEmpty(); logger.Critical.Should().BeEmpty(); }