Exemplo n.º 1
0
        public void SetUp()
        {
            hl7MessageProcessor = new Mock <IHL7MessageProcessor>();
            orchestrator        = new Mock <IOpenHimOrchestrator>();
            logger = new Mock <ILogger <HL7ValidationRequestsController> >();

            fixture         = new Fixture();
            openHimResponse = fixture.Create <OpenHimResponse>();
            orchestrator.Setup(o => o.Do(It.IsAny <string>(), It.IsAny <Response>(), It.IsAny <bool>()))
            .ReturnsAsync(openHimResponse);

            var stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(hl7MessageData));

            var httpContext = new DefaultHttpContext();

            httpContext.Request.Body          = stream;
            httpContext.Request.ContentLength = stream.Length;

            var controllerContext = new ControllerContext()
            {
                HttpContext = httpContext,
            };

            controllerUnderTest = new HL7ValidationRequestsController(hl7MessageProcessor.Object, orchestrator.Object, logger.Object)
            {
                ControllerContext = controllerContext
            };
        }
Exemplo n.º 2
0
        // Don't like this method name. Thinking of a better one...
        public async Task <OpenHimResponse> Do(string requestContent, Response primaryOpenHimConsumerResponse, bool primaryOperationSuccessful = true)
        {
            var openHimResponse = new OpenHimResponse
            {
                MediatorUrn    = _mediatorConfig.MediatorSetup.Urn,
                Status         = primaryOperationSuccessful ? "Success" : "Completed with error(s)",
                Response       = primaryOpenHimConsumerResponse,
                Orchestrations = null,
                Properties     = null
            };

            if (!primaryOperationSuccessful || !_mediatorConfig.HasOrchestrations())
            {
                return(openHimResponse);
            }

            var basicAuth = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{_mediatorConfig.OpenHimAuth.ApiClientName}:{_mediatorConfig.OpenHimAuth.ApiClientPassword}"));

            foreach (var orchestration in _mediatorConfig.Orchestrations)
            {
                var orchestrationClient = _clientFactory.CreateClient(orchestration.Name);
                orchestrationClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", basicAuth);
                orchestrationClient.BaseAddress = new Uri(orchestration.Request.Host);

                var request = new HttpRequestMessage(new HttpMethod(orchestration.Request.Method), $"{orchestration.Request.Path}?{orchestration.Request.Querystring}")
                {
                    Content = new StringContent(requestContent, Encoding.UTF8, "application/json")
                };

                foreach (var header in orchestration.Request.Headers)
                {
                    request.Headers.Add(header.Key, header.Value);
                }

                var response = await orchestrationClient.SendAsync(request);

                openHimResponse.AddOrchestration(
                    new Orchestration
                {
                    Name    = orchestration.Name,
                    Request = new Request
                    {
                        Host        = orchestration.Request.Host,
                        Path        = orchestration.Request.Path,
                        Querystring = orchestration.Request.Querystring,
                        Headers     = orchestration.Request.Headers,
                        Body        = requestContent,
                        Method      = orchestration.Request.Method,
                        Timestamp   = DateTime.UtcNow.ToString("s")
                    },
                    Response = new Response
                    {
                        Headers   = response.Headers.ToDictionary(h => h.Key, h => string.Join(";", h.Value)),
                        Body      = await response.Content.ReadAsStringAsync(),
                        Status    = (short)response.StatusCode,
                        Timestamp = DateTime.UtcNow.ToString("s")
                    }
                });
            }

            return(openHimResponse);
        }