Skip to content

Commit ae6f57a

Browse files
committed
Add validations into maql command
1 parent 8c72e44 commit ae6f57a

7 files changed

Lines changed: 116 additions & 17 deletions

File tree

pom.xml

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,10 @@
131131
<scope>runtime</scope>
132132
</dependency>
133133
<dependency>
134-
<groupId>org.slf4j</groupId>
135-
<artifactId>slf4j-nop</artifactId>
136-
<version>${slf4j.version}</version>
137-
<scope>runtime</scope>
134+
<groupId>ch.qos.logback</groupId>
135+
<artifactId>logback-classic</artifactId>
136+
<version>1.2.3</version>
137+
<scope>test</scope>
138138
</dependency>
139139
<dependency>
140140
<groupId>commons-logging</groupId>
@@ -147,17 +147,17 @@
147147
<artifactId>junit</artifactId>
148148
<version>4.13.1</version>
149149
<scope>test</scope>
150+
<exclusions>
151+
<exclusion>
152+
<groupId>org.hamcrest</groupId>
153+
<artifactId>hamcrest-core</artifactId>
154+
</exclusion>
155+
</exclusions>
150156
</dependency>
151157
<dependency>
152158
<groupId>org.hamcrest</groupId>
153-
<artifactId>hamcrest-core</artifactId>
154-
<version>1.3</version>
155-
<scope>test</scope>
156-
</dependency>
157-
<dependency>
158-
<groupId>org.hamcrest</groupId>
159-
<artifactId>hamcrest-library</artifactId>
160-
<version>1.3</version>
159+
<artifactId>hamcrest</artifactId>
160+
<version>2.2</version>
161161
<scope>test</scope>
162162
</dependency>
163163
<dependency>
@@ -281,6 +281,16 @@
281281
</execution>
282282
</executions>
283283
</plugin>
284+
<plugin>
285+
<groupId>org.apache.maven.plugins</groupId>
286+
<artifactId>maven-surefire-plugin</artifactId>
287+
<version>2.22.2</version>
288+
<configuration>
289+
<!-- addjars plugin is changing ${basedir}, which is causing test resources path issues on Windows -->
290+
<!-- to prevent this, it's necessary to change default ${basedir} working directory to use different variable -->
291+
<workingDirectory>${build.testOutputDirectory}</workingDirectory>
292+
</configuration>
293+
</plugin>
284294
</plugins>
285295
</build>
286296
</project>

src/main/java/cz/geek/gooddata/shell/commands/MaqlCommand.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
import java.io.IOException;
1313
import java.util.List;
1414

