2017-04-20 23:14:23 +02:00
|
|
|
package io.moquette.persistence;
|
|
|
|
|
|
|
|
|
|
import io.moquette.spi.*;
|
|
|
|
|
import io.moquette.spi.IMessagesStore.StoredMessage;
|
2017-09-12 19:48:42 +02:00
|
|
|
import io.moquette.spi.impl.SessionsRepository;
|
2017-04-20 23:14:23 +02:00
|
|
|
import io.moquette.spi.impl.subscriptions.Subscription;
|
|
|
|
|
import io.moquette.spi.impl.subscriptions.Topic;
|
|
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
|
import io.netty.handler.codec.mqtt.MqttQoS;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
import static io.moquette.spi.impl.subscriptions.Topic.asTopic;
|
|
|
|
|
import static io.netty.handler.codec.mqtt.MqttQoS.AT_MOST_ONCE;
|
|
|
|
|
import static io.netty.handler.codec.mqtt.MqttQoS.EXACTLY_ONCE;
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
|
import static org.junit.Assert.assertNotNull;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Defines all test that an implementation of a IMessageStore should satisfy.
|
|
|
|
|
* */
|
|
|
|
|
public abstract class MessageStoreTCK {
|
|
|
|
|
|
|
|
|
|
public static final String TEST_CLIENT = "TestClient";
|
|
|
|
|
protected ISessionsStore sessionsStore;
|
|
|
|
|
protected IMessagesStore messagesStore;
|
2017-09-12 19:48:42 +02:00
|
|
|
protected SessionsRepository sessionsRepository;
|
2017-04-20 23:14:23 +02:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testDropMessagesInSessionDoesntCleanAnyRetainedStoredMessages() {
|
2017-09-12 19:48:42 +02:00
|
|
|
final ClientSession session = sessionsRepository.createNewSession(TEST_CLIENT, true);
|
2017-04-20 23:14:23 +02:00
|
|
|
StoredMessage publishToStore = new StoredMessage("Hello".getBytes(), EXACTLY_ONCE, "/topic");
|
|
|
|
|
publishToStore.setClientID(TEST_CLIENT);
|
|
|
|
|
publishToStore.setRetained(true);
|
|
|
|
|
messagesStore.storeRetained(new Topic("/topic"), publishToStore);
|
|
|
|
|
|
|
|
|
|
// Exercise
|
|
|
|
|
session.cleanSession();
|
|
|
|
|
|
|
|
|
|
// Verify the message store for session is empty.
|
|
|
|
|
StoredMessage storedPublish = messagesStore.searchMatching(new IMatchingCondition() {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean match(Topic key) {
|
|
|
|
|
return key.match(new Topic("/topic"));
|
|
|
|
|
}
|
|
|
|
|
}).iterator().next();
|
|
|
|
|
assertNotNull("The stored retained message must be present after client's session drop", storedPublish);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testStoreRetained() {
|
|
|
|
|
StoredMessage msgStored = new StoredMessage("Hello".getBytes(), MqttQoS.AT_LEAST_ONCE, "/topic");
|
|
|
|
|
msgStored.setClientID(TEST_CLIENT);
|
|
|
|
|
|
|
|
|
|
messagesStore.storeRetained(asTopic("/topic"), msgStored);
|
|
|
|
|
|
|
|
|
|
//Verify the message is in the store
|
|
|
|
|
StoredMessage msgRetrieved = messagesStore.searchMatching(new IMatchingCondition() {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean match(Topic key) {
|
|
|
|
|
return key.match(new Topic("/topic"));
|
|
|
|
|
}
|
|
|
|
|
}).iterator().next();
|
|
|
|
|
|
|
|
|
|
final ByteBuf payload = msgRetrieved.getPayload();
|
|
|
|
|
byte[] content = new byte[payload.readableBytes()];
|
|
|
|
|
payload.readBytes(content);
|
|
|
|
|
assertEquals("Hello", new String(content));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void givenSubscriptionAlreadyStoredIsOverwrittenByAnotherWithSameTopic() {
|
2017-09-12 19:48:42 +02:00
|
|
|
ClientSession session1 = sessionsRepository.createNewSession("SESSION_ID_1", false);
|
2017-04-20 23:14:23 +02:00
|
|
|
|
|
|
|
|
// Subscribe on /topic with QOSType.MOST_ONE
|
|
|
|
|
Subscription oldSubscription = new Subscription(session1.clientID, new Topic("/topic"), AT_MOST_ONCE);
|
|
|
|
|
session1.subscribe(oldSubscription);
|
|
|
|
|
|
|
|
|
|
// Subscribe on /topic again that overrides the previous subscription.
|
|
|
|
|
Subscription overridingSubscription = new Subscription(session1.clientID, new Topic("/topic"), EXACTLY_ONCE);
|
|
|
|
|
session1.subscribe(overridingSubscription);
|
|
|
|
|
|
|
|
|
|
// Verify
|
|
|
|
|
final ISubscriptionsStore subscriptionsStore = sessionsStore.subscriptionStore();
|
2017-09-12 19:48:42 +02:00
|
|
|
List<Subscription> subscriptions = subscriptionsStore.listAllSubscriptions();
|
2017-04-20 23:14:23 +02:00
|
|
|
assertEquals(1, subscriptions.size());
|
2017-09-12 19:48:42 +02:00
|
|
|
Subscription sub = subscriptions.get(0);
|
2017-04-20 23:14:23 +02:00
|
|
|
assertEquals(overridingSubscription.getRequestedQos(), sub.getRequestedQos());
|
|
|
|
|
}
|
|
|
|
|
}
|