Esempio n. 1
0
        public void GetAvailableRelationsForIdTest()
        {
            var tagInstance5 = new AnnotationTagInstance(new AnnotationTag()
            {
                Id = 5
            })
            {
                Id = 5
            };
            var relation35 = new AnnotationTagInstanceRelation(_tester.TagInstance3, tagInstance5)
            {
                Id = 5
            };
            var expected = new List <RelationResult>()
            {
                new RelationResult(_tester.Relation34), new RelationResult(relation35)
            };

            _tester.TestController()
            .WithDbContext(dbContext => dbContext
                           .WithSet <AnnotationTag>(db => db.AddRange(_tester.Tag1, _tester.Tag2, _tester.Tag3, _tester.Tag4))
                           .WithSet <AnnotationTagInstance>(db => db.AddRange(_tester.TagInstance1, _tester.TagInstance2, _tester.TagInstance3, _tester.TagInstance4, tagInstance5))
                           .WithSet <AnnotationTagRelationRule>(db => db.AddRange(_tester.RelationRule12, _tester.RelationRule32, _tester.RelationRule34))
                           .WithSet <AnnotationTagInstanceRelation>(db => db.AddRange(_tester.Relation12, _tester.Relation34, relation35))
                           )
            .Calling(c => c.GetAllowedRelationsForInstance(_tester.TagInstance3.Id))
            .ShouldReturn()
            .Ok()
            .WithModelOfType <List <RelationResult> >()
            .Passing(RelationsEqualPredicate(expected));
        }
        /// <summary>
        /// Should return code 200 and an empty list of tag relations if there are no relations possible for the given tag instance
        /// </summary>
        // TODO [Test]
        public void GetAvailableRelationsForIdTest_NoRelations()
        {
            var expected  = new List <AnnotationTagRelation>();
            var instance3 = new AnnotationTagInstance(_tag3);
            var instances = new List <AnnotationTagInstance>()
            {
                new AnnotationTagInstance(_tag1),
                new AnnotationTagInstance(_tag2),
                instance3,
                new AnnotationTagInstance(_tag4)
            };

            MyMvc
            .Controller <AnnotationController>()
            .WithAuthenticatedUser(user => user.WithClaim("Id", "1"))
            .WithDbContext(dbContext => dbContext
                           .WithSet <User>(db => db.Add(_admin))
                           .WithSet <AnnotationTag>(db => db.AddRange(_tag1, _tag2, _tag3, _tag4))
                           // no relations exist between the tags
                           // TODO How to model that the tag instances are part of the same document?
                           )
            .Calling(c => c.GetAvailableRelationsForInstance(_tag3.Id))
            .ShouldReturn()
            .Ok()
            .WithModelOfType <List <AnnotationTagRelation> >()
            .Passing(actual => expected.SequenceEqual(actual));
        }
Esempio n. 3
0
        internal bool AddTagInstance(int tagModelId)
        {
            AnnotationTag         model    = DbContext.AnnotationTags.Single(m => m.Id == tagModelId);
            AnnotationTagInstance instance = new AnnotationTagInstance(model);

            DbContext.AnnotationTagInstances.Add(instance);
            DbContext.SaveChanges();
            return(true);
        }
Esempio n. 4
0
        /// <summary>
        /// Should return code 200 and an empty list of tag relations if there are no relations possible for the given tag instance
        /// </summary>
        // TODO [Fact]
        public void GetAvailableRelationsForIdTest_NoRelations()
        {
            var expected  = new List <AnnotationTagInstanceRelation>();
            var instance3 = new AnnotationTagInstance(_tester.Tag3);
            var instances = new List <AnnotationTagInstance>()
            {
                new AnnotationTagInstance(_tester.Tag1),
                new AnnotationTagInstance(_tester.Tag2),
                instance3,
                new AnnotationTagInstance(_tester.Tag4)
            };

            _tester.TestControllerWithMockData()
            // no relations exist between the tags
            // TODO How to model that the tag instances are part of the same document?
            .Calling(c => c.GetAllowedRelationsForInstance(_tester.Tag3.Id))
            .ShouldReturn()
            .Ok()
            .WithModelOfType <List <AnnotationTagInstanceRelation> >()
            .Passing(actual => expected.SequenceEqual(actual));
        }
