/*
 * Copyright 2016-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.cloud.stream.binder.kafka.integration;

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

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Processor;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.KafkaNull;
import org.springframework.kafka.test.rule.EmbeddedKafkaRule;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;

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

/**
 * @author Aldo Sinanaj
 * @author Gary Russell
 */
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE,
	properties = "spring.cloud.stream.binding.input.destination=output")
@EnableBinding(Processor.class)
@DirtiesContext
public class KafkaNullConverterTest {

	private static final String KAFKA_BROKERS_PROPERTY = "spring.kafka.bootstrap-servers";

	@Autowired
	private MessageChannel output;

	@Autowired
	private MessageChannel input;

	@Autowired
	private KafkaNullConverterTestConfig config;

	@ClassRule
	public static EmbeddedKafkaRule kafkaEmbedded = new EmbeddedKafkaRule(1, true);

	@BeforeClass
	public static void setup() {
		System.setProperty(KAFKA_BROKERS_PROPERTY,
				kafkaEmbedded.getEmbeddedKafka().getBrokersAsString());
	}

	@AfterClass
	public static void clean() {
		System.clearProperty(KAFKA_BROKERS_PROPERTY);
	}

	@Test
	public void testKafkaNullConverterOutput() throws InterruptedException {
		this.output.send(new GenericMessage<>(KafkaNull.INSTANCE));

		assertThat(this.config.countDownLatchOutput.await(10, TimeUnit.SECONDS)).isTrue();
		assertThat(this.config.outputPayload).isNull();
	}

	@Test
	public void testKafkaNullConverterInput() throws InterruptedException {
		this.input.send(new GenericMessage<>(KafkaNull.INSTANCE));

		assertThat(this.config.countDownLatchInput.await(10, TimeUnit.SECONDS)).isTrue();
		assertThat(this.config.inputPayload).isNull();
	}

	@TestConfiguration
	public static class KafkaNullConverterTestConfig {

		final CountDownLatch countDownLatchOutput = new CountDownLatch(1);

		final CountDownLatch countDownLatchInput = new CountDownLatch(1);

		volatile byte[] outputPayload = new byte[0];

		volatile byte[] inputPayload = new byte[0];

		@KafkaListener(id = "foo", topics = "output")
		public void listen(@Payload(required = false) byte[] in) {
			this.outputPayload = in;
			countDownLatchOutput.countDown();
		}

		@StreamListener(Processor.INPUT)
		public void inputListen(@Payload(required = false) byte[] payload) {
			this.inputPayload = payload;
			countDownLatchInput.countDown();
		}

	}

}

@@ -1215,30 +1215,80 @@
 otation.
-StreamListener
+Input;%0Aimport org.springframework.cloud.stream.annotation.Output
 ;%0Aimport
@@ -1325,26 +1325,32 @@
 eam.
-messaging.Processo
+annotation.StreamListene
 r;%0Ai
@@ -2033,18 +2033,17 @@
 nt.NONE,
-%0A%09
+ 
 properti
@@ -2047,24 +2047,28 @@
 rties = 
+%7B%0A%09%09
 %22spring.
 cloud.st
@@ -2063,86 +2063,52 @@
 ing.
-cloud.stream.binding.input.destination=output%22)%0A@EnableBinding(Processor.class
+kafka.consumer.auto-offset-reset=earliest%22 %7D
 )%0A@D
@@ -2285,17 +2285,26 @@
 Channel 
-o
+kafkaNullO
 utput;%0A%0A
@@ -2339,17 +2339,26 @@
 Channel 
-i
+kafkaNullI
 nput;%0A%0A%09
@@ -2841,25 +2841,34 @@
 on %7B%0A%09%09this.
-o
+kafkaNullO
 utput.send(n
@@ -3132,25 +3132,34 @@
 on %7B%0A%09%09this.
-i
+kafkaNullI
 nput.send(ne
@@ -3357,16 +3357,61 @@
 uration%0A
+%09@EnableBinding(KafkaNullTestChannels.class)%0A
 %09public 
@@ -3728,17 +3728,26 @@
 pics = %22
-o
+kafkaNullO
 utput%22)%0A
@@ -3899,23 +3899,24 @@
 ner(
-Processor.INPUT
+%22kafkaNullInput%22
 )%0A%09%09
@@ -4061,10 +4061,149 @@
 %09%7D%0A%0A%09%7D%0A%0A
+%09public interface KafkaNullTestChannels %7B%0A%0A%09%09@Input%0A%09%09MessageChannel kafkaNullInput();%0A%0A%09%09@Output%0A%09%09MessageChannel kafkaNullOutput();%0A%0A%09%7D%0A%0A
 %7D%0A

Fix test