Exemple #1
0
        public static async Task <IActionResult> GetBlogMetadata(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ExecutionContext context,
            ILogger log)
        {
            try
            {
                var reqBodyData = await GetRequestObject(req);

                string connString = ConfigReader.GetAppSettingOrDefault(
                    context,
                    "azurestorageconnstring",
                    null
                    ); string contentFileRoot = ConfigReader.GetAppSettingOrDefault(
                    context,
                    "contentfileroot",
                    null
                    );

                BlogManager bm = new BlogManager();

                BlogInfo reqData = reqBodyData.ToObject <BlogInfo>();
                BlogInfo toRet   = await bm.GetBlogMetadata(reqData,
                                                            connString,
                                                            contentFileRoot);

                return(new OkObjectResult(toRet));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(new ExceptionResult(e, true));
            }
        }
        public async Task TestGetBlogMetadata()
        {
            BlogManager bm       = new BlogManager();
            var         blogInfo = new BlogInfo
            {
                Name = "getblogmetadata"
            };

            try {
                var response = bm.GetBlogMetadata(blogInfo, connString, contentFileRoot);
                Task.WaitAll(response);
                var responseInfo = (BlogInfo)response.Result;
                Assert.Null(responseInfo.Comments);

                // pass in null comments
                blogInfo.Comments = null;
                var comments = bm.Comments(blogInfo, connString, contentFileRoot);
                Task.WaitAll(comments);
                Assert.Null(comments.Result);

                blogInfo.Comments = "Hello world!";
                comments          = bm.Comments(blogInfo, connString, contentFileRoot);
                Task.WaitAll(comments);
                Assert.AreEqual("Hello world!", comments.Result);

                response = bm.GetBlogMetadata(blogInfo, connString, contentFileRoot);
                Task.WaitAll(response);
                responseInfo = (BlogInfo)response.Result;
                Assert.AreEqual("Hello world!", responseInfo.Comments);
            }
            finally {
                await bm.DeleteBlogIfExists(blogInfo, connString);
            }
        }
        public async Task TestWithInvalidPath()
        {
            BlogManager     bm        = new BlogManager();
            List <BlogInfo> blogInfos = new List <BlogInfo>();

            try {
                blogInfos.Add(new BlogInfo
                {
                    Name = "invalid",
                    Path = "thiswontwork"
                });
                var except = Assert.Throws <AggregateException>(() =>
                {
                    var response = bm.GetMinsToRead(blogInfos, connString, contentFileRoot);
                    Task.WaitAll(response);
                });
                StringAssert.Contains("An invalid request URI was provided.", except.Message);
            }
            finally {
                foreach (var blog in blogInfos)
                {
                    await bm.DeleteBlogIfExists(blog, connString);
                }
            }
        }
        public async Task TestAddLike()
        {
            BlogManager bm       = new BlogManager();
            var         blogInfo = new BlogInfo
            {
                Name  = "addlike",
                Likes = 2
            };

            try {
                var response = bm.Likes(blogInfo, connString, contentFileRoot);
                Task.WaitAll(response);
                Assert.AreEqual(2, response.Result);

                blogInfo.Likes = 1;
                response       = bm.Likes(blogInfo, connString, contentFileRoot);
                Task.WaitAll(response);
                Assert.AreEqual(3, response.Result);


                blogInfo.Likes = -1;
                response       = bm.Likes(blogInfo, connString, contentFileRoot);
                Task.WaitAll(response);
                Assert.AreEqual(2, response.Result);

                // Make like count go negative, the API should stop at 0
                blogInfo.Likes = -18;
                response       = bm.Likes(blogInfo, connString, contentFileRoot);
                Task.WaitAll(response);
                Assert.AreEqual(0, response.Result);
            }
            finally {
                await bm.DeleteBlogIfExists(blogInfo, connString);
            }
        }
        public async Task TestMultipleFileNames()
        {
            BlogManager     bm        = new BlogManager();
            List <BlogInfo> blogInfos = new List <BlogInfo>();

            try {
                blogInfos.Add(new BlogInfo
                {
                    Name = "fivehundredwords"
                });
                blogInfos.Add(new BlogInfo
                {
                    Name = "empty"
                });
                var response = bm.GetMinsToRead(blogInfos, connString, contentFileRoot);
                Task.WaitAll(response);
                Assert.AreEqual(2, response.Result.Count);
                Assert.AreEqual("1", response.Result[0].MinsToRead);
                Assert.AreEqual("fivehundredwords", response.Result[0].Name);
            }
            finally {
                foreach (var blog in blogInfos)
                {
                    await bm.DeleteBlogIfExists(blog, connString);
                }
            }
        }
        public void TestNullName()
        {
            BlogManager     bm        = new BlogManager();
            List <BlogInfo> blogInfos = new List <BlogInfo>();

            blogInfos.Add(new BlogInfo
            {
            });
            var except = Assert.Throws <AggregateException>(() =>
            {
                var response = bm.GetMinsToRead(blogInfos, connString, contentFileRoot);
                Task.WaitAll(response);
            });

            StringAssert.Contains("Pass in a unique blogname in the list of names", except.Message);
        }
        public async Task TestSingleEmptyFile_NoPath()
        {
            BlogManager     bm        = new BlogManager();
            List <BlogInfo> blogInfos = new List <BlogInfo>();

            try {
                blogInfos.Add(new BlogInfo
                {
                    Name = "empty"
                });
                var response = bm.GetMinsToRead(blogInfos, connString, contentFileRoot);
                Task.WaitAll(response);
                Assert.AreEqual(1, response.Result.Count);
                Assert.AreEqual("0", response.Result[0].MinsToRead);
                Assert.AreEqual("empty", response.Result[0].Name);
            }
            finally {
                foreach (var blog in blogInfos)
                {
                    await bm.DeleteBlogIfExists(blog, connString);
                }
            }
        }
        public async Task TestComments()
        {
            BlogManager bm       = new BlogManager();
            var         blogInfo = new BlogInfo
            {
                Name     = "comments",
                Comments = "Hello world 1"
            };

            try {
                var response = bm.Comments(blogInfo, connString, contentFileRoot);
                Task.WaitAll(response);
                Assert.AreEqual("Hello world 1", response.Result);

                blogInfo.Comments = "Hello world 2";
                response          = bm.Comments(blogInfo, connString, contentFileRoot);
                Task.WaitAll(response);
                Assert.AreEqual("Hello world 2Hello world 1", response.Result);
            }
            finally {
                await bm.DeleteBlogIfExists(blogInfo, connString);
            }
        }