Author: ngn
Date: Tue Jul 27 07:08:33 2010
New Revision: 979560

URL: http://svn.apache.org/viewvc?rev=979560&view=rev
Log:
Finished BoshHandlerTest (VYSPER-220, by Bogdan Pistol)

Modified:
    
mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/test/java/org/apache/vysper/xmpp/extension/xep0124/BoshHandlerTest.java

Modified: 
mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/test/java/org/apache/vysper/xmpp/extension/xep0124/BoshHandlerTest.java
URL: 
http://svn.apache.org/viewvc/mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/test/java/org/apache/vysper/xmpp/extension/xep0124/BoshHandlerTest.java?rev=979560&r1=979559&r2=979560&view=diff
==============================================================================
--- 
mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/test/java/org/apache/vysper/xmpp/extension/xep0124/BoshHandlerTest.java
 (original)
+++ 
mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/test/java/org/apache/vysper/xmpp/extension/xep0124/BoshHandlerTest.java
 Tue Jul 27 07:08:33 2010
@@ -36,8 +36,11 @@ import org.apache.vysper.xml.fragment.XM
 import org.apache.vysper.xmpp.addressing.EntityImpl;
 import org.apache.vysper.xmpp.authorization.SASLMechanism;
 import org.apache.vysper.xmpp.protocol.NamespaceURIs;
+import org.apache.vysper.xmpp.protocol.SessionStateHolder;
+import org.apache.vysper.xmpp.protocol.StanzaProcessor;
 import org.apache.vysper.xmpp.server.ServerFeatures;
 import org.apache.vysper.xmpp.server.ServerRuntimeContext;
+import org.apache.vysper.xmpp.server.SessionContext;
 import org.apache.vysper.xmpp.stanza.Stanza;
 import org.apache.vysper.xmpp.stanza.StanzaBuilder;
 import org.easymock.Capture;
