Skip to content

Commit

Permalink
test: improve KafkaApplication.main test
Browse files Browse the repository at this point in the history
  • Loading branch information
ouvreboite committed Sep 22, 2020
1 parent 1052516 commit a54ae52
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
public class KafkaApplication {
private static final Logger LOGGER = LoggerFactory.getLogger(KafkaApplication.class);

enum Application{PRODUCER,STREAM,SINK}

private final ProducerApplication producerApplication;
private final StreamApplication streamApplication;
private final SinkApplication sinkApplication;
Expand All @@ -43,7 +45,7 @@ public static void main(String[] args) throws IOException {
AnnotationConfigApplicationContext context = setupSpringContext(config);
KafkaApplication app = context.getBean(KafkaApplication.class);

app.run(applicationToRun);
app.run(Application.valueOf(applicationToRun));
}

private static AnnotationConfigApplicationContext setupSpringContext(ConnectionConfiguration config) {
Expand All @@ -56,7 +58,8 @@ private static AnnotationConfigApplicationContext setupSpringContext(ConnectionC
return context;
}

public void run(String applicationToRun){

public void run(Application applicationToRun){
LOGGER.info("Creating topics if needed");
topicCreator.createTopicIfNeeded(
Topics.STATION_AVAILABILITIES,
Expand All @@ -66,13 +69,12 @@ public void run(String applicationToRun){
Topics.STATION_STATUS
);


LOGGER.info("Launch application {}", applicationToRun);
switch (applicationToRun){
case "PRODUCER": producerApplication.start(); break;
case "STREAM": streamApplication.start(); break;
case "SINK": sinkApplication.start(); break;
default: throw new IllegalArgumentException("Unknown application : "+applicationToRun);
case PRODUCER: producerApplication.start(); break;
case STREAM: streamApplication.start(); break;
case SINK: sinkApplication.start(); break;
default: throw new IllegalArgumentException("Unmanaged application : "+applicationToRun);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static fr.velinfo.kafka.KafkaApplication.Application.*;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

Expand All @@ -34,25 +35,28 @@ void init(){
}

@Test
void run_shouldThrow_whenApplicationUnknown() {
Assertions.assertThrows(IllegalArgumentException.class, () -> kafkaApplication.run("something"));
void main_shouldThrowWhenApplicationUnknown() {
Assertions.assertThrows(
IllegalArgumentException.class,
() -> KafkaApplication.main(new String[]{"unknown","src/test/resources/connection.properties"})
);
}

@Test
void run_shouldStartProducer() {
kafkaApplication.run("PRODUCER");
kafkaApplication.run(PRODUCER);
verify(producerApplication, times(1)).start();
}

@Test
void run_shouldStartStream() {
kafkaApplication.run("STREAM");
kafkaApplication.run(STREAM);
verify(streamApplication, times(1)).start();
}

@Test
void run_shouldStartSink() {
kafkaApplication.run("SINK");
kafkaApplication.run(SINK);
verify(sinkApplication, times(1)).start();
}

Expand Down
5 changes: 5 additions & 0 deletions source/kafka/src/test/resources/connection.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
bootstrap.servers=localhost:9092
schema.registry.url=http://localhost:8081
database.url=jdbc:postgresql://localhost:5432/velinfo_stats
database.user=velinfo
database.password=dev_password

0 comments on commit a54ae52

Please sign in to comment.