Java Code Examples for org.springframework.kafka.test.utils.KafkaTestUtils#producerProps()

The following examples show how to use org.springframework.kafka.test.utils.KafkaTestUtils#producerProps() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DeserializtionErrorHandlerByBinderTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 7 votes vote down vote up
@Test
@Ignore
public void test() {
	Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka);
	DefaultKafkaProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>(
			senderProps);
	KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf, true);
	template.setDefaultTopic("foos");
	template.sendDefault(1, 7, "hello");

	Map<String, Object> consumerProps = KafkaTestUtils.consumerProps("foobar",
			"false", embeddedKafka);
	consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
	DefaultKafkaConsumerFactory<String, String> cf = new DefaultKafkaConsumerFactory<>(
			consumerProps);
	Consumer<String, String> consumer1 = cf.createConsumer();
	embeddedKafka.consumeFromAnEmbeddedTopic(consumer1,
			"error.foos.foobar-group");

	ConsumerRecord<String, String> cr = KafkaTestUtils.getSingleRecord(consumer1,
			"error.foos.foobar-group");
	assertThat(cr.value()).isEqualTo("hello");
	assertThat(cr.partition()).isEqualTo(0);

	// Ensuring that the deserialization was indeed done by the binder
	verify(conversionDelegate).deserializeOnInbound(any(Class.class),
			any(KStream.class));
}
 
Example 2
Source File: KafkaStreamsBinderMultipleInputTopicsTest.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 6 votes vote down vote up
private void receiveAndValidate()
		throws Exception {
	Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka);
	DefaultKafkaProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>(
			senderProps);
	KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf, true);
	template.setDefaultTopic("words1");
	template.sendDefault("foobar1");
	template.setDefaultTopic("words2");
	template.sendDefault("foobar2");
	// Sleep a bit so that both the messages are processed before reading from the
	// output topic.
	// Else assertions might fail arbitrarily.
	Thread.sleep(5000);
	ConsumerRecords<String, String> received = KafkaTestUtils.getRecords(consumer);
	List<String> wordCounts = new ArrayList<>(2);

	received.records("counts")
			.forEach((consumerRecord) -> wordCounts.add((consumerRecord.value())));
	System.out.println(wordCounts);
	assertThat(wordCounts.contains("{\"word\":\"foobar1\",\"count\":1}")).isTrue();
	assertThat(wordCounts.contains("{\"word\":\"foobar2\",\"count\":1}")).isTrue();
}
 
Example 3
Source File: WordCountMultipleBranchesIntegrationTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 6 votes vote down vote up
private void receiveAndValidate(ConfigurableApplicationContext context)
		throws Exception {
	Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka);
	DefaultKafkaProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>(
			senderProps);
	KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf, true);
	template.setDefaultTopic("words");
	template.sendDefault("english");
	ConsumerRecord<String, String> cr = KafkaTestUtils.getSingleRecord(consumer,
			"counts");
	assertThat(cr.value().contains("\"word\":\"english\",\"count\":1")).isTrue();

	template.sendDefault("french");
	template.sendDefault("french");
	cr = KafkaTestUtils.getSingleRecord(consumer, "foo");
	assertThat(cr.value().contains("\"word\":\"french\",\"count\":2")).isTrue();

	template.sendDefault("spanish");
	template.sendDefault("spanish");
	template.sendDefault("spanish");
	cr = KafkaTestUtils.getSingleRecord(consumer, "bar");
	assertThat(cr.value().contains("\"word\":\"spanish\",\"count\":3")).isTrue();
}
 
Example 4
Source File: KafkaBoardClientEmbeddedKafkaTests.java    From event-store-demo with GNU General Public License v3.0 6 votes vote down vote up
private void receiveAndValidateBoard( ConfigurableApplicationContext context ) throws Exception {

        Map<String, Object> senderProps = KafkaTestUtils.producerProps( embeddedKafka );
        DefaultKafkaProducerFactory<String, String> pf = new DefaultKafkaProducerFactory<>( senderProps );
        KafkaTemplate<String, String> template = new KafkaTemplate<>( pf, true );
        template.setDefaultTopic( RECEIVER_TOPIC );

        ObjectMapper mapper = context.getBean( ObjectMapper.class );
        BoardClient boardClient = context.getBean( BoardClient.class );

        UUID boardUuid = UUID.randomUUID();
        BoardInitialized boardInitialized = createTestBoardInitializedEvent( boardUuid );
        String event = mapper.writeValueAsString( boardInitialized );
        template.sendDefault( event );

        Thread.sleep( 1000 );

        Board board = boardClient.find( boardUuid );
        assertThat( board, is( notNullValue() ) );
        assertThat( board.getBoardUuid(), is( equalTo( boardUuid ) ) );
        assertThat( board.getName(), is( equalTo( "New Board" ) ) );
        assertThat( board.getStories().isEmpty(), is( equalTo( true ) ) );

    }
 
