예제 #1
0
        public async Task <BookFindResponse> FindBookByIsbnAsync(string isbn)
        {
            var query = HttpUtility.ParseQueryString(string.Empty);

            query["bibkeys"] = $"ISBN:{isbn}";
            query["jscmd"]   = "data";
            query["format"]  = "json";
            var queryString = "?" + query.ToString();
            var response    = await httpClientWrapper.GetAsync(queryString);

            var responseObject = JsonConvert.DeserializeObject <JObject>(await response.Content.ReadAsStringAsync());

            BookFindResponse result = null;

            if (responseObject.ToString() != "{}")
            {
                var dict = responseObject.ToObject <Dictionary <string, JObject> >();
                var val  = dict.First().Value;
                result = new BookFindResponse
                {
                    Author = val["authors"][0]["name"].ToString(),
                    Isbn   = isbn,
                    Title  = val["title"].ToString(),
                    Genres = new List <string>()
                };

                if (val["subjects"] != null)
                {
                    result.Genres = val["subjects"].Select(x => x["name"].ToString());
                }
            }

            return(result);
        }
예제 #2
0
        public async Task Get_BookReturnedByService_ReturnsBookFindResponse()
        {
            string isbn = "01234";
            var    book = new BookFindResponse
            {
                Author = "Testy Testerson",
                Genres = new List <string>
                {
                    "Test fiction",
                    "Computing"
                },
                Isbn  = isbn,
                Title = "On testing and its benefits: A beginners guide"
            };

            mockBookService.Setup(x => x.FindBookByIsbnAsync(It.IsAny <string>()))
            .ReturnsAsync(book);

            var actionResult = await bookController.Get(isbn);

            var okResult = actionResult.Result as OkObjectResult;

            Assert.NotNull(okResult);

            var value = okResult.Value as BookFindResponse;

            Assert.NotNull(value);
        }