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 Success_should_fail_if_given_empty_result() { var id = Guid.NewGuid(); var taskType = new Urn("nid", "nss"); Action action = () => ApiTaskResult <SuccessResult, FailureResult> .Success(SuccessResult.Empty, id, taskType); action.Should().Throw <ArgumentException>(); }
public void TryGetTaskError_should_return_error_if_api_task_result_is_successful() { var taskResult = ApiTaskResult <SuccessResult, FailureResult> .Success(new SuccessResult(), Guid.NewGuid(), new Urn("nid", "nss")); var success = taskResult.TryGetTaskError(out var result); success.Should().BeFalse(); result.Should().BeNull(); }
public void TryGetSuccessResult_should_return_success_result_if_api_task_result_is_successful() { var successResult = new SuccessResult(); var taskResult = ApiTaskResult <SuccessResult, FailureResult> .Success(successResult, Guid.NewGuid(), new Urn("nid", "nss")); var success = taskResult.TryGetSuccessResult(out var result); success.Should().BeTrue(); result.Should().Be(successResult); }
public void Success_should_create_result_in_the_success_state() { var id = Guid.NewGuid(); var taskType = new Urn("nid", "nss"); var expectedDto = new ExpectedResult { Id = id, TaskType = taskType, TaskState = ApiTaskState.Succeed }; var result = ApiTaskResult <SuccessResult, FailureResult> .Success(new SuccessResult(), id, taskType); result.Should().BeEquivalentTo(expectedDto); }
public void ConvertToSingleApiResult_should_convert_success_result_to_success_result() { var id = Guid.NewGuid(); var taskType = new Urn("nid", "nss"); var expectedDto = new ExpectedResult { Id = id, TaskType = taskType, TaskState = ApiTaskState.Succeed }; var taskResult = ApiTaskResult <SuccessResult, FailureResult> .Success(new SuccessResult(value : 10), id, taskType); var oneResultApiResult = taskResult.ConvertToSingleApiResult( x => x.Value, x => new ApiError(HttpStatusCode.Forbidden, x.Value.ToString()) ); oneResultApiResult.Should().BeEquivalentTo(expectedDto); oneResultApiResult.TryGetSuccessResult(out var result).Should().BeTrue(); result.Should().Be(10); }
public override ApiTaskResult <TResult> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { using var jsonDocument = JsonDocument.ParseValue(ref reader); var taskId = jsonDocument.RootElement.GetProperty(propNames.IdPropName.AsUtf8()).GetGuid(); var taskStateValue = jsonDocument.RootElement.GetProperty(propNames.TaskStatePropName.AsUtf8()).GetString(); var taskState = (ApiTaskState)Enum.Parse(typeof(ApiTaskState), taskStateValue !, true); var taskTypeValue = jsonDocument.RootElement.GetProperty(propNames.TaskTypePropName.AsUtf8()).GetString(); var taskType = taskTypeValue == null ? null : Urn.Parse(taskTypeValue); switch (taskState) { case ApiTaskState.Running: return(ApiTaskResult <TResult> .Running(taskId, taskType)); case ApiTaskState.Succeed: var resultJson = jsonDocument.RootElement.GetProperty(propNames.TaskResultPropName.AsUtf8()).GetRawText(); var successResult = DeserializeAs <TResult>(resultJson, propNames.TaskResultPropName, taskStateValue); return(ApiTaskResult <TResult> .Success(successResult, taskId, taskType)); case ApiTaskState.Failed: var errorJson = jsonDocument.RootElement.GetProperty(propNames.ApiErrorPropName.AsUtf8()).GetRawText(); var error = DeserializeAs <ApiError>(errorJson, propNames.ApiErrorPropName, taskStateValue); return(ApiTaskResult <TResult> .TaskFailure(error, taskId, taskType)); default: throw Errors.UnexpectedEnumMember(nameof(taskState), taskState); } T DeserializeAs <T>(string json, in Utf8String propName, string?taskStatePropValue) { return(JsonSerializer.Deserialize <T>(json, options) ?? throw Errors.JsonPropertyCannotBeNullIfAnotherPropertyHasValue( propName.ToString(), propNames.TaskStatePropName.ToString(), taskStatePropValue)); } }