Пример #1
0
        /// <summary>
        ///     Gets HelloWorldData from the web API
        /// </summary>
        /// <returns>A HelloWorldData mode</returns>
        public HellowWorldData GetHelloWorldContent()
        {
            HellowWorldData hellowWorldData = null;

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

            // 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 hellowWorldDataResponse = this.restClient.Execute <HellowWorldData>(this.restRequest);

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

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

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

            return(hellowWorldData);
        }
        public void UnitTestHelloWorldConsoleAppRunNullDataSuccess()
        {
            const string    Data           = null;
            HellowWorldData helloWorldData = GetHelloWorldData(Data);

            // Set up dependencies
            this.helloWorldWebServiceMock.Setup(m => m.GetHelloWorldContent()).Returns(helloWorldData);

            // Call the method to test
            this.helloWorldConsoleApp.Run(null);

            // Check values
            Assert.AreEqual(this.logMessageList.Count, 1);
            Assert.AreEqual(this.logMessageList[0], "No data was found!");
        }