|
1 | 1 | package io.github.majusko.pulsar; |
2 | 2 |
|
| 3 | +import io.github.majusko.pulsar.collector.ConsumerCollector; |
| 4 | +import io.github.majusko.pulsar.collector.ConsumerHolder; |
| 5 | +import io.github.majusko.pulsar.constant.Serialization; |
| 6 | +import io.github.majusko.pulsar.consumer.ConsumerBuilder; |
| 7 | +import io.github.majusko.pulsar.producer.ProducerFactory; |
3 | 8 | import io.github.majusko.pulsar.producer.PulsarTemplate; |
| 9 | +import org.apache.pulsar.client.api.Consumer; |
4 | 10 | import org.apache.pulsar.client.api.PulsarClientException; |
| 11 | +import org.apache.pulsar.shade.org.apache.commons.lang3.tuple.ImmutablePair; |
| 12 | +import org.junit.Assert; |
| 13 | +import org.junit.Before; |
5 | 14 | import org.junit.jupiter.api.Test; |
6 | 15 | import org.junit.runner.RunWith; |
7 | 16 | import org.springframework.beans.factory.annotation.Autowired; |
|
10 | 19 | import org.springframework.test.context.ActiveProfiles; |
11 | 20 | import org.springframework.test.context.junit4.SpringRunner; |
12 | 21 |
|
| 22 | +import java.util.HashSet; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Set; |
| 26 | + |
13 | 27 | @RunWith(SpringRunner.class) |
14 | 28 | @SpringBootTest |
15 | 29 | @ActiveProfiles("test") |
16 | 30 | @Import(TestProducerConfiguration.class) |
17 | 31 | class PulsarJavaSpringBootStarterApplicationTests { |
18 | 32 |
|
19 | | - @Autowired |
20 | | - private PulsarTemplate<MyMsg> testProducerConfiguration; |
| 33 | + @Autowired |
| 34 | + private ConsumerBuilder consumerBuilder; |
| 35 | + |
| 36 | + @Autowired |
| 37 | + private ConsumerCollector consumerCollector; |
| 38 | + |
| 39 | + @Autowired |
| 40 | + private ProducerFactory producerFactory; |
| 41 | + |
| 42 | + @Autowired |
| 43 | + private PulsarTemplate<MyMsg> producer; |
| 44 | + |
| 45 | + @Test |
| 46 | + void testProducerSendMethod() throws PulsarClientException { |
| 47 | + producer.send("topic-one", new MyMsg("bb")); |
| 48 | + } |
| 49 | + |
| 50 | + @Before |
| 51 | + public void init() throws InterruptedException { |
| 52 | + Thread.sleep(100); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void testConsumerRegistration1() throws Exception { |
| 57 | + final List<Consumer> consumers = consumerBuilder.getConsumers(); |
| 58 | + |
| 59 | + Assert.assertEquals(1, consumers.size()); |
| 60 | + |
| 61 | + final Consumer consumer = consumers.stream().findFirst().orElseThrow(Exception::new); |
| 62 | + |
| 63 | + Assert.assertNotNull(consumer); |
| 64 | + Assert.assertEquals("mock-topic", consumer.getTopic()); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void testConsumerRegistration2() throws Exception { |
| 69 | + final Class<TestConsumerConfiguration> clazz = TestConsumerConfiguration.class; |
| 70 | + final String descriptor = clazz.getName() + "#" + clazz.getDeclaredMethods()[0].getName(); |
| 71 | + final ConsumerHolder consumerHolder = consumerCollector.getConsumer(descriptor).orElseThrow(Exception::new); |
| 72 | + |
| 73 | + Assert.assertNotNull(consumerHolder); |
| 74 | + Assert.assertEquals("mock-topic", consumerHolder.getAnnotation().topic()); |
| 75 | + Assert.assertEquals(TestConsumerConfiguration.class, consumerHolder.getBean().getClass()); |
| 76 | + Assert.assertEquals("mockTheListener", consumerHolder.getHandler().getName()); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void testProducerRegistration() { |
| 81 | + |
| 82 | + final Map<String, ImmutablePair<Class<?>, Serialization>> topics = producerFactory.getTopics(); |
| 83 | + |
| 84 | + Assert.assertEquals(2, topics.size()); |
| 85 | + |
| 86 | + final Set<String> topicNames = new HashSet<>(topics.keySet()); |
21 | 87 |
|
22 | | - @Test |
23 | | - void contextLoads() throws PulsarClientException { |
24 | | - testProducerConfiguration.send("aa", new MyMsg("bb")); |
25 | | - } |
| 88 | + Assert.assertTrue(topicNames.contains("topic-one")); |
| 89 | + Assert.assertTrue(topicNames.contains("topic-two")); |
| 90 | + } |
26 | 91 | } |
0 commit comments