/*
 * Copyright 2012-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.autoconfigure.kafka;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.apache.kafka.clients.admin.NewTopic;
import org.apache.kafka.clients.producer.Producer;
import org.junit.After;
import org.junit.ClassRule;
import org.junit.Test;

import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.kafka.test.rule.KafkaEmbedded;
import org.springframework.messaging.handler.annotation.Header;

import static org.assertj.core.api.Assertions.assertThat;

/**
 * Integration tests for {@link KafkaAutoConfiguration}.
 *
 * @author Gary Russell
 */
public class KafkaAutoConfigurationIntegrationTests {

	private static final String TEST_TOPIC = "testTopic";

	private static final String ADMIN_CREATED_TOPIC = "adminCreatedTopic";

	@ClassRule
	public static final KafkaEmbedded kafkaEmbedded = new KafkaEmbedded(1, true,
			TEST_TOPIC);

	private AnnotationConfigApplicationContext context;

	@After
	public void close() {
		if (this.context != null) {
			this.context.close();
		}
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Test
	public void testEndToEnd() throws Exception {
		load(KafkaConfig.class,
				"spring.kafka.bootstrap-servers:" + kafkaEmbedded.getBrokersAsString(),
				"spring.kafka.consumer.group-id=testGroup",
				"spring.kafka.consumer.auto-offset-reset=earliest");
		KafkaTemplate<String, String> template = this.context
				.getBean(KafkaTemplate.class);
		template.send(TEST_TOPIC, "foo", "bar");
		Listener listener = this.context.getBean(Listener.class);
		assertThat(listener.latch.await(30, TimeUnit.SECONDS)).isTrue();
		assertThat(listener.key).isEqualTo("foo");
		assertThat(listener.received).isEqualTo("bar");

		DefaultKafkaProducerFactory producerFactory = this.context
				.getBean(DefaultKafkaProducerFactory.class);
		Producer producer = producerFactory.createProducer();
		assertThat(producer.partitionsFor(ADMIN_CREATED_TOPIC).size()).isEqualTo(10);
		producer.close();
	}

	private void load(Class<?> config, String... environment) {
		this.context = doLoad(new Class<?>[] { config }, environment);
	}

	private AnnotationConfigApplicationContext doLoad(Class<?>[] configs,
			String... environment) {
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
		applicationContext.register(configs);
		applicationContext.register(KafkaAutoConfiguration.class);
		TestPropertyValues.of(environment).applyTo(applicationContext);
		applicationContext.refresh();
		return applicationContext;
	}

	public static class KafkaConfig {

		@Bean
		public Listener listener() {
			return new Listener();
		}

		@Bean
		public NewTopic adminCreated() {
			return new NewTopic(ADMIN_CREATED_TOPIC, 10, (short) 1);
		}

	}

	public static class Listener {

		private final CountDownLatch latch = new CountDownLatch(1);

		private volatile String received;

		private volatile String key;

		@KafkaListener(topics = TEST_TOPIC)
		public void listen(String foo,
				@Header(KafkaHeaders.RECEIVED_MESSAGE_KEY) String key) {
			this.received = foo;
			this.key = key;
			this.latch.countDown();
		}

	}

}

@@ -21,9 +21,9 @@
 -201
-7
+8
  the
@@ -1401,29 +1401,24 @@
 st.rule.
-Kafka
 Embedded
 ;%0Aimport
@@ -1409,16 +1409,25 @@
 Embedded
+KafkaRule
 ;%0Aimport
@@ -1853,29 +1853,24 @@
 c final 
-Kafka
 Embedded
  kafkaEm
@@ -1865,42 +1865,46 @@
 dded
- k
+K
 afka
-E
+Rule e
 mbedded
+Kafka
  = new 
-Kafka
 Embedded
 (1, 
@@ -1899,16 +1899,25 @@
 Embedded
+KafkaRule
 (1, true
@@ -2255,21 +2255,19 @@
 %22 + 
-kafka
+get
 Embedded
 .get
@@ -2262,20 +2262,21 @@
 Embedded
-.get
+Kafka
 BrokersA
@@ -3546,16 +3546,16 @@
 resh();%0A
-
 %09%09return
@@ -3575,24 +3575,144 @@
 ontext;%0A%09%7D%0A%0A
+%09private String getEmbeddedKafkaBrokersAsString() %7B%0A%09%09return embeddedKafka.getEmbeddedKafka().getBrokersAsString();%0A%09%7D%0A%0A
 %09public stat

Fix deprecation in KafkaAutoConfigurationIntegrationTests