Ejemplo n.º 1
0
        public void ExportManager_chains_exporter_to_packager()
        {
            // Arrange
            XUri dekiApiUri     = new XUri("http://mock/@api/deki");
            XDoc exportDocument = new XDoc("export");
            XUri item1Uri       = dekiApiUri.At("foo", "bar", "abc");
            XDoc item1Doc       = new XDoc("item1");
            XUri item2Uri       = dekiApiUri.At("foo", "bar", "def");
            XDoc item2Doc       = new XDoc("item2");
            XDoc exportResponse = new XDoc("export")
                                  .Start("requests")
                                  .Start("request")
                                  .Attr("method", "GET")
                                  .Attr("dataid", "abc")
                                  .Attr("href", item1Uri)
                                  .Start("header").Attr("name", "h_1").Attr("value", "v_1").End()
                                  .Start("header").Attr("name", "h_2").Attr("value", "v_2").End()
                                  .End()
                                  .Start("request")
                                  .Attr("method", "GET")
                                  .Attr("dataid", "def")
                                  .Attr("href", item2Uri)
                                  .End()
                                  .End()
                                  .Start("manifest")
                                  .Start("foo").Attr("dataid", "abc").End()
                                  .Start("bar").Attr("dataid", "def").End()
                                  .End();
            AutoMockPlug mock = MockPlug.Register(dekiApiUri);

            mock.Expect().Verb("POST").Uri(dekiApiUri.At("site", "export").With("relto", "0")).RequestDocument(exportDocument).Response(DreamMessage.Ok(exportResponse));
            mock.Expect().Verb("GET").Uri(item1Uri).RequestHeader("h_1", "v_1").RequestHeader("h_2", "v_2").Response(DreamMessage.Ok(item1Doc));
            mock.Expect().Verb("GET").Uri(item2Uri).Response(DreamMessage.Ok(item2Doc));
            var writes            = new List <string>();
            var mockPackageWriter = new Mock <IPackageWriter>();

            mockPackageWriter.Setup(x => x.WriteDataAsync(It.IsAny <ExportItem>(), It.IsAny <Result>()))
            .Returns(() => new Result().WithReturn())
            .Callback((ExportItem item, Result result) => writes.Add(item.DataId))
            .AtMost(2)
            .Verifiable();
            mockPackageWriter.Setup(x => x.WriteManifest(It.IsAny <XDoc>(), It.IsAny <Result>()))
            .Returns(() => new Result().WithReturn())
            .AtMostOnce()
            .Verifiable();

            // Act
            ExportManager manager = ExportManager.CreateAsync(Plug.New(dekiApiUri), exportDocument, 0, mockPackageWriter.Object, new Result <ExportManager>()).Wait();

            manager.ExportAsync(new Result()).Wait();

            // Assert
            Assert.IsTrue(mock.WaitAndVerify(TimeSpan.FromSeconds(1)), mock.VerificationFailure);
            Assert.AreEqual(2, manager.TotalItems);
            Assert.AreEqual(2, manager.CompletedItems);
            Assert.AreEqual(new[] { "abc", "def" }, writes.ToArray());
            mockPackageWriter.Verify(x => x.Dispose(), Times.Once());
            mockPackageWriter.VerifyAll();
        }
Ejemplo n.º 2
0
        public void Exporter_can_retrieve_items_by_dataid()
        {
            // Arrange
            XUri dekiApiUri     = new XUri("http://mock/@api/deki");
            XDoc exportDocument = new XDoc("export");
            XUri item1Uri       = dekiApiUri.At("foo", "bar", "abc");
            XDoc item1Doc       = new XDoc("item1");
            XUri item2Uri       = dekiApiUri.At("foo", "bar", "def");
            XDoc item2Doc       = new XDoc("item2");
            XDoc exportResponse = new XDoc("export")
                                  .Start("requests")
                                  .Start("request")
                                  .Attr("method", "GET")
                                  .Attr("dataid", "abc")
                                  .Attr("href", item1Uri)
                                  .Start("header").Attr("name", "h_1").Attr("value", "v_1").End()
                                  .Start("header").Attr("name", "h_2").Attr("value", "v_2").End()
                                  .End()
                                  .Start("request")
                                  .Attr("method", "GET")
                                  .Attr("dataid", "def")
                                  .Attr("href", item2Uri)
                                  .End()
                                  .End()
                                  .Start("manifest")
                                  .Start("foo").Attr("dataid", "abc").End()
                                  .Start("bar").Attr("dataid", "def").End()
                                  .End();
            AutoMockPlug mock = MockPlug.Register(dekiApiUri);

            mock.Expect().Verb("POST").Uri(dekiApiUri.At("site", "export").With("relto", "0")).RequestDocument(exportDocument).Response(DreamMessage.Ok(exportResponse));
            mock.Expect().Verb("GET").Uri(item1Uri).RequestHeader("h_1", "v_1").RequestHeader("h_2", "v_2").Response(DreamMessage.Ok(item1Doc));
            mock.Expect().Verb("GET").Uri(item2Uri).Response(DreamMessage.Ok(item2Doc));

            // Act
            Exporter   exporter = Exporter.CreateAsync(Plug.New(dekiApiUri), exportDocument, 0, new Result <Exporter>()).Wait();
            ExportItem item1    = exporter.GetItemAsync("abc", new Result <ExportItem>()).Wait();
            ExportItem item2    = exporter.GetItemAsync("def", new Result <ExportItem>()).Wait();

            //Assert
            Assert.IsTrue(mock.WaitAndVerify(TimeSpan.FromSeconds(1)));
            Assert.AreEqual(exportResponse["manifest"], exporter.Manifest);
            Assert.AreEqual(new string[] { "abc", "def" }, exporter.DataIds);
            Assert.AreEqual(exporter.Manifest["*[@dataid='abc']"], item1.ItemManifest);
            Assert.AreEqual(exporter.Manifest["*[@dataid='def']"], item2.ItemManifest);
            Assert.AreEqual(item1Doc, XDocFactory.From(new StreamReader(item1.Data), MimeType.TEXT_XML));
            Assert.AreEqual(item2Doc, XDocFactory.From(new StreamReader(item2.Data), MimeType.TEXT_XML));
        }
