Skip to content

Improve test coverage for de.rub.nds.crawler package #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -412,6 +417,26 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
</plugin>
<!-- Code coverage with JaCoCo -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.13</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<goals>
<goal>report</goal>
</goals>
<phase>test</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
131 changes: 131 additions & 0 deletions src/test/java/de/rub/nds/crawler/CommonMainTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* TLS-Crawler - A TLS scanning tool to perform large scale scans with the TLS-Scanner
*
* Copyright 2018-2023 Ruhr University Bochum, Paderborn University, and Hackmanit GmbH
*
* Licensed under Apache License, Version 2.0
* http://www.apache.org/licenses/LICENSE-2.0.txt
*/
package de.rub.nds.crawler;

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

import de.rub.nds.crawler.config.ControllerCommandConfig;
import de.rub.nds.crawler.config.WorkerCommandConfig;
import de.rub.nds.crawler.core.Controller;
import de.rub.nds.crawler.core.Worker;
import de.rub.nds.crawler.dummy.DummyControllerCommandConfig;
import de.rub.nds.crawler.orchestration.RabbitMqOrchestrationProvider;
import de.rub.nds.crawler.persistence.MongoPersistenceProvider;
import org.junit.jupiter.api.Test;
import org.mockito.MockedConstruction;
import org.mockito.Mockito;

public class CommonMainTest {

@Test
public void testMainWithNoCommand() {
ControllerCommandConfig controllerConfig = new DummyControllerCommandConfig();
WorkerCommandConfig workerConfig = new WorkerCommandConfig();

// Test no command given - should return early without starting controller/worker
CommonMain.main(new String[] {}, controllerConfig, workerConfig);
// The test passes if no exception is thrown
}

@Test
public void testMainWithInvalidCommand() {
ControllerCommandConfig controllerConfig = new DummyControllerCommandConfig();
WorkerCommandConfig workerConfig = new WorkerCommandConfig();

// Test invalid command - should return early without starting controller/worker
CommonMain.main(new String[] {"invalid"}, controllerConfig, workerConfig);
// The test passes if no exception is thrown
}

@Test
public void testMainWithControllerCommand() {
ControllerCommandConfig controllerConfig = new DummyControllerCommandConfig();
controllerConfig.setHostFile("src/test/resources/targets.txt");

WorkerCommandConfig workerConfig = new WorkerCommandConfig();

try (MockedConstruction<Controller> controllerMock =
Mockito.mockConstruction(
Controller.class,
(mock, context) -> {
Mockito.doNothing().when(mock).start();
});
MockedConstruction<RabbitMqOrchestrationProvider> rabbitMock =
Mockito.mockConstruction(RabbitMqOrchestrationProvider.class);
MockedConstruction<MongoPersistenceProvider> mongoMock =
Mockito.mockConstruction(MongoPersistenceProvider.class)) {

CommonMain.main(new String[] {"controller"}, controllerConfig, workerConfig);

assertEquals(1, controllerMock.constructed().size());
Controller controller = controllerMock.constructed().get(0);
Mockito.verify(controller).start();
}
}

@Test
public void testMainWithWorkerCommand() {
ControllerCommandConfig controllerConfig = new DummyControllerCommandConfig();
WorkerCommandConfig workerConfig = new WorkerCommandConfig();

try (MockedConstruction<Worker> workerMock =
Mockito.mockConstruction(
Worker.class,
(mock, context) -> {
Mockito.doNothing().when(mock).start();
});
MockedConstruction<RabbitMqOrchestrationProvider> rabbitMock =
Mockito.mockConstruction(RabbitMqOrchestrationProvider.class);
MockedConstruction<MongoPersistenceProvider> mongoMock =
Mockito.mockConstruction(MongoPersistenceProvider.class)) {

CommonMain.main(new String[] {"worker"}, controllerConfig, workerConfig);

assertEquals(1, workerMock.constructed().size());
Worker worker = workerMock.constructed().get(0);
Mockito.verify(worker).start();
}
}

@Test
public void testMainWithSingleConfigParam() {
ControllerCommandConfig controllerConfig = new DummyControllerCommandConfig();

// Test the overloaded method
CommonMain.main(new String[] {}, controllerConfig);
// The test passes if no exception is thrown
}

@Test
public void testMainDefaultCase() {
ControllerCommandConfig controllerConfig = new DummyControllerCommandConfig();
WorkerCommandConfig workerConfig = new WorkerCommandConfig();

// Test an unrecognized command - should call usage()
CommonMain.main(new String[] {"unknown"}, controllerConfig, workerConfig);
// The test passes if no exception is thrown
}

@Test
public void testConstructor() {
// Test that the constructor can be instantiated
// This is mainly for coverage purposes
CommonMain commonMain = new CommonMain();
assertNotNull(commonMain);
}

@Test
public void testMainWithUnknownButValidCommand() {
// Since we can't easily create a command that JCommander accepts
// but isn't handled by the switch statement, we'll have to accept
// that the default case is unreachable in normal circumstances.
// This is a limitation of the current implementation.
assertTrue(true);
}
}
Loading
Loading