forked from KaotoIO/kaoto
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(test): Add tests for @kaoto/camel-catalog
The first batch for @kaoto/camel-catalog tests fix: KaotoIO#1250
- Loading branch information
Showing
5 changed files
with
236 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 27 additions & 4 deletions
31
packages/catalog-generator/src/main/java/io/kaoto/camelcatalog/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,36 @@ | ||
package io.kaoto.camelcatalog; | ||
|
||
import java.util.logging.Logger; | ||
|
||
import io.kaoto.camelcatalog.beans.ConfigBean; | ||
import io.kaoto.camelcatalog.commands.GenerateCommand; | ||
import io.kaoto.camelcatalog.commands.GenerateCommandOptions; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
ConfigBean configBean = GenerateCommandOptions.configure(args); | ||
GenerateCommand generateCommand = new GenerateCommand(configBean); | ||
generateCommand.run(); | ||
private static final Logger LOGGER = Logger.getLogger(Main.class.getName()); | ||
static final int EXIT_CODE_SUCCESS = 0; | ||
static final int EXIT_CODE_FAILURE = 1; | ||
|
||
public static void main(String[] args) { | ||
ConfigBean configBean = new ConfigBean(); | ||
GenerateCommandOptions generateCommandOptions = new GenerateCommandOptions(configBean); | ||
int exitCode = EXIT_CODE_SUCCESS; | ||
|
||
try { | ||
generateCommandOptions.configure(args); | ||
} catch (Exception e) { | ||
LOGGER.severe("Error: " + e.getMessage()); | ||
generateCommandOptions.printHelp(); | ||
exitCode = EXIT_CODE_FAILURE; | ||
} | ||
|
||
GenerateCommand generateCommand = new GenerateCommand(configBean); | ||
generateCommand.run(); | ||
|
||
exit(exitCode); | ||
} | ||
|
||
static void exit(int status) { | ||
System.exit(status); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 66 additions & 3 deletions
69
packages/catalog-generator/src/test/java/io/kaoto/camelcatalog/MainTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,76 @@ | ||
package io.kaoto.camelcatalog; | ||
|
||
import java.io.IOException; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.Mockito.doNothing; | ||
import static org.mockito.Mockito.doThrow; | ||
import static org.mockito.Mockito.mockConstruction; | ||
import static org.mockito.Mockito.mockStatic; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.mockito.MockedConstruction; | ||
import org.mockito.MockedStatic; | ||
|
||
import io.kaoto.camelcatalog.commands.GenerateCommand; | ||
import io.kaoto.camelcatalog.commands.GenerateCommandOptions; | ||
import static io.kaoto.camelcatalog.Main.EXIT_CODE_SUCCESS; | ||
import static io.kaoto.camelcatalog.Main.EXIT_CODE_FAILURE; | ||
public class MainTest { | ||
|
||
@Test | ||
public void testGenerate() throws IOException { | ||
// Main.main(new String[] {}); | ||
public void testMainNormalExecution() throws Exception { | ||
String[] args = { "-o", "outputDir", "-n", "catalogName", "-k", "kameletsVersion" }; | ||
int[] exitCode = { 99 }; | ||
|
||
try ( | ||
MockedConstruction<GenerateCommandOptions> mockedGenerateCommandOptions = mockConstruction( | ||
GenerateCommandOptions.class, (mock, context) -> { | ||
doNothing().when(mock).configure(args); | ||
}); | ||
MockedConstruction<GenerateCommand> mockedGenerateCommand = mockConstruction(GenerateCommand.class, | ||
(mock, context) -> { | ||
doNothing().when(mock).run(); | ||
}); | ||
MockedStatic<Main> mockedMain = mockStatic(Main.class);) { | ||
mockedMain.when(() -> Main.main(args)).thenCallRealMethod(); | ||
mockedMain.when(() -> Main.exit(EXIT_CODE_SUCCESS)).then(invocation -> { | ||
exitCode[0] = EXIT_CODE_SUCCESS; | ||
return null; | ||
}); | ||
|
||
Main.main(args); | ||
|
||
verify(mockedGenerateCommandOptions.constructed().get(0)).configure(args); | ||
verify(mockedGenerateCommand.constructed().get(0)).run(); | ||
assertTrue(exitCode[0] == EXIT_CODE_SUCCESS); | ||
} | ||
} | ||
|
||
@Test | ||
public void testMainAbnormalExecution() throws Exception { | ||
String[] args = { "-o", "outputDir", "-n", "catalogName", "-k", "kameletsVersion" }; | ||
int[] exitCode = { 99 }; | ||
try ( | ||
MockedConstruction<GenerateCommandOptions> mockedGenerateCommandOptions = mockConstruction( | ||
GenerateCommandOptions.class, (mock, context) -> { | ||
doThrow(new RuntimeException()).when(mock).configure(args); | ||
}); | ||
MockedConstruction<GenerateCommand> mockedGenerateCommand = mockConstruction(GenerateCommand.class, | ||
(mock, context) -> { | ||
doNothing().when(mock).run(); | ||
}); | ||
MockedStatic<Main> mockedMain = mockStatic(Main.class);) { | ||
mockedMain.when(() -> Main.main(args)).thenCallRealMethod(); | ||
mockedMain.when(() -> Main.exit(EXIT_CODE_FAILURE)).then(invocation -> { | ||
exitCode[0] = EXIT_CODE_FAILURE; | ||
return null; | ||
}); | ||
|
||
Main.main(args); | ||
|
||
verify(mockedGenerateCommandOptions.constructed().get(0)).configure(args); | ||
verify(mockedGenerateCommand.constructed().get(0)).run(); | ||
assertTrue(exitCode[0] == EXIT_CODE_FAILURE); | ||
} | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
...og-generator/src/test/java/io/kaoto/camelcatalog/commands/GenerateCommandOptionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package io.kaoto.camelcatalog.commands; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.Mockito.spy; | ||
|
||
import java.io.PrintStream; | ||
import java.nio.charset.Charset; | ||
|
||
import org.apache.commons.cli.ParseException; | ||
import org.apache.commons.io.output.ByteArrayOutputStream; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.kaoto.camelcatalog.beans.ConfigBean; | ||
import io.kaoto.camelcatalog.generator.Util; | ||
|
||
public class GenerateCommandOptionsTest { | ||
@Test | ||
public void testConfigureWithAllRequiredOptions() throws ParseException { | ||
ConfigBean configBean = new ConfigBean(); | ||
GenerateCommandOptions generateCommandOptions = new GenerateCommandOptions(configBean); | ||
String[] args = { "-o", "outputDir", "-n", "catalogName", "-k", "kameletsVersion" }; | ||
|
||
generateCommandOptions.configure(args); | ||
String outputDir = Util.getNormalizedFolder("outputDir"); | ||
|
||
assertEquals(outputDir, configBean.getOutputFolder().toString()); | ||
assertEquals("catalogName", configBean.getCatalogsName()); | ||
assertEquals("kameletsVersion", configBean.getKameletsVersion()); | ||
} | ||
|
||
@Test | ||
public void testConfigureWithMissingRequiredOptions() { | ||
ConfigBean configBean = new ConfigBean(); | ||
GenerateCommandOptions generateCommandOptions = new GenerateCommandOptions(configBean); | ||
String[] args = { "-o", "outputDir" }; | ||
|
||
Exception exception = assertThrows(ParseException.class, () -> { | ||
generateCommandOptions.configure(args); | ||
}); | ||
|
||
String expectedMessage = "Missing required option"; | ||
String actualMessage = exception.getMessage(); | ||
assertTrue(actualMessage.contains(expectedMessage)); | ||
} | ||
|
||
@Test | ||
public void testConfigureWithOptionalOptions() throws ParseException { | ||
ConfigBean configBean = new ConfigBean(); | ||
GenerateCommandOptions generateCommandOptions = new GenerateCommandOptions(configBean); | ||
String[] args = { "-o", "outputDir", "-n", "catalogName", "-k", "kameletsVersion", "-m", "mainVersion", "-q", | ||
"quarkusVersion", "-s", "springbootVersion" }; | ||
|
||
generateCommandOptions.configure(args); | ||
|
||
assertEquals("catalogName", configBean.getCatalogsName()); | ||
assertEquals("kameletsVersion", configBean.getKameletsVersion()); | ||
assertFalse(configBean.getCatalogVersionSet().isEmpty()); | ||
} | ||
|
||
@Test | ||
public void testConfigureWithInvalidOptions() { | ||
ConfigBean configBean = new ConfigBean(); | ||
GenerateCommandOptions generateCommandOptions = new GenerateCommandOptions(configBean); | ||
String[] args = { "-x", "invalidOption" }; | ||
|
||
Exception exception = assertThrows(ParseException.class, () -> { | ||
generateCommandOptions.configure(args); | ||
}); | ||
|
||
String expectedMessage = "Unrecognized option"; | ||
String actualMessage = exception.getMessage(); | ||
assertTrue(actualMessage.contains(expectedMessage)); | ||
} | ||
|
||
@Test | ||
public void testPrintHelp() { | ||
ConfigBean configBean = new ConfigBean(); | ||
GenerateCommandOptions generateCommandOptions = new GenerateCommandOptions(configBean); | ||
|
||
// Redirect System.out to capture the printHelp output | ||
ByteArrayOutputStream outContent = new ByteArrayOutputStream(); | ||
System.setOut(new PrintStream(outContent)); | ||
|
||
generateCommandOptions.printHelp(); | ||
|
||
String expectedOutput = "usage: catalog-generator"; | ||
String actualOutput = outContent.toString(Charset.defaultCharset()); | ||
assertTrue(actualOutput.contains(expectedOutput)); | ||
|
||
// Reset System.out | ||
System.setOut(System.out); | ||
} | ||
|
||
@Test | ||
public void testAddDefaultVersions() throws Exception { | ||
ConfigBean configBean = spy(new ConfigBean()); | ||
GenerateCommandOptions generateCommandOptions = new GenerateCommandOptions(configBean); | ||
String[] args = { "-o", "outputDir", "-n", "catalogName", "-k", "kameletsVersion" }; | ||
|
||
generateCommandOptions.configure(args); | ||
|
||
assertTrue(configBean.getCatalogVersionSet().size() == 3); | ||
|
||
} | ||
} |