Skip to content

Commit

Permalink
#292 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
h1alexbel committed Jun 14, 2023
1 parent 3feccd0 commit e7c3a46
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 13 deletions.
77 changes: 77 additions & 0 deletions src/main/java/io/github/eocqrs/kafka/fake/TopicDoesNotExists.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* The MIT License (MIT)
*
* 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 java.io.IOException;
import java.io.Serial;

/**
* Topic Does Not Exists.
*
* @author Aliaksei Bialiauski (abialiauski.dev@gmail.com)
* @since 0.3.5
*/
public final class TopicDoesNotExists extends IOException {

/**
* Serial version UID.
*/
@Serial
private static final long serialVersionUID = 3128213925812696697L;

/**
* Empty ctor.
*/
public TopicDoesNotExists() {
}

Check warning on line 48 in src/main/java/io/github/eocqrs/kafka/fake/TopicDoesNotExists.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/io/github/eocqrs/kafka/fake/TopicDoesNotExists.java#L47-L48

Added lines #L47 - L48 were not covered by tests

/**
* Ctor.
*
* @param message Message
*/
public TopicDoesNotExists(final String message) {
super(message);
}

/**
* Ctor.
*
* @param message Message
* @param cause Cause
*/
public TopicDoesNotExists(final String message, final Throwable cause) {
super(message, cause);
}

Check warning on line 67 in src/main/java/io/github/eocqrs/kafka/fake/TopicDoesNotExists.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/io/github/eocqrs/kafka/fake/TopicDoesNotExists.java#L66-L67

Added lines #L66 - L67 were not covered by tests

/**
* Ctor.
*
* @param cause Cause
*/
public TopicDoesNotExists(final Throwable cause) {
super(cause);
}

Check warning on line 76 in src/main/java/io/github/eocqrs/kafka/fake/TopicDoesNotExists.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/io/github/eocqrs/kafka/fake/TopicDoesNotExists.java#L75-L76

Added lines #L75 - L76 were not covered by tests
}
84 changes: 71 additions & 13 deletions src/test/java/io/github/eocqrs/kafka/fake/FkProducerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@

import io.github.eocqrs.kafka.Producer;
import io.github.eocqrs.kafka.data.KfData;
import io.github.eocqrs.xfake.InFile;
import io.github.eocqrs.xfake.Synchronized;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertThrows;

/**
* Test case for {@link FkProducer}.
*
Expand All @@ -39,18 +42,73 @@
final class FkProducerTest {

@Test
void createsFakeProducer() {
final Producer<String, String> producer = new FkProducer<>();
assertThrows(
UnsupportedOperationException.class,
() ->
producer.send(
"fake", new KfData<>("fake data", "fake-topic", 0)
void createsFakeProducer() throws Exception {
final Producer<String, String> producer =
new FkProducer<>(
new InXml(
new Synchronized(
new InFile(
"test-producer-create", "<broker/>"
)
)
)
);
MatcherAssert.assertThat(
"Fake producer creates",
producer,
Matchers.notNullValue()
);
Assertions.assertDoesNotThrow(producer::close);
}

@Test
void sendsMessageWithoutTopicExistence() throws Exception {
final Producer<String, String> producer =
new FkProducer<>(
new InXml(
new Synchronized(
new InFile(
"test-producer-no-topic",
"<broker/>"
)
)
)
);
Assertions.assertThrows(
TopicDoesNotExists.class,
() ->
producer.send("test-key", new KfData<>("data", "does.not.exist", 0))
);
assertThrows(
UnsupportedOperationException.class,
producer::close
Assertions.assertDoesNotThrow(producer::close);
}

@Test
void sendsMessage() throws Exception {
final String topic = "test.fake";
final String data = "data";
final FkBroker broker = new InXml(
new Synchronized(
new InFile(
"test-producer-send", "<broker/>"
)
)
).with(new TopicDirs(topic).value());
final Producer<String, String> producer =
new FkProducer<>(
broker
);
producer.send("test-key", new KfData<>(data, topic, 0));
MatcherAssert.assertThat(
"Message is send in right format",
broker.data(
"broker/topics/topic[name = '%s']/datasets/dataset[value = '%s']/text()"
.formatted(
topic,
data
)
).isEmpty(),
Matchers.equalTo(false)
);
Assertions.assertDoesNotThrow(producer::close);
}
}
}

0 comments on commit e7c3a46

Please sign in to comment.