Skip to content

Commit

Permalink
chore(test): Add tests for @kaoto/camel-catalog
Browse files Browse the repository at this point in the history
The first batch for @kaoto/camel-catalog tests

fix: KaotoIO#1250
  • Loading branch information
lordrip committed Aug 13, 2024
1 parent 7b447cc commit a4dcbc6
Show file tree
Hide file tree
Showing 5 changed files with 236 additions and 38 deletions.
12 changes: 12 additions & 0 deletions packages/catalog-generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@
<version>${version.junit}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.12.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.12.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@

public class GenerateCommandOptions {
private static final Logger LOGGER = Logger.getLogger(GenerateCommandOptions.class.getName());
private static Options options = new Options();
private Options options = new Options();
private ConfigBean configBean;

public static ConfigBean configure(String[] args) {
public GenerateCommandOptions(ConfigBean configBean) {
this.configBean = configBean;
}

public void configure(String[] args) throws ParseException {
Option outputOption = Option.builder().argName("outputDir").option("o").longOpt("output")
.desc("Output directory. It will be cleaned before generating the catalogs").hasArg()
.required()
Expand Down Expand Up @@ -57,41 +62,27 @@ public static ConfigBean configure(String[] args) {
options.addOption(camelSpringbootVersionOption);
options.addOption(verboseOption);

ConfigBean configBean = new ConfigBean();
CommandLineParser parser = new DefaultParser();
CommandLine cmd = parser.parse(options, args);
configBean.setOutputFolder(Util.getNormalizedFolder(cmd.getOptionValue(outputOption.getOpt())));
configBean.setCatalogsName(cmd.getOptionValue(catalogsNameOption.getOpt()));
configBean.setKameletsVersion(cmd.getOptionValue(kameletsVersionOption.getOpt()));

try {
CommandLineParser parser = new DefaultParser();
CommandLine cmd = parser.parse(options, args);
configBean.setOutputFolder(Util.getNormalizedFolder(cmd.getOptionValue(outputOption.getOpt())));
configBean.setCatalogsName(cmd.getOptionValue(catalogsNameOption.getOpt()));
configBean.setKameletsVersion(cmd.getOptionValue(kameletsVersionOption.getOpt()));
addRuntimeVersions(configBean, cmd, camelMainVersionOption, CatalogRuntime.Main);
addRuntimeVersions(configBean, cmd, camelQuarkusVersionOption, CatalogRuntime.Quarkus);
addRuntimeVersions(configBean, cmd, camelSpringbootVersionOption, CatalogRuntime.SpringBoot);

addRuntimeVersions(configBean, cmd, camelMainVersionOption, CatalogRuntime.Main);
addRuntimeVersions(configBean, cmd, camelQuarkusVersionOption, CatalogRuntime.Quarkus);
addRuntimeVersions(configBean, cmd, camelSpringbootVersionOption, CatalogRuntime.SpringBoot);

if (configBean.getCatalogVersionSet().isEmpty()) {
addDefaultVersions(configBean);
}
} catch (ParseException e) {
LOGGER.severe("Missing required options");
printHelpAndExit();
if (configBean.getCatalogVersionSet().isEmpty()) {
addDefaultVersions(configBean);
}

return configBean;
}

private static void printHelpAndExit() {
printHelp();
System.exit(1);
}

private static void printHelp() {
public void printHelp() {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("catalog-generator", options);
formatter.printHelp("catalog-generator", this.options);
}

private static void addRuntimeVersions(ConfigBean configBean, CommandLine cmd, Option option,
private void addRuntimeVersions(ConfigBean configBean, CommandLine cmd, Option option,
CatalogRuntime runtime) {
String[] versions = cmd.getOptionValues(option.getOpt());
if (versions != null) {
Expand All @@ -101,10 +92,11 @@ private static void addRuntimeVersions(ConfigBean configBean, CommandLine cmd, O
}
}

private static void addDefaultVersions(ConfigBean configBean) {
private void addDefaultVersions(ConfigBean configBean) {
// If no version is specified, we will generate the main catalog with the
// installed version
LOGGER.warning("\nNo Camel Main catalog version specified. \nGenerating the main catalog with the installed version");
LOGGER.warning(
"\nNo Camel Main catalog version specified. \nGenerating the main catalog with the installed version");

CamelCatalog camelCatalog = new DefaultCamelCatalog();
configBean.addCatalogVersion(new CatalogCliArgument(CatalogRuntime.Main, camelCatalog.getCatalogVersion()));
Expand Down
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);
}
}
}
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);

}
}

0 comments on commit a4dcbc6

Please sign in to comment.