summaryrefslogtreecommitdiffstats
path: root/test/com
diff options
context:
space:
mode:
Diffstat (limited to 'test/com')
-rw-r--r--test/com/isode/stroke/queries/IQRouterTest.java186
-rw-r--r--test/com/isode/stroke/queries/RequestTest.java447
-rw-r--r--test/com/isode/stroke/queries/ResponderTest.java160
3 files changed, 793 insertions, 0 deletions
diff --git a/test/com/isode/stroke/queries/IQRouterTest.java b/test/com/isode/stroke/queries/IQRouterTest.java
new file mode 100644
index 0000000..6e25891
--- /dev/null
+++ b/test/com/isode/stroke/queries/IQRouterTest.java
@@ -0,0 +1,186 @@
+/*
+ * Copyright (c) 2010 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+/*
+ * Copyright (c) 2015 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+package com.isode.stroke.queries;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import org.junit.Test;
+import org.junit.Before;
+import com.isode.stroke.queries.IQHandler;
+import com.isode.stroke.queries.IQRouter;
+import com.isode.stroke.queries.DummyIQChannel;
+import com.isode.stroke.jid.JID;
+import com.isode.stroke.elements.IQ;
+import com.isode.stroke.elements.ErrorPayload;
+
+public class IQRouterTest {
+
+ private DummyIQChannel channel_;
+
+ private class DummyIQHandler implements IQHandler {
+
+ public DummyIQHandler(boolean handle, IQRouter router) {
+ this.handle = handle;
+ this.router = router;
+ this.called = 0;
+ router.addHandler(this);
+ }
+
+ public void delete() {
+ router.removeHandler(this);
+ }
+
+ @Override
+ public boolean handleIQ(IQ iq) {
+ called++;
+ return handle;
+ }
+
+ public boolean handle;
+ public IQRouter router;
+ public int called;
+ }
+
+ private class RemovingIQHandler implements IQHandler {
+
+ public RemovingIQHandler(IQRouter router) {
+ this.router = router;
+ this.called = 0;
+ router.addHandler(this);
+ }
+
+ @Override
+ public boolean handleIQ(IQ iq) {
+ called++;
+ router.removeHandler(this);
+ return false;
+ }
+
+ public IQRouter router;
+ public int called;
+ }
+
+ @Before
+ public void setUp() {
+ channel_ = new DummyIQChannel();
+ }
+
+ @Test
+ public void testRemoveHandler() {
+ IQRouter testling = new IQRouter(channel_);
+ DummyIQHandler handler1 = new DummyIQHandler(true, testling);
+ DummyIQHandler handler2 = new DummyIQHandler(true, testling);
+ testling.removeHandler(handler1);
+
+ channel_.onIQReceived.emit(new IQ());
+
+ assertEquals(0, handler1.called);
+ assertEquals(1, handler2.called);
+ }
+
+ @Test
+ public void testRemoveHandler_AfterHandleIQ() {
+ IQRouter testling = new IQRouter(channel_);
+ DummyIQHandler handler2 = new DummyIQHandler(true, testling);
+ DummyIQHandler handler1 = new DummyIQHandler(true, testling);
+
+ channel_.onIQReceived.emit(new IQ());
+ testling.removeHandler(handler1);
+ channel_.onIQReceived.emit(new IQ());
+
+ assertEquals(1, handler1.called);
+ assertEquals(1, handler2.called);
+ }
+
+ @Test
+ public void testHandleIQ_SuccesfulHandlerFirst() {
+ IQRouter testling = new IQRouter(channel_);
+ DummyIQHandler handler2 = new DummyIQHandler(false, testling);
+ DummyIQHandler handler1 = new DummyIQHandler(true, testling);
+
+ channel_.onIQReceived.emit(new IQ());
+
+ assertEquals(1, handler1.called);
+ assertEquals(0, handler2.called);
+ assertEquals(0, channel_.iqs_.size());
+ }
+
+ @Test
+ public void testHandleIQ_SuccesfulHandlerLast() {
+ IQRouter testling = new IQRouter(channel_);
+ DummyIQHandler handler2 = new DummyIQHandler(true, testling);
+ DummyIQHandler handler1 = new DummyIQHandler(false, testling);
+
+ channel_.onIQReceived.emit(new IQ());
+
+ assertEquals(1, handler1.called);
+ assertEquals(1, handler2.called);
+ assertEquals(0, channel_.iqs_.size());
+ }
+
+ @Test
+ public void testHandleIQ_NoSuccesfulHandler() {
+ IQRouter testling = new IQRouter(channel_);
+ DummyIQHandler handler = new DummyIQHandler(false, testling);
+
+ channel_.onIQReceived.emit(new IQ());
+
+ assertEquals(1, channel_.iqs_.size());
+ assertNotNull(channel_.iqs_.get(0).getPayload(new ErrorPayload()));
+ }
+
+ @Test
+ public void testHandleIQ_HandlerRemovedDuringHandle() {
+ IQRouter testling = new IQRouter(channel_);
+ DummyIQHandler handler2 = new DummyIQHandler(true, testling);
+ RemovingIQHandler handler1 = new RemovingIQHandler(testling);
+
+ channel_.onIQReceived.emit(new IQ());
+ channel_.onIQReceived.emit(new IQ());
+
+ assertEquals(1, handler1.called);
+ assertEquals(2, handler2.called);
+ }
+
+ @Test
+ public void testSendIQ_WithFrom() {
+ IQRouter testling = new IQRouter(channel_);
+ testling.setFrom(new JID("foo@bar.com/baz"));
+
+ testling.sendIQ(new IQ());
+
+ assertEquals(new JID("foo@bar.com/baz"), channel_.iqs_.get(0).getFrom());
+ }
+
+ @Test
+ public void testSendIQ_WithoutFrom() {
+ IQRouter testling = new IQRouter(channel_);
+
+ testling.sendIQ(new IQ());
+
+ assertEquals(new JID(), channel_.iqs_.get(0).getFrom());
+ }
+
+ @Test
+ public void testHandleIQ_WithFrom() {
+ IQRouter testling = new IQRouter(channel_);
+ testling.setFrom(new JID("foo@bar.com/baz"));
+ DummyIQHandler handler = new DummyIQHandler(false, testling);
+
+ channel_.onIQReceived.emit(new IQ());
+
+ assertEquals(new JID("foo@bar.com/baz"), channel_.iqs_.get(0).getFrom());
+ }
+} \ No newline at end of file
diff --git a/test/com/isode/stroke/queries/RequestTest.java b/test/com/isode/stroke/queries/RequestTest.java
new file mode 100644
index 0000000..9c89414
--- /dev/null
+++ b/test/com/isode/stroke/queries/RequestTest.java
@@ -0,0 +1,447 @@
+/*
+ * Copyright (c) 2010 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+/*
+ * Copyright (c) 2015 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+package com.isode.stroke.queries;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import org.junit.Test;
+import org.junit.Before;
+import com.isode.stroke.queries.GenericRequest;
+import com.isode.stroke.queries.IQRouter;
+import com.isode.stroke.queries.DummyIQChannel;
+import com.isode.stroke.jid.JID;
+import com.isode.stroke.elements.IQ;
+import com.isode.stroke.elements.ErrorPayload;
+import com.isode.stroke.elements.Payload;
+import com.isode.stroke.elements.RawXMLPayload;
+import com.isode.stroke.signals.Signal2;
+import com.isode.stroke.signals.Slot2;
+import java.util.Vector;
+
+public class RequestTest {
+
+ private IQRouter router_;
+ private DummyIQChannel channel_;
+ private Payload payload_;
+ private Payload responsePayload_;
+ private int responsesReceived_;
+ private Vector<ErrorPayload> receivedErrors = new Vector<ErrorPayload>();
+
+ public class MyPayload extends Payload {
+
+ public MyPayload() {
+ this("");
+ }
+
+ public MyPayload(final String s) {
+ this.text_ = s;
+ }
+
+ public String text_ = "";
+ }
+
+ public class MyOtherPayload extends Payload {
+
+ }
+
+ public class MyRequest extends Request {
+
+ public MyRequest(IQ.Type type, final JID receiver, Payload payload, IQRouter router) {
+ super(type, receiver, payload, router);
+ }
+
+ public void handleResponse(Payload payload, ErrorPayload error) {
+ onResponse.emit(payload, error);
+ }
+
+ public final Signal2<Payload, ErrorPayload> onResponse = new Signal2<Payload, ErrorPayload>();
+ }
+
+ private void handleResponse(Payload p, ErrorPayload e) {
+ if (e != null) {
+ receivedErrors.add(e);
+ }
+ else {
+ MyPayload payload = (MyPayload)p;
+ assertNotNull(payload);
+ assertEquals("bar", payload.text_);
+ ++responsesReceived_;
+ }
+ }
+
+ private void handleDifferentResponse(Payload p, ErrorPayload e) {
+ assertNull(e);
+ assertNull(p);
+ ++responsesReceived_;
+ }
+
+ private void handleRawXMLResponse(Payload p, ErrorPayload e) {
+ assertNull(e);
+ assertNotNull(p);
+ assertNotNull((MyOtherPayload)p);
+ ++responsesReceived_;
+ }
+
+ private IQ createResponse(final JID from, final String id) {
+ IQ iq = new IQ(IQ.Type.Result);
+ iq.setFrom(from);
+ iq.addPayload(responsePayload_);
+ iq.setID(id);
+ return iq;
+ }
+
+ private IQ createError(final JID from, final String id) {
+ IQ iq = new IQ(IQ.Type.Error);
+ iq.setFrom(from);
+ iq.setID(id);
+ return iq;
+ }
+
+ @Before
+ public void setUp() {
+ channel_ = new DummyIQChannel();
+ router_ = new IQRouter(channel_);
+ payload_ = (Payload)(new MyPayload("foo"));
+ responsePayload_ = (Payload)(new MyPayload("bar"));
+ responsesReceived_ = 0;
+ }
+
+ @Test
+ public void testSendSet() {
+ MyRequest testling = new MyRequest(IQ.Type.Set, new JID("foo@bar.com/baz"), payload_, router_);
+ testling.send();
+
+ assertEquals(1, (channel_.iqs_.size()));
+ assertEquals(new JID("foo@bar.com/baz"), channel_.iqs_.get(0).getTo());
+ assertEquals(IQ.Type.Set, channel_.iqs_.get(0).getType());
+ assertEquals(("test-id"), channel_.iqs_.get(0).getID());
+ }
+
+ @Test
+ public void testSendGet() {
+ MyRequest testling = new MyRequest(IQ.Type.Get, new JID("foo@bar.com/baz"), payload_, router_);
+ testling.send();
+
+ assertEquals(1, (channel_.iqs_.size()));
+ assertEquals(IQ.Type.Get, channel_.iqs_.get(0).getType());
+ }
+
+ @Test
+ public void testHandleIQ() {
+ MyRequest testling = new MyRequest(IQ.Type.Get, new JID("foo@bar.com/baz"), payload_, router_);
+ testling.onResponse.connect(new Slot2<Payload, ErrorPayload>() {
+ @Override
+ public void call(Payload p, ErrorPayload e) {
+ handleResponse(p, e);
+ }
+ });
+ testling.send();
+
+ channel_.onIQReceived.emit(createResponse(new JID("foo@bar.com/baz"),"test-id"));
+
+ assertEquals(1, responsesReceived_);
+ assertEquals(0, (receivedErrors.size()));
+ assertEquals(1, (channel_.iqs_.size()));
+ }
+
+ // FIXME: Doesn't test that it didn't handle the payload
+ @Test
+ public void testHandleIQ_InvalidID() {
+ MyRequest testling = new MyRequest(IQ.Type.Get, new JID("foo@bar.com/baz"), payload_, router_);
+ testling.onResponse.connect(new Slot2<Payload, ErrorPayload>() {
+ @Override
+ public void call(Payload p, ErrorPayload e) {
+ handleResponse(p, e);
+ }
+ });
+ testling.send();
+
+ channel_.onIQReceived.emit(createResponse(new JID("foo@bar.com/baz"),"different-id"));
+
+ assertEquals(0, responsesReceived_);
+ assertEquals(0, (receivedErrors.size()));
+ assertEquals(1, (channel_.iqs_.size()));
+ }
+
+ @Test
+ public void testHandleIQ_Error() {
+ MyRequest testling = new MyRequest(IQ.Type.Get, new JID("foo@bar.com/baz"), payload_, router_);
+ testling.onResponse.connect(new Slot2<Payload, ErrorPayload>() {
+ @Override
+ public void call(Payload p, ErrorPayload e) {
+ handleResponse(p, e);
+ }
+ });
+ testling.send();
+
+ IQ error = createError(new JID("foo@bar.com/baz"),"test-id");
+ Payload errorPayload = new ErrorPayload(ErrorPayload.Condition.InternalServerError);
+ error.addPayload(errorPayload);
+ channel_.onIQReceived.emit(error);
+
+ assertEquals(0, responsesReceived_);
+ assertEquals(1, (receivedErrors.size()));
+ assertEquals(1, (channel_.iqs_.size()));
+ assertEquals(ErrorPayload.Condition.InternalServerError, receivedErrors.get(0).getCondition());
+ }
+
+ @Test
+ public void testHandleIQ_ErrorWithoutPayload() {
+ MyRequest testling = new MyRequest(IQ.Type.Get, new JID("foo@bar.com/baz"), payload_, router_);
+ testling.onResponse.connect(new Slot2<Payload, ErrorPayload>() {
+ @Override
+ public void call(Payload p, ErrorPayload e) {
+ handleResponse(p, e);
+ }
+ });
+ testling.send();
+
+ channel_.onIQReceived.emit(createError(new JID("foo@bar.com/baz"),"test-id"));
+
+ assertEquals(0, responsesReceived_);
+ assertEquals(1, (receivedErrors.size()));
+ assertEquals(1, (channel_.iqs_.size()));
+ assertEquals(ErrorPayload.Condition.UndefinedCondition, receivedErrors.get(0).getCondition());
+ }
+
+ @Test
+ public void testHandleIQ_BeforeSend() {
+ MyRequest testling = new MyRequest(IQ.Type.Get, new JID("foo@bar.com/baz"), payload_, router_);
+ testling.onResponse.connect(new Slot2<Payload, ErrorPayload>() {
+ @Override
+ public void call(Payload p, ErrorPayload e) {
+ handleResponse(p, e);
+ }
+ });
+ channel_.onIQReceived.emit(createResponse(new JID("foo@bar.com/baz"),"test-id"));
+
+ assertEquals(0, responsesReceived_);
+ assertEquals(0, (receivedErrors.size()));
+ assertEquals(0, (channel_.iqs_.size()));
+ }
+
+ @Test
+ public void testHandleIQ_DifferentPayload() {
+ MyRequest testling = new MyRequest(IQ.Type.Get, new JID("foo@bar.com/baz"), payload_, router_);
+ testling.onResponse.connect(new Slot2<Payload, ErrorPayload>() {
+ @Override
+ public void call(Payload p, ErrorPayload e) {
+ handleDifferentResponse(p, e);
+ }
+ });
+ testling.send();
+
+ responsePayload_ = new MyOtherPayload();
+ channel_.onIQReceived.emit(createResponse(new JID("foo@bar.com/baz"),"test-id"));
+
+ assertEquals(1, responsesReceived_);
+ assertEquals(0, (receivedErrors.size()));
+ assertEquals(1, (channel_.iqs_.size()));
+ }
+
+ @Test
+ public void testHandleIQ_RawXMLPayload() {
+ payload_ = new RawXMLPayload("<bla/>");
+ MyRequest testling = new MyRequest(IQ.Type.Get, new JID("foo@bar.com/baz"), payload_, router_);
+ testling.onResponse.connect(new Slot2<Payload, ErrorPayload>() {
+ @Override
+ public void call(Payload p, ErrorPayload e) {
+ handleRawXMLResponse(p, e);
+ }
+ });
+ testling.send();
+
+ responsePayload_ = new MyOtherPayload();
+ channel_.onIQReceived.emit(createResponse(new JID("foo@bar.com/baz"),"test-id"));
+
+ assertEquals(1, responsesReceived_);
+ assertEquals(0, (receivedErrors.size()));
+ assertEquals(1, (channel_.iqs_.size()));
+ }
+
+ @Test
+ public void testHandleIQ_GetWithSameID() {
+ MyRequest testling = new MyRequest(IQ.Type.Get, new JID("foo@bar.com/baz"), payload_, router_);
+ testling.onResponse.connect(new Slot2<Payload, ErrorPayload>() {
+ @Override
+ public void call(Payload p, ErrorPayload e) {
+ handleResponse(p, e);
+ }
+ });
+ testling.send();
+
+ IQ response = createResponse(new JID("foo@bar.com/baz"),"test-id");
+ response.setType(IQ.Type.Get);
+ channel_.onIQReceived.emit(response);
+
+ assertEquals(0, responsesReceived_);
+ assertEquals(0, (receivedErrors.size()));
+ assertEquals(2, (channel_.iqs_.size()));
+ }
+
+ @Test
+ public void testHandleIQ_SetWithSameID() {
+ MyRequest testling = new MyRequest(IQ.Type.Get, new JID("foo@bar.com/baz"), payload_, router_);
+ testling.onResponse.connect(new Slot2<Payload, ErrorPayload>() {
+ @Override
+ public void call(Payload p, ErrorPayload e) {
+ handleResponse(p, e);
+ }
+ });
+ testling.send();
+
+ IQ response = createResponse(new JID("foo@bar.com/baz"), "test-id");
+ response.setType(IQ.Type.Set);
+ channel_.onIQReceived.emit(response);
+
+ assertEquals(0, responsesReceived_);
+ assertEquals(0, (receivedErrors.size()));
+ assertEquals(2, (channel_.iqs_.size()));
+ }
+
+ @Test
+ public void testHandleIQ_IncorrectSender() {
+ MyRequest testling = new MyRequest(IQ.Type.Get, new JID("foo@bar.com/baz"), payload_, router_);
+ router_.setJID(new JID("alice@wonderland.lit/TeaParty"));
+ testling.onResponse.connect(new Slot2<Payload, ErrorPayload>() {
+ @Override
+ public void call(Payload p, ErrorPayload e) {
+ handleResponse(p, e);
+ }
+ });
+ testling.send();
+
+ channel_.onIQReceived.emit(createResponse(new JID("anotherfoo@bar.com/baz"), "test-id"));
+
+ assertEquals(0, responsesReceived_);
+ assertEquals(0, (receivedErrors.size()));
+ assertEquals(1, (channel_.iqs_.size()));
+ }
+
+ @Test
+ public void testHandleIQ_IncorrectSenderForServerQuery() {
+ MyRequest testling = new MyRequest(IQ.Type.Get, new JID(), payload_, router_);
+ router_.setJID(new JID("alice@wonderland.lit/TeaParty"));
+ testling.onResponse.connect(new Slot2<Payload, ErrorPayload>() {
+ @Override
+ public void call(Payload p, ErrorPayload e) {
+ handleResponse(p, e);
+ }
+ });
+ testling.send();
+
+ channel_.onIQReceived.emit(createResponse(new JID("foo@bar.com/baz"), "test-id"));
+
+ assertEquals(0, responsesReceived_);
+ assertEquals(0, (receivedErrors.size()));
+ assertEquals(1, (channel_.iqs_.size()));
+ }
+
+ @Test
+ public void testHandleIQ_IncorrectOtherResourceSenderForServerQuery() {
+ MyRequest testling = new MyRequest(IQ.Type.Get, new JID(), payload_, router_);
+ router_.setJID(new JID("alice@wonderland.lit/TeaParty"));
+ testling.onResponse.connect(new Slot2<Payload, ErrorPayload>() {
+ @Override
+ public void call(Payload p, ErrorPayload e) {
+ handleResponse(p, e);
+ }
+ });
+ testling.send();
+
+ channel_.onIQReceived.emit(createResponse(new JID("alice@wonderland.lit/RabbitHole"), "test-id"));
+
+ assertEquals(0, responsesReceived_);
+ assertEquals(0, (receivedErrors.size()));
+ assertEquals(1, (channel_.iqs_.size()));
+ }
+
+ @Test
+ public void testHandleIQ_ServerRespondsWithDomain() {
+ MyRequest testling = new MyRequest(IQ.Type.Get, new JID(), payload_, router_);
+ router_.setJID(new JID("alice@wonderland.lit/TeaParty"));
+ testling.onResponse.connect(new Slot2<Payload, ErrorPayload>() {
+ @Override
+ public void call(Payload p, ErrorPayload e) {
+ handleResponse(p, e);
+ }
+ });
+ testling.send();
+
+ channel_.onIQReceived.emit(createResponse(new JID("wonderland.lit"),"test-id"));
+
+ assertEquals(0, responsesReceived_);
+ assertEquals(0, (receivedErrors.size()));
+ assertEquals(1, (channel_.iqs_.size()));
+ }
+
+ @Test
+ public void testHandleIQ_ServerRespondsWithBareJID() {
+ MyRequest testling = new MyRequest(IQ.Type.Get, new JID(), payload_, router_);
+ router_.setJID(new JID("alice@wonderland.lit/TeaParty"));
+ testling.onResponse.connect(new Slot2<Payload, ErrorPayload>() {
+ @Override
+ public void call(Payload p, ErrorPayload e) {
+ handleResponse(p, e);
+ }
+ });
+ testling.send();
+
+ channel_.onIQReceived.emit(createResponse(new JID("alice@wonderland.lit"),"test-id"));
+
+ assertEquals(1, responsesReceived_);
+ assertEquals(0, (receivedErrors.size()));
+ assertEquals(1, (channel_.iqs_.size()));
+ }
+
+ // This tests a bug in ejabberd servers (2.0.5)
+ @Test
+ public void testHandleIQ_ServerRespondsWithFullJID() {
+ MyRequest testling = new MyRequest(IQ.Type.Get, new JID(), payload_, router_);
+ router_.setJID(new JID("alice@wonderland.lit/TeaParty"));
+ testling.onResponse.connect(new Slot2<Payload, ErrorPayload>() {
+ @Override
+ public void call(Payload p, ErrorPayload e) {
+ handleResponse(p, e);
+ }
+ });
+ testling.send();
+
+ channel_.onIQReceived.emit(createResponse(new JID("alice@wonderland.lit/TeaParty"),"test-id"));
+
+ assertEquals(1, responsesReceived_);
+ assertEquals(0, (receivedErrors.size()));
+ assertEquals(1, (channel_.iqs_.size()));
+ }
+
+ @Test
+ public void testHandleIQ_ServerRespondsWithoutFrom() {
+ MyRequest testling = new MyRequest(IQ.Type.Get, new JID(), payload_, router_);
+ router_.setJID(new JID("alice@wonderland.lit/TeaParty"));
+ testling.onResponse.connect(new Slot2<Payload, ErrorPayload>() {
+ @Override
+ public void call(Payload p, ErrorPayload e) {
+ handleResponse(p, e);
+ }
+ });
+ testling.send();
+
+ channel_.onIQReceived.emit(createResponse(new JID(),"test-id"));
+
+ assertEquals(1, responsesReceived_);
+ assertEquals(0, (receivedErrors.size()));
+ assertEquals(1, (channel_.iqs_.size()));
+ }
+}
diff --git a/test/com/isode/stroke/queries/ResponderTest.java b/test/com/isode/stroke/queries/ResponderTest.java
new file mode 100644
index 0000000..54c8621
--- /dev/null
+++ b/test/com/isode/stroke/queries/ResponderTest.java
@@ -0,0 +1,160 @@
+/*
+ * Copyright (c) 2010 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+/*
+ * Copyright (c) 2015 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+package com.isode.stroke.queries;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import org.junit.Test;
+import org.junit.Before;
+import com.isode.stroke.queries.Responder;
+import com.isode.stroke.queries.IQRouter;
+import com.isode.stroke.queries.DummyIQChannel;
+import com.isode.stroke.jid.JID;
+import com.isode.stroke.elements.IQ;
+import com.isode.stroke.elements.ErrorPayload;
+import com.isode.stroke.elements.SoftwareVersion;
+import java.util.Vector;
+
+public class ResponderTest {
+
+ private IQRouter router_;
+ private DummyIQChannel channel_;
+ private SoftwareVersion payload_;
+
+ private class MyResponder extends Responder<SoftwareVersion> {
+
+ public MyResponder(IQRouter router) {
+ super(new SoftwareVersion(), router);
+ getRequestResponse_ = true;
+ setRequestResponse_ = true;
+ }
+
+ public boolean handleGetRequest(final JID from, final JID to, final String id, SoftwareVersion payload) {
+ assertEquals(new JID("foo@bar.com/baz"), from);
+ assertEquals("myid", id);
+ getPayloads_.add(payload);
+ return getRequestResponse_;
+ }
+
+ public boolean handleSetRequest(final JID from, final JID to, final String id, SoftwareVersion payload) {
+ assertEquals(new JID("foo@bar.com/baz"), from);
+ assertEquals("myid", id);
+ setPayloads_.add(payload);
+ return setRequestResponse_;
+ }
+
+ public boolean getRequestResponse_;
+ public boolean setRequestResponse_;
+ public Vector<SoftwareVersion> getPayloads_ = new Vector<SoftwareVersion>();
+ public Vector<SoftwareVersion> setPayloads_ = new Vector<SoftwareVersion>();
+ }
+
+ private IQ createRequest(IQ.Type type) {
+ IQ iq = new IQ(type);
+ iq.addPayload(payload_);
+ iq.setID("myid");
+ iq.setFrom(new JID("foo@bar.com/baz"));
+ return iq;
+ }
+
+ @Before
+ public void setUp() {
+ channel_ = new DummyIQChannel();
+ router_ = new IQRouter(channel_);
+ payload_ = new SoftwareVersion("foo");
+ }
+
+ @Test
+ public void testConstructor() {
+ MyResponder testling = new MyResponder(router_);
+
+ channel_.onIQReceived.emit(createRequest(IQ.Type.Set));
+
+ assertEquals(0, testling.setPayloads_.size());
+ }
+
+ @Test
+ public void testStart() {
+ MyResponder testling = new MyResponder(router_);
+
+ testling.start();
+ channel_.onIQReceived.emit(createRequest(IQ.Type.Set));
+
+ assertEquals(1, testling.setPayloads_.size());
+ }
+
+ @Test
+ public void testStop() {
+ MyResponder testling = new MyResponder(router_);
+
+ testling.start();
+ testling.stop();
+ channel_.onIQReceived.emit(createRequest(IQ.Type.Set));
+
+ assertEquals(0, testling.setPayloads_.size());
+ }
+
+ @Test
+ public void testHandleIQ_Set() {
+ MyResponder testling = new MyResponder(router_);
+
+ assertTrue(((IQHandler)testling).handleIQ(createRequest(IQ.Type.Set)));
+
+ assertEquals(1, testling.setPayloads_.size());
+ assertEquals(payload_, testling.setPayloads_.get(0));
+ assertEquals(0, testling.getPayloads_.size());
+ }
+
+ @Test
+ public void testHandleIQ_Get() {
+ MyResponder testling = new MyResponder(router_);
+
+ assertTrue(((IQHandler)testling).handleIQ(createRequest(IQ.Type.Get)));
+
+ assertEquals(1, testling.getPayloads_.size());
+ assertEquals(0, testling.setPayloads_.size());
+ assertEquals(payload_, testling.getPayloads_.get(0));
+ }
+
+ @Test
+ public void testHandleIQ_Error() {
+ MyResponder testling = new MyResponder(router_);
+
+ assertFalse(((IQHandler)testling).handleIQ(createRequest(IQ.Type.Error)));
+
+ assertEquals(0, testling.getPayloads_.size());
+ assertEquals(0, testling.setPayloads_.size());
+ }
+
+ @Test
+ public void testHandleIQ_Result() {
+ MyResponder testling = new MyResponder(router_);
+
+ assertFalse(((IQHandler)testling).handleIQ(createRequest(IQ.Type.Result)));
+
+ assertEquals(0, testling.getPayloads_.size());
+ assertEquals(0, testling.setPayloads_.size());
+ }
+
+ @Test
+ public void testHandleIQ_NoPayload() {
+ MyResponder testling = new MyResponder(router_);
+
+ assertFalse(((IQHandler)testling).handleIQ(new IQ(IQ.Type.Get)));
+
+ assertEquals(0, testling.getPayloads_.size());
+ assertEquals(0, testling.setPayloads_.size());
+ }
+}