Skip to content

Commit b04f6e7

Browse files
committed
write more tests
1 parent f6ddd94 commit b04f6e7

File tree

10 files changed

+588
-58
lines changed

10 files changed

+588
-58
lines changed

base/src/main/java/javasabr/mqtt/base/util/DebugUtils.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.concurrent.ConcurrentHashMap;
88
import java.util.concurrent.ConcurrentMap;
99
import javasabr.rlib.collections.array.Array;
10+
import javasabr.rlib.collections.dictionary.RefToRefDictionary;
1011
import tools.jackson.core.JacksonException;
1112
import tools.jackson.core.JsonGenerator;
1213
import tools.jackson.databind.SerializationContext;
@@ -85,17 +86,34 @@ public ArraySerializer() {
8586

8687
@Override
8788
public void serialize(
88-
Array<?> value,
89+
Array<?> array,
8990
JsonGenerator gen,
9091
SerializationContext provider) throws JacksonException {
9192
gen.writeStartArray();
92-
for (Object element : value) {
93+
for (Object element : array) {
9394
gen.writePOJO(element);
9495
}
9596
gen.writeEndArray();
9697
}
9798
}
9899

100+
public static class RefToRefDictionarySerializer extends StdSerializer<RefToRefDictionary<?, ?>> {
101+
102+
public RefToRefDictionarySerializer() {
103+
super(RefToRefDictionary.class);
104+
}
105+
106+
@Override
107+
public void serialize(
108+
RefToRefDictionary<?, ?> dictionary,
109+
JsonGenerator gen,
110+
SerializationContext provider) throws JacksonException {
111+
gen.writeStartObject();
112+
dictionary.forEach((key, value) -> gen.writePOJOProperty(key.toString(), value));
113+
gen.writeEndObject();
114+
}
115+
}
116+
99117
private static final SimpleFilterProvider DEBUG_FIELDS_FILTER = new SimpleFilterProvider();
100118
private static final IncludeDebugFieldsPropertyFilter INCLUDE_DEBUG_FIELDS_PROPERTY_FILTER =
101119
new IncludeDebugFieldsPropertyFilter();
@@ -113,7 +131,8 @@ public void serialize(
113131
.withIsGetterVisibility(JsonAutoDetect.Visibility.NONE))
114132
.addMixIn(Object.class, DebugFieldsFilterMixIn.class)
115133
.addModule(new SimpleModule()
116-
.addSerializer(new ArraySerializer()))
134+
.addSerializer(new ArraySerializer())
135+
.addSerializer(new RefToRefDictionarySerializer()))
117136
.filterProvider(DEBUG_FIELDS_FILTER)
118137
.build();
119138

model/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ dependencies {
99

1010
testImplementation projects.testSupport
1111
testImplementation libs.rlib.logger.impl
12+
testFixturesApi projects.testSupport
1213
}

model/src/main/java/javasabr/mqtt/model/subscriber/SingleSubscriber.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package javasabr.mqtt.model.subscriber;
22

3+
import com.fasterxml.jackson.annotation.JsonValue;
34
import javasabr.mqtt.model.QoS;
45
import javasabr.mqtt.model.subscribtion.Subscription;
56
import javasabr.mqtt.model.subscribtion.SubscriptionOwner;
@@ -14,4 +15,10 @@ public SingleSubscriber resolveSingle() {
1415
public QoS qos() {
1516
return subscription.qos();
1617
}
18+
19+
@JsonValue
20+
@Override
21+
public String toString() {
22+
return "[" + owner + "]->[" + subscription.topicFilter().rawTopic() + "|" + subscription.qos().index() + "]";
23+
}
1724
}

model/src/main/java/javasabr/mqtt/model/subscribtion/Subscription.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package javasabr.mqtt.model.subscribtion;
22

3+
import com.fasterxml.jackson.annotation.JsonValue;
34
import javasabr.mqtt.model.QoS;
45
import javasabr.mqtt.model.SubscribeRetainHandling;
56
import javasabr.mqtt.model.topic.TopicFilter;
@@ -30,4 +31,11 @@ public record Subscription(
3031
false, Application Messages forwarded using this subscription have the RETAIN flag set to 0. Retained messages sent
3132
when the subscription is established have the RETAIN flag set to 1.
3233
*/
33-
boolean retainAsPublished) {}
34+
boolean retainAsPublished) {
35+
36+
@JsonValue
37+
@Override
38+
public String toString() {
39+
return "[" + topicFilter.rawTopic() + "|" + qos.index() + "|" + retainHandling + "|" + noLocal + "|" + retainAsPublished + "]";
40+
}
41+
}

model/src/main/java/javasabr/mqtt/model/topic/tree/TopicNode.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package javasabr.mqtt.model.topic.tree;
22

33
import java.util.function.Supplier;
4+
import javasabr.mqtt.base.util.DebugUtils;
45
import javasabr.mqtt.model.subscriber.SingleSubscriber;
56
import javasabr.mqtt.model.subscriber.Subscriber;
67
import javasabr.mqtt.model.subscribtion.Subscription;
@@ -25,6 +26,10 @@ class TopicNode extends TopicTreeBase {
2526

2627
private final static Supplier<TopicNode> TOPIC_NODE_FACTORY = TopicNode::new;
2728

29+
static {
30+
DebugUtils.registerIncludedFields("childNodes", "subscribers");
31+
}
32+
2833
@Nullable
2934
volatile LockableRefToRefDictionary<String, TopicNode> childNodes;
3035
@Nullable
@@ -150,4 +155,9 @@ private LockableArray<Subscriber> getOrCreateSubscribers() {
150155
//noinspection ConstantConditions
151156
return subscribers;
152157
}
158+
159+
@Override
160+
public String toString() {
161+
return DebugUtils.toJsonString(this);
162+
}
153163
}

