Example #1
0
        public static void ConvertToEnum_Should_Throws_Exception_With_Non_Enum_T()
        {
            // arrange / act / assert
            ArgumentException ex = Assert.Throws <ArgumentException>(() =>
                                                                     EnumUtility.ConvertToEnum <String>("Bla"));

            Assert.Equal("T must be an enumerated type.", ex.Message);
        }
Example #2
0
        public static void ConvertToEnumList_Should_Return_Parsed_Enum_With_Null_Param()
        {
            // arrange
            List <string> enumList = null;

            // act
            var enumValue = EnumUtility.ConvertToEnum <EnumTest1>(enumList);

            // assert
            Assert.Empty(enumValue);
        }
Example #3
0
        public static void ConvertToEnum_Should_Return_Parsed_Enum_Unkown_Param()
        {
            // arrange
            string enumString = "Some";

            // act
            var enumValue = EnumUtility.ConvertToEnum <EnumTest1>(enumString);

            // assert
            Assert.Equal(EnumTest1.Undefined, enumValue);
        }
Example #4
0
        public void Should_Return_Default_Enum_With_Null()
        {
            // arrange
            string value = null;

            // act
            var result = EnumUtility.ConvertToEnum <TestEnum>(value);

            // assert
            Assert.Equal(TestEnum.Undefined, result);
        }
Example #5
0
        public static void ConvertToEnum_Should_Return_Parsed_Enum()
        {
            // arrange
            var enumToParse = EnumTest2.Test1;

            // act
            var enumValue = EnumUtility.ConvertToEnum <EnumTest1>(enumToParse);

            // assert
            Assert.Equal(EnumTest1.Test1, enumValue);
        }
Example #6
0
        public void Should_Return_Default_Enum_With_Unknow_Value()
        {
            // arrange
            string value = "InvalidValue";

            // act
            var result = EnumUtility.ConvertToEnum <TestEnum>(value);

            // assert
            Assert.Equal(TestEnum.Undefined, result);
        }
Example #7
0
        public void Should_Return_Correct_Parsed_With_Valid_Values()
        {
            // arrange
            string value = "MyValue1";

            // act
            var result = EnumUtility.ConvertToEnum <TestEnum>(value);

            // assert
            Assert.Equal(TestEnum.MyValue1, result);
        }
Example #8
0
        public static void ConvertToEnumList_Should_Return_Parsed_Enum_List()
        {
            // arrange
            List <string> enumList = new List <string> {
                "Test1", "Bla"
            };

            // act
            var enumValue = EnumUtility.ConvertToEnum <EnumTest1>(enumList);

            // assert
            Assert.Equal(2, enumValue.Count);
            Assert.Equal(EnumTest1.Test1, enumValue[0]);
            Assert.Equal(EnumTest1.Undefined, enumValue[1]);
        }
Example #9
0
        public void Should_Return_Exception_With_Not_Enum_Generic()
        {
            // arrange
            string value = "Test";

            // act
            Exception ex = Assert.Throws <ArgumentException>(() =>
            {
                var result = EnumUtility.ConvertToEnum <EnumUtilityTest>(value);
            }
                                                             );

            // assert
            Assert.Equal("T must be an enumerated type.", ex.Message);
        }
Example #10
0
        public async Task <BaseResponse <TSuccess, TError> > SendRequestAsync <TSuccess, TError>(HttpMethod method, string endpoint, object body = null, IDictionary <string, string> query = null, IDictionary <string, string> headers = null)
            where TSuccess : class, new()
            where TError : class, new()
        {
            BaseResponse <TSuccess, TError> response = new BaseResponse <TSuccess, TError>();
            var stopwatch = new Stopwatch();

            stopwatch.Start();

            var restSharpMethod = EnumUtility.ConvertToEnum <Method>(method.Method.ToUpper());
            var restRequest     = new RestRequest(endpoint, restSharpMethod);

            restRequest.AddNewtonsoftRequestHandler(this.NewtonsoftRestsharpJsonSerializer);

            if (headers != null)
            {
                foreach (var header in headers)
                {
                    restRequest.AddHeader(header.Key, header.Value);
                }
            }

            if (query != null)
            {
                foreach (var queryItem in query)
                {
                    restRequest.AddQueryParameter(queryItem.Key, queryItem.Value);
                }
            }

            if (body != null && restSharpMethod != Method.GET)
            {
                restRequest.AddJsonBody(body);
                response.RawRequest = JsonConvert.SerializeObject(body, JsonSerializerSettings);
            }

            var restResponse = await this.RestClient.ExecuteAsync(restRequest, restRequest.Method);

            this.HandleResponse(response, restResponse);

            stopwatch.Stop();
            response.ElapsedTime = stopwatch.ElapsedMilliseconds;

            return(response);
        }
Example #11
0
        /// <summary>
        /// Send request and mount response to client format
        /// </summary>
        /// <typeparam name="T">Response type</typeparam>
        /// <param name="httpMethod">Method to call</param>
        /// <param name="endpoint">Endpoint to call</param>
        /// <param name="body">Body object</param>
        /// <param name="query">Params to mount query string</param>
        /// <param name="headers">Params to mount header string</param>
        /// <param name="authMode">sk|amk|token</param>
        /// <returns>Base response with specific data defined in T</returns>
        public BaseResponse <T> SendRequest <T>(HttpMethod httpMethod, string endpoint, object body, IDictionary <string, string> query = null, IDictionary <string, string> headers = null, string authMode = "sk")
            where T : class, new()
        {
            BaseResponse <T> response = new BaseResponse <T>();
            var stopwatch             = new Stopwatch();

            stopwatch.Start();

            var fullUri = this.GetFullUri(endpoint, query);
            var method  = EnumUtility.ConvertToEnum <Method>(httpMethod.Method.ToUpper());

            var client = this.GetClient(fullUri);

            var restRequest = new RestRequest(method);

            restRequest.AddNewtonsoftRequestHandler(NewtonsoftRestsharpJsonSerializer);

            if (headers != null)
            {
                foreach (var header in headers)
                {
                    restRequest.AddHeader(header.Key, header.Value);
                }
            }

            if (body != null && method != Method.GET)
            {
                restRequest.AddJsonBody(body);
                response.RawRequest = JsonConvert.SerializeObject(body, JsonSerializerSettings);
            }

            client.Authenticator = new HttpBasicAuthenticator(this.GetBasicUser(authMode), "");

            var restResponse = client.Execute <T>(restRequest);

            this.HandleResponse(response, restResponse);

            stopwatch.Stop();
            response.ElapsedTime = stopwatch.ElapsedMilliseconds;

            return(response);
        }