Skip to content

Commit 00bef5c

Browse files
committed
[broker-23] rename topic factory methods
1 parent 03e2223 commit 00bef5c

File tree

10 files changed

+68
-42
lines changed

10 files changed

+68
-42
lines changed

src/main/java/com/ss/mqtt/broker/model/SubscribeTopicFilter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.ss.mqtt.broker.model;
22

3-
import static com.ss.mqtt.broker.util.TopicUtils.newTopicFilter;
3+
import static com.ss.mqtt.broker.util.TopicUtils.buildTopicFilter;
44
import com.ss.mqtt.broker.model.topic.TopicFilter;
55
import lombok.EqualsAndHashCode;
66
import lombok.Getter;
@@ -44,7 +44,7 @@ public class SubscribeTopicFilter {
4444
private final boolean retainAsPublished;
4545

4646
public SubscribeTopicFilter(@NotNull String topicFilter, @NotNull QoS qos) {
47-
this(newTopicFilter(topicFilter), qos, SubscribeRetainHandling.SEND, true, true);
47+
this(buildTopicFilter(topicFilter), qos, SubscribeRetainHandling.SEND, true, true);
4848
}
4949

5050
public SubscribeTopicFilter(@NotNull TopicFilter topicFilter, @NotNull QoS qos) {

src/main/java/com/ss/mqtt/broker/network/packet/in/PublishInPacket.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.ss.mqtt.broker.network.packet.in;
22

33
import static com.ss.mqtt.broker.util.TopicUtils.EMPTY_TOPIC_NAME;
4-
import static com.ss.mqtt.broker.util.TopicUtils.newTopicName;
4+
import static com.ss.mqtt.broker.util.TopicUtils.buildTopicName;
55
import com.ss.mqtt.broker.model.MqttPropertyConstants;
66
import com.ss.mqtt.broker.model.PacketProperty;
77
import com.ss.mqtt.broker.model.QoS;
@@ -298,7 +298,7 @@ public byte getPacketType() {
298298
@Override
299299
protected void readVariableHeader(@NotNull MqttConnection connection, @NotNull ByteBuffer buffer) {
300300
// http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718039
301-
topicName = newTopicName(readString(buffer));
301+
topicName = buildTopicName(readString(buffer));
302302
packetId = qos != QoS.AT_MOST_ONCE ? readUnsignedShort(buffer) : 0;
303303
}
304304

src/main/java/com/ss/mqtt/broker/network/packet/in/SubscribeInPacket.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.ss.mqtt.broker.network.packet.in;
22

3-
import static com.ss.mqtt.broker.util.TopicUtils.newTopicFilter;
3+
import static com.ss.mqtt.broker.util.TopicUtils.buildTopicFilter;
44
import com.ss.mqtt.broker.model.*;
55
import com.ss.mqtt.broker.network.MqttConnection;
66
import com.ss.mqtt.broker.network.packet.PacketType;
@@ -95,7 +95,7 @@ protected void readPayload(@NotNull MqttConnection connection, @NotNull ByteBuff
9595
var noLocal = !isMqtt5 || NumberUtils.isSetBit(options, 2);
9696
var rap = !isMqtt5 || NumberUtils.isSetBit(options, 3);
9797

98-
topicFilters.add(new SubscribeTopicFilter(newTopicFilter(topicFilter), qos, retainHandling, noLocal, rap));
98+
topicFilters.add(new SubscribeTopicFilter(buildTopicFilter(topicFilter), qos, retainHandling, noLocal, rap));
9999
}
100100
}
101101

src/main/java/com/ss/mqtt/broker/network/packet/in/UnsubscribeInPacket.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.ss.mqtt.broker.network.packet.in;
22

3-
import static com.ss.mqtt.broker.util.TopicUtils.newTopicFilter;
3+
import static com.ss.mqtt.broker.util.TopicUtils.buildTopicFilter;
44
import com.ss.mqtt.broker.model.PacketProperty;
55
import com.ss.mqtt.broker.model.topic.TopicFilter;
66
import com.ss.mqtt.broker.network.MqttConnection;
@@ -56,7 +56,7 @@ protected void readPayload(@NotNull MqttConnection connection, @NotNull ByteBuff
5656
}
5757

5858
while (buffer.hasRemaining()) {
59-
topicFilters.add(newTopicFilter(readString(buffer)));
59+
topicFilters.add(buildTopicFilter(readString(buffer)));
6060
}
6161
}
6262

src/main/java/com/ss/mqtt/broker/util/TopicUtils.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,19 @@ public static boolean hasWildcard(@NotNull TopicFilter topicFilter) {
3636
return topic.contains(SINGLE_LEVEL_WILDCARD) || topic.contains(MULTI_LEVEL_WILDCARD);
3737
}
3838

39-
public static @NotNull TopicName newTopicName(@NotNull String topicName) {
39+
public static @NotNull TopicName buildTopicName(@NotNull String topicName) {
4040
if (isInvalidTopicName(topicName)) {
4141
return INVALID_TOPIC_NAME;
4242
} else {
4343
return new TopicName(topicName);
4444
}
4545
}
4646

47-
public static @NotNull TopicFilter newTopicFilter(@NotNull String topicFilter) {
47+
public static @NotNull TopicFilter buildTopicFilter(@NotNull String topicFilter) {
4848
if (isInvalidTopicFilter(topicFilter)) {
4949
return INVALID_TOPIC_FILTER;
5050
} else if (isShared(topicFilter)) {
51-
return newSharedTopicFilter(topicFilter);
51+
return buildSharedTopicFilter(topicFilter);
5252
} else {
5353
return new TopicFilter(topicFilter);
5454
}
@@ -66,7 +66,7 @@ public static boolean hasWildcard(@NotNull TopicFilter topicFilter) {
6666
return newReadOnlyArray(segments);
6767
}
6868

69-
private static @NotNull TopicFilter newSharedTopicFilter(@NotNull String topicFilter) {
69+
private static @NotNull TopicFilter buildSharedTopicFilter(@NotNull String topicFilter) {
7070
int firstSlash = topicFilter.indexOf(DELIMITER) + 1;
7171
int secondSlash = topicFilter.indexOf(DELIMITER, firstSlash);
7272
var group = topicFilter.substring(firstSlash, secondSlash);

src/test/groovy/com/ss/mqtt/broker/test/integration/service/SubscribtionServiceTest.groovy

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import java.util.concurrent.CompletionException
1515

1616
import static com.hivemq.client.mqtt.datatypes.MqttQos.*
1717
import static com.ss.mqtt.broker.model.ActionResult.SUCCESS
18-
import static com.ss.mqtt.broker.util.TopicUtils.newTopicName
18+
import static com.ss.mqtt.broker.util.TopicUtils.buildTopicName
1919
import static org.spockframework.util.Pair.of
2020

2121
class SubscribtionServiceTest extends IntegrationSpecification {
@@ -29,7 +29,7 @@ class SubscribtionServiceTest extends IntegrationSpecification {
2929
def "should clear/restore topic subscribers after disconnect/reconnect"() {
3030
given:
3131
def subscriber = buildExternalMqtt5Client(clientId)
32-
def topicName = newTopicName(topicFilter)
32+
def topicName = buildTopicName(topicFilter)
3333

3434
def matchesCount = 0
3535
SingleSubscriber matchedSubscriber = null
@@ -103,7 +103,7 @@ class SubscribtionServiceTest extends IntegrationSpecification {
103103
.send()
104104
.join()
105105
when:
106-
subscriptionService.forEachTopicSubscriber(newTopicName(topicName), null, action)
106+
subscriptionService.forEachTopicSubscriber(buildTopicName(topicName), null, action)
107107
then:
108108
matchesCount == 1
109109
matchedSubscriber.subscribe.topicFilter.getRawTopic() == targetTopicFilter
@@ -158,7 +158,7 @@ class SubscribtionServiceTest extends IntegrationSpecification {
158158
.send()
159159
.join()
160160
when:
161-
subscriptionService.forEachTopicSubscriber(newTopicName(topicName), clientId, action)
161+
subscriptionService.forEachTopicSubscriber(buildTopicName(topicName), clientId, action)
162162
then:
163163
matchesCount == targetCount
164164
matchedSubscribers[0] == clientId1

src/test/groovy/com/ss/mqtt/broker/test/model/TopicSubscriberTest.groovy

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import com.ss.mqtt.broker.test.network.NetworkUnitSpecification
1010
import spock.lang.Unroll
1111

1212
import static com.ss.mqtt.broker.model.QoS.*
13-
import static com.ss.mqtt.broker.util.TopicUtils.newTopicFilter
14-
import static com.ss.mqtt.broker.util.TopicUtils.newTopicName
13+
import static com.ss.mqtt.broker.util.TopicUtils.buildTopicFilter
14+
import static com.ss.mqtt.broker.util.TopicUtils.buildTopicName
1515

1616
class TopicSubscriberTest extends NetworkUnitSpecification {
1717

@@ -41,16 +41,16 @@ class TopicSubscriberTest extends NetworkUnitSpecification {
4141
}
4242
where:
4343
topicFilters << [
44-
[newTopicFilter("topic/second/in"), newTopicFilter("topic/+/in"), newTopicFilter("topic/#")],
45-
[newTopicFilter("topic/+/in"), newTopicFilter("topic/first/in"), newTopicFilter("topic/out")],
46-
[newTopicFilter("topic/second/in"), newTopicFilter("topic/first/in"), newTopicFilter("topic/out")],
47-
[newTopicFilter("topic/second/in"), newTopicFilter("topic/+/in"), newTopicFilter("topic/#")]
44+
[buildTopicFilter("topic/second/in"), buildTopicFilter("topic/+/in"), buildTopicFilter("topic/#")],
45+
[buildTopicFilter("topic/+/in"), buildTopicFilter("topic/first/in"), buildTopicFilter("topic/out")],
46+
[buildTopicFilter("topic/second/in"), buildTopicFilter("topic/first/in"), buildTopicFilter("topic/out")],
47+
[buildTopicFilter("topic/second/in"), buildTopicFilter("topic/+/in"), buildTopicFilter("topic/#")]
4848
]
4949
topicNames << [
50-
newTopicName("topic/second/in"),
51-
newTopicName("topic/first/in"),
52-
newTopicName("topic/second/in"),
53-
newTopicName("topic/second/in")
50+
buildTopicName("topic/second/in"),
51+
buildTopicName("topic/first/in"),
52+
buildTopicName("topic/second/in"),
53+
buildTopicName("topic/second/in")
5454
]
5555
subscriberQos << [
5656
[AT_LEAST_ONCE, AT_MOST_ONCE, EXACTLY_ONCE],

src/test/groovy/com/ss/mqtt/broker/test/model/TopicTest.groovy

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class TopicTest extends Specification {
1111
@Unroll
1212
def "should create topic name: #stringTopicName"(String stringTopicName, int levelsCount) {
1313
when:
14-
def topicName = newTopicName(stringTopicName)
14+
def topicName = buildTopicName(stringTopicName)
1515
then:
1616
topicName.segments.size() == levelsCount
1717
topicName.rawTopic == stringTopicName
@@ -25,7 +25,7 @@ class TopicTest extends Specification {
2525
@Unroll
2626
def "should fail create topic name: #stringTopicName"(String stringTopicName) {
2727
when:
28-
def topicName = newTopicName(stringTopicName)
28+
def topicName = buildTopicName(stringTopicName)
2929
then:
3030
isInvalid(topicName)
3131
where:
@@ -39,7 +39,7 @@ class TopicTest extends Specification {
3939
@Unroll
4040
def "should create topic filter: #stringTopicFilter"(String stringTopicFilter, int levelsCount) {
4141
when:
42-
def topicFilter = newTopicFilter(stringTopicFilter)
42+
def topicFilter = buildTopicFilter(stringTopicFilter)
4343
then:
4444
topicFilter.segments.size() == levelsCount
4545
topicFilter.rawTopic == stringTopicFilter
@@ -55,7 +55,7 @@ class TopicTest extends Specification {
5555
@Unroll
5656
def "should fail create topic filter: #stringTopicFilter"(String stringTopicFilter) {
5757
when:
58-
def topicFilter = newTopicFilter(stringTopicFilter)
58+
def topicFilter = buildTopicFilter(stringTopicFilter)
5959
then:
6060
isInvalid(topicFilter)
6161
where:
@@ -69,4 +69,31 @@ class TopicTest extends Specification {
6969
"topic/##"
7070
]
7171
}
72+
73+
@Unroll
74+
def "should match topicFilter[#topicFilter] with topicName[#topicName]"(String topicFilter, String topicName) {
75+
expect:
76+
buildTopicName(topicName).match(buildTopicFilter(topicFilter))
77+
where:
78+
topicFilter | topicName
79+
"topic/in" | "topic/in"
80+
"topic/+" | "topic/in"
81+
"topic/#" | "topic/in"
82+
"topic/+/in" | "topic/m/in"
83+
}
84+
85+
@Unroll
86+
def "should not match topicFilter[#topicFilter] with topicName[#topicName]"(String topicFilter, String topicName) {
87+
expect:
88+
!buildTopicName(topicName).match(buildTopicFilter(topicFilter))
89+
where:
90+
topicFilter | topicName
91+
"topic/in" | "topic/m/in"
92+
"topic/in" | "topic/in/m"
93+
"topic/+" | "topic/m/in"
94+
"topic/+" | "topic/in/m"
95+
"topic/#" | "topic"
96+
"topic/+/in" | "topic/m/n"
97+
"topic/+/in" | "topic/in"
98+
}
7299
}

src/test/groovy/com/ss/mqtt/broker/test/network/NetworkUnitSpecification.groovy

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import com.ss.mqtt.broker.model.SubscribeTopicFilter
88
import com.ss.mqtt.broker.model.data.type.StringPair
99
import com.ss.mqtt.broker.model.reason.code.SubscribeAckReasonCode
1010
import com.ss.mqtt.broker.model.reason.code.UnsubscribeAckReasonCode
11-
import com.ss.mqtt.broker.model.topic.TopicName
1211
import com.ss.mqtt.broker.network.MqttConnection
1312
import com.ss.mqtt.broker.network.client.MqttClient
1413
import com.ss.mqtt.broker.test.UnitSpecification
@@ -19,8 +18,8 @@ import spock.lang.Shared
1918

2019
import java.nio.charset.StandardCharsets
2120

22-
import static com.ss.mqtt.broker.util.TopicUtils.newTopicFilter
23-
import static com.ss.mqtt.broker.util.TopicUtils.newTopicName
21+
import static com.ss.mqtt.broker.util.TopicUtils.buildTopicFilter
22+
import static com.ss.mqtt.broker.util.TopicUtils.buildTopicName
2423

2524
class NetworkUnitSpecification extends UnitSpecification {
2625

@@ -55,21 +54,21 @@ class NetworkUnitSpecification extends UnitSpecification {
5554
public static final authMethod = "testAuthMethod"
5655
public static final authData = "testAuthData".getBytes(StandardCharsets.UTF_8)
5756
public static final reasonString = "reasonString"
58-
public static final publishTopic = newTopicName("publish/Topic")
57+
public static final publishTopic = buildTopicName("publish/Topic")
5958
public static final responseTopic = "response/Topic"
6059
public static final topicFilter = "topic/Filter"
61-
public static final topicFilter1Obj311 = new SubscribeTopicFilter(newTopicFilter(topicFilter), QoS.AT_LEAST_ONCE)
60+
public static final topicFilter1Obj311 = new SubscribeTopicFilter(buildTopicFilter(topicFilter), QoS.AT_LEAST_ONCE)
6261
public static final topicFilter1Obj5 = new SubscribeTopicFilter(
63-
newTopicFilter(topicFilter),
62+
buildTopicFilter(topicFilter),
6463
QoS.AT_LEAST_ONCE,
6564
SubscribeRetainHandling.DO_NOT_SEND,
6665
true,
6766
false,
6867
)
6968
public static final topicFilter2 = "topic/Filter2"
70-
public static final topicFilter2Obj311 = new SubscribeTopicFilter(newTopicFilter(topicFilter2), QoS.EXACTLY_ONCE)
69+
public static final topicFilter2Obj311 = new SubscribeTopicFilter(buildTopicFilter(topicFilter2), QoS.EXACTLY_ONCE)
7170
public static final topicFilter2Obj5 = new SubscribeTopicFilter(
72-
newTopicFilter(topicFilter2),
71+
buildTopicFilter(topicFilter2),
7372
QoS.EXACTLY_ONCE,
7473
SubscribeRetainHandling.DO_NOT_SEND,
7574
true,

src/test/groovy/com/ss/mqtt/broker/test/util/TopicUtilsTest.groovy

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class TopicUtilsTest extends Specification {
1111
@Unroll
1212
def "should create valid topic name: #topicName"(String topicName) {
1313
expect:
14-
!isInvalid(newTopicName(topicName))
14+
!isInvalid(buildTopicName(topicName))
1515
where:
1616
topicName | _
1717
"topic/Name" | _
@@ -21,7 +21,7 @@ class TopicUtilsTest extends Specification {
2121
@Unroll
2222
def "should create valid topic filter: #topicFilter"(String topicFilter) {
2323
expect:
24-
!isInvalid(newTopicFilter(topicFilter))
24+
!isInvalid(buildTopicFilter(topicFilter))
2525
where:
2626
topicFilter | _
2727
"topic/Filter" | _
@@ -33,7 +33,7 @@ class TopicUtilsTest extends Specification {
3333
@Unroll
3434
def "should detect invalid topic name: #topicName"(String topicName) {
3535
expect:
36-
isInvalid(newTopicName(topicName))
36+
isInvalid(buildTopicName(topicName))
3737
where:
3838
topicName | _
3939
"topic/+" | _
@@ -45,7 +45,7 @@ class TopicUtilsTest extends Specification {
4545
@Unroll
4646
def "should detect invalid topic filter: #topicFilter"(String topicFilter) {
4747
expect:
48-
isInvalid(newTopicFilter(topicFilter))
48+
isInvalid(buildTopicFilter(topicFilter))
4949
where:
5050
topicFilter | _
5151
"topic/" | _

0 commit comments

Comments
 (0)