Exemplo n.º 1
0
        public async Task Test_01_CreateBlogWithoutContentAsync()
        {
            using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                BlogHandler blogHandler = new BlogHandler(dbContext);
                GroupHandler groupHandler = new GroupHandler(dbContext);
                BlogAccessControlHandler acHandler = new BlogAccessControlHandler(dbContext);
                BlogAccessControlXGroupHandler acxgHandler = new BlogAccessControlXGroupHandler(dbContext);

                //1. test normal.
                Blog newBlog = await blogHandler.CreateBlogAsync(testPerson.ID, "testBlog", BlogInfoAccessInfo.All, null);

                Blog testBlog_1 = await blogHandler.GetByIdAsync(newBlog.ID);
                BlogAccessControl testAC_1 = await acHandler.Fetch(x => x.BlogID == testBlog_1.ID).FirstOrDefaultAsync();
                
                Assert.IsNotNull(testBlog_1);
                Assert.IsNotNull(testAC_1);

                Assert.AreEqual(testBlog_1.PersonID, testPerson.ID);
                Assert.AreEqual(testBlog_1.Content, "testBlog");
                Assert.AreEqual(testAC_1.AccessLevel, BlogInfoAccessInfo.All);

                //2. set BlogInfoAccessInfo GroupOnly and set GroupID is null then check it.
                bool isChecked = false;
                try
                {
                    newBlog = await blogHandler.CreateBlogAsync(testPerson.ID, "testBlogTwo", BlogInfoAccessInfo.GroupOnly, null);
                }
                catch (Exception ex)
                {
                    isChecked = true;
                    Assert.AreEqual(ex.GetType(), typeof(DisplayableException));
                    Assert.AreEqual(ex.Message, "未指定组");
                }
                Assert.IsTrue(isChecked);

                //3. set not exist GroupID. expect Exception is type of foreign key exception.
                isChecked = false;
                try
                {
                    newBlog = await blogHandler.CreateBlogAsync(testPerson.ID, "testBlogThree", BlogInfoAccessInfo.GroupOnly, 9999);
                }
                catch (Exception ex)
                {
                    isChecked = true;
                    Assert.AreNotEqual(ex.GetType(), typeof(AssertException));
                }
                Assert.IsTrue(isChecked);
            }

            using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                BlogHandler blogHandler = new BlogHandler(dbContext);
                GroupHandler groupHandler = new GroupHandler(dbContext);
                BlogAccessControlHandler acHandler = new BlogAccessControlHandler(dbContext);
                BlogAccessControlXGroupHandler acxgHandler = new BlogAccessControlXGroupHandler(dbContext);

                //4. create test group object and test it.
                Group testGroup = await groupHandler.CreateGroupAsync(testPerson.ID, "testGroup", GroupType.GroupList);
                bool isChecked = false;
                try
                {
                    Blog testBlog4Group = await blogHandler.CreateBlogAsync(testPerson.ID, "testBlogFour", BlogInfoAccessInfo.GroupOnly, testGroup.ID);

                    Blog testBlog_2 = await blogHandler.GetByIdAsync(testBlog4Group.ID);
                    BlogAccessControl testAC_2 = await acHandler.Fetch(x => x.BlogID == testBlog_2.ID).FirstOrDefaultAsync();
                    BlogAccessControlXGroup testACXG = await acxgHandler.Fetch(x => x.GroupID == testGroup.ID && x.BlogAccessControlID == testAC_2.ID).FirstOrDefaultAsync();

                    Assert.IsNotNull(testBlog_2);
                    Assert.IsNotNull(testAC_2);
                    Assert.IsNotNull(testACXG);

                    Assert.AreEqual(testBlog_2.PersonID, testPerson.ID);
                    Assert.AreEqual(testBlog_2.Content, "testBlogFour");
                    Assert.AreEqual(testAC_2.AccessLevel, BlogInfoAccessInfo.GroupOnly);
                }
                catch (Exception)
                {
                    isChecked = true;
                }
                Assert.IsFalse(isChecked);
            }
        }
