public void ServiceThrowsOnUpdateToTermWithoutName()
        {
            var service = new XmlTermsService(String.Empty);

            var nameless   = new Term(null, String.Empty);
            var whitespace = new Term(" ", String.Empty);

            // Test null.
            try
            {
                service.UpdateTerm(XmlTermsServiceFixture.SampleTerms[0], nameless, Scheduler.Immediate);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(ArgumentException));
                Assert.IsTrue(((ArgumentException)ex).ParamName == "newTerm");
            }

            // Test whitespace.
            try
            {
                service.UpdateTerm(XmlTermsServiceFixture.SampleTerms[0], whitespace, Scheduler.Immediate);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(ArgumentException));
                Assert.IsTrue(((ArgumentException)ex).ParamName == "newTerm");
            }
        }
        public void ServiceThrowsOnUpdateNullTerm()
        {
            // Check validation of old term.
            var service = new XmlTermsService(String.Empty);

            try
            {
                service.UpdateTerm(null, XmlTermsServiceFixture.SampleTerms[0], Scheduler.Immediate);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(ArgumentNullException));
                Assert.IsTrue(((ArgumentNullException)ex).ParamName == "oldTerm");
            }

            // Check validation of new term.
            service = new XmlTermsService(String.Empty);
            try
            {
                service.UpdateTerm(XmlTermsServiceFixture.SampleTerms[0], null, Scheduler.Immediate);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(ArgumentNullException));
                Assert.IsTrue(((ArgumentNullException)ex).ParamName == "newTerm");
            }
        }
        public void ObservableUpdatesAndPushesTerm()
        {
            var fileName = XmlTermsServiceFixture.TermsToFile(
                XmlTermsServiceFixture.SampleTerms,
                "ObservableUpdatesAndPushesTerm.xml");

            var service = new XmlTermsService(fileName);
            var oldTerm = XmlTermsServiceFixture.SampleTerms[0];
            var newTerm = new Term("new term0", "new def 0");

            Term pushed = null;

            service
            .UpdateTerm(oldTerm, newTerm, Scheduler.Immediate)
            .Subscribe(p => pushed = p);

            Assert.AreEqual(newTerm, pushed);

            // Check that old term is deleted.

            var xTerm = XDocument.Load(fileName)
                        .Descendants(TermXmlNames.TermElement)
                        .FirstOrDefault(x => x.Attribute(TermXmlNames.TermNameAttribute).Value == oldTerm.Name);

            Assert.IsNull(xTerm);

            // Check that new term is added.

            xTerm = XDocument.Load(fileName)
                    .Descendants(TermXmlNames.TermElement)
                    .FirstOrDefault(x => x.Attribute(TermXmlNames.TermNameAttribute).Value == newTerm.Name);

            Assert.IsNotNull(xTerm);
            Assert.AreEqual(newTerm.Definition, xTerm.Attribute(TermXmlNames.TermDefinitionAttribute).Value);
        }
        public void ObservableThrowsOnUpdateNonExistentTerm()
        {
            var service = new XmlTermsService(XmlTermsServiceFixture.TermsToFile(
                                                  XmlTermsServiceFixture.SampleTerms,
                                                  "ObservableThrowsOnUpdateNonExistentTerm.xml"));
            var newTerm = new Term("nonExistentTerm", String.Empty);

            Exception ex = null;

            service
            .UpdateTerm(newTerm, newTerm, Scheduler.Immediate)
            .Subscribe(_ => { }, ex2 => ex = ex2, () => { });

            Assert.IsInstanceOfType(ex, typeof(InvalidOperationException));
            Assert.IsTrue(ex.Message.Contains("doesn't exist"));
        }
        public void ObservableThrowsOnUpdateAnotherExistentTerm()
        {
            var service = new XmlTermsService(XmlTermsServiceFixture.TermsToFile(
                                                  XmlTermsServiceFixture.SampleTerms,
                                                  "ObservableThrowsOnUpdateAnotherExistentTerm.xml"));
            var oldTerm = XmlTermsServiceFixture.SampleTerms[0];
            var newTerm = XmlTermsServiceFixture.SampleTerms[1];

            Exception ex = null;

            service
            .UpdateTerm(oldTerm, newTerm, Scheduler.Immediate)
            .Subscribe(_ => { }, ex2 => ex = ex2, () => { });

            Assert.IsInstanceOfType(ex, typeof(InvalidOperationException));
            Assert.IsTrue(ex.Message.Contains("already exists"));
        }