public void Should_handle_basic_CRUD_operations()
        {
            // arrange
            var target = new TestTarget
            {
                Id = 0,
                Data = new TestTargetData
                {
                    TestString = "Test Target Data"
                }
            };
            var dataProvider = new MssqlDataProvider();
            var repository = new TargetRepository(dataProvider);

            // act
            var result = repository.Update(target);

            // assert
            Assert.That(result, Is.GreaterThan(0));

            // now Get
            var persistedTarget = repository.Get<TestTarget>(target.TableName, result);
            Assert.That(persistedTarget.Id, Is.GreaterThan(0));
            Assert.That(persistedTarget.Data, Is.Not.Null);

            var targetData = (TestTargetData) persistedTarget.Data;
            Assert.That(targetData.TestString, Is.EqualTo("Test Target Data"));

            // remove the record
            repository.Delete(persistedTarget);
        }
        public void Should_return_ancestors_calling_GetAncestors()
        {
            // arrange
            var testTarget = new TestTarget
            {
                Id = 345,
                Data = new TestTargetData { TestString = "Its a target!" }
            };
            var node = new HierarchyNode
            {
                Id = 34,
                LeftId = 2,
                RightId = 3,
                TargetId = testTarget.Id
            };
            var ancestor1 = new HierarchyNode
            {
                Id = 22,
                LeftId = 1,
                RightId = 6,
                TargetId = testTarget.Id
            };
            var ancestor2 = new HierarchyNode
            {
                Id = 11,
                LeftId = 1,
                RightId = 13,
                TargetId = testTarget.Id
            };

            var mockHierarchyDataProvider = new Mock<IHierarchyDataProvider>();
            mockHierarchyDataProvider.Setup(p => p.GetAncestors(It.IsAny<HierarchyEntry>(), It.IsAny<HierarchyNode>(), true, true))
                .Returns(new List<HierarchyNode> { ancestor2, ancestor1, node });
            var mockHierarchyEntryRepository = new Mock<IHierarchyEntryRepository>();
            var mockTargetEntryRepository = new Mock<ITargetEntryRepository>();
            var mockTargetRepository = new Mock<ITargetRepository>();
            var mockTargetTypes = new List<Type>();
            var service = new HierarchyService(mockHierarchyDataProvider.Object, mockHierarchyEntryRepository.Object, mockTargetEntryRepository.Object, mockTargetRepository.Object, mockTargetTypes);

            // act
            var resultNode = service.GetAncestors(hierarchyEntry, node, true, true);

            // assert
            Assert.That(resultNode, Is.Not.Null);
            Assert.That(resultNode.Count, Is.EqualTo(3));
            Assert.That(resultNode[0].Id, Is.EqualTo(ancestor2.Id));
            Assert.That(resultNode[0].LeftId, Is.EqualTo(ancestor2.LeftId));
            Assert.That(resultNode[0].RightId, Is.EqualTo(ancestor2.RightId));
            Assert.That(resultNode[0].TargetId, Is.EqualTo(ancestor2.TargetId));
            Assert.That(resultNode[1].Id, Is.EqualTo(ancestor1.Id));
            Assert.That(resultNode[1].LeftId, Is.EqualTo(ancestor1.LeftId));
            Assert.That(resultNode[1].RightId, Is.EqualTo(ancestor1.RightId));
            Assert.That(resultNode[1].TargetId, Is.EqualTo(ancestor1.TargetId));
            Assert.That(resultNode[2].Id, Is.EqualTo(node.Id));
            Assert.That(resultNode[2].LeftId, Is.EqualTo(node.LeftId));
            Assert.That(resultNode[2].RightId, Is.EqualTo(node.RightId));
            Assert.That(resultNode[2].TargetId, Is.EqualTo(node.TargetId));

            mockHierarchyDataProvider.Verify(p => p.GetAncestors(It.IsAny<HierarchyEntry>(), It.IsAny<HierarchyNode>(), true, true));
        }
        public void Should_return_children_calling_GetChildren()
        {
            // arrange
            var testTarget = new TestTarget
            {
                Id = 345,
                Data = new TestTargetData { TestString = "Its a target!" }
            };
            var parentNode = new HierarchyNode
            {
                Id = 23,
                LeftId = 1,
                RightId = 6,
                TargetId = testTarget.Id
            };
            var child1 = new HierarchyNode
            {
                Id = 45,
                LeftId = 2,
                RightId = 3,
                TargetId = testTarget.Id
            };
            var child2 = new HierarchyNode
            {
                Id = 46,
                LeftId = 4,
                RightId = 5,
                TargetId = testTarget.Id
            };

            var mockHierarchyDataProvider = new Mock<IHierarchyDataProvider>();
            mockHierarchyDataProvider.Setup(p => p.GetChildren(It.IsAny<HierarchyEntry>(), It.IsAny<HierarchyNode>()))
                .Returns(new List<HierarchyNode> { child1, child2 });
            var mockHierarchyEntryRepository = new Mock<IHierarchyEntryRepository>();
            var mockTargetEntryRepository = new Mock<ITargetEntryRepository>();
            var mockTargetRepository = new Mock<ITargetRepository>();
            var mockTargetTypes = new List<Type>();
            var service = new HierarchyService(mockHierarchyDataProvider.Object, mockHierarchyEntryRepository.Object, mockTargetEntryRepository.Object, mockTargetRepository.Object, mockTargetTypes);

            // act
            var resultNode = service.GetChildren(hierarchyEntry, parentNode);

            // assert
            Assert.That(resultNode, Is.Not.Null);
            Assert.That(resultNode.Count, Is.EqualTo(2));
            Assert.That(resultNode[0].Id, Is.EqualTo(child1.Id));
            Assert.That(resultNode[0].LeftId, Is.EqualTo(child1.LeftId));
            Assert.That(resultNode[0].RightId, Is.EqualTo(child1.RightId));
            Assert.That(resultNode[0].TargetId, Is.EqualTo(child1.TargetId));

            mockHierarchyDataProvider.Verify(p => p.GetChildren(It.IsAny<HierarchyEntry>(), It.IsAny<HierarchyNode>()));
        }
        public void Should_return_valid_Root_Node_calling_Initializehierarchy()
        {
            // arrange
            var rootNodeTarget = new TestTarget
            {
                Id = 0,
                Data = new TestTargetData
                {
                    TestString = "Profile Root"
                }
            };
            var mockHierarchyDataProvider = new Mock<IHierarchyDataProvider>();
            mockHierarchyDataProvider.Setup(p => p.Add(It.IsAny<HierarchyEntry>(), It.IsAny<HierarchyNode>()));
            var mockHierarchyEntryRepository = new Mock<IHierarchyEntryRepository>();
            var mockTargetEntryRepository = new Mock<ITargetEntryRepository>();
            var mockTargetRepository = new Mock<ITargetRepository>();
            var mockTargetTypes = new List<Type>();

            var service = new HierarchyService(mockHierarchyDataProvider.Object, mockHierarchyEntryRepository.Object, mockTargetEntryRepository.Object, mockTargetRepository.Object, mockTargetTypes);

            // act
            var rootHierarchyNode = service.InitializeHierarchy(hierarchyEntry, rootNodeTarget);

            // assert
            Assert.That(rootHierarchyNode, Is.Not.Null);
            Assert.That(rootHierarchyNode.LeftId, Is.EqualTo(1));
            Assert.That(rootHierarchyNode.RightId, Is.EqualTo(2));
            Assert.That(rootHierarchyNode.Id, Is.EqualTo(0));

            mockHierarchyDataProvider
                .Verify(p => p.Add(It.IsAny<HierarchyEntry>(), It.IsAny<HierarchyNode>()));
        }
        public void Should_return_valid_node_calling_InsertNode()
        {
            // arrange
            var parentNode = new HierarchyNode
            {
                Id = 0,
                LeftId = 1,
                RightId = 2
            };
            var childTarget = new TestTarget
            {
                Id = 345,
                Data = new TestTargetData
                {
                    TestString =  "Its a boy!"
                }
            };

            var mockHierarchyDataProvider = new Mock<IHierarchyDataProvider>();
            mockHierarchyDataProvider.Setup(p => p.PrepareForInsertNode(It.IsAny<HierarchyEntry>(), It.IsAny<HierarchyNode>()));
            mockHierarchyDataProvider.Setup(p => p.Add(It.IsAny<HierarchyEntry>(), It.IsAny<HierarchyNode>()));
            var mockHierarchyEntryRepository = new Mock<IHierarchyEntryRepository>();
            var mockTargetEntryRepository = new Mock<ITargetEntryRepository>();
            var mockTargetRepository = new Mock<ITargetRepository>();
            var mockTargetTypes = new List<Type>();
            var service = new HierarchyService(mockHierarchyDataProvider.Object, mockHierarchyEntryRepository.Object, mockTargetEntryRepository.Object, mockTargetRepository.Object, mockTargetTypes);

            // act
            var newHierarchyNode = service.InsertNode(hierarchyEntry, parentNode, childTarget);

            // assert
            Assert.That(newHierarchyNode, Is.Not.Null);
            Assert.That(newHierarchyNode.LeftId, Is.EqualTo(parentNode.RightId));
            Assert.That(newHierarchyNode.RightId, Is.EqualTo(parentNode.RightId + 1));
            Assert.That(newHierarchyNode.Id, Is.EqualTo(0));

            mockHierarchyDataProvider.Verify(p => p.PrepareForInsertNode(It.IsAny<HierarchyEntry>(), It.IsAny<HierarchyNode>()));
            mockHierarchyDataProvider.Verify(p => p.Add(It.IsAny<HierarchyEntry>(), It.IsAny<HierarchyNode>()));
        }
        public void Should_return_parent_calling_GetParent()
        {
            // arrange
            var testTarget = new TestTarget
            {
                Id = 345,
                Data = new TestTargetData { TestString = "Its a target!" }
            };
            var childNode = new HierarchyNode
            {
                Id = 34,
                LeftId = 2,
                RightId = 3,
                TargetId = testTarget.Id
            };
            var parentNode = new HierarchyNode
            {
                Id = 22,
                LeftId = 1,
                RightId = 6,
                TargetId = testTarget.Id
            };

            var mockHierarchyDataProvider = new Mock<IHierarchyDataProvider>();
            mockHierarchyDataProvider.Setup(p => p.GetParent(It.IsAny<HierarchyEntry>(), It.IsAny<HierarchyNode>()))
                .Returns(parentNode);
            var mockHierarchyEntryRepository = new Mock<IHierarchyEntryRepository>();
            var mockTargetEntryRepository = new Mock<ITargetEntryRepository>();
            var mockTargetRepository = new Mock<ITargetRepository>();
            var mockTargetTypes = new List<Type>();
            var service = new HierarchyService(mockHierarchyDataProvider.Object, mockHierarchyEntryRepository.Object, mockTargetEntryRepository.Object, mockTargetRepository.Object, mockTargetTypes);

            // act
            var resultNode = service.GetParent(hierarchyEntry, childNode);

            // assert
            Assert.That(resultNode, Is.Not.Null);
            Assert.That(resultNode.Id, Is.EqualTo(parentNode.Id));
            Assert.That(resultNode.LeftId, Is.EqualTo(parentNode.LeftId));
            Assert.That(resultNode.RightId, Is.EqualTo(parentNode.RightId));
            Assert.That(resultNode.TargetId, Is.EqualTo(parentNode.TargetId));

            mockHierarchyDataProvider.Verify(p => p.GetParent(It.IsAny<HierarchyEntry>(), It.IsAny<HierarchyNode>()));
        }