15+
import static java.lang.String.format;
16+
import static java.nio.charset.StandardCharsets.UTF_8;
17+
1518
/**
19+
*
1620
*/
1721
@Component
1822
public class MaqlCommand extends AbstractGoodDataCommand {
@@ -30,16 +34,24 @@ public boolean isAvailable() {
3034

3135
@CliCommand(value = "maql", help = "Execute MAQL DDL")
3236
public String maql(@CliOption(key = {"maql", ""}, help = "MAQL DDL") String maql,
33-
@CliOption(key = "file", help = "MAQL DDL file") File file) throws IOException {
37+
@CliOption(key = "file", help = "MAQL DDL file (executes each row separately)") File file) {
3438
if (file != null) {
35-
final List<String> maqls = FileUtils.readLines(file);
36-
getGoodData().getModelService().updateProjectModel(getCurrentProject(), maqls).get();
37-
} else if (maql != null) {
39+
try {
40+
final List<String> maqls = FileUtils.readLines(file, UTF_8);
41+
if (maqls.size() > 0) {
42+
getGoodData().getModelService().updateProjectModel(getCurrentProject(), maqls).get();
43+
} else {
44+
throw new IllegalArgumentException("maql file cannot be empty");
45+
}
46+
} catch (IOException e) {
47+
throw new IllegalArgumentException(format("file %s doesn't exist", file.getAbsolutePath()));
48+
}
49+
} else if (maql != null && maql.length() > 0) {
3850
getGoodData().getModelService().updateProjectModel(getCurrentProject(), maql).get();
3951
} else {
4052
throw new IllegalArgumentException("maql or file argument has to be specified");
4153
}
42-
return "Executed";
54+
return "MAQL Executed";
4355
}
4456

4557
}

src/main/resources/logback.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
<root level="OFF">
4+
</root>
5+
</configuration>

src/test/java/cz/geek/gooddata/shell/AbstractShellIntegrationTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import org.junit.AfterClass;
66
import org.junit.Before;
77
import org.junit.BeforeClass;
8+
import org.junit.runner.RunWith;
9+
import org.mockito.junit.MockitoJUnitRunner;
810
import org.springframework.context.ApplicationContext;
911
import org.springframework.shell.Bootstrap;
1012
import org.springframework.shell.core.JLineShellComponent;
@@ -13,6 +15,7 @@
1315
* Parent instantiating the shell inside a test case, so we can execute the command and then perform assertions
1416
* on the return value CommandResult.
1517
*/
18+
@RunWith(MockitoJUnitRunner.class)
1619
public abstract class AbstractShellIntegrationTest {
1720

1821
private static JLineShellComponent shell;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package cz.geek.gooddata.shell.commands;
2+
3+
import com.gooddata.project.Project;
4+
import cz.geek.gooddata.shell.AbstractShellIntegrationTest;
5+
import cz.geek.gooddata.shell.components.GoodDataHolder;
6+
import org.junit.Test;
7+
import org.mockito.Mock;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
import org.springframework.shell.core.CommandResult;
11+
12+
import static java.util.Objects.requireNonNull;
13+
import static org.hamcrest.CoreMatchers.instanceOf;
14+
import static org.hamcrest.CoreMatchers.is;
15+
import static org.hamcrest.MatcherAssert.assertThat;
16+
import static org.hamcrest.Matchers.matchesPattern;
17+
18+
public class MaqlCommandTest extends AbstractShellIntegrationTest {
19+
20+
private final Logger logger = LoggerFactory.getLogger(getClass());
21+
22+
@Mock
23+
Project project;
24+
25+
@Test
26+
public void maqlWithoutProjectToUse() {
27+
final CommandResult commandResult = getShell().executeCommand("maql ''");
28+
29+
assertThat(commandResult.isSuccess(), is(false));
30+
}
31+
32+
@Test
33+
public void maqlWithNonexistentFile() {
34+
final GoodDataHolder holder = getContext().getBean(GoodDataHolder.class);
35+
holder.setCurrentProject(project);
36+
37+
final CommandResult commandResult = getShell().executeCommand("maql --file nonexistent");
38+
39+
final Throwable exception = commandResult.getException();
40+
assertThat(exception, is(instanceOf(IllegalArgumentException.class)));
41+
assertThat(exception.getMessage(), matchesPattern("file .* doesn't exist"));
42+
}
43+
44+
@Test
45+
public void maqlWithEmptyFile() {
46+
final GoodDataHolder holder = getContext().getBean(GoodDataHolder.class);
47+
holder.setCurrentProject(project);
48+
final String emptyFilePath = requireNonNull(getClass().getClassLoader().getResource("empty.file")).getPath();
49+
50+
logger.info("maqlWithEmptyFile test is about to run: maql --file {}", emptyFilePath);
51+
final CommandResult commandResult = getShell().executeCommand("maql --file " + emptyFilePath);
52+
53+
final Throwable exception = commandResult.getException();
54+
assertThat(exception, is(instanceOf(IllegalArgumentException.class)));
55+
assertThat(exception.getMessage(), is("maql file cannot be empty"));
56+
}
57+
}

src/test/resources/empty.file

Whitespace-only changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
4+
<encoder>
5+
<pattern>[%p] %m%n</pattern>
6+
</encoder>
7+
</appender>
8+
<!-- <logger name="org.apache.commons.httpclient.HttpMethodDirector" level="DEBUG"/>-->
9+
<root level="INFO">
10+
<appender-ref ref="Console"/>
11+
</root>
12+
</configuration>

0 commit comments

Comments
 (0)