public void Given_RequestMessage_When_ToRoutineResponseMessage_ForPublish_Invoked_Then_It_Should_Return_Result(HttpStatusCode responseStatusCode, string upn, InterfaceType @interface, string routine) { var response = new Mock <Response>(); response.SetupGet(p => p.Status).Returns((int)responseStatusCode); var correlationId = Guid.NewGuid(); var spanId = Guid.NewGuid(); var request = new PublishRequestMessage() { Upn = upn, CorrelationId = correlationId, Interface = @interface, SpanId = spanId, Routine = routine, }; var eventId = Guid.NewGuid(); var routineId = Guid.NewGuid(); var statusCode = HttpStatusCode.OK; var result = AzureResponseExtensions.ToRoutineResponseMessage(response.Object, request, eventId, routineId, statusCode); result.Should().BeOfType <ObjectResult>(); result.Value.Should().BeOfType <RoutineResponseMessage>(); result.StatusCode.Should().Be((int)statusCode); (result.Value as RoutineResponseMessage).Upn.Should().Be(upn); (result.Value as RoutineResponseMessage).CorrelationId.Should().Be(correlationId); (result.Value as RoutineResponseMessage).Interface.Should().Be(@interface); (result.Value as RoutineResponseMessage).SpanId.Should().Be(spanId); (result.Value as RoutineResponseMessage).EventId.Should().Be(eventId); (result.Value as RoutineResponseMessage).RoutineId.Should().Be(routineId); (result.Value as RoutineResponseMessage).Routine.Should().Be(routine); (result.Value as RoutineResponseMessage).Targets.Should().BeNull(); }
/// <summary> /// Gets the <see cref="ObjectResult"/> object from the list of <see cref="ExerciseEntity"/> objects. /// </summary> /// <param name="entities">List of <see cref="ExerciseEntity"/> objects.</param> /// <param name="request"><see cref="PublishRequestMessage"/> object.</param> /// <param name="eventId">Event ID.</param> /// <param name="httpStatusCode"><see cref="HttpStatusCode"/> value.</param> /// <returns>Returns the <see cref="ObjectResult"/> object.</returns> public static ObjectResult ToPublishResponseMessage(this List <ExerciseEntity> entities, PublishRequestMessage request, Guid eventId, HttpStatusCode httpStatusCode = HttpStatusCode.OK) { if (!entities.Any()) { var result = new ErrorObjectResult() { Upn = request.Upn, CorrelationId = request.CorrelationId, Interface = request.Interface, SpanId = request.SpanId, EventId = eventId, Message = EventType.RecordNotFound.ToDisplayName(), StatusCode = (int)HttpStatusCode.NotFound, }; return(result); } var exercises = entities.Select(p => new Exercise() { ExerciseId = p.ExerciseId, Name = p.Exercise, Target = p.Target, Sets = p.Sets.FromJson <List <ExerciseSet> >(), AdditionalNotes = p.AdditionalNotes, } ) .ToList(); var msg = new PublishResponseMessage() { Upn = request.Upn, CorrelationId = request.CorrelationId, Interface = request.Interface, SpanId = request.SpanId, EventId = eventId, RoutineId = request.RoutineId, Routine = request.Routine, Exercises = exercises, }; return(new ObjectResult(msg) { StatusCode = (int)httpStatusCode }); }
public void Given_Error_StatusCode_When_ToRoutineResponseMessage_ForPublish_Invoked_Then_It_Should_Return_ErrorObjectResult(HttpStatusCode responseStatusCode, string reasonPhrase) { var response = new Mock <Response>(); response.SetupGet(p => p.Status).Returns((int)responseStatusCode); response.SetupGet(p => p.ReasonPhrase).Returns(reasonPhrase); var request = new PublishRequestMessage(); var eventId = Guid.NewGuid(); var routineId = Guid.NewGuid(); var statusCode = HttpStatusCode.OK; var result = AzureResponseExtensions.ToRoutineResponseMessage(response.Object, request, eventId, routineId, statusCode); result.Should().BeOfType <ObjectResult>(); result.Value.Should().BeOfType <ErrorResponseMessage>(); result.StatusCode.Should().Be((int)responseStatusCode); (result.Value as ErrorResponseMessage).Message.Should().Be($"{(int)responseStatusCode}: {reasonPhrase}"); }
/// <summary> /// Gets the <see cref="ObjectResult"/> object containing the <see cref="RoutineResponseMessage"/> object. /// </summary> /// <param name="response"><see cref="Response"/> object.</param> /// <param name="request"><see cref="RoutineRequestMessage"/> object.</param> /// <param name="eventId">Event ID.</param> /// <param name="routineId">Routine ID.</param> /// <param name="httpStatusCode"><see cref="HttpStatusCode"/> value.</param> /// <returns>Returns the <see cref="ObjectResult"/> object.</returns> public static ObjectResult ToRoutineResponseMessage(this Response response, PublishRequestMessage request, Guid eventId, Guid routineId, HttpStatusCode httpStatusCode = HttpStatusCode.OK) { if (response.Status >= (int)HttpStatusCode.BadRequest) { var result = new ErrorObjectResult() { Upn = request.Upn, CorrelationId = request.CorrelationId, Interface = request.Interface, SpanId = request.SpanId, EventId = eventId, Message = $"{response.Status}: {response.ReasonPhrase}", StatusCode = response.Status, }; return(result); } var msg = new RoutineResponseMessage() { Upn = request.Upn, CorrelationId = request.CorrelationId, Interface = request.Interface, SpanId = request.SpanId, EventId = eventId, RoutineId = routineId, Routine = request.Routine, }; return(new ObjectResult(msg) { StatusCode = (int)httpStatusCode }); }