private ServiceResult <T> Execute <T>(ServiceDto request)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (m_testInfo == null)
            {
                return(ServiceResult.Failure(ServiceErrors.CreateInvalidRequest("Facility test name is missing; set the FacilityTest HTTP header.")));
            }

            string uncapitalize(string value) => value.Substring(0, 1).ToLowerInvariant() + value.Substring(1);

            string methodName = uncapitalize(request.GetType().Name.Substring(0, request.GetType().Name.Length - "RequestDto".Length));

            if (methodName != m_testInfo.Method)
            {
                return(ServiceResult.Failure(ServiceErrors.CreateInvalidRequest($"Unexpected method name for test {m_testInfo.Test}. expected={m_testInfo.Method} actual={methodName}")));
            }

            var actualRequest = (JObject)ServiceJsonUtility.ToJToken(request);

            if (!JToken.DeepEquals(m_testInfo.Request, actualRequest))
            {
                return(ServiceResult.Failure(ServiceErrors.CreateInvalidRequest($"Request did not match for test {m_testInfo.Test}. expected={ServiceJsonUtility.ToJson(m_testInfo.Request)} actual={ServiceJsonUtility.ToJson(actualRequest)}")));
            }

            if (m_testInfo.Error != null)
            {
                var error          = ServiceJsonUtility.FromJToken <ServiceErrorDto>(m_testInfo.Error);
                var errorRoundTrip = ServiceJsonUtility.ToJToken(error);
                if (!JToken.DeepEquals(m_testInfo.Error, errorRoundTrip))
                {
                    return(ServiceResult.Failure(ServiceErrors.CreateInvalidRequest($"Error round trip failed for test {m_testInfo.Test}. expected={ServiceJsonUtility.ToJson(m_testInfo.Error)} actual={ServiceJsonUtility.ToJson(errorRoundTrip)}")));
                }
                return(ServiceResult.Failure(error));
            }
            else
            {
                var response          = ServiceJsonUtility.FromJToken <T>(m_testInfo.Response);
                var responseRoundTrip = ServiceJsonUtility.ToJToken(response);
                if (!JToken.DeepEquals(m_testInfo.Response, responseRoundTrip))
                {
                    return(ServiceResult.Failure(ServiceErrors.CreateInvalidRequest($"Response round trip failed for test {m_testInfo.Test}. expected={ServiceJsonUtility.ToJson(m_testInfo.Response)} actual={ServiceJsonUtility.ToJson(responseRoundTrip)}")));
                }
                return(ServiceResult.Success(response));
            }
        }
    private ServiceResult <T> Execute <T>(ServiceDto request)
    {
        if (request == null)
        {
            throw new ArgumentNullException(nameof(request));
        }

        var methodName          = Uncapitalize(request.GetType().Name.Substring(0, request.GetType().Name.Length - "RequestDto".Length));
        var testsWithMethodName = m_tests.Where(x => x.Method == methodName).ToList();

        if (testsWithMethodName.Count == 0)
        {
            return(ServiceResult.Failure(ServiceErrors.CreateInvalidRequest($"No tests found for method {methodName}.")));
        }

        var actualRequest            = m_jsonSerializer.ToServiceObject(request);
        var testsWithMatchingRequest = testsWithMethodName.Where(x => ServiceObjectUtility.DeepEquals(x.Request, actualRequest)).ToList();

        if (testsWithMatchingRequest.Count != 1)
        {
            return(ServiceResult.Failure(ServiceErrors.CreateInvalidRequest(
                                             $"{testsWithMatchingRequest.Count} of {testsWithMethodName.Count} tests for method {methodName} matched request: " +
                                             $"{m_jsonSerializer.ToJson(actualRequest)} ({string.Join(", ", testsWithMethodName.Select(x => m_jsonSerializer.ToJson(x.Request)))})")));
        }
        var testInfo = testsWithMatchingRequest[0];

        if (testInfo.Error != null)
        {
            var error          = m_jsonSerializer.FromServiceObject <ServiceErrorDto>(testInfo.Error);
            var errorRoundTrip = m_jsonSerializer.ToServiceObject(error);
            if (!ServiceObjectUtility.DeepEquals(testInfo.Error, errorRoundTrip))
            {
                return(ServiceResult.Failure(ServiceErrors.CreateInvalidRequest($"Error round trip failed for test {testInfo.Test}. expected={m_jsonSerializer.ToJson(testInfo.Error)} actual={m_jsonSerializer.ToJson(errorRoundTrip)}")));
            }
            return(ServiceResult.Failure(error));
        }
        else
        {
            var response          = m_jsonSerializer.FromServiceObject <T>(testInfo.Response !);
            var responseRoundTrip = m_jsonSerializer.ToServiceObject(response);
            if (!ServiceObjectUtility.DeepEquals(testInfo.Response, responseRoundTrip))
            {
                return(ServiceResult.Failure(ServiceErrors.CreateInvalidRequest($"Response round trip failed for test {testInfo.Test}. expected={m_jsonSerializer.ToJson(testInfo.Response)} actual={m_jsonSerializer.ToJson(responseRoundTrip)}")));
            }
            return(ServiceResult.Success(response));
        }
    }
Exemplo n.º 3
0
        public void ServiceDto_Property_Count()
        {
            var service = new ServiceDto();

            Assert.AreEqual(17, service.GetType().GetProperties().Count());
        }