|
90 | 90 | import org.apache.pulsar.client.impl.MessageImpl; |
91 | 91 | import org.apache.pulsar.client.impl.auth.AuthenticationTls; |
92 | 92 | import org.apache.pulsar.client.impl.conf.ClientConfigurationData; |
| 93 | +import org.apache.pulsar.common.api.proto.CompressionType; |
| 94 | +import org.apache.pulsar.common.api.proto.MessageMetadata; |
93 | 95 | import org.apache.pulsar.common.policies.data.AuthAction; |
94 | 96 | import org.apache.pulsar.common.policies.data.AutoSubscriptionCreationOverride; |
95 | 97 | import org.apache.pulsar.common.policies.data.AutoTopicCreationOverride; |
@@ -2801,6 +2803,67 @@ public static void main(String[] args) { |
2801 | 2803 | } |
2802 | 2804 | } |
2803 | 2805 |
|
| 2806 | + @Test |
| 2807 | + public void topicsPrintMessageCompressionType() throws Exception { |
| 2808 | + PulsarAdmin admin = mock(PulsarAdmin.class); |
| 2809 | + Topics mockTopics = mock(Topics.class); |
| 2810 | + when(admin.topics()).thenReturn(mockTopics); |
| 2811 | + CmdTopics cmdTopics = new CmdTopics(() -> admin); |
| 2812 | + |
| 2813 | + MessageImpl<byte[]> compressed = mockMessage(CompressionType.LZ4); |
| 2814 | + MessageImpl<byte[]> uncompressed = mockMessage(null); |
| 2815 | + when(mockTopics.peekMessages("persistent://myprop/ns1/ds1", "sub1", 2, false, |
| 2816 | + TransactionIsolationLevel.READ_COMMITTED)).thenReturn(List.of(compressed, uncompressed)); |
| 2817 | + when(mockTopics.examineMessage("persistent://myprop/ns1/ds1", "latest", 1)).thenReturn(compressed); |
| 2818 | + when(mockTopics.getMessageById("persistent://myprop/ns1/ds1", 10, 2)).thenReturn(compressed); |
| 2819 | + |
| 2820 | + String peekOutput = captureStdOut( |
| 2821 | + () -> cmdTopics.run(split("peek-messages persistent://myprop/ns1/ds1 -s sub1 -n 2"))); |
| 2822 | + assertEquals(peekOutput.split("Compression type: LZ4", -1).length - 1, 1, peekOutput); |
| 2823 | + |
| 2824 | + String examineOutput = captureStdOut( |
| 2825 | + () -> cmdTopics.run(split("examine-messages persistent://myprop/ns1/ds1"))); |
| 2826 | + assertTrue(examineOutput.contains("Compression type: LZ4"), examineOutput); |
| 2827 | + |
| 2828 | + String getByIdOutput = captureStdOut( |
| 2829 | + () -> cmdTopics.run(split("get-message-by-id persistent://myprop/ns1/ds1 -l 10 -e 2"))); |
| 2830 | + assertTrue(getByIdOutput.contains("Compression type: LZ4"), getByIdOutput); |
| 2831 | + |
| 2832 | + when(mockTopics.examineMessage("persistent://myprop/ns1/ds1", "latest", 1)).thenReturn(uncompressed); |
| 2833 | + String uncompressedOutput = captureStdOut( |
| 2834 | + () -> cmdTopics.run(split("examine-messages persistent://myprop/ns1/ds1"))); |
| 2835 | + assertFalse(uncompressedOutput.contains("Compression type"), uncompressedOutput); |
| 2836 | + } |
| 2837 | + |
| 2838 | + @SuppressWarnings("unchecked") |
| 2839 | + private static MessageImpl<byte[]> mockMessage(CompressionType compressionType) { |
| 2840 | + MessageImpl<byte[]> message = mock(MessageImpl.class); |
| 2841 | + MessageMetadata metadata = new MessageMetadata(); |
| 2842 | + if (compressionType != null) { |
| 2843 | + metadata.setCompression(compressionType); |
| 2844 | + } |
| 2845 | + when(message.getMessageBuilder()).thenReturn(metadata); |
| 2846 | + when(message.getMessageId()).thenReturn(new MessageIdImpl(1L, 1L, 1)); |
| 2847 | + when(message.getData()).thenReturn(new byte[]{}); |
| 2848 | + return message; |
| 2849 | + } |
| 2850 | + |
| 2851 | + private interface ThrowingRunnable { |
| 2852 | + void run() throws Exception; |
| 2853 | + } |
| 2854 | + |
| 2855 | + private static String captureStdOut(ThrowingRunnable runnable) throws Exception { |
| 2856 | + PrintStream originalOut = System.out; |
| 2857 | + ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| 2858 | + System.setOut(new PrintStream(output, true, StandardCharsets.UTF_8)); |
| 2859 | + try { |
| 2860 | + runnable.run(); |
| 2861 | + } finally { |
| 2862 | + System.setOut(originalOut); |
| 2863 | + } |
| 2864 | + return output.toString(StandardCharsets.UTF_8); |
| 2865 | + } |
| 2866 | + |
2804 | 2867 | String[] split(String s) { |
2805 | 2868 | return s.split(" "); |
2806 | 2869 | } |
|