void processSubscribe(MqttSubscribeMessage msg) { final String clientID = NettyUtils.clientID(channel); if (!connected) { LOG.warn("SUBSCRIBE received on already closed connection, CId={}, channel: {}", clientID, channel); dropConnection(); return; } postOffice.subscribeClientToTopics(msg, clientID, NettyUtils.userName(channel), this); }
private void subscribe(MqttQoS topic, String newsTopic, MQTTConnection connection) { MqttSubscribeMessage subscribe = MqttMessageBuilders.subscribe() .addSubscription(topic, newsTopic) .messageId(1) .build(); sut.subscribeClientToTopics(subscribe, connection.getClientId(), null, this.connection); MqttSubAckMessage subAck = ((EmbeddedChannel) this.connection.channel).readOutbound(); assertEquals(topic.value(), (int) subAck.payload().grantedQoSLevels().get(0)); }
@Test public void testSubscribedToNotAuthorizedTopic() { NettyUtils.userName(channel, FAKE_USER_NAME); IAuthorizatorPolicy prohibitReadOnNewsTopic = mock(IAuthorizatorPolicy.class); when(prohibitReadOnNewsTopic.canRead(eq(new Topic(NEWS_TOPIC)), eq(FAKE_USER_NAME), eq(FAKE_CLIENT_ID))) .thenReturn(false); sut = new PostOffice(subscriptions, new MemoryRetainedRepository(), sessionRegistry, ConnectionTestUtils.NO_OBSERVERS_INTERCEPTOR, new Authorizator(prohibitReadOnNewsTopic)); connection.processConnect(connectMessage); ConnectionTestUtils.assertConnectAccepted(channel); //Exercise MqttSubscribeMessage subscribe = MqttMessageBuilders.subscribe() .addSubscription(AT_MOST_ONCE, NEWS_TOPIC) .messageId(1) .build(); sut.subscribeClientToTopics(subscribe, FAKE_CLIENT_ID, FAKE_USER_NAME, connection); // Verify MqttSubAckMessage subAckMsg = channel.readOutbound(); verifyFailureQos(subAckMsg); }
private void subscribe(MqttQoS topic, String newsTopic, MQTTConnection connection) { MqttSubscribeMessage subscribe = MqttMessageBuilders.subscribe() .addSubscription(topic, newsTopic) .messageId(1) .build(); sut.subscribeClientToTopics(subscribe, connection.getClientId(), null, this.connection); MqttSubAckMessage subAck = ((EmbeddedChannel) this.connection.channel).readOutbound(); assertEquals(topic.value(), (int) subAck.payload().grantedQoSLevels().get(0)); }
protected void subscribe(EmbeddedChannel channel, String topic, MqttQoS desiredQos) { MqttSubscribeMessage subscribe = MqttMessageBuilders.subscribe() .addSubscription(desiredQos, topic) .messageId(1) .build(); sut.subscribeClientToTopics(subscribe, FAKE_CLIENT_ID, null, connection); MqttSubAckMessage subAck = channel.readOutbound(); assertEquals(desiredQos.value(), (int) subAck.payload().grantedQoSLevels().get(0)); final String clientId = NettyUtils.clientID(channel); Subscription expectedSubscription = new Subscription(clientId, new Topic(topic), desiredQos); final Set<Subscription> matchedSubscriptions = subscriptions.matchWithoutQosSharpening(new Topic(topic)); assertEquals(1, matchedSubscriptions.size()); final Subscription onlyMatchedSubscription = matchedSubscriptions.iterator().next(); assertEquals(expectedSubscription, onlyMatchedSubscription); }
protected void subscribe(MQTTConnection connection, String topic, MqttQoS desiredQos) { EmbeddedChannel channel = (EmbeddedChannel) connection.channel; MqttSubscribeMessage subscribe = MqttMessageBuilders.subscribe() .addSubscription(desiredQos, topic) .messageId(1) .build(); sut.subscribeClientToTopics(subscribe, connection.getClientId(), null, connection); MqttSubAckMessage subAck = channel.readOutbound(); assertEquals(desiredQos.value(), (int) subAck.payload().grantedQoSLevels().get(0)); final String clientId = connection.getClientId(); Subscription expectedSubscription = new Subscription(clientId, new Topic(topic), desiredQos); final Set<Subscription> matchedSubscriptions = subscriptions.matchWithoutQosSharpening(new Topic(topic)); assertEquals(1, matchedSubscriptions.size()); final Subscription onlyMatchedSubscription = matchedSubscriptions.iterator().next(); assertEquals(expectedSubscription, onlyMatchedSubscription); }
protected void subscribe(MQTTConnection connection, String topic, MqttQoS desiredQos) { EmbeddedChannel channel = (EmbeddedChannel) connection.channel; MqttSubscribeMessage subscribe = MqttMessageBuilders.subscribe() .addSubscription(desiredQos, topic) .messageId(1) .build(); sut.subscribeClientToTopics(subscribe, connection.getClientId(), null, connection); MqttSubAckMessage subAck = channel.readOutbound(); assertEquals(desiredQos.value(), (int) subAck.payload().grantedQoSLevels().get(0)); final String clientId = connection.getClientId(); Subscription expectedSubscription = new Subscription(clientId, new Topic(topic), desiredQos); final Set<Subscription> matchedSubscriptions = subscriptions.matchWithoutQosSharpening(new Topic(topic)); assertEquals(1, matchedSubscriptions.size()); final Subscription onlyMatchedSubscription = matchedSubscriptions.iterator().next(); assertEquals(expectedSubscription, onlyMatchedSubscription); }
protected void subscribe(MQTTConnection connection, String topic, MqttQoS desiredQos) { EmbeddedChannel channel = (EmbeddedChannel) connection.channel; MqttSubscribeMessage subscribe = MqttMessageBuilders.subscribe() .addSubscription(desiredQos, topic) .messageId(1) .build(); sut.subscribeClientToTopics(subscribe, connection.getClientId(), null, connection); MqttSubAckMessage subAck = channel.readOutbound(); assertEquals(desiredQos.value(), (int) subAck.payload().grantedQoSLevels().get(0)); final String clientId = connection.getClientId(); Subscription expectedSubscription = new Subscription(clientId, new Topic(topic), desiredQos); final Set<Subscription> matchedSubscriptions = subscriptions.matchWithoutQosSharpening(new Topic(topic)); assertEquals(1, matchedSubscriptions.size()); final Subscription onlyMatchedSubscription = matchedSubscriptions.iterator().next(); assertEquals(expectedSubscription, onlyMatchedSubscription); }
protected void subscribe(MQTTConnection connection, String topic, MqttQoS desiredQos) { EmbeddedChannel channel = (EmbeddedChannel) connection.channel; MqttSubscribeMessage subscribe = MqttMessageBuilders.subscribe() .addSubscription(desiredQos, topic) .messageId(1) .build(); sut.subscribeClientToTopics(subscribe, connection.getClientId(), null, connection); MqttSubAckMessage subAck = channel.readOutbound(); assertEquals(desiredQos.value(), (int) subAck.payload().grantedQoSLevels().get(0)); final String clientId = connection.getClientId(); Subscription expectedSubscription = new Subscription(clientId, new Topic(topic), desiredQos); final Set<Subscription> matchedSubscriptions = subscriptions.matchQosSharpening(new Topic(topic)); assertEquals(1, matchedSubscriptions.size()); //assertTrue(matchedSubscriptions.size() >=1); final Subscription onlyMatchedSubscription = matchedSubscriptions.iterator().next(); assertEquals(expectedSubscription, onlyMatchedSubscription); // assertTrue(matchedSubscriptions.contains(expectedSubscription)); }
@Test public void testSubscribeWithBadFormattedTopic() { connection.processConnect(connectMessage); ConnectionTestUtils.assertConnectAccepted(channel); assertEquals("After CONNECT subscription MUST be empty", 0, subscriptions.size()); //Exercise MqttSubscribeMessage subscribe = MqttMessageBuilders.subscribe() .addSubscription(AT_MOST_ONCE, BAD_FORMATTED_TOPIC) .messageId(1) .build(); this.sut.subscribeClientToTopics(subscribe, FAKE_CLIENT_ID, FAKE_USER_NAME, connection); MqttSubAckMessage subAckMsg = channel.readOutbound(); assertEquals("Bad topic CAN'T add any subscription", 0, subscriptions.size()); verifyFailureQos(subAckMsg); }