Skip to content

Commit

Permalink
wrote comments
Browse files Browse the repository at this point in the history
  • Loading branch information
AnetteTaivere committed Jun 4, 2024
1 parent fa38aeb commit d951514
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 6 deletions.
17 changes: 15 additions & 2 deletions src/test/java/GoblintAnalysisTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.*;

/**
* Goblint analysis test.
* <p>
* The class is responsible for testing analyzing
* when an analysis event is triggered.
*
* @author Anette Taivere
* @author Karoliine Holter
*/
@ExtendWith(SystemStubsExtension.class)
class GoblintAnalysisTest extends TestHelper {

Expand All @@ -49,6 +58,10 @@ class GoblintAnalysisTest extends TestHelper {
@SystemStub
private SystemOut systemOut;

/**
* BeforeEach method configures mock behaviors
* and expectations to create a controlled testing environment.
*/
@BeforeEach
void before() {
mockGoblintServerIsAlive(goblintServer, goblintConfWatcher);
Expand Down Expand Up @@ -99,8 +112,8 @@ void analyzeFailedWithGoblintAborted() {
}

/**
* Mock test to ensure @analyze function
* messages user when analysis fails due to Goblint responding with VerifyError
* Mock test to ensure @analyze function messages user
* when analysis fails due to Goblint responding with VerifyError
*/
@Test
void analyzeFailedWithGoblintVerifyError() {
Expand Down
41 changes: 38 additions & 3 deletions src/test/java/GoblintConfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,20 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.*;

/**
* Goblint configuration test.
* <p>
* The class is responsible for testing configuration settings.
* It ensures proper functionality of the settings and handles
* any errors that may occur during the process.
*
* @author Anette Taivere
* @author Karoliine Holter
*/

@ExtendWith(SystemStubsExtension.class)
public class GoblintConfTest {

@SystemStub
private SystemOut systemOut;

Expand All @@ -48,50 +59,74 @@ public class GoblintConfTest {
Collection<? extends Module> files = new ArrayDeque<>();
AnalysisConsumer analysisConsumer = mock(AnalysisConsumer.class);

/**
* Mock test to ensure refreshing
* the Goblint configuration succeeds.
*/
@Test
void refreshGoblintConfigSucceeds() {
// Simulate Goblint server as alive
doReturn(true).when(goblintServer).isAlive();

// Mock GobPieConfiguration methods to return specific values
when(gobPieConfiguration.preAnalyzeCommand()).thenReturn(new ArrayList<>());
when(gobPieConfiguration.incrementalAnalysis()).thenReturn(true);
when(gobPieConfiguration.goblintConf()).thenReturn("goblint.json");

// Mock GoblintService methods to return successful analysis results and config operations
when(goblintService.analyze(new AnalyzeParams(false))).thenReturn(CompletableFuture.completedFuture(new GoblintAnalysisResult(List.of("Success"))));
when(goblintService.reset_config()).thenReturn(CompletableFuture.completedFuture(null));
when(goblintService.read_config(new Params(new File("goblint.json").getAbsolutePath()))).thenReturn(CompletableFuture.completedFuture(null));

// Create instances of GoblintConfWatcher and GoblintAnalysis
GoblintConfWatcher goblintConfWatcher = new GoblintConfWatcher(magpieServer, goblintService, gobPieConfiguration, new FileWatcher(Path.of("")));
GoblintAnalysis goblintAnalysis = new GoblintAnalysis(magpieServer, goblintServer, goblintService, gobPieConfiguration, goblintConfWatcher);

// Call analyze method to trigger configuration refresh
goblintAnalysis.analyze(files, analysisConsumer, true);

// Assert that configValid is true after configuration refresh
assertTrue(goblintConfWatcher.configValid);

// Check that if config is valid and not modified, refresh is not necessary and is skipped
goblintAnalysis.analyze(files, analysisConsumer, true);
assertTrue(goblintConfWatcher.configValid);
}

/**
* Mock test to ensure @analyse function
* messages user when Goblint configuration refresh process fails
*/
@Test
void refreshGoblintConfigFails() {
// Simulate Goblint server as alive
doReturn(true).when(goblintServer).isAlive();

// Mock GobPieConfiguration methods to return specific values
when(gobPieConfiguration.preAnalyzeCommand()).thenReturn(new ArrayList<>());
when(gobPieConfiguration.incrementalAnalysis()).thenReturn(true);
when(gobPieConfiguration.goblintConf()).thenReturn("goblint.json");

// Mock GoblintService methods to return successful analysis results and config operations
when(goblintService.analyze(new AnalyzeParams(false))).thenReturn(CompletableFuture.completedFuture(new GoblintAnalysisResult(List.of("Success"))));
when(goblintService.reset_config()).thenReturn(CompletableFuture.completedFuture(null));
when(goblintService.read_config(new Params())).thenReturn(CompletableFuture.completedFuture(null));

// Mock FileWatcher to simulate modification in the file
FileWatcher fileWatcher = spy(new FileWatcher(Path.of("")));
when(fileWatcher.checkModified()).thenReturn(true);

// Create instances of GoblintConfWatcher and GoblintAnalysis
GoblintConfWatcher goblintConfWatcher = new GoblintConfWatcher(magpieServer, goblintService, gobPieConfiguration, fileWatcher);
GoblintAnalysis goblintAnalysis = new GoblintAnalysis(magpieServer, goblintServer, goblintService, gobPieConfiguration, goblintConfWatcher);

// Call analyze method to trigger configuration refresh
goblintAnalysis.analyze(files, analysisConsumer, true);

// Assert that configValid is false after configuration refresh fails
assertFalse(goblintConfWatcher.configValid);

// Assert that the appropriate error message is printed
assertTrue(systemOut.getLines().anyMatch(line -> line.contains("Goblint was unable to successfully read the new configuration: ")));
}

}
}
32 changes: 31 additions & 1 deletion src/test/java/GoblintServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

/**
* Goblint server test.
* <p>
* The class is responsible to ensure that @startGoblintServer
* and @checkGoblintVersions functions execute correctly and informing
* users of any errors encountered.
*
* @author Anette Taivere
* @author Karoliine Holter
*/
@ExtendWith(SystemStubsExtension.class)
public class GoblintServerTest {

Expand All @@ -26,12 +36,18 @@ public class GoblintServerTest {
*/
@Test
public void testStartGoblintServer() {
// Mock everything needed for creating goblintServer
MagpieServer magpieServer = mock(MagpieServer.class);
GobPieConfiguration gobPieConfiguration = mock(GobPieConfiguration.class);
GoblintServer goblintServer = spy(new GoblintServer(magpieServer, gobPieConfiguration));

// Mock behavior to return the constructed run command
doReturn(List.of("sleep", "10s")).when(goblintServer).constructGoblintRunCommand();

// Call startGoblintServer method
goblintServer.startGoblintServer();

// Assert that appropriate message is printed to the console
assertTrue(systemOut.getLines().anyMatch(line -> line.contains("Goblint run with command: ")));
}

Expand All @@ -41,13 +57,16 @@ public void testStartGoblintServer() {
*/
@Test
public void testStartGoblintServerFailed() {
// Mock everything needed for creating goblintServer
MagpieServer magpieServer = mock(MagpieServer.class);
GobPieConfiguration gobPieConfiguration = mock(GobPieConfiguration.class);
GoblintServer goblintServer = spy(new GoblintServer(magpieServer, gobPieConfiguration));

// Mock behavior to return the executable command and abstract debugging
when(gobPieConfiguration.goblintExecutable()).thenReturn("goblint");
when(gobPieConfiguration.abstractDebugging()).thenReturn(true);

// Assert that starting Goblint server throws GobPieException
GobPieException thrown = assertThrows(GobPieException.class, goblintServer::startGoblintServer);
assertEquals("Running Goblint failed.", thrown.getMessage());
}
Expand All @@ -59,15 +78,20 @@ public void testStartGoblintServerFailed() {
*/
@Test
public void testCheckGoblintVersion() {
// Mock everything needed for creating goblintServer
MagpieServer magpieServer = mock(MagpieServer.class);
GobPieConfiguration gobPieConfiguration = mock(GobPieConfiguration.class);
GoblintServer goblintServer = new GoblintServer(magpieServer, gobPieConfiguration);

// Mock behavior to return the executable command
when(gobPieConfiguration.goblintExecutable()).thenReturn("echo");

// Call checkGoblintVersion method
String output = goblintServer.checkGoblintVersion();

// Assert that the output contains version information
assertTrue(output.contains("version"));
// Assert that appropriate messages are printed to the console
assertTrue(systemOut.getLines().anyMatch(line -> line.contains("Waiting for command: [echo, --version] to run...")));
assertTrue(systemOut.getLines().anyMatch(line -> line.contains("Executing [echo, --version]")));
}
Expand All @@ -79,13 +103,19 @@ public void testCheckGoblintVersion() {
@Test
public void testCheckGoblintVersionFailed() {
MagpieServer magpieServer = mock(MagpieServer.class);

// Prepare test data
String fileName = "gobpieTest7.json";
String gobPieConfFileName = GobPieConfTest.class.getResource(fileName).getFile();

// Mock everything needed for creating goblintServer
GobPieConfReader gobPieConfReader = new GobPieConfReader(magpieServer, gobPieConfFileName);
GobPieConfiguration gobPieConfiguration = gobPieConfReader.readGobPieConfiguration();
GoblintServer goblintServer = new GoblintServer(magpieServer, gobPieConfiguration);

// Assert that checking the Goblint version throws GobPieException
GobPieException thrown = assertThrows(GobPieException.class, goblintServer::checkGoblintVersion);
assertEquals("Checking version failed.", thrown.getMessage());
}

}
}

0 comments on commit d951514

Please sign in to comment.