Exemplo n.º 2
0
        public async Task Test_02_CreateBlogIncludeContentAsync()
        {
            using (KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                BlogHandler blogHandler = new BlogHandler(dbContext);
                ContentHandler contentHandler = new ContentHandler(dbContext);
                BlogXContentHandler bxcHandler = new BlogXContentHandler(dbContext);
                GroupHandler groupHandler = new GroupHandler(dbContext);
                BlogAccessControlHandler acHandler = new BlogAccessControlHandler(dbContext);
                BlogAccessControlXGroupHandler acxgHandler = new BlogAccessControlXGroupHandler(dbContext);

                List<long> contentIds = new List<long>();

                Content content = new Content()
                {
                    ContentPath = "testPath",
                    ContentBinary = new byte[] { 1, 3, 5 },
                    Type = ContentType.Photo,
                    MimeType = "jpg"
                };
                contentHandler.Add(content);
                contentHandler.SaveChanges();

                contentIds.Add(content.ID);

                Stopwatch sw = Stopwatch.StartNew();

                //1. test normal.
                Blog testBlog = await blogHandler.CreateBlogAsync(testPerson.ID, "testBlog", BlogInfoAccessInfo.All, null, contentIds);

                var time = sw.ElapsedMilliseconds;

                Blog testBlog_1 = await blogHandler.GetByIdAsync(testBlog.ID);
                BlogAccessControl testAC_1 = await acHandler.Fetch(x => x.BlogID == testBlog_1.ID).FirstOrDefaultAsync();
                BlogXContent testBXC_1 = await bxcHandler.Fetch(x => x.BlogID == testBlog_1.ID).FirstOrDefaultAsync();
                Content testContent_1 = testBXC_1.Content;

                Assert.IsNotNull(testBlog_1);
                Assert.IsNotNull(testAC_1);
                Assert.IsNotNull(testBXC_1);
                Assert.IsNotNull(testContent_1);

                Assert.AreEqual(testBlog_1.PersonID, testPerson.ID);
                Assert.AreEqual(testBlog_1.Content, "testBlog");
                Assert.AreEqual(testAC_1.AccessLevel, BlogInfoAccessInfo.All);
                Assert.AreEqual(testContent_1.ContentPath, "testPath");
                Assert.AreEqual(testContent_1.ContentBinary, new byte[] { 1, 3, 5 });

                //2. create test group object and test it.
                contentIds = new List<long>();

                Content content1 = new Content()
                {                    
                    ContentPath = "testFilePathYesOrNo",
                    ContentBinary = new byte[] { 23, 31, 45, 78, 99 },
                    Type = ContentType.Video,
                    MimeType = "jpg"
                };
                contentHandler.Add(content1);
                contentHandler.SaveChanges();

                contentIds.Add(content1.ID);

                Group testGroup = await groupHandler.CreateGroupAsync(testPerson.ID, "testGroup", GroupType.GroupList);

                testBlog = await blogHandler.CreateBlogAsync(testPerson.ID, "testBlogGroupOnly", BlogInfoAccessInfo.GroupOnly, testGroup.ID, contentIds);

                Blog testBlog_2 = await blogHandler.GetByIdAsync(testBlog.ID);
                BlogAccessControl testAC_2 = await acHandler.Fetch(x => x.BlogID == testBlog_2.ID).FirstOrDefaultAsync();
                BlogAccessControlXGroup testACXG_2 = await acxgHandler.Fetch(x => x.GroupID == testGroup.ID && x.BlogAccessControlID == testAC_2.ID).FirstOrDefaultAsync();
                BlogXContent testBXC_2 = await bxcHandler.Fetch(x => x.BlogID == testBlog_2.ID).FirstOrDefaultAsync();
                Content testContent_2 = testBXC_2.Content;

                Assert.IsNotNull(testBlog_2);
                Assert.IsNotNull(testAC_2);
                Assert.IsNotNull(testACXG_2);
                Assert.IsNotNull(testBXC_2);
                Assert.IsNotNull(testContent_2);

                Assert.AreEqual(testBlog_2.PersonID, testPerson.ID);
                Assert.AreEqual(testBlog_2.Content, "testBlogGroupOnly");
                Assert.AreEqual(testAC_2.AccessLevel, BlogInfoAccessInfo.GroupOnly);
                Assert.AreEqual(testContent_2.ContentPath, "testFilePathYesOrNo");
                Assert.AreEqual(testContent_2.ContentBinary, new byte[] { 23, 31, 45, 78, 99 });
                Assert.AreEqual(testContent_2.Type, ContentType.Video);    
            }

            using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                BlogHandler blogHandler = new BlogHandler(dbContext);
                ContentHandler contentHandler = new ContentHandler(dbContext);
                BlogXContentHandler bxcHandler = new BlogXContentHandler(dbContext);
                GroupHandler groupHandler = new GroupHandler(dbContext);
                BlogAccessControlHandler acHandler = new BlogAccessControlHandler(dbContext);
                BlogAccessControlXGroupHandler acxgHandler = new BlogAccessControlXGroupHandler(dbContext);

                List<long> contentIds = new List<long>();

                //3. create test group object but set BlogInfoAccessInfo other value.

                Content content1 = new Content()
                {
                    ContentPath = "testFilePathYesOrNo",
                    ContentBinary = new byte[] { 23, 31, 45, 78, 99 },
                    Type = ContentType.Video,
                    MimeType = "jpg"
                };
                contentHandler.Add(content1);
                contentHandler.SaveChanges();

                contentIds.Add(content1.ID);

                Group testGroup_1 = await groupHandler.CreateGroupAsync(testPerson.ID, "testGroupWithTest", GroupType.GroupList);

                Blog testBlog = await blogHandler.CreateBlogAsync(testPerson.ID, "testBlogNotSetGroupOnly", BlogInfoAccessInfo.MyselfOnly, testGroup_1.ID, contentIds);

                Blog testBlog_3 = await blogHandler.GetByIdAsync(testBlog.ID);
                BlogAccessControl testAC_3 = await acHandler.Fetch(x => x.BlogID == testBlog_3.ID).FirstOrDefaultAsync();
                BlogAccessControlXGroup testACXG_3 = await acxgHandler.Fetch(x => x.GroupID == testGroup_1.ID && x.BlogAccessControlID == testAC_3.ID).FirstOrDefaultAsync();
                BlogXContent testBXC_3 = await bxcHandler.Fetch(x => x.BlogID == testBlog_3.ID).FirstOrDefaultAsync();
                Content testContent_3 = testBXC_3.Content;

                Assert.IsNotNull(testBlog_3);
                Assert.IsNotNull(testAC_3);
                Assert.IsNotNull(testBXC_3);
                Assert.IsNotNull(testContent_3);

                //it expect null because BlogInfoAccessInfo is not GroupOnly.
                Assert.IsNull(testACXG_3);

                Assert.AreEqual(testBlog_3.PersonID, testPerson.ID);
                Assert.AreEqual(testBlog_3.Content, "testBlogNotSetGroupOnly");
                Assert.AreEqual(testAC_3.AccessLevel, BlogInfoAccessInfo.MyselfOnly);
                Assert.AreEqual(testContent_3.ContentPath, "testFilePathYesOrNo");
                Assert.AreEqual(testContent_3.ContentBinary, new byte[] { 23, 31, 45, 78, 99 });
                Assert.AreEqual(testContent_3.Type, ContentType.Video);

                //4. create multiple content and test it.
                contentIds = new List<long>();

                for (int i = 0; i < 3; i++)
                {
                    Content c = new Content()
                    {
                        ContentPath = "testMultiplePath",
                        ContentBinary = new byte[] { 12, 3, 4, 5, 7 },
                        Type = ContentType.Video,
                        MimeType = "jpg"
                    };
                    contentHandler.Add(c);
                    contentHandler.SaveChanges();

                    contentIds.Add(c.ID);
                }
                
                bool isChecked = false;
                try
                {
                    Blog testBlog_MultipleContent_1 = await blogHandler.CreateBlogAsync(testPerson.ID, "testBlogMultipleContent1", BlogInfoAccessInfo.MyselfOnly, testGroup_1.ID, contentIds);
                }
                catch (Exception ex)
                {
                    isChecked = true;
                    Assert.AreEqual(ex.GetType(), typeof(DisplayableException));
                    Assert.AreEqual(ex.Message, "发表视频不能超过1个");
                }
                Assert.IsTrue(isChecked);

                //5. create multiple content but different type and test it.
                contentIds = new List<long>();

                Content cPhoto = new Content()
                {
                    ContentPath = "testMultiplePath",
                    ContentBinary = new byte[] { 12, 3, 4, 5, 7 },
                    Type = ContentType.Photo,
                    MimeType = "jpg"
                };
                Content cMusic = new Content()
                {
                    ContentPath = "testMultiplePath",
                    ContentBinary = new byte[] { 12, 3, 4, 5, 7 },
                    Type = ContentType.Photo,
                    MimeType = "jpg"
                };
                Content cVideo = new Content()
                {
                    ContentPath = "testMultiplePath",
                    ContentBinary = new byte[] { 12, 3, 4, 5, 7 },
                    Type = ContentType.Video,
                    MimeType = "jpg"
                };
                contentHandler.Add(cPhoto);
                contentHandler.Add(cMusic);
                contentHandler.Add(cVideo);
                contentHandler.SaveChanges();

                contentIds.Add(cPhoto.ID);
                contentIds.Add(cMusic.ID);
                contentIds.Add(cVideo.ID);

                isChecked = false;
                try
                {
                    Blog testBlog_MultipleContent_2 = await blogHandler.CreateBlogAsync(testPerson.ID, "testBlogMultipleContent1", BlogInfoAccessInfo.MyselfOnly, testGroup_1.ID, contentIds);
                }
                catch (Exception ex)
                {
                    isChecked = true;
                    Assert.AreEqual(ex.GetType(), typeof(DisplayableException));
                    Assert.AreEqual(ex.Message, "不能发不同类型内容的Blog");
                }
                Assert.IsTrue(isChecked);

                //6. create some photo content and test it.
                contentIds = new List<long>();

                for (int i = 0; i < 6; i++)
                {
                    Content photo = new Content()
                    {
                        ContentPath = "testPhotoContentPath",
                        ContentBinary = new byte[] { 12, 3, 4, 5, 7 },
                        Type = ContentType.Photo,
                        MimeType = "jpg"
                    };
                    contentHandler.Add(photo);
                    contentHandler.SaveChanges();

                    contentIds.Add(photo.ID);
                }

                Group testGroup_2 = await groupHandler.CreateGroupAsync(testPerson.ID, "testGroupWithTest", GroupType.GroupList);

                Blog testBlog_MultiplePhoto_3 = await blogHandler.CreateBlogAsync(testPerson.ID, "testBlogMultiplePhoto", BlogInfoAccessInfo.GroupOnly, testGroup_2.ID, contentIds);

                Blog testPhotoBlog = await blogHandler.GetByIdAsync(testBlog_MultiplePhoto_3.ID);
                BlogAccessControl testPhotoAC = await acHandler.Fetch(x => x.BlogID == testPhotoBlog.ID).FirstOrDefaultAsync();
                BlogAccessControlXGroup testPhotoACXG = await acxgHandler.Fetch(x => x.GroupID == testGroup_2.ID && x.BlogAccessControlID == testPhotoAC.ID).FirstOrDefaultAsync();
                List<BlogXContent> testPhotoBXC = await bxcHandler.Fetch(x => x.BlogID == testPhotoBlog.ID).ToListAsync();


                Assert.IsFalse(testPhotoBlog.IsDeleted);
                Assert.IsNotNull(testPhotoBlog);
                Assert.IsNotNull(testPhotoAC);
                Assert.IsNotNull(testPhotoACXG);
                Assert.IsNotNull(testPhotoBXC);

                Assert.AreEqual(testPhotoBXC.Count, 6);
                Assert.AreEqual(testPhotoBlog.PersonID, testPerson.ID);
                Assert.AreEqual(testPhotoBlog.Content, "testBlogMultiplePhoto");
                Assert.AreEqual(testPhotoAC.AccessLevel, BlogInfoAccessInfo.GroupOnly);

                foreach (var photoBXC in testPhotoBXC)
                {
                    Assert.IsNotNull(photoBXC.Content);
                    Assert.AreEqual(photoBXC.Content.Type, ContentType.Photo);
                    Assert.AreEqual(photoBXC.Content.ContentPath, "testPhotoContentPath");
                    Assert.AreEqual(photoBXC.Content.ContentBinary, new byte[] { 12, 3, 4, 5, 7 });
                }
            }
        }