Skip to content

Commit

Permalink
#303 fk records
Browse files Browse the repository at this point in the history
  • Loading branch information
h1alexbel committed Jun 27, 2023
1 parent 862526b commit b5de1f6
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/main/java/io/github/eocqrs/kafka/fake/FkRecords.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2023 Aliaksei Bialiauski, EO-CQRS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package io.github.eocqrs.kafka.fake;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.common.TopicPartition;
import org.cactoos.Scalar;
import org.cactoos.list.ListOf;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Fake Records.
*
* @author Aliaksei Bialiauski (abialiauski.dev@gmail.com)
* @since 0.3.5
*/
public final class FkRecords implements
Scalar<ConsumerRecords<Object, String>> {

public static final int DEFAULT_PARTITION = 0;
public static final long ZERO_OFFSET = 0L;
private final String topic;
private final Collection<String> datasets;

public FkRecords(
final String tpc,
final Collection<String> dtst
) {
this.topic = tpc;
this.datasets = dtst;
}

@Override
public ConsumerRecords<Object, String> value() throws Exception {
final Map<TopicPartition, List<ConsumerRecord<Object, String>>> part
= new HashMap<>();
final List<ConsumerRecord<Object, String>> recs = new ListOf<>();
this.datasets.forEach(
dataset -> recs.add(
new ConsumerRecord<>(
this.topic,
DEFAULT_PARTITION,
ZERO_OFFSET,
null,
dataset
)
)
);
part.put(new TopicPartition(this.topic, DEFAULT_PARTITION), recs);
return new ConsumerRecords<>(part);
}
}
70 changes: 70 additions & 0 deletions src/test/java/io/github/eocqrs/kafka/fake/FkRecordsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (c) 2023 Aliaksei Bialiauski, EO-CQRS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package io.github.eocqrs.kafka.fake;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.cactoos.list.ListOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;

import java.util.function.Consumer;

/**
* Test case for {@link FkRecords}.
*
* @author Aliaksei Bialiauski (abialiauski.dev@gmail.com)
* @since 0.3.5
*/
final class FkRecordsTest {

@Test
void readsRecordsInRightFormat() throws Exception {
final String topic = "test";
final String value = "test";
new FkRecords(topic, new ListOf<>(value))
.value()
.forEach(rec ->
MatcherAssert.assertThat(
"Records in right format",
rec.value(),
Matchers.equalTo(value)
));
}

@Test
void readsCountInRightFormat() throws Exception {
final String topic = "test";
MatcherAssert.assertThat(
"Records Count in right format",
new FkRecords(
topic,
new ListOf<>(
"first",
"second"
)
).value().count(),
Matchers.equalTo(2)
);
}
}

0 comments on commit b5de1f6

Please sign in to comment.