Exemplo n.º 1
0
        public HelloWorldInfraModel GetData()
        {
            HelloWorldInfraModel modelData = null;

            // Set the URL for the request
            this.restClient.BaseUrl = this.uriService.GetUri(this.appSettings.Get(AppSettingsKeys.HelloWorldApiUrlKey));

            // Setup the request
            this.restRequest.Resource = "HelloWorld";
            this.restRequest.Method   = Method.GET;

            // Clear the request parameters
            this.restRequest.Parameters.Clear();

            // Execute the call and get the response
            var response = this.restClient.Execute <HelloWorldInfraModel>(this.restRequest);

            // Check for data in the response
            if (response != null)
            {
                // Check if any actual data was returned
                if (response.Data != null)
                {
                    modelData = response.Data;
                }
                else
                {
                    var errorMessage = "Error in RestSharp, most likely in endpoint URL." + " Error message: "
                                       + response.ErrorMessage + " HTTP Status Code: "
                                       + response.StatusCode + " HTTP Status Description: "
                                       + response.StatusDescription;

                    // Check for existing exception
                    if (response.ErrorMessage != null && response.ErrorException != null)
                    {
                        // Log an informative exception including the RestSharp exception
                        this.logger.Error(errorMessage, null, response.ErrorException);
                    }
                    else
                    {
                        // Log an informative exception including the RestSharp content
                        this.logger.Error(errorMessage, null, new Exception(response.Content));
                    }
                }
            }
            else
            {
                // Log the exception
                const string ErrorMessage =
                    "Did not get any response from the Hello World Web Api for the Method: GET /helloworld";

                this.logger.Error(ErrorMessage, null, new Exception(ErrorMessage));
            }

            return(modelData);
        }
        public void UnitTestHelloWorldConsoleAppRunNormalDataNullDataNullErrorException()
        {
            // Create return models for dependencies
            const string         WebApiIUrl        = "http://www.somesiteheretesting.com";
            var                  uri               = new Uri(WebApiIUrl);
            var                  mockParameters    = new Mock <List <Parameter> >();
            var                  mockRestResponse  = new Mock <IRestResponse <HelloWorldInfraModel> >();
            HelloWorldInfraModel modelData         = null;
            const string         ErrorMessage      = "Error Message";
            const HttpStatusCode StatusCode        = HttpStatusCode.InternalServerError;
            const string         StatusDescription = "Status Description";
            Exception            errorException    = null;
            const string         ProfileContent    = "Content here";

            var errorMessage = "Error in RestSharp, most likely in endpoint URL."
                               + " Error message: " + ErrorMessage
                               + " HTTP Status Code: " + StatusCode
                               + " HTTP Status Description: " + StatusDescription;

            // Set up dependencies
            this.appSettingsMock.Setup(m => m.Get(AppSettingsKeys.HelloWorldApiUrlKey)).Returns(WebApiIUrl);
            this.uriServiceMock.Setup(m => m.GetUri(WebApiIUrl)).Returns(uri);
            this.restRequestMock.Setup(m => m.Parameters).Returns(mockParameters.Object);
            this.restClientMock.Setup(m => m.Execute <HelloWorldInfraModel>(It.IsAny <IRestRequest>())).Returns(mockRestResponse.Object);
            mockRestResponse.Setup(m => m.Data).Returns(modelData);
            mockRestResponse.Setup(m => m.ErrorMessage).Returns(ErrorMessage);
            mockRestResponse.Setup(m => m.StatusCode).Returns(StatusCode);
            mockRestResponse.Setup(m => m.StatusDescription).Returns(StatusDescription);
            mockRestResponse.Setup(m => m.ErrorException).Returns(errorException);
            mockRestResponse.Setup(m => m.Content).Returns(ProfileContent);

            // Call the method to test
            var response = this.helleHelloWorldWebService.GetData();

            // Check values
            Assert.IsNull(response);
            Assert.AreEqual(this.logMessageList.Count, 1);
            Assert.AreEqual(this.logMessageList[0], errorMessage);
            Assert.AreEqual(this.exceptionList.Count, 1);
            Assert.AreEqual(this.exceptionList[0].Message, ProfileContent);
        }