Skip to content
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
36 changes: 24 additions & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<spring.version>4.3.29.RELEASE</spring.version>
<slf4j.version>1.7.21</slf4j.version>
<main.class>org.springframework.shell.Bootstrap</main.class>
<!-- workaround because addjars plugin is changing the ${basedir} property -->
<real.base.dir>${basedir}</real.base.dir>
</properties>

<dependencies>
Expand Down Expand Up @@ -131,10 +133,10 @@
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>${slf4j.version}</version>
<scope>runtime</scope>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
Expand All @@ -147,17 +149,17 @@
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -281,6 +283,16 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<!-- addjars plugin is changing ${basedir}, which is causing test resources path issues on Windows -->
<!-- to prevent this, it's necessary to change default ${basedir} working directory to use different property -->
<workingDirectory>${real.base.dir}</workingDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>
22 changes: 17 additions & 5 deletions src/main/java/cz/geek/gooddata/shell/commands/MaqlCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<String> maqls = FileUtils.readLines(file);
getGoodData().getModelService().updateProjectModel(getCurrentProject(), maqls).get();
} else if (maql != null) {
try {
final List<String> 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";
}

}
5 changes: 5 additions & 0 deletions src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<root level="OFF">
</root>
</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
57 changes: 57 additions & 0 deletions src/test/java/cz/geek/gooddata/shell/commands/MaqlCommandTest.java
Original file line number Diff line number Diff line change
@@ -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"));
}
}
Empty file added src/test/resources/empty.file
Empty file.
12 changes: 12 additions & 0 deletions src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%p] %m%n</pattern>
</encoder>
</appender>
<!-- <logger name="org.apache.commons.httpclient.HttpMethodDirector" level="DEBUG"/>-->
<root level="INFO">
<appender-ref ref="Console"/>
</root>
</configuration>