diff --git a/pom.xml b/pom.xml index d84cecb..7090be1 100644 --- a/pom.xml +++ b/pom.xml @@ -14,6 +14,8 @@ 4.3.29.RELEASE 1.7.21 org.springframework.shell.Bootstrap + + ${basedir} @@ -131,10 +133,10 @@ runtime - org.slf4j - slf4j-nop - ${slf4j.version} - runtime + ch.qos.logback + logback-classic + 1.2.3 + test commons-logging @@ -147,17 +149,17 @@ junit 4.13.1 test + + + org.hamcrest + hamcrest-core + + org.hamcrest - hamcrest-core - 1.3 - test - - - org.hamcrest - hamcrest-library - 1.3 + hamcrest + 2.2 test @@ -281,6 +283,16 @@ + + org.apache.maven.plugins + maven-surefire-plugin + 2.22.2 + + + + ${real.base.dir} + + diff --git a/src/main/java/cz/geek/gooddata/shell/commands/MaqlCommand.java b/src/main/java/cz/geek/gooddata/shell/commands/MaqlCommand.java index 9027c74..6c8138a 100644 --- a/src/main/java/cz/geek/gooddata/shell/commands/MaqlCommand.java +++ b/src/main/java/cz/geek/gooddata/shell/commands/MaqlCommand.java @@ -12,7 +12,11 @@ import java.io.IOException; import java.util.List; +import static java.lang.String.format; +import static java.nio.charset.StandardCharsets.UTF_8; + /** + * */ @Component public class MaqlCommand extends AbstractGoodDataCommand { @@ -30,16 +34,24 @@ public boolean isAvailable() { @CliCommand(value = "maql", help = "Execute MAQL DDL") public String maql(@CliOption(key = {"maql", ""}, help = "MAQL DDL") String maql, - @CliOption(key = "file", help = "MAQL DDL file") File file) throws IOException { + @CliOption(key = "file", help = "MAQL DDL file (executes each row separately)") File file) { if (file != null) { - final List maqls = FileUtils.readLines(file); - getGoodData().getModelService().updateProjectModel(getCurrentProject(), maqls).get(); - } else if (maql != null) { + try { + final List maqls = FileUtils.readLines(file, UTF_8); + if (maqls.size() > 0) { + getGoodData().getModelService().updateProjectModel(getCurrentProject(), maqls).get(); + } else { + throw new IllegalArgumentException("maql file cannot be empty"); + } + } catch (IOException e) { + throw new IllegalArgumentException(format("file %s doesn't exist", file.getAbsolutePath())); + } + } else if (maql != null && maql.length() > 0) { getGoodData().getModelService().updateProjectModel(getCurrentProject(), maql).get(); } else { throw new IllegalArgumentException("maql or file argument has to be specified"); } - return "Executed"; + return "MAQL Executed"; } } diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml new file mode 100644 index 0000000..63dfc53 --- /dev/null +++ b/src/main/resources/logback.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/src/test/java/cz/geek/gooddata/shell/AbstractShellIntegrationTest.java b/src/test/java/cz/geek/gooddata/shell/AbstractShellIntegrationTest.java index 77d02f1..5046e97 100644 --- a/src/test/java/cz/geek/gooddata/shell/AbstractShellIntegrationTest.java +++ b/src/test/java/cz/geek/gooddata/shell/AbstractShellIntegrationTest.java @@ -5,6 +5,8 @@ import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; import org.springframework.context.ApplicationContext; import org.springframework.shell.Bootstrap; import org.springframework.shell.core.JLineShellComponent; @@ -13,6 +15,7 @@ * Parent instantiating the shell inside a test case, so we can execute the command and then perform assertions * on the return value CommandResult. */ +@RunWith(MockitoJUnitRunner.class) public abstract class AbstractShellIntegrationTest { private static JLineShellComponent shell; diff --git a/src/test/java/cz/geek/gooddata/shell/commands/MaqlCommandTest.java b/src/test/java/cz/geek/gooddata/shell/commands/MaqlCommandTest.java new file mode 100644 index 0000000..9fcc1f9 --- /dev/null +++ b/src/test/java/cz/geek/gooddata/shell/commands/MaqlCommandTest.java @@ -0,0 +1,57 @@ +package cz.geek.gooddata.shell.commands; + +import com.gooddata.project.Project; +import cz.geek.gooddata.shell.AbstractShellIntegrationTest; +import cz.geek.gooddata.shell.components.GoodDataHolder; +import org.junit.Test; +import org.mockito.Mock; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.shell.core.CommandResult; + +import static java.util.Objects.requireNonNull; +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.matchesPattern; + +public class MaqlCommandTest extends AbstractShellIntegrationTest { + + private final Logger logger = LoggerFactory.getLogger(getClass()); + + @Mock + Project project; + + @Test + public void maqlWithoutProjectToUse() { + final CommandResult commandResult = getShell().executeCommand("maql ''"); + + assertThat(commandResult.isSuccess(), is(false)); + } + + @Test + public void maqlWithNonexistentFile() { + final GoodDataHolder holder = getContext().getBean(GoodDataHolder.class); + holder.setCurrentProject(project); + + final CommandResult commandResult = getShell().executeCommand("maql --file nonexistent"); + + final Throwable exception = commandResult.getException(); + assertThat(exception, is(instanceOf(IllegalArgumentException.class))); + assertThat(exception.getMessage(), matchesPattern("file .* doesn't exist")); + } + + @Test + public void maqlWithEmptyFile() { + final GoodDataHolder holder = getContext().getBean(GoodDataHolder.class); + holder.setCurrentProject(project); + final String emptyFilePath = requireNonNull(getClass().getClassLoader().getResource("empty.file")).getPath(); + + logger.info("maqlWithEmptyFile test is about to run: maql --file {}", emptyFilePath); + final CommandResult commandResult = getShell().executeCommand("maql --file " + emptyFilePath); + + final Throwable exception = commandResult.getException(); + assertThat(exception, is(instanceOf(IllegalArgumentException.class))); + assertThat(exception.getMessage(), is("maql file cannot be empty")); + } +} \ No newline at end of file diff --git a/src/test/resources/empty.file b/src/test/resources/empty.file new file mode 100644 index 0000000..e69de29 diff --git a/src/test/resources/logback-test.xml b/src/test/resources/logback-test.xml new file mode 100644 index 0000000..b66846f --- /dev/null +++ b/src/test/resources/logback-test.xml @@ -0,0 +1,12 @@ + + + + + [%p] %m%n + + + + + + +