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 ObservableLoadsNoTermsFromEmptyValidXml()
        {
            var service = new XmlTermsService(XmlTermsServiceFixture.TermsToFile(
                                                  Enumerable.Empty <Term>(),
                                                  "ObservableLoadsNoTermsFromEmptyValidXml.xml"));

            var serviceTerms = new List <Term>();

            service
            .LoadTerms(Scheduler.Immediate)
            .Subscribe(terms => serviceTerms.AddRange(terms));

            ReactiveAssert.AreElementsEqual(Enumerable.Empty <Term>(), serviceTerms);
        }
        public void ObservableThrowsOnAddExistingTerm()
        {
            var service = new XmlTermsService(XmlTermsServiceFixture.TermsToFile(
                                                  XmlTermsServiceFixture.SampleTerms,
                                                  "ObservableThrowsOnAddExistingTerm.xml"));

            Exception ex = null;

            service
            .AddTerm(XmlTermsServiceFixture.SampleTerms[0], Scheduler.Immediate)
            .Subscribe(_ => { }, ex2 => ex = ex2, () => { });

            Assert.IsInstanceOfType(ex, typeof(InvalidOperationException));
            Assert.IsTrue(ex.Message.Contains("already exists"));
        }
        public void ObservableThrowsOnRemoveNonExistentTerm()
        {
            var service = new XmlTermsService(XmlTermsServiceFixture.TermsToFile(
                                                  XmlTermsServiceFixture.SampleTerms,
                                                  "ObservableThrowsOnRemoveNonExistentTerm.xml"));
            var newTerm = new Term("nonExistentTerm", String.Empty);

            Exception ex = null;

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

            Assert.IsInstanceOfType(ex, typeof(InvalidOperationException));
            Assert.IsTrue(ex.Message.Contains("doesn't exist"));
        }
        public void ObservableThrowsFromInvalidXml()
        {
            var service = new XmlTermsService(XmlTermsServiceFixture.TermsToFile(
                                                  new Term[]
            {
                XmlTermsServiceFixture.SampleTerms[0],
                new Term(null, String.Empty),
            },
                                                  "ObservableThrowsFromInvalidXml.xml"));

            ReactiveAssert.Throws <InvalidTermsStorageException>(
                () =>
            {
                service
                .LoadTerms(Scheduler.Immediate)
                .Subscribe();
            });
        }
        public void ObservableShouldntPushTermsFromInvalidXml()
        {
            var mix = new Term[]
            {
                XmlTermsServiceFixture.SampleTerms[0],
                new Term(null, null),
                XmlTermsServiceFixture.SampleTerms[1],
            };

            var service = new XmlTermsService(
                XmlTermsServiceFixture.TermsToFile(mix, "ObservableShouldntPushTermsFromInvalidXml.xml"));

            bool onNextInvoked = false;

            service
            .LoadTerms(Scheduler.Immediate)
            .Subscribe(terms => onNextInvoked = true, _ => { }, () => { });

            Assert.IsFalse(onNextInvoked);
        }
        public void ObservableAddsAndPushesNewTerm()
        {
            var fileName = XmlTermsServiceFixture.TermsToFile(
                XmlTermsServiceFixture.SampleTerms,
                "ObservableAddsAndPushesNewTerm.xml");

            var service = new XmlTermsService(fileName);
            var term    = new Term("newTerm", "newDef");

            Term pushed = null;

            service.AddTerm(term, Scheduler.Immediate).Subscribe(p => pushed = p);

            Assert.AreEqual(term, pushed);

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

            Assert.IsNotNull(xTerm);
            Assert.AreEqual(term.Definition, xTerm.Attribute(TermXmlNames.TermDefinitionAttribute).Value);
        }
        public void ObservableRemovesAndPushesTerm()
        {
            var fileName = XmlTermsServiceFixture.TermsToFile(
                XmlTermsServiceFixture.SampleTerms,
                "ObservableRemovesTerm.xml");

            var service = new XmlTermsService(fileName);
            var term    = XmlTermsServiceFixture.SampleTerms[0];

            Term pushed = null;

            service
            .RemoveTerm(term, Scheduler.Immediate)
            .Subscribe(p => pushed = p);

            Assert.AreEqual(term, pushed);

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

            Assert.IsNull(xTerm);
        }