@@ -47,16 +50,15 @@ import org.eclipse.jetty.continuation.Co
 import org.eclipse.jetty.continuation.ContinuationListener;
 import org.junit.After;
 import org.junit.Before;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.xml.sax.SAXException;
 
 public class BoshHandlerTest {
-    
+
     private IMocksControl mocksControl;
-    
+
     private ServerRuntimeContext serverRuntimeContext;
-    
+
     private BoshHandler boshHandler;
 
     @Before
@@ -72,7 +74,8 @@ public class BoshHandlerTest {
     }
 
     @Test
-    public void testProcessSessionCreation() throws IOException, SAXException {
+    public void testProcess() throws IOException, SAXException {
+        // test session creation
         HttpServletRequest httpServletRequest = 
mocksControl.createMock(HttpServletRequest.class);
         expect(serverRuntimeContext.getNextSessionId()).andReturn("200");
         expect(serverRuntimeContext.getServerEnitity()).andReturn(new 
EntityImpl(null, "vysper.org", null));
@@ -84,20 +87,20 @@ public class BoshHandlerTest {
         continuation.setAttribute("request", httpServletRequest);
         continuation.addContinuationListener(EasyMock.<ContinuationListener> 
anyObject());
         continuation.suspend();
-        
+
         ServerFeatures serverFeatures = 
mocksControl.createMock(ServerFeatures.class);
         
expect(serverRuntimeContext.getServerFeatures()).andReturn(serverFeatures);
         
expect(serverFeatures.getAuthenticationMethods()).andReturn(Collections.<SASLMechanism>
 emptyList());
-        
+
         Capture<BoshResponse> captured = new Capture<BoshResponse>();
         continuation.setAttribute(eq("response"), EasyMock.<BoshResponse> 
capture(captured));
         continuation.resume();
         mocksControl.replay();
-        
+
         Stanza boshRequest = createSessionRequest();
         boshHandler.process(httpServletRequest, boshRequest);
         mocksControl.verify();
-        
+
         Stanza response = new XMLUtil(new 
String(captured.getValue().getContent())).parse();
         assertNotNull(response);
         assertEquals("body", response.getName());
@@ -115,23 +118,69 @@ public class BoshHandlerTest {
         XMLElement saslMechanisms = streamFeatures.getInnerElements().get(0);
         assertEquals("mechanisms", saslMechanisms.getName());
         assertEquals(NamespaceURIs.URN_IETF_PARAMS_XML_NS_XMPP_SASL, 
saslMechanisms.getNamespaceURI());
+
+        // test session retrieval, retrieves the session above identified by 
sid=200
+        mocksControl.reset();
+        
expect(httpServletRequest.getAttribute(Continuation.ATTRIBUTE)).andReturn(continuation);
+        expectLastCall().atLeastOnce();
+        continuation.setTimeout(anyLong());
+        continuation.suspend();
+        continuation.setAttribute("request", httpServletRequest);
+        continuation.addContinuationListener(EasyMock.<ContinuationListener> 
anyObject());
+        StanzaProcessor stanzaProcessor = 
mocksControl.createMock(StanzaProcessor.class);
+        
expect(serverRuntimeContext.getStanzaProcessor()).andReturn(stanzaProcessor);
+        Capture<Stanza> stanzaCaptured = new Capture<Stanza>();
+        stanzaProcessor.processStanza(eq(serverRuntimeContext), 
EasyMock.<SessionContext> anyObject(),
+                EasyMock.<Stanza> capture(stanzaCaptured), 
EasyMock.<SessionStateHolder> anyObject());
+        mocksControl.replay();
+        boshRequest = createSaslRequest();
+        boshHandler.process(httpServletRequest, boshRequest);
+        mocksControl.verify();
+        Stanza stanza = stanzaCaptured.getValue();
+        assertNotNull(stanza);
+        assertEquals("auth", stanza.getName());
+        assertEquals(NamespaceURIs.URN_IETF_PARAMS_XML_NS_XMPP_SASL, 
stanza.getNamespaceURI());
+        assertEquals("DIGEST-MD5", stanza.getAttributeValue("mechanism"));
     }
-    
+
     @Test
-    @Ignore("Not yet implemented")
-    public void testGetSessionCreationStanza() {
+    public void testGetEmptyResponse() {
+        Stanza response = boshHandler.getEmptyResponse();
+        assertNotNull(response);
+        assertEquals("body", response.getName());
+        assertEquals(NamespaceURIs.XEP0124_BOSH, response.getNamespaceURI());
+        assertEquals(0, response.getAttributes().size());
+        assertEquals(0, response.getInnerElements().size());
     }
 
     @Test
-    @Ignore("Not yet implemented")
     public void testWrapStanza() {
+        StanzaBuilder stanzaBuilder = new StanzaBuilder("iq", 
NamespaceURIs.JABBER_CLIENT);
+        Stanza stanza = stanzaBuilder.build();
+        Stanza body = boshHandler.wrapStanza(stanza);
+        assertNotNull(body);
+        assertEquals("body", body.getName());
+        assertEquals(NamespaceURIs.XEP0124_BOSH, body.getNamespaceURI());
+        assertEquals(1, body.getInnerElements().size());
+        assertEquals("iq", body.getInnerElements().get(0).getName());
+        assertEquals(NamespaceURIs.JABBER_CLIENT, 
body.getInnerElements().get(0).getNamespaceURI());
     }
 
     @Test
-    @Ignore("Not yet implemented")
-    public void testMergeStanzas() {
+    public void testMergeResponses() {
+        Stanza response1 = createPingStanzaResponse("vysper.org", 
"[email protected]/resource", "100");
+        Stanza response2 = createPingStanzaResponse("vysper.org", 
"[email protected]/resource", "101");
+        assertEquals(response1, boshHandler.mergeResponses(response1, null));
+        assertEquals(response1, boshHandler.mergeResponses(null, response1));
+        Stanza merged = boshHandler.mergeResponses(response1, response2);
+        assertNotNull(merged);
+        assertEquals("body", merged.getName());
+        assertEquals(NamespaceURIs.XEP0124_BOSH, merged.getNamespaceURI());
+        assertEquals(2, merged.getInnerElements().size());
+        assertEquals(response1, merged.getInnerElements().get(0));
+        assertEquals(response2, merged.getInnerElements().get(0));
     }
-    
+
     private Stanza createSessionRequest() {
         StanzaBuilder body = new StanzaBuilder("body", 
NamespaceURIs.XEP0124_BOSH);
         body.addAttribute("rid", "100");
@@ -144,22 +193,21 @@ public class BoshHandlerTest {
         return body.build();
     }
 
-    private Stanza createPingRequestStanza(String to, String id) {
+    private Stanza createSaslRequest() {
         StanzaBuilder body = new StanzaBuilder("body", 
NamespaceURIs.XEP0124_BOSH);
-        StanzaBuilder iq = body.startInnerElement("iq", 
NamespaceURIs.JABBER_CLIENT).addAttribute("to", to)
-                .addAttribute("type", "get").addAttribute("id", id);
-        iq.startInnerElement("ping", 
NamespaceURIs.URN_XMPP_PING).endInnerElement();
-        iq.endInnerElement();
-        body.endInnerElement();
+        body.addAttribute("rid", "101");
+        body.addAttribute("sid", "200");
+        body.startInnerElement("auth", 
NamespaceURIs.URN_IETF_PARAMS_XML_NS_XMPP_SASL)
+                .addAttribute("mechanism", "DIGEST-MD5").endInnerElement();
         return body.build();
     }
 
-    private static Stanza createPingResponseStanza(String from, String to, 
String id) {
+    private static Stanza createPingStanzaResponse(String from, String to, 
String id) {
         StanzaBuilder body = new StanzaBuilder("body", 
NamespaceURIs.XEP0124_BOSH);
         body.startInnerElement("iq", 
NamespaceURIs.JABBER_CLIENT).addAttribute("from", from)
                 .addAttribute("type", "result").addAttribute("to", 
to).addAttribute("id", id);
         body.endInnerElement();
         return body.build();
     }
-    
+
 }
\ No newline at end of file


Reply via email to