Example 5
Source File: MultipleFunctionsInSameAppTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 6 votes vote down vote up
private void receiveAndValidate(String in, String... out) throws InterruptedException {
	Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka);
	DefaultKafkaProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>(senderProps);
	try {
		KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf, true);
		template.setDefaultTopic(in);
		template.sendDefault("coffee");
		ConsumerRecord<String, String> cr = KafkaTestUtils.getSingleRecord(consumer, out[0]);
		assertThat(cr.value().contains("coffee")).isTrue();

		template.sendDefault("electronics");
		cr = KafkaTestUtils.getSingleRecord(consumer, out[1]);
		assertThat(cr.value().contains("electronics")).isTrue();

		Assert.isTrue(countDownLatch.await(5, TimeUnit.SECONDS), "Analyze (BiConsumer) method didn't receive all the expected records");
	}
	finally {
		pf.destroy();
	}
}
 
Example 6
Source File: TracingKafkaTest.java    From java-kafka-client with Apache License 2.0 6 votes vote down vote up
@Test
public void with_interceptors() throws Exception {
  Map<String, Object> senderProps = KafkaTestUtils
      .producerProps(embeddedKafka.getEmbeddedKafka());
  senderProps
      .put(ProducerConfig.INTERCEPTOR_CLASSES_CONFIG, TracingProducerInterceptor.class.getName());
  KafkaProducer<Integer, String> producer = new KafkaProducer<>(senderProps);

  producer.send(new ProducerRecord<>("messages", 1, "test"));

  final CountDownLatch latch = new CountDownLatch(1);
  createConsumer(latch, 1, true, null);

  producer.close();

  List<MockSpan> mockSpans = mockTracer.finishedSpans();
  assertEquals(2, mockSpans.size());
  checkSpans(mockSpans);
  assertNull(mockTracer.activeSpan());
}
 
Example 7
Source File: KafkaStreamsNativeEncodingDecodingTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 6 votes vote down vote up
@Test
public void test() {
	Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka);
	DefaultKafkaProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>(
			senderProps);
	KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf, true);
	template.setDefaultTopic("decode-words");
	template.sendDefault("foobar");
	StopWatch stopWatch = new StopWatch();
	stopWatch.start();
	System.out.println("Starting: ");
	ConsumerRecord<String, String> cr = KafkaTestUtils.getSingleRecord(consumer,
			"decode-counts");
	stopWatch.stop();
	System.out.println("Total time: " + stopWatch.getTotalTimeSeconds());
	assertThat(cr.value().equals("Count for foobar : 1")).isTrue();

	verify(conversionDelegate).serializeOnOutbound(any(KStream.class));
	verify(conversionDelegate).deserializeOnInbound(any(Class.class),
			any(KStream.class));
}
 
Example 8
Source File: KafkaStreamsBinderWordCountIntegrationTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 6 votes vote down vote up
private void receiveAndValidate(String in, String out) {
	Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka);
	DefaultKafkaProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>(
			senderProps);
	try {
		KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf, true);
		template.setDefaultTopic(in);
		template.sendDefault("foobar");
		ConsumerRecord<String, String> cr = KafkaTestUtils.getSingleRecord(consumer,
				out);
		assertThat(cr.value().contains("\"word\":\"foobar\",\"count\":1")).isTrue();
	}
	finally {
		pf.destroy();
	}
}
 
Example 9
Source File: TracingKafkaTest.java    From java-kafka-client with Apache License 2.0 5 votes vote down vote up
private Producer<Integer, String> createProducerWithDecorators(
    Collection<SpanDecorator> spanDecorators) {
  Map<String, Object> senderProps = KafkaTestUtils
      .producerProps(embeddedKafka.getEmbeddedKafka());
  KafkaProducer kafkaProducer = new KafkaProducer<>(senderProps);
  TracingKafkaProducerBuilder tracingKafkaProducerBuilder =
      new TracingKafkaProducerBuilder<>(kafkaProducer, mockTracer);
  if (spanDecorators != null) {
    tracingKafkaProducerBuilder = tracingKafkaProducerBuilder.withDecorators(spanDecorators);
  }

  return tracingKafkaProducerBuilder.build();
}
 
