|
| 1 | +/* |
| 2 | + * Copyright 2019-present HiveMQ GmbH |
| 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 com.hivemq.extensions.log; |
| 17 | + |
| 18 | +import com.hivemq.client.mqtt.datatypes.MqttQos; |
| 19 | +import com.hivemq.client.mqtt.mqtt5.Mqtt5BlockingClient; |
| 20 | +import com.hivemq.client.mqtt.mqtt5.Mqtt5Client; |
| 21 | +import com.hivemq.client.mqtt.mqtt5.message.publish.Mqtt5PayloadFormatIndicator; |
| 22 | +import org.jetbrains.annotations.NotNull; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.slf4j.event.Level; |
| 25 | +import org.testcontainers.hivemq.HiveMQContainer; |
| 26 | +import org.testcontainers.junit.jupiter.Container; |
| 27 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 28 | +import org.testcontainers.utility.MountableFile; |
| 29 | + |
| 30 | +import java.nio.charset.StandardCharsets; |
| 31 | + |
| 32 | +import static com.hivemq.extensions.log.DockerImageNames.HIVEMQ; |
| 33 | +import static org.awaitility.Awaitility.await; |
| 34 | + |
| 35 | +/** |
| 36 | + * @since 1.2.0 |
| 37 | + */ |
| 38 | +@Testcontainers |
| 39 | +public class FullConfigXmlIT { |
| 40 | + |
| 41 | + @Container |
| 42 | + final @NotNull HiveMQContainer hivemq = new HiveMQContainer(HIVEMQ) // |
| 43 | + .withExtension(MountableFile.forClasspathResource("hivemq-mqtt-message-log-extension")) |
| 44 | + .waitForExtension("HiveMQ Mqtt Message Log Extension") |
| 45 | + .withFileInExtensionHomeFolder(MountableFile.forClasspathResource("fullConfig.xml"), |
| 46 | + "hivemq-mqtt-message-log-extension", |
| 47 | + "/conf/config.xml") |
| 48 | + .withLogConsumer(outputFrame -> System.out.print("HiveMQ: " + outputFrame.getUtf8String())); |
| 49 | + |
| 50 | + @Test |
| 51 | + void test() { |
| 52 | + final Mqtt5BlockingClient client = Mqtt5Client.builder() |
| 53 | + .identifier("test-client") |
| 54 | + .serverHost(hivemq.getHost()) |
| 55 | + .serverPort(hivemq.getMqttPort()) |
| 56 | + .buildBlocking(); |
| 57 | + |
| 58 | + client.connectWith() |
| 59 | + .willPublish() |
| 60 | + .topic("will") |
| 61 | + .qos(MqttQos.EXACTLY_ONCE) |
| 62 | + .payload("willPayload".getBytes(StandardCharsets.UTF_8)) |
| 63 | + .contentType("text/plain") |
| 64 | + .correlationData("willCorrelationData".getBytes(StandardCharsets.UTF_8)) |
| 65 | + .payloadFormatIndicator(Mqtt5PayloadFormatIndicator.UTF_8) |
| 66 | + .responseTopic("willResponse") |
| 67 | + .retain(false) |
| 68 | + .messageExpiryInterval(10_000) |
| 69 | + .userProperties() |
| 70 | + .add("willProperty", "willValue") |
| 71 | + .applyUserProperties() |
| 72 | + .delayInterval(50_000) |
| 73 | + .applyWillPublish() |
| 74 | + .send(); |
| 75 | + await().until(() -> hivemq.getLogs() |
| 76 | + .contains( |
| 77 | + "Received CONNECT from client 'test-client': Protocol version: 'V_5', Clean Start: 'true', Session Expiry Interval: '0', Keep Alive: '60', Maximum Packet Size: '268435460', Receive Maximum: '65535', Topic Alias Maximum: '0', Request Problem Information: 'true', Request Response Information: 'false', Username: 'null', Password: 'null', Auth Method: 'null', Auth Data (Base64): 'null', User Properties: 'null', Will: { Topic: 'will', Payload: 'willPayload', QoS: '2', Retained: 'false', Message Expiry Interval: '10000', Duplicate Delivery: 'false', Correlation Data: 'willCorrelationData', Response Topic: 'willResponse', Content Type: 'text/plain', Payload Format Indicator: 'UTF_8', Subscription Identifiers: '[]', User Properties: [Name: 'willProperty', Value: 'willValue'], Will Delay: '50000' }")); |
| 78 | + await().until(() -> hivemq.getLogs() |
| 79 | + .contains( |
| 80 | + "Sent CONNACK to client 'test-client': Reason Code: 'SUCCESS', Session Present: 'false', Session Expiry Interval: 'null', Assigned ClientId 'null', Maximum QoS: 'EXACTLY_ONCE', Maximum Packet Size: '268435460', Receive Maximum: '10', Topic Alias Maximum: '5', Reason String: 'null', Response Information: 'null', Server Keep Alive: 'null', Server Reference: 'null', Shared Subscription Available: 'true', Wildcards Available: 'true', Retain Available: 'true', Subscription Identifiers Available: 'true', Auth Method: 'null', Auth Data (Base64): 'null', User Properties: 'null'")); |
| 81 | + |
| 82 | + client.subscribeWith().topicFilter("#").send(); |
| 83 | + await().until(() -> hivemq.getLogs() |
| 84 | + .contains( |
| 85 | + "Received SUBSCRIBE from client 'test-client': Topics: { [Topic: '#', QoS: '2', Retain As Published: 'false', No Local: 'false', Retain Handling: 'SEND'] }, Subscription Identifier: '1', User Properties: 'null'")); |
| 86 | + await().until(() -> hivemq.getLogs() |
| 87 | + .contains( |
| 88 | + "Sent SUBACK to client 'test-client': Suback Reason Codes: { [Reason Code: 'GRANTED_QOS_2'] }, Reason String: 'null', User Properties: 'null'")); |
| 89 | + |
| 90 | + client.publishWith() |
| 91 | + .topic("publish") |
| 92 | + .qos(MqttQos.EXACTLY_ONCE) |
| 93 | + .payload("payload1".getBytes(StandardCharsets.UTF_8)) |
| 94 | + .contentType("text/plain") |
| 95 | + .correlationData("willCorrelationData".getBytes(StandardCharsets.UTF_8)) |
| 96 | + .payloadFormatIndicator(Mqtt5PayloadFormatIndicator.UTF_8) |
| 97 | + .responseTopic("publishResponse") |
| 98 | + .retain(false) |
| 99 | + .messageExpiryInterval(10_000) |
| 100 | + .userProperties() |
| 101 | + .add("publishProperty", "publishValue") |
| 102 | + .applyUserProperties() |
| 103 | + .send(); |
| 104 | + await().until(() -> hivemq.getLogs() |
| 105 | + .contains( |
| 106 | + "Received PUBLISH from client 'test-client' for topic 'publish': Payload: 'payload1', QoS: '2', Retained: 'false', Message Expiry Interval: '10000', Duplicate Delivery: 'false', Correlation Data: 'willCorrelationData', Response Topic: 'publishResponse', Content Type: 'text/plain', Payload Format Indicator: 'UTF_8', Subscription Identifiers: '[]', User Properties: [Name: 'publishProperty', Value: 'publishValue']")); |
| 107 | + await().until(() -> hivemq.getLogs() |
| 108 | + .contains( |
| 109 | + "Sent PUBREC to client 'test-client': Reason Code: 'SUCCESS', Reason String: 'null', User Properties: 'null'")); |
| 110 | + await().until(() -> hivemq.getLogs() |
| 111 | + .contains( |
| 112 | + "Received PUBREL from client 'test-client': Reason Code: 'SUCCESS', Reason String: 'null', User Properties: 'null'")); |
| 113 | + await().until(() -> hivemq.getLogs() |
| 114 | + .contains( |
| 115 | + "Sent PUBCOMP to client 'test-client': Reason Code: 'SUCCESS', Reason String: 'null', User Properties: 'null'")); |
| 116 | + |
| 117 | + await().until(() -> hivemq.getLogs() |
| 118 | + .contains( |
| 119 | + "Sent PUBLISH to client 'test-client' on topic 'publish': Payload: 'payload1', QoS: '2', Retained: 'false', Message Expiry Interval: '10000', Duplicate Delivery: 'false', Correlation Data: 'willCorrelationData', Response Topic: 'publishResponse', Content Type: 'text/plain', Payload Format Indicator: 'UTF_8', Subscription Identifiers: '[1]', User Properties: [Name: 'publishProperty', Value: 'publishValue']")); |
| 120 | + await().until(() -> hivemq.getLogs() |
| 121 | + .contains( |
| 122 | + "Received PUBREC from client 'test-client': Reason Code: 'SUCCESS', Reason String: 'null', User Properties: 'null'")); |
| 123 | + await().until(() -> hivemq.getLogs() |
| 124 | + .contains( |
| 125 | + "Sent PUBREL to client 'test-client': Reason Code: 'SUCCESS', Reason String: 'null', User Properties: 'null'")); |
| 126 | + await().until(() -> hivemq.getLogs() |
| 127 | + .contains( |
| 128 | + "Received PUBCOMP from client 'test-client': Reason Code: 'SUCCESS', Reason String: 'null', User Properties: 'null'")); |
| 129 | + |
| 130 | + client.publishWith() |
| 131 | + .topic("publish") |
| 132 | + .qos(MqttQos.AT_LEAST_ONCE) |
| 133 | + .payload("payload2".getBytes(StandardCharsets.UTF_8)) |
| 134 | + .contentType("text/plain") |
| 135 | + .correlationData("willCorrelationData".getBytes(StandardCharsets.UTF_8)) |
| 136 | + .payloadFormatIndicator(Mqtt5PayloadFormatIndicator.UTF_8) |
| 137 | + .responseTopic("publishResponse") |
| 138 | + .retain(false) |
| 139 | + .messageExpiryInterval(10_000) |
| 140 | + .userProperties() |
| 141 | + .add("publishProperty", "publishValue") |
| 142 | + .applyUserProperties() |
| 143 | + .send(); |
| 144 | + await().until(() -> hivemq.getLogs() |
| 145 | + .contains( |
| 146 | + "Received PUBLISH from client 'test-client' for topic 'publish': Payload: 'payload2', QoS: '1', Retained: 'false', Message Expiry Interval: '10000', Duplicate Delivery: 'false', Correlation Data: 'willCorrelationData', Response Topic: 'publishResponse', Content Type: 'text/plain', Payload Format Indicator: 'UTF_8', Subscription Identifiers: '[]', User Properties: [Name: 'publishProperty', Value: 'publishValue']")); |
| 147 | + await().until(() -> hivemq.getLogs() |
| 148 | + .contains( |
| 149 | + "Sent PUBACK to client 'test-client': Reason Code: 'SUCCESS', Reason String: 'null', User Properties: 'null'")); |
| 150 | + await().until(() -> hivemq.getLogs() |
| 151 | + .contains( |
| 152 | + "Sent PUBLISH to client 'test-client' on topic 'publish': Payload: 'payload2', QoS: '1', Retained: 'false', Message Expiry Interval: '10000', Duplicate Delivery: 'false', Correlation Data: 'willCorrelationData', Response Topic: 'publishResponse', Content Type: 'text/plain', Payload Format Indicator: 'UTF_8', Subscription Identifiers: '[1]', User Properties: [Name: 'publishProperty', Value: 'publishValue']")); |
| 153 | + await().until(() -> hivemq.getLogs() |
| 154 | + .contains( |
| 155 | + "Received PUBACK from client 'test-client': Reason Code: 'SUCCESS', Reason String: 'null', User Properties: 'null'")); |
| 156 | + |
| 157 | + client.unsubscribeWith().topicFilter("#").send(); |
| 158 | + await().until(() -> hivemq.getLogs() |
| 159 | + .contains( |
| 160 | + "Received UNSUBSCRIBE from client 'test-client': Topics: { [Topic: '#'] }, User Properties: 'null'")); |
| 161 | + await().until(() -> hivemq.getLogs() |
| 162 | + .contains( |
| 163 | + "Sent UNSUBACK to client 'test-client': Unsuback Reason Codes: { [Reason Code: 'SUCCESS'] }, Reason String: 'null', User Properties: 'null'")); |
| 164 | + |
| 165 | + client.disconnect(); |
| 166 | + await().until(() -> hivemq.getLogs() |
| 167 | + .contains( |
| 168 | + "Received DISCONNECT from client 'test-client': Reason Code: 'NORMAL_DISCONNECTION', Reason String: 'null', Server Reference: 'null', Session Expiry: 'null', User Properties: 'null'")); |
| 169 | + } |
| 170 | +} |
0 commit comments