Repository: apache/kafka
File: streams/src/test/java/org/apache/kafka/streams/kstream/internals/AbstractStreamTest.java (Java)

Code before:
import org.apache.kafka.streams.TopologyTestDriver;
import org.apache.kafka.streams.kstream.Consumed;
import org.apache.kafka.streams.kstream.KStream;
import org.apache.kafka.streams.kstream.ValueTransformerSupplier;
import org.apache.kafka.streams.kstream.ValueTransformerWithKeySupplier;
import org.apache.kafka.streams.kstream.internals.graph.ProcessorGraphNode;
import org.apache.kafka.streams.kstream.internals.graph.ProcessorParameters;
import org.junit.Test;

import java.util.Random;

import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertTrue;

public class AbstractStreamTest {

    @Test
    public void testToInternalValueTransformerSupplierSuppliesNewTransformers() {
        final ValueTransformerSupplier<?, ?> valueTransformerSupplier = createMock(ValueTransformerSupplier.class);
        expect(valueTransformerSupplier.get()).andAnswer(NoopValueTransformer::new).atLeastOnce();
        replay(valueTransformerSupplier);
        final ValueTransformerWithKeySupplier<?, ?, ?> valueTransformerWithKeySupplier =
            AbstractStream.toValueTransformerWithKeySupplier(valueTransformerSupplier);
        valueTransformerWithKeySupplier.get();
        valueTransformerWithKeySupplier.get();
        valueTransformerWithKeySupplier.get();
        verify(valueTransformerSupplier);
    }

    @Test
    public void testToInternalValueTransformerWithKeySupplierSuppliesNewTransformers() {
        final ValueTransformerWithKeySupplier<?, ?, ?> valueTransformerWithKeySupplier =
            createMock(ValueTransformerWithKeySupplier.class);
        expect(valueTransformerWithKeySupplier.get()).andAnswer(NoopValueTransformerWithKey::new).atLeastOnce();
        replay(valueTransformerWithKeySupplier);
        valueTransformerWithKeySupplier.get();
        valueTransformerWithKeySupplier.get();
        valueTransformerWithKeySupplier.get();
        verify(valueTransformerWithKeySupplier);
    }

    @Test
    public void testShouldBeExtensible() {
        final StreamsBuilder builder = new StreamsBuilder();
        final int[] expectedKeys = new int[]{1, 2, 3, 4, 5, 6, 7};
        final MockApiProcessorSupplier<Integer, String, Void, Void> supplier = new MockApiProcessorSupplier<>();
        final String topicName = "topic";


Review comment (refactor):
These two separate objects are needed because otherwise I get:
```
org.apache.kafka.streams.kstream.ValueTransformerSupplier$MockitoMock$ktegTSa6 generates single reference. org.apache.kafka.streams.kstream.ValueTransformerSupplier$MockitoMock$ktegTSa6#get() must return a new object each time it is called.
java.lang.IllegalArgumentException: org.apache.kafka.streams.kstream.ValueTransformerSupplier$MockitoMock$ktegTSa6 generates single reference. org.apache.kafka.streams.kstream.ValueTransformerSupplier$MockitoMock$ktegTSa6#get() must return a new object each time it is called.
	at org.apache.kafka.streams.internals.ApiUtils.checkSupplier(ApiUtils.java:99)
	at org.apache.kafka.streams.kstream.internals.AbstractStream.toValueTransformerWithKeySupplier(AbstractStream.java:117)
	at org.apache.kafka.streams.kstream.internals.AbstractStreamTest.testToInternalValueTransformerSupplierSuppliesNewTransformers(AbstractStreamTest.java:57)
```
since the `get` method is called twice in
```
    public static <VR, V> void checkSupplier(final ValueTransformerSupplier<V, VR> supplier) {
        if (supplier.get() == supplier.get()) { // <-- TWO INVOCATIONS HERE
            final String supplierClass = supplier.getClass().getName();
            throw new IllegalArgumentException(String.format("%s generates single reference." +
                    " %s#get() must return a new object each time it is called.", supplierClass, supplierClass));
        }
    }
```

Code after:
import org.apache.kafka.streams.TopologyTestDriver;
import org.apache.kafka.streams.kstream.Consumed;
import org.apache.kafka.streams.kstream.KStream;
import org.apache.kafka.streams.kstream.ValueTransformerSupplier;
import org.apache.kafka.streams.kstream.ValueTransformerWithKeySupplier;
import org.apache.kafka.streams.kstream.internals.graph.ProcessorGraphNode;
import org.apache.kafka.streams.kstream.internals.graph.ProcessorParameters;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.Random;

import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class AbstractStreamTest {

    @Test
    public void testToInternalValueTransformerSupplierSuppliesNewTransformers() {
        final ValueTransformerSupplier<?, ?> valueTransformerSupplier = mock(ValueTransformerSupplier.class);
        when(valueTransformerSupplier.get())
            .thenReturn(new NoopValueTransformer<>())
            .thenReturn(new NoopValueTransformer<>());
        final ValueTransformerWithKeySupplier<?, ?, ?> valueTransformerWithKeySupplier =
            AbstractStream.toValueTransformerWithKeySupplier(valueTransformerSupplier);
        valueTransformerWithKeySupplier.get();
        valueTransformerWithKeySupplier.get();
        valueTransformerWithKeySupplier.get();
    }

    @Test
    public void testToInternalValueTransformerWithKeySupplierSuppliesNewTransformers() {
        final ValueTransformerWithKeySupplier<?, ?, ?> valueTransformerWithKeySupplier =
            mock(ValueTransformerWithKeySupplier.class);
        when(valueTransformerWithKeySupplier.get()).thenReturn(new NoopValueTransformerWithKey<>());
        valueTransformerWithKeySupplier.get();
        valueTransformerWithKeySupplier.get();
        valueTransformerWithKeySupplier.get();
    }

    @Test
    public void testShouldBeExtensible() {
        final StreamsBuilder builder = new StreamsBuilder();
        final int[] expectedKeys = new int[]{1, 2, 3, 4, 5, 6, 7};
        final MockApiProcessorSupplier<Integer, String, Void, Void> supplier = new MockApiProcessorSupplier<>();
        final String topicName = "topic";

        final ExtendedKStream<Integer, String> stream = new ExtendedKStream<>(builder.stream(topicName, Consumed.with(Serdes.Integer(), Serdes.String())));