Example #1
0
        public void AddNodeTest()
        {
            //arrange
            var exp1    = new Experiment("exp1");
            var exp2    = new Experiment("exp2");
            var exp2c   = new Experiment("exp2");
            var exp11   = new Experiment("exp1-1");
            var exp12   = new Experiment("exp1-2");
            var sig121  = new Signal("sig1-2-1");
            var sig121c = new Signal("sig1-2-1");

            //act
            //add 2 root
            //    /exp1
            //    /exp2
            myCoreService.AddJdbcEntityToAsync(Guid.Empty, exp1).Wait();
            myCoreService.AddJdbcEntityToAsync(Guid.Empty, exp2).Wait();

            //1.1 add entity with same name to root.
            string errorSameNameOnRoot = "";

            try
            {
                myCoreService.AddJdbcEntityToAsync(Guid.Empty, exp2c).Wait();
            }
            catch (Exception e)
            {
                //this is async call so exception is wrapped
                errorSameNameOnRoot = e.InnerException.Message;
            }
            Assert.AreEqual(ErrorMessages.NameDuplicateError, errorSameNameOnRoot);

            //1.2 add signal to root, expecting error, signal's parent can only be experiment.
            string errorSigOnRoot = "";

            try
            {
                myCoreService.AddJdbcEntityToAsync(Guid.Empty, sig121).Wait();
            }
            catch (Exception e)
            {
                //this is async call so exception is wrapped
                errorSigOnRoot = e.InnerException.Message;
            }
            Assert.AreEqual(ErrorMessages.NotValidParentError, errorSigOnRoot);

            //add child
            //    /exp1/exp1-1/
            //    /exp1/exp1-2/sig1-2-1
            myCoreService.AddJdbcEntityToAsync(exp1.Id, exp11).Wait();
            myCoreService.AddJdbcEntityToAsync(exp1.Id, exp12).Wait();
            myCoreService.AddJdbcEntityToAsync(exp12.Id, sig121).Wait();

            //1.3 add signal to another signal, expecting error, signal's parent can only be experiment.
            string errorAddToSignal = "";

            try
            {
                myCoreService.AddJdbcEntityToAsync(sig121.Id, sig121c).Wait();
            }
            catch (Exception e)
            {
                //this is async call so exception is wrapped
                errorAddToSignal = e.InnerException.Message;
            }
            Assert.AreEqual(ErrorMessages.NotValidParentError, errorAddToSignal);

            //1.4 add the sig with the same name to the parent errexpected
            string errorSameName = "";

            try
            {
                myCoreService.AddJdbcEntityToAsync(exp12.Id, sig121c).Wait();
            }
            catch (Exception e)
            {
                errorSameName = e.InnerException.Message;
            }
            Assert.AreEqual(ErrorMessages.NameDuplicateError, errorSameName);

            //1.5 the parent is  non exist, error expected
            string errorParentNonExistWhenAdd = "";

            try
            {
                myCoreService.AddJdbcEntityToAsync(Guid.NewGuid(), sig121c).Wait();
            }
            catch (Exception e)
            {
                //this is async call so exception is wrapped
                errorParentNonExistWhenAdd = e.InnerException.Message;
            }
            Assert.AreEqual(ErrorMessages.ParentNotExistError, errorParentNonExistWhenAdd);

            //1.6 the parent is  non exist, error expected
            string errorParentNonExist2 = "";

            try
            {
                var tNothing = myCoreService.GetAllChildrenIdAsync(Guid.NewGuid()).Result;
            }
            catch (Exception e)
            {
                //this is async call so exception is wrapped
                errorParentNonExist2 = e.InnerException.Message;
            }
            Assert.AreEqual(ErrorMessages.ParentNotExistError, errorParentNonExist2);

            //1.7 assert id and entity.
            //    /exp1/exp1-1/
            //    /exp1/exp1-2/sig1-2-1
            var roots = myCoreService.GetAllChildrenAsync().Result;

            CollectionAssert.AreEqual(new List <Guid> {
                exp1.Id, exp2.Id
            }, roots.Select(r => r.Id).ToList());

            var rootsById = myCoreService.GetAllChildrenAsync(Guid.Empty).Result;

            CollectionAssert.AreEqual(new List <Guid> {
                exp1.Id, exp2.Id
            }, rootsById.Select(r => r.Id).ToList());

            var rootIdsById = myCoreService.GetAllChildrenIdAsync(Guid.Empty).Result;

            CollectionAssert.AreEqual(new List <Guid> {
                exp1.Id, exp2.Id
            }, rootIdsById.ToList());

            var secLevId = myCoreService.GetAllChildrenIdAsync(exp1).Result;

            CollectionAssert.AreEquivalent(new List <Guid> {
                exp11.Id, exp12.Id
            }, secLevId.ToList());

            var secLevIdById = myCoreService.GetAllChildrenIdAsync(exp1.Id).Result;

            CollectionAssert.AreEquivalent(new List <Guid> {
                exp11.Id, exp12.Id
            }, secLevIdById.ToList());

            var trdLevIdById = myCoreService.GetAllChildrenIdAsync(exp12.Id).Result;

            CollectionAssert.AreEquivalent(new List <Guid> {
                sig121.Id
            }, trdLevIdById.ToList());

            var shouldBeEmpty = myCoreService.GetAllChildrenAsync(exp11).Result;

            Assert.AreEqual(shouldBeEmpty.Count(), 0);

            var sig121ParentId = myCoreService.GetParentIdAsync(sig121.Id).Result;

            Assert.AreEqual(exp12.Id, sig121ParentId);

            var errorParentNullId = myCoreService.GetParentIdAsync(Guid.NewGuid()).Result;

            Assert.AreEqual(Guid.Empty, errorParentNullId);

            Assert.AreEqual(exp11.EntityType, JDBCEntityType.Experiment);
            Assert.AreEqual(sig121.EntityType, JDBCEntityType.Signal);
        }