Esempio n. 5
0
        internal EntityResult UpdateDocument(int topicId, int userId, string htmlContent)
        {
            try
            {
                DbContext.Topics.Include(t => t.Document).Single(t => t.Id == topicId);
            }
            catch (InvalidOperationException)
            {
                return(EntityResult.Error("Unknown Topic"));
            }
            // already exitsts


            Document document;

            try
            {
                document           = GetDocumentById(topicId);
                document.UpdaterId = userId;
                document.Content   = htmlContent;
            }
            catch (InvalidOperationException)
            {
                document = new Document(topicId, userId, htmlContent);
                DbContext.Add(document);
            }

            // document is saved, so now we can parse it
            var stream       = new System.IO.StringReader("<pseudo-root>" + htmlContent + "</pseudo-root>");
            var xmlReader    = XmlReader.Create(stream);
            var tagInstances = new List <AnnotationTagInstance>();

            try
            {
                while (xmlReader.Read())
                {
                    if (xmlReader.NodeType != XmlNodeType.Element)
                    {
                        continue;
                    }
                    if (!xmlReader.HasAttributes)
                    {
                        continue;
                    }

                    var tagModelId    = int.Parse(xmlReader.GetAttribute("data-tag-model-id"));
                    var tagInstanceId = int.Parse(xmlReader.GetAttribute("data-tag-id"));
                    var tagValue      = xmlReader.ReadElementContentAsString();
                    var rx            = new Regex("<span[^>]+?data-tag-id=\"" + tagInstanceId + "\".*?>");
                    var tagPosition   = rx.Match(htmlContent).Index;

                    var tagModel = DbContext.AnnotationTags.First(t => t.Id == tagModelId);
                    var tag      = new AnnotationTagInstance(tagModel)
                    {
                        IdInDocument       = tagInstanceId,
                        Value              = tagValue,
                        PositionInDocument = tagPosition,
                        Document           = document
                    };

                    tagInstances.Add(tag);
                }
                if (DbContext.AnnotationTagInstances.Any(i => i.Document == document))
                {
                    var oldInstances = DbContext.AnnotationTagInstances.Where(i => i.Document == document);
                    DbContext.AnnotationTagInstances.RemoveRange(oldInstances);
                }
                DbContext.AnnotationTagInstances.AddRange(tagInstances);
            }
            catch (Exception)
            {
                return(EntityResult.Error("Parsing Error"));
            }

            try
            {
                DbContext.SaveChanges();
                return(EntityResult.Successfull());
            }
            catch (Exception e)
            {
                return(EntityResult.Error(e.Message));
            }
        }
        public ControllerTester()
        {
            Admin = new User
            {
                Id    = 1,
                UId   = AdminUId,
                Email = "*****@*****.**",
                Role  = "Administrator"
            };
            Student = new User
            {
                Id    = 2,
                UId   = StudentUId,
                Email = "*****@*****.**",
                Role  = "Student"
            };
            Supervisor = new User
            {
                Id    = 3,
                UId   = SupervisorUId,
                Email = "*****@*****.**",
                Role  = "Supervisor"
            };
            Reviewer = new User
            {
                Id    = 6,
                UId   = ReviewerUId,
                Email = "*****@*****.**",
                Role  = "Reviewer"
            };

            /*
             * Layer1   Layer2
             * |-tag1   |-tag2
             * |-tag3   |-tag4
             *
             * Layer Relation Rules:
             * Layer1 -> Layer2
             *
             * Annotation AnnotationTag Relations:
             * tag1 -> tag2
             * tag3 -> tag2
             * tag3 -> tag4
             */
            Layer1 = new Layer()
            {
                Id = 1, Name = "Time"
            };
            Layer2 = new Layer()
            {
                Id = 2, Name = "Perspective"
            };
            Tag1 = new AnnotationTag()
            {
                Id = 1, Layer = Layer1.Name
            };
            Tag2 = new AnnotationTag()
            {
                Id = 2, Layer = Layer2.Name
            };
            Tag3 = new AnnotationTag()
            {
                Id = 3, Layer = Layer1.Name
            };
            Tag4 = new AnnotationTag()
            {
                Id = 4, Layer = Layer2.Name
            };
            Tag1.ChildTags = new List <AnnotationTag>()
            {
                Tag3
            };
            Tag2.ChildTags = new List <AnnotationTag>()
            {
                Tag4
            };
            RelationRule12 = new AnnotationTagRelationRule()
            {
                Id = 3, SourceTagId = Tag1.Id, TargetTagId = Tag2.Id, Title = "Tag Relation Rule 1->2"
            };
            RelationRule32 = new AnnotationTagRelationRule()
            {
                Id = 5, SourceTagId = Tag3.Id, TargetTagId = Tag2.Id, Title = "Tag Relation Rule 3->2"
            };
            RelationRule34 = new AnnotationTagRelationRule()
            {
                Id = 7, SourceTagId = Tag3.Id, TargetTagId = Tag4.Id, Title = "Tag Relation Rule 3->4"
            };
            TagInstance1 = new AnnotationTagInstance(Tag1)
            {
                Id = 1
            };
            TagInstance2 = new AnnotationTagInstance(Tag2)
            {
                Id = 2
            };
            TagInstance3 = new AnnotationTagInstance(Tag3)
            {
                Id = 3
            };
            TagInstance4 = new AnnotationTagInstance(Tag4)
            {
                Id = 4
            };
            Relation12 = new AnnotationTagInstanceRelation(TagInstance1, TagInstance2)
            {
                Id = 3
            };
            Relation32 = new AnnotationTagInstanceRelation(TagInstance3, TagInstance2)
            {
                Id = 5
            };
            Relation34 = new AnnotationTagInstanceRelation(TagInstance3, TagInstance4)
            {
                Id = 7
            };
            LayerRelationRule = new LayerRelationRule()
            {
                Id            = 3,
                SourceLayer   = Layer1,
                SourceLayerId = Layer1.Id,
                TargetLayer   = Layer2,
                TargetLayerId = Layer2.Id,
                Color         = "test-color",
                ArrowStyle    = "test-style"
            };
            TopicOne = new Topic
            {
                Id          = 1,
                Title       = "Paderborner Dom",
                Status      = "InReview",
                Deadline    = new DateTime(2017, 5, 04),
                CreatedById = Supervisor.Id,
                Description = "Church"
            };
            TopicTwo = new Topic
            {
                Id          = 2,
                Title       = "Westerntor",
                Status      = "InProgress",
                Deadline    = new DateTime(2017, 4, 18),
                CreatedById = Supervisor.Id,
                Description = "Shopping"
            };
            UnreadNotification = new Notification
            {
                NotificationId = 1,
                UserId         = Student.Id,
                UpdaterId      = Supervisor.Id,
                Type           = NotificationType.TOPIC_ASSIGNED_TO,
                TopicId        = TopicOne.Id,
                IsRead         = false
            };
            ReadNotification = new Notification
            {
                NotificationId = 2,
                UserId         = Student.Id,
                UpdaterId      = Supervisor.Id,
                Type           = NotificationType.TOPIC_ASSIGNED_TO,
                TopicId        = TopicTwo.Id,
                IsRead         = true
            };
            Subscription = new Subscription // Adding a new subscription
            {
                SubscriptionId = 1,
                SubscriberId   = Student.Id,
                Subscriber     = Student,
                Type           = NotificationType.TOPIC_ASSIGNED_TO
            };
            SupervisorUser = new TopicUser
            {
                TopicId = TopicOne.Id,
                UserId  = Supervisor.Id,
                Role    = Supervisor.Role
            };
            StudentUser = new TopicUser
            {
                TopicId = TopicOne.Id,
                UserId  = Student.Id,
                Role    = Student.Role
            };
            ReviewerUser = new TopicUser
            {
                TopicId = TopicOne.Id,
                UserId  = Reviewer.Id,
                Role    = Reviewer.Role
            };
            FirstDocument = new Document
            {
                TopicId   = TopicOne.Id,
                UpdaterId = Admin.Id,
                Content   = "Hello"
            };
            TagInstanceForDocument = new AnnotationTagInstance
            {
                Id         = 5,
                TagModelId = Tag1.Id,
                Document   = FirstDocument
            };
        }