Example 10
Source File: DeserializtionErrorHandlerByBinderTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
@Test
public void test() {
	Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka);
	DefaultKafkaProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>(
			senderProps);
	KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf, true);
	template.setDefaultTopic("goos");
	template.sendDefault(1, 7, "hello");

	Map<String, Object> consumerProps = KafkaTestUtils.consumerProps("foobar",
			"false", embeddedKafka);
	consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
	DefaultKafkaConsumerFactory<String, String> cf = new DefaultKafkaConsumerFactory<>(
			consumerProps);
	Consumer<String, String> consumer1 = cf.createConsumer();
	embeddedKafka.consumeFromAnEmbeddedTopic(consumer1,
			"error.goos.foobar-group");

	ConsumerRecord<String, String> cr = KafkaTestUtils.getSingleRecord(consumer1,
			"error.goos.foobar-group");
	assertThat(cr.value()).isEqualTo("hello");
	assertThat(cr.partition()).isEqualTo(0);

	// Ensuring that the deserialization was indeed done by the binder
	verify(conversionDelegate).deserializeOnInbound(any(Class.class),
			any(KStream.class));
}
 
Example 11
Source File: DeserializationErrorHandlerByKafkaTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
@Test
@Ignore
public void test() {
	Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka);
	DefaultKafkaProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>(
			senderProps);
	KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf, true);
	template.setDefaultTopic("abc-DeserializationErrorHandlerByKafkaTests-In");
	template.sendDefault(1, null, "foobar");

	Map<String, Object> consumerProps = KafkaTestUtils.consumerProps("foobar",
			"false", embeddedKafka);
	consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
	DefaultKafkaConsumerFactory<String, String> cf = new DefaultKafkaConsumerFactory<>(
			consumerProps);
	Consumer<String, String> consumer1 = cf.createConsumer();
	embeddedKafka.consumeFromAnEmbeddedTopic(consumer1, "error.abc-DeserializationErrorHandlerByKafkaTests-In.group");

	ConsumerRecord<String, String> cr = KafkaTestUtils.getSingleRecord(consumer1,
			"error.abc-DeserializationErrorHandlerByKafkaTests-In.group");
	assertThat(cr.value()).isEqualTo("foobar");
	assertThat(cr.partition()).isEqualTo(0); // custom partition function

	// Ensuring that the deserialization was indeed done by Kafka natively
	verify(conversionDelegate, never()).deserializeOnInbound(any(Class.class),
			any(KStream.class));
	verify(conversionDelegate, never()).serializeOnOutbound(any(KStream.class));
}
 
Example 12
Source File: TracingKafkaStreamsTest.java    From java-kafka-client with Apache License 2.0 5 votes vote down vote up
@Test
public void test() {
  Map<String, Object> senderProps = KafkaTestUtils
      .producerProps(embeddedKafka.getEmbeddedKafka());

  Properties config = new Properties();
  config.put(StreamsConfig.APPLICATION_ID_CONFIG, "stream-app");
  config.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, senderProps.get("bootstrap.servers"));
  config.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.Integer().getClass());
  config.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass());

  Producer<Integer, String> producer = createProducer();
  ProducerRecord<Integer, String> record = new ProducerRecord<>("stream-test", 1, "test");
  producer.send(record);

  final Serde<String> stringSerde = Serdes.String();
  final Serde<Integer> intSerde = Serdes.Integer();

  StreamsBuilder builder = new StreamsBuilder();
  KStream<Integer, String> kStream = builder.stream("stream-test");

  kStream.map((key, value) -> new KeyValue<>(key, value + "map"))
      .to("stream-out", Produced.with(intSerde, stringSerde));

  KafkaStreams streams = new KafkaStreams(builder.build(), config,
      new TracingKafkaClientSupplier(mockTracer));
  streams.start();

  await().atMost(15, TimeUnit.SECONDS).until(reportedSpansSize(), equalTo(3));

  streams.close();
  producer.close();

  List<MockSpan> spans = mockTracer.finishedSpans();
  assertEquals(3, spans.size());
  checkSpans(spans);

  assertNull(mockTracer.activeSpan());
}
 
Example 13
Source File: TracingKafkaTest.java    From java-kafka-client with Apache License 2.0 5 votes vote down vote up
private Producer<Integer, String> createProducerWithSpanNameProvider(
    BiFunction<String, ProducerRecord, String> spanNameProvider) {
  Map<String, Object> senderProps = KafkaTestUtils
      .producerProps(embeddedKafka.getEmbeddedKafka());
  KafkaProducer kafkaProducer = new KafkaProducer<>(senderProps);
  TracingKafkaProducerBuilder tracingKafkaProducerBuilder =
      new TracingKafkaProducerBuilder<>(kafkaProducer, mockTracer);
  if (spanNameProvider != null) {
    tracingKafkaProducerBuilder = tracingKafkaProducerBuilder
        .withSpanNameProvider(spanNameProvider);
  }

  return tracingKafkaProducerBuilder.build();
}
 