model/src/test/groovy/javasabr/mqtt/model/topic/TopicCreationTest.groovy

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,61 @@ class TopicCreationTest extends UnitSpecification {
2222
"/topic/name/" | 4 | ["", "topic", "name", ""]
2323
"topic/na me" | 2 | ["topic", "na me"]
2424
}
25+
26+
@Unroll
27+
def "should create topic filter:[#topicFilter] with levels [#levels] and segments #segments"() {
28+
given:
29+
def created = TopicFilter.valueOf(topicFilter)
30+
expect:
31+
created.levelsCount() == levels
32+
List.of(created.segments()) == segments
33+
where:
34+
topicFilter | levels | segments
35+
"t" | 1 | ["t"]
36+
"topic" | 1 | ["topic"]
37+
"/" | 2 | ["", ""]
38+
"/topic" | 2 | ["", "topic"]
39+
"topic/" | 2 | ["topic", ""]
40+
"/topic/name/" | 4 | ["", "topic", "name", ""]
41+
"topic/na me" | 2 | ["topic", "na me"]
42+
"#" | 1 | ["#"]
43+
"/#" | 2 | ["", "#"]
44+
"/topic/#" | 3 | ["", "topic", "#"]
45+
"+" | 1 | ["+"]
46+
"/+" | 2 | ["", "+"]
47+
"+/+" | 2 | ["+", "+"]
48+
"/topic/+" | 3 | ["", "topic", "+"]
49+
"/topic/+/filter" | 4 | ["", "topic", "+", "filter"]
50+
"+/topic/+/filter" | 4 | ["+", "topic", "+", "filter"]
51+
"topic/+/filter/+/test" | 5 | ["topic", "+", "filter", "+", "test"]
52+
}
53+
54+
@Unroll
55+
def "should create shared topic filter:[#sharedTopicFilter] with levels [#levels], [#shareName] and segments #segments"() {
56+
given:
57+
def created = SharedTopicFilter.valueOf(sharedTopicFilter)
58+
expect:
59+
created.levelsCount() == levels
60+
created.shareName() == shareName
61+
List.of(created.segments()) == segments
62+
where:
63+
sharedTopicFilter | levels | shareName | segments
64+
'$share/name1/t' | 1 | "name1" | ["t"]
65+
'$share/name2/topic' | 1 | "name2" | ["topic"]
66+
'$share/name3/' | 1 | "name3" | [""]
67+
'$share/name4/topic' | 1 | "name4" | ["topic"]
68+
'$share/name5/topic/' | 2 | "name5" | ["topic", ""]
69+
'$share/name6/topic/name/' | 3 | "name6" | ["topic", "name", ""]
70+
'$share/name7/topic/na me' | 2 | "name7" | ["topic", "na me"]
71+
'$share/name8/#' | 1 | "name8" | ["#"]
72+
'$share/name9/#' | 1 | "name9" | ["#"]
73+
'$share/name10/topic/#' | 2 | "name10" | ["topic", "#"]
74+
'$share/name11/+' | 1 | "name11" | ["+"]
75+
'$share/name12/+' | 1 | "name12" | ["+"]
76+
'$share/name13/+/+' | 2 | "name13" | ["+", "+"]
77+
'$share/name14/topic/+' | 2 | "name14" | ["topic", "+"]
78+
'$share/name15/topic/+/filter' | 3 | "name15" | ["topic", "+", "filter"]
79+
'$share/name16/+/topic/+/filter' | 4 | "name16" | ["+", "topic", "+", "filter"]
80+
'$share/name17/topic/+/filter/+/test' | 5 | "name17" | ["topic", "+", "filter", "+", "test"]
81+
}
2582
}

model/src/test/groovy/javasabr/mqtt/model/topic/TopicValidatorTest.groovy

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class TopicValidatorTest extends UnitSpecification {
2727
"+/+" | false
2828
"#" | false
2929
"/to$NULL_CHAR/ne/" | false
30+
'$sys/topic' | false
3031
}
3132

3233
@Unroll
@@ -62,6 +63,9 @@ class TopicValidatorTest extends UnitSpecification {
6263
"+/+topic/filter" | false
6364
"+/#/filter" | false
6465
"#/filter" | false
66+
'$sys/topic/filter/' | false
67+
'$sys/#' | false
68+
'$sys/topic/+' | false
6569
}
6670

6771
@Unroll
@@ -98,5 +102,8 @@ class TopicValidatorTest extends UnitSpecification {
98102
'$share/group1/' | false
99103
'$share' | false
100104
'$share//' | false
105+
'$share/group1/$sys/topic/filter/' | false
106+
'$share/group1/$sys/#' | false
107+
'$share/group1/$sys/topic/+' | false
101108
}
102109
}

0 commit comments

Comments
 (0)