Re-enabled commented tests
This commit is contained in:
+237
-227
@@ -1,7 +1,9 @@
|
|||||||
package org.dna.mqtt.moquette.messaging.spi.impl;
|
package org.dna.mqtt.moquette.messaging.spi.impl;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
//import org.apache.mina.core.filterchain.IoFilter;
|
//import org.apache.mina.core.filterchain.IoFilter;
|
||||||
//import org.apache.mina.core.filterchain.IoFilterAdapter;
|
//import org.apache.mina.core.filterchain.IoFilterAdapter;
|
||||||
//import org.apache.mina.core.session.DummySession;
|
//import org.apache.mina.core.session.DummySession;
|
||||||
@@ -31,231 +33,239 @@ import org.junit.Test;
|
|||||||
* @author andrea
|
* @author andrea
|
||||||
*/
|
*/
|
||||||
public class ProtocolProcessorTest {
|
public class ProtocolProcessorTest {
|
||||||
// final static String FAKE_CLIENT_ID = "FAKE_123";
|
final static String FAKE_CLIENT_ID = "FAKE_123";
|
||||||
// final static String FAKE_TOPIC = "/news";
|
final static String FAKE_TOPIC = "/news";
|
||||||
//
|
|
||||||
// ServerChannel m_session;
|
ServerChannel m_session;
|
||||||
// byte m_returnCode;
|
byte m_returnCode;
|
||||||
// ConnectMessage connMsg;
|
ConnectMessage connMsg;
|
||||||
// ProtocolProcessor m_processor;
|
ProtocolProcessor m_processor;
|
||||||
//
|
|
||||||
// IStorageService m_storageService;
|
IStorageService m_storageService;
|
||||||
// SubscriptionsStore subscriptions;
|
SubscriptionsStore subscriptions;
|
||||||
// AbstractMessage m_receivedMessage;
|
AbstractMessage m_receivedMessage;
|
||||||
//
|
|
||||||
// IoSession m_minaSession;
|
class DummyChannel implements ServerChannel {
|
||||||
//
|
|
||||||
//
|
private Map<Object, Object> m_attributes = new HashMap<Object, Object>();
|
||||||
// @Before
|
|
||||||
// public void setUp() throws InterruptedException {
|
public Object getAttribute(Object key) {
|
||||||
// connMsg = new ConnectMessage();
|
return m_attributes.get(key);
|
||||||
// connMsg.setProcotolVersion((byte) 0x03);
|
}
|
||||||
//
|
|
||||||
// m_minaSession = new DummySession();
|
public void setAttribute(Object key, Object value) {
|
||||||
//
|
m_attributes.put(key, value);
|
||||||
// m_session = new MinaChannel(m_minaSession);
|
}
|
||||||
//
|
|
||||||
// m_minaSession.getFilterChain().addFirst("MessageCatcher", new IoFilterAdapter() {
|
public void setIdleTime(int idleTime) {
|
||||||
//
|
}
|
||||||
// @Override
|
|
||||||
// public void filterWrite(IoFilter.NextFilter nextFilter, IoSession session,
|
public void close(boolean immediately) {
|
||||||
// WriteRequest writeRequest) throws Exception {
|
|
||||||
// try {
|
}
|
||||||
// m_receivedMessage = (AbstractMessage) writeRequest.getMessage();
|
|
||||||
// if (m_receivedMessage instanceof ConnAckMessage) {
|
public void write(Object value) {
|
||||||
// ConnAckMessage buf = (ConnAckMessage) m_receivedMessage;
|
try {
|
||||||
// m_returnCode = buf.getReturnCode();
|
m_receivedMessage = (AbstractMessage) value;
|
||||||
// }
|
if (m_receivedMessage instanceof ConnAckMessage) {
|
||||||
// } catch (Exception ex) {
|
ConnAckMessage buf = (ConnAckMessage) m_receivedMessage;
|
||||||
// throw new AssertionError("Wrong return code");
|
m_returnCode = buf.getReturnCode();
|
||||||
// }
|
}
|
||||||
// }
|
} catch (Exception ex) {
|
||||||
// });
|
throw new AssertionError("Wrong return code");
|
||||||
//
|
}
|
||||||
// //sleep to let the messaging batch processor to process the initEvent
|
}
|
||||||
// Thread.sleep(300);
|
}
|
||||||
//
|
|
||||||
// m_storageService = new MemoryStorageService();
|
|
||||||
// //m_storageService.initStore();
|
@Before
|
||||||
//
|
public void setUp() throws InterruptedException {
|
||||||
// subscriptions = new SubscriptionsStore();
|
connMsg = new ConnectMessage();
|
||||||
// subscriptions.init(m_storageService);
|
connMsg.setProcotolVersion((byte) 0x03);
|
||||||
// m_processor = new ProtocolProcessor();
|
|
||||||
// m_processor.init(subscriptions, m_storageService);
|
m_session = new DummyChannel();
|
||||||
// }
|
|
||||||
//
|
//sleep to let the messaging batch processor to process the initEvent
|
||||||
// @Test
|
Thread.sleep(300);
|
||||||
// public void testHandleConnect_BadProtocol() {
|
|
||||||
// connMsg.setProcotolVersion((byte) 0x02);
|
m_storageService = new MemoryStorageService();
|
||||||
//
|
//m_storageService.initStore();
|
||||||
// //Exercise
|
|
||||||
// m_processor.processConnect(m_session, connMsg);
|
subscriptions = new SubscriptionsStore();
|
||||||
//
|
subscriptions.init(m_storageService);
|
||||||
// //Verify
|
m_processor = new ProtocolProcessor();
|
||||||
// assertEquals(ConnAckMessage.UNNACEPTABLE_PROTOCOL_VERSION, m_returnCode);
|
m_processor.init(subscriptions, m_storageService);
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// @Test
|
@Test
|
||||||
// public void testConnect_badClientID() {
|
public void testHandleConnect_BadProtocol() {
|
||||||
// connMsg.setClientID("extremely_long_clientID_greater_than_23");
|
connMsg.setProcotolVersion((byte) 0x02);
|
||||||
//
|
|
||||||
// //Exercise
|
//Exercise
|
||||||
// m_processor.processConnect(m_session, connMsg);
|
m_processor.processConnect(m_session, connMsg);
|
||||||
//
|
|
||||||
// //Verify
|
//Verify
|
||||||
// assertEquals(ConnAckMessage.IDENTIFIER_REJECTED, m_returnCode);
|
assertEquals(ConnAckMessage.UNNACEPTABLE_PROTOCOL_VERSION, m_returnCode);
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// @Test
|
@Test
|
||||||
// public void testWill() {
|
public void testConnect_badClientID() {
|
||||||
// connMsg.setClientID("123");
|
connMsg.setClientID("extremely_long_clientID_greater_than_23");
|
||||||
// connMsg.setWillFlag(true);
|
|
||||||
// connMsg.setWillTopic("topic");
|
//Exercise
|
||||||
// connMsg.setWillMessage("Topic message");
|
m_processor.processConnect(m_session, connMsg);
|
||||||
//
|
|
||||||
//
|
//Verify
|
||||||
// //Exercise
|
assertEquals(ConnAckMessage.IDENTIFIER_REJECTED, m_returnCode);
|
||||||
// //m_handler.setMessaging(mockedMessaging);
|
}
|
||||||
// m_processor.processConnect(m_session, connMsg);
|
|
||||||
//
|
@Test
|
||||||
// //Verify
|
public void testWill() {
|
||||||
// assertEquals(ConnAckMessage.CONNECTION_ACCEPTED, m_returnCode);
|
connMsg.setClientID("123");
|
||||||
// //TODO verify the call
|
connMsg.setWillFlag(true);
|
||||||
// /*verify(mockedMessaging).publish(eq("topic"), eq("Topic message".getBytes()),
|
connMsg.setWillTopic("topic");
|
||||||
// any(AbstractMessage.QOSType.class), anyBoolean(), eq("123"), any(IoSession.class));*/
|
connMsg.setWillMessage("Topic message");
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @Test
|
//Exercise
|
||||||
// public void testPublish() throws InterruptedException {
|
//m_handler.setMessaging(mockedMessaging);
|
||||||
// final Subscription subscription = new Subscription(FAKE_CLIENT_ID,
|
m_processor.processConnect(m_session, connMsg);
|
||||||
// FAKE_TOPIC, AbstractMessage.QOSType.MOST_ONE, true);
|
|
||||||
//
|
//Verify
|
||||||
// //subscriptions.matches(topic) redefine the method to return true
|
assertEquals(ConnAckMessage.CONNECTION_ACCEPTED, m_returnCode);
|
||||||
// SubscriptionsStore subs = new SubscriptionsStore() {
|
//TODO verify the call
|
||||||
// @Override
|
/*verify(mockedMessaging).publish(eq("topic"), eq("Topic message".getBytes()),
|
||||||
// public List<Subscription> matches(String topic) {
|
any(AbstractMessage.QOSType.class), anyBoolean(), eq("123"), any(IoSession.class));*/
|
||||||
// if (topic.equals(FAKE_TOPIC)) {
|
}
|
||||||
// return Arrays.asList(subscription);
|
|
||||||
// } else {
|
@Test
|
||||||
// throw new IllegalArgumentException("Expected " + FAKE_TOPIC + " buf found " + topic);
|
public void testPublish() throws InterruptedException {
|
||||||
// }
|
final Subscription subscription = new Subscription(FAKE_CLIENT_ID,
|
||||||
// }
|
FAKE_TOPIC, AbstractMessage.QOSType.MOST_ONE, true);
|
||||||
//
|
|
||||||
//
|
//subscriptions.matches(topic) redefine the method to return true
|
||||||
// };
|
SubscriptionsStore subs = new SubscriptionsStore() {
|
||||||
//
|
@Override
|
||||||
// //simulate a connect that register a clientID to an IoSession
|
public List<Subscription> matches(String topic) {
|
||||||
// subs.init(m_storageService);
|
if (topic.equals(FAKE_TOPIC)) {
|
||||||
// m_processor.init(subs, m_storageService);
|
return Arrays.asList(subscription);
|
||||||
// ConnectMessage connectMessage = new ConnectMessage();
|
} else {
|
||||||
// connectMessage.setProcotolVersion((byte)3);
|
throw new IllegalArgumentException("Expected " + FAKE_TOPIC + " buf found " + topic);
|
||||||
// connectMessage.setClientID(FAKE_CLIENT_ID);
|
}
|
||||||
// connectMessage.setCleanSession(subscription.isCleanSession());
|
}
|
||||||
// m_processor.processConnect(m_session, connectMessage);
|
|
||||||
//
|
|
||||||
//
|
};
|
||||||
// //Exercise
|
|
||||||
// PublishEvent pubEvt = new PublishEvent(FAKE_TOPIC, AbstractMessage.QOSType.MOST_ONE, "Hello".getBytes(), false, "FakeCLI", null);
|
//simulate a connect that register a clientID to an IoSession
|
||||||
// m_processor.processPublish(pubEvt);
|
subs.init(m_storageService);
|
||||||
//
|
m_processor.init(subs, m_storageService);
|
||||||
// //Verify
|
ConnectMessage connectMessage = new ConnectMessage();
|
||||||
// assertNotNull(m_receivedMessage);
|
connectMessage.setProcotolVersion((byte)3);
|
||||||
// //TODO check received message attributes
|
connectMessage.setClientID(FAKE_CLIENT_ID);
|
||||||
// }
|
connectMessage.setCleanSession(subscription.isCleanSession());
|
||||||
//
|
m_processor.processConnect(m_session, connectMessage);
|
||||||
// @Test
|
|
||||||
// public void testSubscribe() {
|
|
||||||
// //Exercise
|
//Exercise
|
||||||
// SubscribeMessage msg = new SubscribeMessage();
|
PublishEvent pubEvt = new PublishEvent(FAKE_TOPIC, AbstractMessage.QOSType.MOST_ONE, "Hello".getBytes(), false, "FakeCLI", null);
|
||||||
// msg.addSubscription(new SubscribeMessage.Couple((byte)AbstractMessage.QOSType.MOST_ONE.ordinal(), FAKE_TOPIC));
|
m_processor.processPublish(pubEvt);
|
||||||
// m_processor.processSubscribe(m_session, msg, FAKE_CLIENT_ID, false);
|
|
||||||
//
|
//Verify
|
||||||
// //Verify
|
assertNotNull(m_receivedMessage);
|
||||||
// assertTrue(m_receivedMessage instanceof SubAckMessage);
|
//TODO check received message attributes
|
||||||
// Subscription expectedSubscription = new Subscription(FAKE_CLIENT_ID, FAKE_TOPIC, AbstractMessage.QOSType.MOST_ONE, false);
|
}
|
||||||
// assertTrue(subscriptions.contains(expectedSubscription));
|
|
||||||
// }
|
@Test
|
||||||
//
|
public void testSubscribe() {
|
||||||
// @Test
|
//Exercise
|
||||||
// public void testDoubleSubscribe() {
|
SubscribeMessage msg = new SubscribeMessage();
|
||||||
// SubscribeMessage msg = new SubscribeMessage();
|
msg.addSubscription(new SubscribeMessage.Couple((byte)AbstractMessage.QOSType.MOST_ONE.ordinal(), FAKE_TOPIC));
|
||||||
// msg.addSubscription(new SubscribeMessage.Couple((byte)AbstractMessage.QOSType.MOST_ONE.ordinal(), FAKE_TOPIC));
|
m_processor.processSubscribe(m_session, msg, FAKE_CLIENT_ID, false);
|
||||||
//
|
|
||||||
// subscriptions.clearAllSubscriptions();
|
//Verify
|
||||||
// assertEquals(0, subscriptions.size());
|
assertTrue(m_receivedMessage instanceof SubAckMessage);
|
||||||
//
|
Subscription expectedSubscription = new Subscription(FAKE_CLIENT_ID, FAKE_TOPIC, AbstractMessage.QOSType.MOST_ONE, false);
|
||||||
// m_processor.processSubscribe(m_session, msg, FAKE_CLIENT_ID, false);
|
assertTrue(subscriptions.contains(expectedSubscription));
|
||||||
//
|
}
|
||||||
// //Exercise
|
|
||||||
// m_processor.processSubscribe(m_session, msg, FAKE_CLIENT_ID, false);
|
@Test
|
||||||
//
|
public void testDoubleSubscribe() {
|
||||||
// //Verify
|
SubscribeMessage msg = new SubscribeMessage();
|
||||||
// assertEquals(1, subscriptions.size());
|
msg.addSubscription(new SubscribeMessage.Couple((byte)AbstractMessage.QOSType.MOST_ONE.ordinal(), FAKE_TOPIC));
|
||||||
// Subscription expectedSubscription = new Subscription(FAKE_CLIENT_ID, FAKE_TOPIC, AbstractMessage.QOSType.MOST_ONE, false);
|
|
||||||
// assertTrue(subscriptions.contains(expectedSubscription));
|
subscriptions.clearAllSubscriptions();
|
||||||
// }
|
assertEquals(0, subscriptions.size());
|
||||||
//
|
|
||||||
//
|
m_processor.processSubscribe(m_session, msg, FAKE_CLIENT_ID, false);
|
||||||
// @Test
|
|
||||||
// public void testPublishOfRetainedMessage_afterNewSubscription() throws Exception {
|
//Exercise
|
||||||
// m_minaSession.getFilterChain().remove("MessageCatcher");
|
m_processor.processSubscribe(m_session, msg, FAKE_CLIENT_ID, false);
|
||||||
// m_minaSession.getFilterChain().addFirst("MessageCatcher", new IoFilterAdapter() {
|
|
||||||
//
|
//Verify
|
||||||
// @Override
|
assertEquals(1, subscriptions.size());
|
||||||
// public void filterWrite(IoFilter.NextFilter nextFilter, IoSession session,
|
Subscription expectedSubscription = new Subscription(FAKE_CLIENT_ID, FAKE_TOPIC, AbstractMessage.QOSType.MOST_ONE, false);
|
||||||
// WriteRequest writeRequest) throws Exception {
|
assertTrue(subscriptions.contains(expectedSubscription));
|
||||||
// try {
|
}
|
||||||
// System.out.println("filterReceived class " + writeRequest.getMessage().getClass().getName());
|
|
||||||
// if (writeRequest.getMessage() instanceof PublishMessage) {
|
|
||||||
// m_receivedMessage = (AbstractMessage) writeRequest.getMessage();
|
@Test
|
||||||
// }
|
public void testPublishOfRetainedMessage_afterNewSubscription() throws Exception {
|
||||||
//
|
/*ServerChannel */m_session = new DummyChannel() {
|
||||||
// if (m_receivedMessage instanceof ConnAckMessage) {
|
@Override
|
||||||
// ConnAckMessage buf = (ConnAckMessage) m_receivedMessage;
|
public void write(Object value) {
|
||||||
// m_returnCode = buf.getReturnCode();
|
try {
|
||||||
// }
|
System.out.println("filterReceived class " + value.getClass().getName());
|
||||||
// } catch (Exception ex) {
|
if (value instanceof PublishMessage) {
|
||||||
// throw new AssertionError("Wrong return code");
|
m_receivedMessage = (AbstractMessage) value;
|
||||||
// }
|
}
|
||||||
// }
|
|
||||||
// });
|
if (m_receivedMessage instanceof ConnAckMessage) {
|
||||||
//
|
ConnAckMessage buf = (ConnAckMessage) m_receivedMessage;
|
||||||
//// //simulate a connect that register a clientID to an IoSession
|
m_returnCode = buf.getReturnCode();
|
||||||
// final Subscription subscription = new Subscription(FAKE_CLIENT_ID,
|
}
|
||||||
// FAKE_TOPIC, AbstractMessage.QOSType.MOST_ONE, true);
|
} catch (Exception ex) {
|
||||||
//
|
throw new AssertionError("Wrong return code");
|
||||||
// //subscriptions.matches(topic) redefine the method to return true
|
}
|
||||||
// SubscriptionsStore subs = new SubscriptionsStore() {
|
}
|
||||||
// @Override
|
};
|
||||||
// public List<Subscription> matches(String topic) {
|
|
||||||
// if (topic.equals(FAKE_TOPIC)) {
|
//simulate a connect that register a clientID to an IoSession
|
||||||
// return Arrays.asList(subscription);
|
final Subscription subscription = new Subscription(FAKE_CLIENT_ID,
|
||||||
// } else {
|
FAKE_TOPIC, AbstractMessage.QOSType.MOST_ONE, true);
|
||||||
// throw new IllegalArgumentException("Expected " + FAKE_TOPIC + " buf found " + topic);
|
|
||||||
// }
|
//subscriptions.matches(topic) redefine the method to return true
|
||||||
// }
|
SubscriptionsStore subs = new SubscriptionsStore() {
|
||||||
// };
|
@Override
|
||||||
// subs.init(new MemoryStorageService());
|
public List<Subscription> matches(String topic) {
|
||||||
//
|
if (topic.equals(FAKE_TOPIC)) {
|
||||||
// //simulate a connect that register a clientID to an IoSession
|
return Arrays.asList(subscription);
|
||||||
// m_processor.init(subs, m_storageService);
|
} else {
|
||||||
// ConnectMessage connectMessage = new ConnectMessage();
|
throw new IllegalArgumentException("Expected " + FAKE_TOPIC + " buf found " + topic);
|
||||||
// connectMessage.setClientID(FAKE_CLIENT_ID);
|
}
|
||||||
// connectMessage.setProcotolVersion((byte)3);
|
}
|
||||||
// connectMessage.setCleanSession(subscription.isCleanSession());
|
};
|
||||||
// m_processor.processConnect(m_session, connectMessage);
|
subs.init(new MemoryStorageService());
|
||||||
// PublishEvent pubEvt = new PublishEvent(FAKE_TOPIC, AbstractMessage.QOSType.MOST_ONE, "Hello".getBytes(), true, "FakeCLI", null);
|
|
||||||
// m_processor.processPublish(pubEvt);
|
//simulate a connect that register a clientID to an IoSession
|
||||||
//
|
m_processor.init(subs, m_storageService);
|
||||||
// //Exercise
|
ConnectMessage connectMessage = new ConnectMessage();
|
||||||
// SubscribeMessage msg = new SubscribeMessage();
|
connectMessage.setClientID(FAKE_CLIENT_ID);
|
||||||
// msg.addSubscription(new SubscribeMessage.Couple((byte)QOSType.MOST_ONE.ordinal(), "#"));
|
connectMessage.setProcotolVersion((byte)3);
|
||||||
// m_processor.processSubscribe(m_session, msg, FAKE_CLIENT_ID, false);
|
connectMessage.setCleanSession(subscription.isCleanSession());
|
||||||
//
|
m_processor.processConnect(m_session, connectMessage);
|
||||||
// //Verify
|
PublishEvent pubEvt = new PublishEvent(FAKE_TOPIC, AbstractMessage.QOSType.MOST_ONE, "Hello".getBytes(), true, "FakeCLI", null);
|
||||||
// assertNotNull(m_receivedMessage);
|
m_processor.processPublish(pubEvt);
|
||||||
// assertTrue(m_receivedMessage instanceof PublishMessage);
|
|
||||||
// PublishMessage pubMessage = (PublishMessage) m_receivedMessage;
|
//Exercise
|
||||||
// assertEquals(FAKE_TOPIC, pubMessage.getTopicName());
|
SubscribeMessage msg = new SubscribeMessage();
|
||||||
// }
|
msg.addSubscription(new SubscribeMessage.Couple((byte)QOSType.MOST_ONE.ordinal(), "#"));
|
||||||
|
m_processor.processSubscribe(m_session, msg, FAKE_CLIENT_ID, false);
|
||||||
|
|
||||||
|
//Verify
|
||||||
|
assertNotNull(m_receivedMessage);
|
||||||
|
assertTrue(m_receivedMessage instanceof PublishMessage);
|
||||||
|
PublishMessage pubMessage = (PublishMessage) m_receivedMessage;
|
||||||
|
assertEquals(FAKE_TOPIC, pubMessage.getTopicName());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user