Commit 1980b07882050189ba99199fd6f1516ad0500ed9
Merge branch 'develop/3.2' of github.com:thingsboard/thingsboard into develop/3.2
Showing
2 changed files
with
90 additions
and
0 deletions
1 | +/** | |
2 | + * Copyright © 2016-2020 The Thingsboard Authors | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | +package org.thingsboard.server.transport.mqtt.util; | |
17 | + | |
18 | +import lombok.extern.slf4j.Slf4j; | |
19 | + | |
20 | +import java.util.regex.Pattern; | |
21 | + | |
22 | +@Slf4j | |
23 | +public class MqttTopicRegexUtil { | |
24 | + | |
25 | + public static Pattern toRegex(String topicFilter) { | |
26 | + String regex = topicFilter | |
27 | + .replace("\\", "\\\\") | |
28 | + .replace("+", "[^/]+") | |
29 | + .replace("/#", "($|/.*)"); | |
30 | + log.debug("Converting [{}] to [{}]", topicFilter, regex); | |
31 | + return Pattern.compile(regex); | |
32 | + } | |
33 | + | |
34 | +} | ... | ... |
1 | +/** | |
2 | + * Copyright © 2016-2020 The Thingsboard Authors | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | +package org.thingsboard.server.transport.mqtt.util; | |
17 | + | |
18 | +import org.junit.Test; | |
19 | +import org.junit.runner.RunWith; | |
20 | +import org.mockito.runners.MockitoJUnitRunner; | |
21 | + | |
22 | +import javax.script.ScriptException; | |
23 | +import java.util.regex.Pattern; | |
24 | + | |
25 | +import static org.junit.Assert.assertFalse; | |
26 | +import static org.junit.Assert.assertTrue; | |
27 | + | |
28 | +@RunWith(MockitoJUnitRunner.class) | |
29 | +public class MqttTopicRegexUtilTest { | |
30 | + | |
31 | + private static String TEST_STR_1 = "Sensor/Temperature/House/48"; | |
32 | + private static String TEST_STR_2 = "Sensor/Temperature"; | |
33 | + private static String TEST_STR_3 = "Sensor/Temperature2/House/48"; | |
34 | + | |
35 | + @Test | |
36 | + public void metadataCanBeUpdated() throws ScriptException { | |
37 | + Pattern filter = MqttTopicRegexUtil.toRegex("Sensor/Temperature/House/+"); | |
38 | + assertTrue(filter.matcher(TEST_STR_1).matches()); | |
39 | + assertFalse(filter.matcher(TEST_STR_2).matches()); | |
40 | + | |
41 | + filter = MqttTopicRegexUtil.toRegex("Sensor/+/House/#"); | |
42 | + assertTrue(filter.matcher(TEST_STR_1).matches()); | |
43 | + assertFalse(filter.matcher(TEST_STR_2).matches()); | |
44 | + | |
45 | + filter = MqttTopicRegexUtil.toRegex("Sensor/#"); | |
46 | + assertTrue(filter.matcher(TEST_STR_1).matches()); | |
47 | + assertTrue(filter.matcher(TEST_STR_2).matches()); | |
48 | + assertTrue(filter.matcher(TEST_STR_3).matches()); | |
49 | + | |
50 | + filter = MqttTopicRegexUtil.toRegex("Sensor/Temperature/#"); | |
51 | + assertTrue(filter.matcher(TEST_STR_1).matches()); | |
52 | + assertTrue(filter.matcher(TEST_STR_2).matches()); | |
53 | + assertFalse(filter.matcher(TEST_STR_3).matches()); | |
54 | + } | |
55 | + | |
56 | +} | ... | ... |