context: /* --------------------------------------------------------------------------
   * Copyrights
   *
   * Portions created by or assigned to Cursive Systems, Inc. are
   * Copyright (c) 2002-2008 Cursive Systems, Inc.  All Rights Reserved.  Contact
   * information for Cursive Systems, Inc. is available at
   * http://www.cursive.net/.
   *
   * License
   *
   * Jabber-Net is licensed under the LGPL.
   * See LICENSE.txt for details.
   * --------------------------------------------------------------------------*/
  using System.Xml;
  using Jabber;
  using Jabber.Connection;
  using Jabber.Protocol;
  using Jabber.Protocol.Client;
  using Jabber.Protocol.IQ;
  using Microsoft.VisualStudio.TestTools.UnitTesting;
  using Rhino.Mocks;
  using Rhino.Mocks.Interfaces;
  
  namespace Tests.Jabber.Connection
  {
      [TestClass]
      public class DiscoManagerTest
      {
          private MockRepository mocks;
          private DiscoManager dm;
          private XmppStream stream;
          private IIQTracker tracker;
          private XmlDocument doc;
  
          [TestInitialize]
          public void setup()
          {
              mocks = new MockRepository();
              dm = new DiscoManager();
  
              stream = mocks.DynamicMock<XmppStream>();
              tracker = mocks.DynamicMock<IIQTracker>();
              dm.Stream = stream;
  
              doc = new XmlDocument();
          }
  
          [TestCleanup]
          public void cleanUp()
          {
              dm.Clear();
          }
  
          private readonly JID jid = new JID("test.com");
          private readonly string NODE = "TEST_NODE";
          private readonly string FEATURE = "TEST_FEATURE";
  
          [TestMethod]
          public void IntialDiscoTest()
          {
              IEventRaiser onAuth;
  
              IQ sentIq = null;
              IqCB sentCallback = null;
  
              mocks.BackToRecordAll();
              using (mocks.Record())
              {
                  Expect.Call(stream.Server).Return(jid);
                  stream.OnAuthenticate += null;
                  onAuth = LastCall.IgnoreArguments().GetEventRaiser();
  
                  Expect.Call(stream.Document).Return(doc);
                  SetupTrackerBeginIq(
                      delegate(IQ arg0, IqCB arg1, object arg2)
                      {
                          // Grab the iq and callback so this part of
                          // the code can finish. Call the callback later.
                          sentIq = arg0;
                          sentCallback = arg1;
  
                          string id = arg0.GetAttribute("id");
                          string original = arg0.OuterXml.Replace(" ", "");
                          string comparison = GetInfoXml(id).Replace(" ", "");
                          return original == comparison;
                      });
  
                  Expect.Call(stream.Document).Return(doc);
                  SetupTrackerBeginIq(
                      delegate(IQ arg0, IqCB arg1, object arg2)
                      {
                          string id = arg0.GetAttribute("id");
                          string original = arg0.OuterXml;
                          return original.Replace(" ", "") ==
                                 GetItemsForServiceXml(id).Replace(" ", "");
                      });
              }
  
              using (mocks.Playback())
              {
                  DiscoManager newDm = new DiscoManager();
                  newDm.Stream = stream;
  
                  onAuth.Raise(new object[] { null });
  
                  if (sentIq != null)
                  {
                      string id = sentIq.GetAttribute("id");
                      if (sentCallback != null)
                          sentCallback(null, CreateDiscoInfoResponse(id), newDm.Root);
                  }
              }
          }
  
          [TestMethod]
          public void BeginGetItemsTest()
          {
              mocks.BackToRecordAll();
              using (mocks.Record())
              {
                  Expect.Call(stream.Document).Return(doc);
                  SetupTrackerBeginIq(
                      delegate(IQ arg0, IqCB arg1, object arg2)
                      {
                          string id = arg0.GetAttribute("id");
                          string original = arg0.OuterXml.Replace(" ", "");
                          string comparison = GetItemsXml(id).Replace(" ", "");
                          return original == comparison;
                      });
              }
  
              using (mocks.Playback())
              {
                  dm.BeginGetItems(jid, NODE, delegate { }, null);
              }
          }
  
          [TestMethod]
          public void BeginGetFeaturesTest()
          {
              mocks.BackToRecordAll();
              using (mocks.Record())
              {
                  Expect.Call(stream.Document).Return(doc);
                  SetupTrackerBeginIq(
                      delegate(IQ arg0, IqCB arg1, object arg2)
                      {
                          string id = arg0.GetAttribute("id");
                          string original = arg0.OuterXml.Replace(" ", "");
                          string comparison = GetFeaturesXml(id).Replace(" ", "");
                          return original == comparison;
                      });
              }
  
              using (mocks.Playback())
              {
                  dm.BeginGetFeatures(jid, NODE, delegate { }, null);
              }
          }
  
          [TestMethod]
          public void BeginGetServiceTest()
          {
              mocks.BackToRecordAll();
              using (mocks.Record())
              {
                  Expect.Call(stream.Server).Return(jid);
                  Expect.Call(stream.Document).Return(doc);
                  SetupTrackerBeginIq(
                      delegate(IQ arg0, IqCB arg1, object arg2)
                      {
                          string id = arg0.GetAttribute("id");
                          string original = arg0.OuterXml.Replace(" ", "");
                          string comparison = GetItemsForServiceXml(id).Replace(" ", "");
                          return original == comparison;
                      });
              }
  
              using (mocks.Playback())
              {
                  dm.BeginFindServiceWithFeature(FEATURE, delegate { }, null);
              }
          }
  
          private IQ CreateDiscoInfoResponse(string id)
          {
              IQ returnIq = new IQ(doc);
              returnIq.SetAttribute("id", id);
              returnIq.SetAttribute("from", jid);
              returnIq.SetAttribute("type", "result");
  
              DiscoInfo info = new DiscoInfo(doc);
              info.AddIdentity("server", "im", "jabber2 4.2.16.6", null);
              info.AddFeature(URI.DISCO_ITEMS);
              info.AddFeature(URI.DISCO_INFO);
  
              returnIq.Query = info;
  
              return returnIq;
          }
  
          private string GetInfoXml(string id)
          {
              return
                  string.Format(
                      "<iq id=\"{0}\" type=\"get\" to=\"{1}\">" +
                        "<query xmlns=\"{2}\"/>" +
                      "</iq>",
                      id, jid, URI.DISCO_INFO);
          }
  
          private string GetItemsForServiceXml(string id)
          {
              return
                  string.Format(
                      "<iq id=\"{0}\" type=\"get\" to=\"{1}\">" +
                        "<query xmlns=\"{2}\"/>" +
                      "</iq>",
                      id, jid, URI.DISCO_ITEMS);
          }
  
          private string GetFeaturesXml(string id)
          {
              return
                  string.Format(
  
gt:                     "<iq id=\"{0}\" type=\"get\" to=\"{1}\">" +