/*
 * Copyright 2018-2019 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
 *
 *      https://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;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.apache.kafka.clients.producer.Callback;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.TopicPartition;
import org.junit.ClassRule;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.InOrder;

import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.cloud.stream.binder.ExtendedProducerProperties;
import org.springframework.cloud.stream.binder.kafka.properties.KafkaBinderConfigurationProperties;
import org.springframework.cloud.stream.binder.kafka.properties.KafkaProducerProperties;
import org.springframework.cloud.stream.binder.kafka.provisioning.KafkaTopicProvisioner;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.test.rule.EmbeddedKafkaRule;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.retry.support.RetryTemplate;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.willReturn;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;

/**
 * @author Gary Russell
 * @since 2.0
 *
 */
public class KafkaTransactionTests {

	private static Map<String, String> brokerProperties = new HashMap<>();

	static {
		brokerProperties.put("transaction.state.log.replication.factor", "1");
		brokerProperties.put("transaction.state.log.min.isr", "1");
	}

	@ClassRule
	public static final EmbeddedKafkaRule embeddedKafka = new EmbeddedKafkaRule(1).brokerProperties(brokerProperties);

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Test
	@Ignore
	public void testProducerRunsInTx() {
		KafkaProperties kafkaProperties = new TestKafkaProperties();
		kafkaProperties.setBootstrapServers(Collections
				.singletonList(embeddedKafka.getEmbeddedKafka().getBrokersAsString()));
		KafkaBinderConfigurationProperties configurationProperties = new KafkaBinderConfigurationProperties(
				kafkaProperties);
		configurationProperties.getTransaction().setTransactionIdPrefix("foo-");
		configurationProperties.getTransaction().getProducer().setUseNativeEncoding(true);
		KafkaTopicProvisioner provisioningProvider = new KafkaTopicProvisioner(
				configurationProperties, kafkaProperties);
		provisioningProvider.setMetadataRetryOperations(new RetryTemplate());
		final Producer mockProducer = mock(Producer.class);

		KafkaProducerProperties extension1 = configurationProperties
				.getTransaction().getProducer().getExtension();
		extension1.getConfiguration().put(ProducerConfig.RETRIES_CONFIG, "1");
		extension1.getConfiguration().put(ProducerConfig.ACKS_CONFIG, "all");

		willReturn(Collections.singletonList(new TopicPartition("foo", 0)))
				.given(mockProducer).partitionsFor(anyString());
		KafkaMessageChannelBinder binder = new KafkaMessageChannelBinder(
				configurationProperties, provisioningProvider) {

			@Override
			protected DefaultKafkaProducerFactory<byte[], byte[]> getProducerFactory(
					String transactionIdPrefix,
					ExtendedProducerProperties<KafkaProducerProperties> producerProperties) {
				DefaultKafkaProducerFactory<byte[], byte[]> producerFactory = spy(
						super.getProducerFactory(transactionIdPrefix,
								producerProperties));
				willReturn(mockProducer).given(producerFactory).createProducer("foo-");
				return producerFactory;
			}

		};
		GenericApplicationContext applicationContext = new GenericApplicationContext();
		applicationContext.refresh();
		binder.setApplicationContext(applicationContext);
		DirectChannel channel = new DirectChannel();
		KafkaProducerProperties extension = new KafkaProducerProperties();
		ExtendedProducerProperties<KafkaProducerProperties> properties = new ExtendedProducerProperties<>(
				extension);
		binder.bindProducer("foo", channel, properties);
		channel.send(new GenericMessage<>("foo".getBytes()));
		InOrder inOrder = inOrder(mockProducer);
		inOrder.verify(mockProducer).beginTransaction();
		inOrder.verify(mockProducer).send(any(ProducerRecord.class), any(Callback.class));
		inOrder.verify(mockProducer).commitTransaction();
		inOrder.verify(mockProducer).close();
		inOrder.verifyNoMoreInteractions();
		assertThat(TestUtils.getPropertyValue(channel,
				"dispatcher.theOneHandler.useNativeEncoding", Boolean.class)).isTrue();
	}

}

@@ -1046,33 +1046,8 @@
 le;%0A
-import org.junit.Ignore;%0A
 impo
@@ -2784,17 +2784,8 @@
 est%0A
-%09@Ignore%0A
 %09pub
@@ -5290,16 +5290,21 @@
 ).close(
+any()
 );%0A%09%09inO

Fix mock producer.close() for latest SK snapshot