Example 14
Source File: KafkaStreamsBinderWordCountFunctionTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 5 votes vote down vote up
private void receiveAndValidate(String in, String out) {
	Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka);
	DefaultKafkaProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>(senderProps);
	try {
		KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf, true);
		template.setDefaultTopic(in);
		template.sendDefault("foobar");
		ConsumerRecord<String, String> cr = KafkaTestUtils.getSingleRecord(consumer, out);
		assertThat(cr.value().contains("\"word\":\"foobar\",\"count\":1")).isTrue();
	}
	finally {
		pf.destroy();
	}
}
 
Example 15
Source File: KafkaStreamsWordCountApplicationTests.java    From spring-cloud-stream-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void testKafkaStreamsWordCountProcessor() {
	Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka);
	DefaultKafkaProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>(senderProps);
	try {
		KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf, true);
		template.setDefaultTopic("words");
		template.sendDefault("foobar");
		ConsumerRecords<String, String> cr = KafkaTestUtils.getRecords(consumer);
		assertThat(cr.count()).isGreaterThanOrEqualTo(1);
	}
	finally {
		pf.destroy();
	}
}
 
Example 16
Source File: TracingKafkaStreamsTest.java    From java-kafka-client with Apache License 2.0 4 votes vote down vote up
private Producer<Integer, String> createProducer() {
  Map<String, Object> senderProps = KafkaTestUtils
      .producerProps(embeddedKafka.getEmbeddedKafka());
  KafkaProducer<Integer, String> kafkaProducer = new KafkaProducer<>(senderProps);
  return new TracingKafkaProducer<>(kafkaProducer, mockTracer);
}
 
Example 17
Source File: KafkaBinderTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testPolledConsumerWithDlq() throws Exception {
	KafkaTestBinder binder = getBinder();
	PollableSource<MessageHandler> inboundBindTarget = new DefaultPollableMessageSource(
			this.messageConverter);
	ExtendedConsumerProperties<KafkaConsumerProperties> properties = createConsumerProperties();
	properties.getExtension().setPollTimeout(1);
	properties.setMaxAttempts(2);
	properties.setBackOffInitialInterval(0);
	properties.getExtension().setEnableDlq(true);
	Map<String, Object> producerProps = KafkaTestUtils
			.producerProps(embeddedKafka.getEmbeddedKafka());
	Binding<PollableSource<MessageHandler>> binding = binder.bindPollableConsumer(
			"pollableDlq", "group-pcWithDlq", inboundBindTarget, properties);
	KafkaTemplate template = new KafkaTemplate(
			new DefaultKafkaProducerFactory<>(producerProps));
	template.send("pollableDlq", "testPollableDLQ");
	try {
		int n = 0;
		while (n++ < 100) {
			inboundBindTarget.poll(m -> {
				throw new RuntimeException("test DLQ");
			});
			Thread.sleep(100);
		}
	}
	catch (MessageHandlingException e) {
		assertThat(e.getCause().getMessage()).isEqualTo("test DLQ");
	}
	Map<String, Object> consumerProps = KafkaTestUtils.consumerProps("dlq", "false",
			embeddedKafka.getEmbeddedKafka());
	consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
	ConsumerFactory cf = new DefaultKafkaConsumerFactory<>(consumerProps);
	Consumer consumer = cf.createConsumer();
	embeddedKafka.getEmbeddedKafka().consumeFromAnEmbeddedTopic(consumer,
			"error.pollableDlq.group-pcWithDlq");
	ConsumerRecord deadLetter = KafkaTestUtils.getSingleRecord(consumer,
			"error.pollableDlq.group-pcWithDlq");
	assertThat(deadLetter).isNotNull();
	assertThat(deadLetter.value()).isEqualTo("testPollableDLQ");
	binding.unbind();
	consumer.close();
}
 
Example 18
Source File: KafkaClientITest.java    From java-specialagent with Apache License 2.0 4 votes vote down vote up
private static Producer<Long,String> createProducer(final EmbeddedKafkaRule embeddedKafkaRule) {
  final Map<String,Object> senderProps = KafkaTestUtils.producerProps(embeddedKafkaRule.getEmbeddedKafka());
  return new KafkaProducer<>(senderProps);
}
 
Example 19
Source File: SpringMessagingITest.java    From java-specialagent with Apache License 2.0 4 votes vote down vote up
@Bean
public ProducerFactory<String,String> producerFactory() {
  return new DefaultKafkaProducerFactory<>(KafkaTestUtils.producerProps(kafkaEmbedded));
}
 
Example 20
Source File: SpringKafkaTest.java    From java-specialagent with Apache License 2.0 4 votes vote down vote up
@Bean
public ProducerFactory<Integer,String> producerFactory() {
  return new DefaultKafkaProducerFactory<>(KafkaTestUtils.producerProps(embeddedKafkaRule.getEmbeddedKafka()));
}