Ejemplo n.º 3
0
        public void Exporter_hits_export_feature_on_creation_using_reltopath()
        {
            // Arrange
            XUri dekiApiUri     = new XUri("http://mock/@api/deki");
            XDoc exportDocument = new XDoc("export")
                                  .Start("page")
                                  .Attr("path", "/")
                                  .Attr("recursive", "true")
                                  .Attr("exclude", "all")
                                  .End();
            XDoc exportResponse = new XDoc("export")
                                  .Start("requests")
                                  .End()
                                  .Start("manifest")
                                  .Elem("justanode")
                                  .End();
            AutoMockPlug mock = MockPlug.Register(dekiApiUri);

            mock.Expect("POST", dekiApiUri.At("site", "export").With("reltopath", "/foo/bar"), exportDocument, DreamMessage.Ok(exportResponse));

            // Act
            Exporter exporter = Exporter.CreateAsync(Plug.New(dekiApiUri), exportDocument, "/foo/bar", new Result <Exporter>()).Wait();

            //Assert
            Assert.IsTrue(mock.WaitAndVerify(TimeSpan.FromSeconds(1)));
            Assert.AreEqual(exportResponse["manifest"], exporter.Manifest);
        }
        public void Cache_hit_and_expire()
        {
            Plug            deki         = Plug.New("http://mock/deki");
            AutoMockPlug    autoMock     = MockPlug.Register(deki.Uri);
            Action          expire       = null;
            PageChangeCache cache        = new PageChangeCache(deki, (key, trigger) => expire = trigger);
            DateTime        timestamp    = DateTime.UtcNow;
            XUri            pageUri      = deki.Uri.At("pages", "10").With("redirects", "0");
            XDoc            pageResponse = new XDoc("page")
                                           .Elem("uri.ui", "http://foo.com/@api/deki/pages/10")
                                           .Elem("title", "foo")
                                           .Elem("path", "foo/bar");
            XUri feedUri = deki.Uri
                           .At("pages", "10", "feed")
                           .With("redirects", "0")
                           .With("format", "raw")
                           .With("since", timestamp.Subtract(TimeSpan.FromSeconds(10)).ToString("yyyyMMddHHmmss"));
            XDoc changeResponse = new XDoc("table")
                                  .Start("change")
                                  .Elem("rc_summary", "Two edits")
                                  .Elem("rc_comment", "edit 1")
                                  .Elem("rc_comment", "edit 2")
                                  .End();

            _log.Debug("first get");
            autoMock.Expect("GET", pageUri, (XDoc)null, DreamMessage.Ok(pageResponse));
            autoMock.Expect("GET", feedUri, (XDoc)null, DreamMessage.Ok(changeResponse));
            Assert.IsNotNull(Coroutine.Invoke(cache.GetPageData, (uint)10, "foo", timestamp, CultureInfo.InvariantCulture, (string)null, new Result <PageChangeData>()).Wait());
            Assert.IsTrue(autoMock.WaitAndVerify(10.Seconds()));

            _log.Debug("second get, cache hit");
            autoMock.Reset();
            Assert.IsNotNull(Coroutine.Invoke(cache.GetPageData, (uint)10, "foo", timestamp, CultureInfo.InvariantCulture, (string)null, new Result <PageChangeData>()).Wait());
            Assert.IsTrue(autoMock.WaitAndVerify(2.Seconds()));

            _log.Debug("third get, cache miss");
            autoMock.Reset();
            autoMock.Expect("GET", pageUri, (XDoc)null, DreamMessage.Ok(pageResponse));
            autoMock.Expect("GET", feedUri, (XDoc)null, DreamMessage.Ok(changeResponse));
            expire();
            Assert.IsNotNull(Coroutine.Invoke(cache.GetPageData, (uint)10, "foo", timestamp, CultureInfo.InvariantCulture, (string)null, new Result <PageChangeData>()).Wait());
            Assert.IsTrue(autoMock.WaitAndVerify(10.Seconds()));
        }
        public void Get_page_data_in_user_timezone()
        {
            Plug            deki         = Plug.New("http://mock/deki");
            AutoMockPlug    autoMock     = MockPlug.Register(deki.Uri);
            PageChangeCache cache        = new PageChangeCache(deki, (key, trigger) => { });
            DateTime        timestamp    = DateTime.Parse("2009/02/01 10:10:00");
            XUri            pageUri      = deki.Uri.At("pages", "10").With("redirects", "0");
            XDoc            pageResponse = new XDoc("page")
                                           .Elem("uri.ui", "http://foo.com/@api/deki/pages/10")
                                           .Elem("title", "foo")
                                           .Elem("path", "foo/bar");
            XUri feedUri = deki.Uri
                           .At("pages", "10", "feed")
                           .With("redirects", "0")
                           .With("format", "raw")
                           .With("since", timestamp.Subtract(TimeSpan.FromSeconds(10)).ToString("yyyyMMddHHmmss"));
            XDoc changeResponse = new XDoc("table")
                                  .Start("change")
                                  .Elem("rc_summary", "Two edits")
                                  .Elem("rc_comment", "edit 1")
                                  .Elem("rc_comment", "edit 2")
                                  .Elem("rc_timestamp", "20090201101000")
                                  .End();

            _log.Debug("first get");
            autoMock.Expect("GET", pageUri, (XDoc)null, DreamMessage.Ok(pageResponse));
            autoMock.Expect("GET", feedUri, (XDoc)null, DreamMessage.Ok(changeResponse));
            PageChangeData data = Coroutine.Invoke(cache.GetPageData, (uint)10, "foo", timestamp, CultureInfo.InvariantCulture, "-09:00", new Result <PageChangeData>()).Wait();

            Assert.IsTrue(autoMock.WaitAndVerify(TimeSpan.FromSeconds(10)));

            //string plainbody = "foo\r\n[ http://foo.com/@api/deki/pages/10 ]\r\n\r\n - edit 1 (Sun, 01 Feb 2009 01:10:00 -09:00)\r\n   [ http://foo.com/@api/deki/pages/10?revision ]\r\n\r\n";
            XDoc htmlBody = XDocFactory.From("<html><p><b><a href=\"http://foo.com/@api/deki/pages/10\">foo</a></b> ( Last edited by <a href=\"http://foo.com/User%3A\" /> )<br /><small><a href=\"http://foo.com/@api/deki/pages/10\">http://foo.com/@api/deki/pages/10</a></small><br /><small><a href=\"http://foo.com/index.php?title=Special%3APageAlerts&amp;id=10\">Unsubscribe</a></small></p><p><ol><li>edit 1 ( <a href=\"http://foo.com/@api/deki/pages/10?revision\">Sun, 01 Feb 2009 01:10:00 -09:00</a> by <a href=\"http://foo.com/User%3A\" /> )</li></ol></p><br /></html>", MimeType.TEXT_XML);

            Assert.AreEqual(htmlBody.ToString(), data.HtmlBody.ToString());
        }
Ejemplo n.º 6
0
        public void Importer_hits_import_feature_with_reltopath()
        {
            // Arrange
            XUri dekiApiUri     = new XUri("http://mock/@api/deki");
            XDoc importManifest = new XDoc("manifest");
            XDoc importResponse = new XDoc("requests")
                                  .Start("request").Attr("dataid", "a").End()
                                  .Start("request").Attr("dataid", "b").End()
                                  .Start("request").Attr("dataid", "c").End();
            AutoMockPlug mock = MockPlug.Register(dekiApiUri);

            mock.Expect("POST", dekiApiUri.At("site", "import").With("reltopath", "/foo/bar"), importManifest, DreamMessage.Ok(importResponse));

            // Act
            Importer importer = Importer.CreateAsync(Plug.New(dekiApiUri), importManifest, "/foo/bar", new Result <Importer>()).Wait();

            //Assert
            Assert.IsTrue(mock.WaitAndVerify(TimeSpan.FromSeconds(1)));
            Assert.AreEqual(importManifest, importer.Manifest);
            Assert.AreEqual(new[] { "a", "b", "c" }, importer.Items.Select(x => x.DataId).ToArray());
        }