aliehsaeedii commented on code in PR #22196:
URL: https://github.com/apache/kafka/pull/22196#discussion_r3558037377


##########
streams/src/test/java/org/apache/kafka/streams/state/TimestampedKeyValueStoreWithHeadersContractTest.java:
##########
@@ -0,0 +1,199 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.kafka.streams.state;
+
+import org.apache.kafka.common.header.Headers;
+import org.apache.kafka.common.header.internals.RecordHeader;
+import org.apache.kafka.common.header.internals.RecordHeaders;
+import org.apache.kafka.common.serialization.Serdes;
+import org.apache.kafka.streams.KeyValue;
+import org.apache.kafka.streams.StreamsConfig;
+import org.apache.kafka.test.InternalMockProcessorContext;
+import org.apache.kafka.test.StreamsTestUtils;
+import org.apache.kafka.test.TestUtils;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Contract tests for {@link TimestampedKeyValueStoreWithHeaders}.
+ */
+public class TimestampedKeyValueStoreWithHeadersContractTest {
+
+    private TimestampedKeyValueStoreWithHeaders<String, String> store;
+    private InternalMockProcessorContext<String, String> context;
+
+    @BeforeEach
+    public void setUp() {
+        final File dir = TestUtils.tempDirectory();
+        final Properties props = StreamsTestUtils.getStreamsConfig();
+        context = new InternalMockProcessorContext<>(
+            dir,
+            Serdes.String(),
+            Serdes.String(),
+            new StreamsConfig(props)
+        );
+        store = Stores.timestampedKeyValueStoreWithHeadersBuilder(
+            Stores.inMemoryKeyValueStore("contract-store"),
+            Serdes.String(),
+            Serdes.String()
+        ).withLoggingDisabled().withCachingDisabled().build();
+        store.init(context, store);
+    }
+
+    @AfterEach
+    public void tearDown() {
+        if (store != null) {
+            store.close();
+        }
+    }
+
+    @Test
+    public void shouldRoundTripValueTimestampAndHeadersViaPutAndGet() {
+        final Headers headers = headersWith("schema-id", "42");
+        store.put("k1", ValueTimestampHeaders.make("v1", 1000L, headers));
+
+        final ValueTimestampHeaders<String> result = store.get("k1");
+        assertEquals("v1", result.value());
+        assertEquals(1000L, result.timestamp());
+        assertEquals(headers, result.headers());
+    }
+
+    @Test
+    public void shouldReturnNullForMissingKey() {
+        assertNull(store.get("missing"));
+    }
+
+    @Test
+    public void shouldTreatNullValueAsTombstone() {
+        final Headers headers = headersWith("h", "v");
+        store.put("k", ValueTimestampHeaders.make("value", 10L, headers));
+        assertEquals("value", store.get("k").value());
+
+        store.put("k", null);
+        assertNull(store.get("k"));
+    }
+
+    @Test
+    public void shouldDeleteKeyAndReturnPriorValue() {
+        final Headers headers = headersWith("h", "v");
+        store.put("k", ValueTimestampHeaders.make("value", 10L, headers));
+
+        final ValueTimestampHeaders<String> deleted = store.delete("k");
+        assertEquals("value", deleted.value());
+        assertEquals(10L, deleted.timestamp());
+        assertEquals(headers, deleted.headers());
+        assertNull(store.get("k"));
+    }
+
+    @Test
+    public void shouldPreserveHeadersAcrossRange() {
+        final Headers h1 = headersWith("id", "1");
+        final Headers h2 = headersWith("id", "2");
+        final Headers h3 = headersWith("id", "3");
+
+        store.put("a", ValueTimestampHeaders.make("va", 100L, h1));
+        store.put("b", ValueTimestampHeaders.make("vb", 200L, h2));
+        store.put("c", ValueTimestampHeaders.make("vc", 300L, h3));
+
+        final List<KeyValue<String, ValueTimestampHeaders<String>>> collected 
= new ArrayList<>();
+        try (KeyValueIterator<String, ValueTimestampHeaders<String>> it = 
store.range("a", "c")) {
+            while (it.hasNext()) {
+                collected.add(it.next());
+            }
+        }
+
+        assertEquals(3, collected.size());
+        assertEquals("a", collected.get(0).key);
+        assertEquals(h1, collected.get(0).value.headers());
+        assertEquals("b", collected.get(1).key);
+        assertEquals(h2, collected.get(1).value.headers());
+        assertEquals("c", collected.get(2).key);
+        assertEquals(h3, collected.get(2).value.headers());
+    }
+
+    @Test
+    public void shouldPreserveHeadersAcrossReverseRange() {
+        final Headers h1 = headersWith("id", "1");
+        final Headers h2 = headersWith("id", "2");
+
+        store.put("a", ValueTimestampHeaders.make("va", 100L, h1));
+        store.put("b", ValueTimestampHeaders.make("vb", 200L, h2));
+
+        final List<KeyValue<String, ValueTimestampHeaders<String>>> collected 
= new ArrayList<>();
+        try (KeyValueIterator<String, ValueTimestampHeaders<String>> it = 
store.reverseRange("a", "b")) {
+            while (it.hasNext()) {
+                collected.add(it.next());
+            }
+        }
+
+        assertEquals(2, collected.size());
+        assertEquals("b", collected.get(0).key);
+        assertEquals(h2, collected.get(0).value.headers());
+        assertEquals("a", collected.get(1).key);
+        assertEquals(h1, collected.get(1).value.headers());
+    }
+
+    @Test
+    public void shouldReturnEmptyIteratorWhenStoreIsEmpty() {
+        try (KeyValueIterator<String, ValueTimestampHeaders<String>> it = 
store.all()) {
+            assertFalse(it.hasNext());
+        }
+    }
+
+    @Test
+    public void shouldPutIfAbsentAndNotOverwriteExisting() {
+        final Headers first = headersWith("h", "first");
+        final Headers second = headersWith("h", "second");
+
+        assertNull(store.putIfAbsent("k", ValueTimestampHeaders.make("v1", 1L, 
first)));
+
+        final ValueTimestampHeaders<String> previous =
+            store.putIfAbsent("k", ValueTimestampHeaders.make("v2", 2L, 
second));
+        assertEquals("v1", previous.value());
+        assertEquals(first, previous.headers());
+        assertEquals("v1", store.get("k").value());
+        assertEquals(first, store.get("k").headers());
+    }
+
+    @Test
+    public void shouldPreserveEmptyHeaders() {
+        store.put("k", ValueTimestampHeaders.make("v", 10L, new 
RecordHeaders()));
+
+        final ValueTimestampHeaders<String> result = store.get("k");
+        assertEquals("v", result.value());
+        assertEquals(new RecordHeaders(), result.headers());
+        assertTrue(result.headers().toArray().length == 0);

Review Comment:
   As above.



##########
streams/src/test/java/org/apache/kafka/streams/state/SessionStoreWithHeadersContractTest.java:
##########
@@ -0,0 +1,223 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.kafka.streams.state;
+
+import org.apache.kafka.common.header.Headers;
+import org.apache.kafka.common.header.internals.RecordHeader;
+import org.apache.kafka.common.header.internals.RecordHeaders;
+import org.apache.kafka.common.serialization.Serdes;
+import org.apache.kafka.streams.KeyValue;
+import org.apache.kafka.streams.StreamsConfig;
+import org.apache.kafka.streams.kstream.Windowed;
+import org.apache.kafka.streams.kstream.internals.SessionWindow;
+import org.apache.kafka.test.InternalMockProcessorContext;
+import org.apache.kafka.test.StreamsTestUtils;
+import org.apache.kafka.test.TestUtils;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.File;
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Contract tests for {@link SessionStoreWithHeaders}.
+ */
+public class SessionStoreWithHeadersContractTest {
+
+    private static final long RETENTION = 10_000L;
+
+    private SessionStoreWithHeaders<String, String> store;
+    private InternalMockProcessorContext<String, String> context;
+
+    @BeforeEach
+    public void setUp() {
+        final File dir = TestUtils.tempDirectory();
+        final Properties props = StreamsTestUtils.getStreamsConfig();
+        context = new InternalMockProcessorContext<>(
+            dir,
+            Serdes.String(),
+            Serdes.String(),
+            new StreamsConfig(props)
+        );
+        store = Stores.sessionStoreWithHeadersBuilder(
+            Stores.inMemorySessionStore("contract-session-store", 
Duration.ofMillis(RETENTION)),
+            Serdes.String(),
+            Serdes.String()
+        ).withLoggingDisabled().withCachingDisabled().build();
+        store.init(context, store);
+    }
+
+    @AfterEach
+    public void tearDown() {
+        if (store != null) {
+            store.close();
+        }
+    }
+
+    @Test
+    public void shouldRoundTripAggregationAndHeadersViaPutAndFetchSession() {
+        final Headers headers = headersWith("schema-id", "42");
+        final Windowed<String> key = windowed("k", 100L, 200L);
+
+        store.put(key, AggregationWithHeaders.make("agg", headers));
+
+        final AggregationWithHeaders<String> result = store.fetchSession("k", 
100L, 200L);
+        assertEquals("agg", result.aggregation());
+        assertEquals(headers, result.headers());
+    }
+
+    @Test
+    public void shouldReturnNullForMissingSession() {
+        assertNull(store.fetchSession("missing", 0L, 10L));
+    }
+
+    @Test
+    public void shouldTreatNullAggregationAsTombstone() {
+        final Headers headers = headersWith("h", "v");
+        final Windowed<String> key = windowed("k", 100L, 200L);
+
+        store.put(key, AggregationWithHeaders.make("agg", headers));
+        assertEquals("agg", store.fetchSession("k", 100L, 200L).aggregation());
+
+        store.put(key, null);
+        assertNull(store.fetchSession("k", 100L, 200L));
+    }
+
+    @Test
+    public void shouldRemoveSessionByWindowedKey() {
+        final Headers headers = headersWith("h", "v");
+        final Windowed<String> key = windowed("k", 100L, 200L);
+
+        store.put(key, AggregationWithHeaders.make("agg", headers));
+        store.remove(key);
+
+        assertNull(store.fetchSession("k", 100L, 200L));
+    }
+
+    @Test
+    public void shouldPreserveHeadersAcrossFetchByKey() {
+        final Headers h1 = headersWith("id", "1");
+        final Headers h2 = headersWith("id", "2");
+        final Headers h3 = headersWith("id", "3");
+
+        store.put(windowed("k", 100L, 150L), AggregationWithHeaders.make("a1", 
h1));
+        store.put(windowed("k", 200L, 250L), AggregationWithHeaders.make("a2", 
h2));
+        store.put(windowed("k", 300L, 350L), AggregationWithHeaders.make("a3", 
h3));
+
+        final List<KeyValue<Long, Headers>> collected = new ArrayList<>();
+        try (KeyValueIterator<Windowed<String>, 
AggregationWithHeaders<String>> it = store.fetch("k")) {
+            while (it.hasNext()) {
+                final KeyValue<Windowed<String>, 
AggregationWithHeaders<String>> next = it.next();
+                collected.add(KeyValue.pair(next.key.window().start(), 
next.value.headers()));
+            }
+        }
+
+        assertEquals(3, collected.size());
+        assertEquals(100L, collected.get(0).key.longValue());
+        assertEquals(h1, collected.get(0).value);
+        assertEquals(200L, collected.get(1).key.longValue());
+        assertEquals(h2, collected.get(1).value);
+        assertEquals(300L, collected.get(2).key.longValue());
+        assertEquals(h3, collected.get(2).value);
+    }
+
+    @Test
+    public void shouldPreserveHeadersAcrossFindSessions() {
+        final Headers h1 = headersWith("id", "early");
+        final Headers h2 = headersWith("id", "late");
+
+        store.put(windowed("k", 100L, 150L), AggregationWithHeaders.make("a1", 
h1));
+        store.put(windowed("k", 200L, 250L), AggregationWithHeaders.make("a2", 
h2));
+
+        final List<KeyValue<Long, Headers>> collected = new ArrayList<>();
+        try (KeyValueIterator<Windowed<String>, 
AggregationWithHeaders<String>> it =
+                 store.findSessions("k", 100L, 250L)) {
+            while (it.hasNext()) {
+                final KeyValue<Windowed<String>, 
AggregationWithHeaders<String>> next = it.next();
+                collected.add(KeyValue.pair(next.key.window().start(), 
next.value.headers()));
+            }
+        }
+
+        assertEquals(2, collected.size());
+        assertEquals(100L, collected.get(0).key.longValue());
+        assertEquals(h1, collected.get(0).value);
+        assertEquals(200L, collected.get(1).key.longValue());
+        assertEquals(h2, collected.get(1).value);
+    }
+
+    @Test
+    public void shouldPreserveHeadersAcrossBackwardFindSessions() {
+        final Headers h1 = headersWith("id", "early");
+        final Headers h2 = headersWith("id", "late");
+
+        store.put(windowed("k", 100L, 150L), AggregationWithHeaders.make("a1", 
h1));
+        store.put(windowed("k", 200L, 250L), AggregationWithHeaders.make("a2", 
h2));
+
+        final List<KeyValue<Long, Headers>> collected = new ArrayList<>();
+        try (KeyValueIterator<Windowed<String>, 
AggregationWithHeaders<String>> it =
+                 store.backwardFindSessions("k", 100L, 250L)) {
+            while (it.hasNext()) {
+                final KeyValue<Windowed<String>, 
AggregationWithHeaders<String>> next = it.next();
+                collected.add(KeyValue.pair(next.key.window().start(), 
next.value.headers()));
+            }
+        }
+
+        assertEquals(2, collected.size());
+        assertEquals(200L, collected.get(0).key.longValue());
+        assertEquals(h2, collected.get(0).value);
+        assertEquals(100L, collected.get(1).key.longValue());
+        assertEquals(h1, collected.get(1).value);
+    }
+
+    @Test
+    public void shouldReturnEmptyIteratorForMissingKey() {
+        try (KeyValueIterator<Windowed<String>, 
AggregationWithHeaders<String>> it = store.fetch("missing")) {
+            assertFalse(it.hasNext());
+        }
+    }
+
+    @Test
+    public void shouldPreserveEmptyHeaders() {
+        final Windowed<String> key = windowed("k", 100L, 200L);
+        store.put(key, AggregationWithHeaders.make("agg", new 
RecordHeaders()));
+
+        final AggregationWithHeaders<String> result = store.fetchSession("k", 
100L, 200L);
+        assertEquals("agg", result.aggregation());
+        assertEquals(new RecordHeaders(), result.headers());
+        assertTrue(result.headers().toArray().length == 0);

Review Comment:
   Use `assertEquals(0, result.headers().toArray().length)` here — `assertTrue` 
on an equality gives a worse failure message if it breaks.



##########
streams/src/test/java/org/apache/kafka/streams/state/internals/PlainToHeadersWindowStoreIteratorAdapterTest.java:
##########
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.kafka.streams.state.internals;
+
+import org.apache.kafka.streams.KeyValue;
+import org.apache.kafka.streams.state.KeyValueIterator;
+import org.apache.kafka.streams.state.WindowStoreIterator;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.mockito.junit.jupiter.MockitoSettings;
+import org.mockito.quality.Strictness;
+
+import static 
org.apache.kafka.streams.state.HeadersBytesStore.convertFromPlainToHeaderFormat;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+@MockitoSettings(strictness = Strictness.STRICT_STUBS)
+public class PlainToHeadersWindowStoreIteratorAdapterTest {
+
+    private static final byte[] PLAIN_VALUE = "value".getBytes();
+    private static final byte[] VALUE_WITH_EMPTY_HEADERS_AND_TS =
+        convertFromPlainToHeaderFormat(PLAIN_VALUE);
+
+    @Mock
+    private WindowStoreIterator<byte[]> innerIterator;
+
+    @Test
+    public void shouldImplementWindowStoreIteratorInterface() {
+        final PlainToHeadersWindowStoreIteratorAdapter adapter =
+            new PlainToHeadersWindowStoreIteratorAdapter(innerIterator);
+
+        assertInstanceOf(WindowStoreIterator.class, adapter);
+        assertInstanceOf(KeyValueIterator.class, adapter);
+        assertInstanceOf(PlainToHeadersIteratorAdapter.class, adapter);
+    }
+
+    @Test
+    public void shouldPrependEmptyHeadersAndSentinelTimestampOnNext() {
+        when(innerIterator.hasNext()).thenReturn(true);
+        when(innerIterator.next()).thenReturn(KeyValue.pair(42L, PLAIN_VALUE));
+
+        final PlainToHeadersWindowStoreIteratorAdapter adapter =
+            new PlainToHeadersWindowStoreIteratorAdapter(innerIterator);
+
+        assertTrue(adapter.hasNext());
+        final KeyValue<Long, byte[]> result = adapter.next();
+        assertEquals(42L, result.key.longValue());
+        assertArrayEquals(VALUE_WITH_EMPTY_HEADERS_AND_TS, result.value);
+    }
+
+    @Test
+    public void shouldReturnNullValueWhenInnerValueIsNull() {
+        when(innerIterator.next()).thenReturn(KeyValue.pair(42L, null));

Review Comment:
   This covers a null inner value. The adapter also has a `plainKeyValue == 
null` branch (whole KeyValue null) that returns null — that path is still 
untested.



##########
streams/src/test/java/org/apache/kafka/streams/state/internals/TimestampedToHeadersStoreAdapterTest.java:
##########
@@ -0,0 +1,367 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.kafka.streams.state.internals;
+
+import org.apache.kafka.common.utils.Bytes;
+import org.apache.kafka.streams.KeyValue;
+import org.apache.kafka.streams.query.KeyQuery;
+import org.apache.kafka.streams.query.PositionBound;
+import org.apache.kafka.streams.query.QueryConfig;
+import org.apache.kafka.streams.query.QueryResult;
+import org.apache.kafka.streams.query.RangeQuery;
+import org.apache.kafka.streams.state.KeyValueIterator;
+import org.apache.kafka.streams.state.KeyValueStore;
+import org.apache.kafka.streams.state.TimestampedBytesStore;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.mockito.junit.jupiter.MockitoSettings;
+import org.mockito.quality.Strictness;
+
+import java.util.Arrays;
+
+import static 
org.apache.kafka.streams.state.HeadersBytesStore.convertToHeaderFormat;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.withSettings;
+
+@ExtendWith(MockitoExtension.class)
+@MockitoSettings(strictness = Strictness.STRICT_STUBS)
+public class TimestampedToHeadersStoreAdapterTest {
+
+    @Mock
+    private KeyValueIterator<Bytes, byte[]> mockIterator;
+
+    @SuppressWarnings("unchecked")
+    private KeyValueStore<Bytes, byte[]> mockStore;
+
+    private TimestampedToHeadersStoreAdapter adapter;
+
+    @SuppressWarnings("unchecked")
+    @BeforeEach
+    public void setUp() {
+        mockStore = mock(KeyValueStore.class, 
withSettings().extraInterfaces(TimestampedBytesStore.class));
+        when(mockStore.persistent()).thenReturn(true);
+        adapter = new TimestampedToHeadersStoreAdapter(mockStore);
+    }
+
+    @Test
+    @SuppressWarnings("unchecked")
+    public void shouldThrowIfStoreIsNotPersistent() {
+        final KeyValueStore<Bytes, byte[]> nonPersistentStore =
+            mock(KeyValueStore.class, 
withSettings().extraInterfaces(TimestampedBytesStore.class));
+        when(nonPersistentStore.persistent()).thenReturn(false);
+
+        final IllegalArgumentException exception = assertThrows(
+            IllegalArgumentException.class,
+            () -> new TimestampedToHeadersStoreAdapter(nonPersistentStore)
+        );
+
+        assertTrue(exception.getMessage().contains("Provided store must be a 
persistent store"));
+    }
+
+    @Test
+    @SuppressWarnings("unchecked")
+    public void shouldThrowIfStoreIsNotTimestamped() {
+        final KeyValueStore<Bytes, byte[]> nonTimestampedStore = 
mock(KeyValueStore.class);
+        when(nonTimestampedStore.persistent()).thenReturn(true);
+
+        final IllegalArgumentException exception = assertThrows(
+            IllegalArgumentException.class,
+            () -> new TimestampedToHeadersStoreAdapter(nonTimestampedStore)
+        );
+
+        assertTrue(exception.getMessage().contains("Provided store must be a 
timestamped store"));
+    }
+
+    @Test
+    public void shouldPutRawTimestampedValueToStore() {
+        final Bytes key = new Bytes("key".getBytes());
+        final byte[] rawTimestampedValue =
+            new byte[] {0, 0, 0, 0, 0, 0, 0, 42, 'v', 'a', 'l'};
+        final byte[] valueWithHeaders = 
convertToHeaderFormat(rawTimestampedValue);
+
+        adapter.put(key, valueWithHeaders);
+
+        verify(mockStore).put(eq(key), eq(rawTimestampedValue));
+    }
+
+    @Test
+    public void shouldGetAndConvertToHeaderFormat() {
+        final Bytes key = new Bytes("key".getBytes());
+        final byte[] rawTimestampedValue =
+            new byte[] {0, 0, 0, 0, 0, 0, 0, 42, 'v', 'a', 'l'};
+        when(mockStore.get(key)).thenReturn(rawTimestampedValue);
+
+        final byte[] result = adapter.get(key);
+
+        assertArrayEquals(convertToHeaderFormat(rawTimestampedValue), result);
+    }
+
+    @Test
+    public void shouldReturnNullWhenStoreReturnsNull() {
+        final Bytes key = new Bytes("key".getBytes());
+        when(mockStore.get(key)).thenReturn(null);
+
+        assertNull(adapter.get(key));
+    }
+
+    @Test
+    public void shouldPutIfAbsentAndConvertResult() {
+        final Bytes key = new Bytes("key".getBytes());
+        final byte[] rawTimestampedValue =
+            new byte[] {0, 0, 0, 0, 0, 0, 0, 42, 'v', 'a', 'l'};
+        final byte[] valueWithHeaders = 
convertToHeaderFormat(rawTimestampedValue);
+        final byte[] oldRawValue =
+            new byte[] {0, 0, 0, 0, 0, 0, 0, 10, 'o', 'l', 'd'};
+        when(mockStore.putIfAbsent(eq(key), 
eq(rawTimestampedValue))).thenReturn(oldRawValue);
+
+        final byte[] result = adapter.putIfAbsent(key, valueWithHeaders);
+
+        assertArrayEquals(convertToHeaderFormat(oldRawValue), result);
+    }
+
+    @Test
+    public void shouldDeleteAndConvertResult() {
+        final Bytes key = new Bytes("key".getBytes());
+        final byte[] oldRawValue =
+            new byte[] {0, 0, 0, 0, 0, 0, 0, 10, 'o', 'l', 'd'};
+        when(mockStore.delete(key)).thenReturn(oldRawValue);
+
+        final byte[] result = adapter.delete(key);
+
+        assertArrayEquals(convertToHeaderFormat(oldRawValue), result);
+    }
+
+    @Test
+    public void shouldPutAllEntries() {
+        final Bytes key1 = new Bytes("key1".getBytes());
+        final Bytes key2 = new Bytes("key2".getBytes());
+        final byte[] rawValue1 =
+            new byte[] {0, 0, 0, 0, 0, 0, 0, 1, 'v', '1'};
+        final byte[] rawValue2 =
+            new byte[] {0, 0, 0, 0, 0, 0, 0, 2, 'v', '2'};
+        final byte[] value1 = convertToHeaderFormat(rawValue1);
+        final byte[] value2 = convertToHeaderFormat(rawValue2);
+
+        adapter.putAll(Arrays.asList(
+            KeyValue.pair(key1, value1),
+            KeyValue.pair(key2, value2)
+        ));
+
+        verify(mockStore).put(eq(key1), eq(rawValue1));
+        verify(mockStore).put(eq(key2), eq(rawValue2));
+    }
+
+    @Test
+    public void shouldWrapRangeIterator() {
+        final Bytes from = new Bytes("a".getBytes());
+        final Bytes to = new Bytes("z".getBytes());
+        when(mockStore.range(from, to)).thenReturn(mockIterator);
+
+        final KeyValueIterator<Bytes, byte[]> result = adapter.range(from, to);
+
+        assertNotNull(result);
+        assertInstanceOf(TimestampedToHeadersIteratorAdapter.class, result);
+    }
+
+    @Test
+    public void shouldWrapReverseRangeIterator() {
+        final Bytes from = new Bytes("a".getBytes());
+        final Bytes to = new Bytes("z".getBytes());
+        when(mockStore.reverseRange(from, to)).thenReturn(mockIterator);
+
+        final KeyValueIterator<Bytes, byte[]> result = 
adapter.reverseRange(from, to);
+
+        assertNotNull(result);
+        assertInstanceOf(TimestampedToHeadersIteratorAdapter.class, result);
+    }
+
+    @Test
+    public void shouldWrapAllIterator() {
+        when(mockStore.all()).thenReturn(mockIterator);
+
+        final KeyValueIterator<Bytes, byte[]> result = adapter.all();
+
+        assertNotNull(result);
+        assertInstanceOf(TimestampedToHeadersIteratorAdapter.class, result);
+    }
+
+    @Test
+    public void shouldWrapReverseAllIterator() {
+        when(mockStore.reverseAll()).thenReturn(mockIterator);
+
+        final KeyValueIterator<Bytes, byte[]> result = adapter.reverseAll();
+
+        assertNotNull(result);
+        assertInstanceOf(TimestampedToHeadersIteratorAdapter.class, result);
+    }
+
+    @Test
+    public void shouldWrapPrefixScanIterator() {
+        when(mockStore.prefixScan(any(), any())).thenReturn(mockIterator);
+
+        final KeyValueIterator<Bytes, byte[]> result =
+            adapter.prefixScan("prefix", (topic, data) -> data.getBytes());
+
+        assertNotNull(result);
+        assertInstanceOf(TimestampedToHeadersIteratorAdapter.class, result);
+    }
+
+    @Test
+    public void shouldHandleKeyQuery() {
+        final Bytes key = new Bytes("test-key".getBytes());
+        final byte[] rawTimestampedValue =
+            new byte[] {0, 0, 0, 0, 0, 0, 0, 42, 'v', 'a', 'l'};
+        final KeyQuery<Bytes, byte[]> query = KeyQuery.withKey(key);
+
+        final QueryResult<byte[]> mockResult = 
QueryResult.forResult(rawTimestampedValue);
+        when(mockStore.query(eq(query), any(PositionBound.class), 
any(QueryConfig.class)))
+            .thenReturn(mockResult);
+
+        final QueryResult<byte[]> result =
+            adapter.query(query, PositionBound.unbounded(), new 
QueryConfig(false));
+
+        assertTrue(result.isSuccess());
+        assertArrayEquals(convertToHeaderFormat(rawTimestampedValue), 
result.getResult());
+    }
+
+    @Test
+    public void shouldHandleKeyQueryWithNullResult() {
+        final Bytes key = new Bytes("test-key".getBytes());
+        final KeyQuery<Bytes, byte[]> query = KeyQuery.withKey(key);
+
+        final QueryResult<byte[]> mockResult = QueryResult.forResult(null);
+        when(mockStore.query(eq(query), any(PositionBound.class), 
any(QueryConfig.class)))
+            .thenReturn(mockResult);
+
+        final QueryResult<byte[]> result =
+            adapter.query(query, PositionBound.unbounded(), new 
QueryConfig(false));
+
+        assertTrue(result.isSuccess());
+        assertNull(result.getResult());
+    }
+
+    @Test
+    public void shouldHandleFailedKeyQuery() {
+        final Bytes key = new Bytes("test-key".getBytes());
+        final KeyQuery<Bytes, byte[]> query = KeyQuery.withKey(key);
+
+        final QueryResult<byte[]> mockResult = 
QueryResult.forUnknownQueryType(query, mockStore);
+        when(mockStore.query(eq(query), any(PositionBound.class), 
any(QueryConfig.class)))
+            .thenReturn(mockResult);
+
+        final QueryResult<byte[]> result =
+            adapter.query(query, PositionBound.unbounded(), new 
QueryConfig(false));
+
+        assertFalse(result.isSuccess());
+    }
+
+    @Test
+    public void shouldHandleRangeQuery() {

Review Comment:
   The `else` branch of `query()` (delegating query types other than 
KeyQuery/RangeQuery straight to the store) has no test. Worth adding one.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to