private ApiTaskResult <TResult, TFailureResult> DeserializeResult( JsonDocument jsonDocument, string?taskStateValue, Guid taskId, Urn?taskType, JsonSerializerOptions?options) { var resultJson = jsonDocument.RootElement.GetProperty(propNames.TaskResultPropName.AsUtf8()).GetRawText(); Exception?failureSerializationError = null; try { var failureResult = DeserializeAs <TFailureResult>(resultJson, propNames.TaskResultPropName, taskStateValue, options); if (!failureResult.IsEmpty) { return(ApiTaskResult <TResult, TFailureResult> .FailureResult(failureResult, taskId, taskType)); } } catch (Exception ex) { failureSerializationError = ex; } var successResult = DeserializeAs <TResult>(resultJson, propNames.TaskResultPropName, taskStateValue, options); if (successResult.IsEmpty && failureSerializationError is not null) { throw failureSerializationError; } return(ApiTaskResult <TResult, TFailureResult> .Success(successResult, taskId, taskType)); }
public void TryGetTaskError_should_return_error_if_api_task_result_is_failure() { var taskResult = ApiTaskResult <SuccessResult, FailureResult> .FailureResult(new FailureResult(), Guid.NewGuid(), new Urn("nid", "nss")); var success = taskResult.TryGetTaskError(out var result); success.Should().BeFalse(); result.Should().BeNull(); }
public void FailureResult_should_fail_if_given_empty_result() { var id = Guid.NewGuid(); var taskType = new Urn("nid", "nss"); Action action = () => ApiTaskResult <SuccessResult, FailureResult> .FailureResult(FailureResult.Empty, id, taskType); action.Should().Throw <ArgumentException>(); }
public void TryGetFailureResult_should_return_failure_result_if_api_task_result_is_failure() { var failureResult = new FailureResult(); var taskResult = ApiTaskResult <SuccessResult, FailureResult> .FailureResult(failureResult, Guid.NewGuid(), new Urn("nid", "nss")); var success = taskResult.TryGetFailureResult(out var result); success.Should().BeTrue(); result.Should().Be(failureResult); }
public void FailureResult_should_create_result_in_the_failed_state() { var id = Guid.NewGuid(); var taskType = new Urn("nid", "nss"); var expectedDto = new ExpectedResult { Id = id, TaskType = taskType, TaskState = ApiTaskState.Failed }; var result = ApiTaskResult <SuccessResult, FailureResult> .FailureResult(new FailureResult(), id, taskType); result.Should().BeEquivalentTo(expectedDto); }
public void ConvertToSingleApiResult_should_convert_failure_result_to_api_error() { var id = Guid.NewGuid(); var taskType = new Urn("nid", "nss"); var expectedDto = new ExpectedResult { Id = id, TaskType = taskType, TaskState = ApiTaskState.Failed }; var expectedApiError = new ApiError(HttpStatusCode.Forbidden, "10"); var taskResult = ApiTaskResult <SuccessResult, FailureResult> .FailureResult(new FailureResult(value : 10), id, taskType); var oneResultApiResult = taskResult.ConvertToSingleApiResult( x => x.Value, x => new ApiError(HttpStatusCode.Forbidden, x.Value.ToString()) ); oneResultApiResult.Should().BeEquivalentTo(expectedDto); oneResultApiResult.TryGetTaskError(out var apiError).Should().BeTrue(); apiError.Should().BeEquivalentTo(expectedApiError); }