Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cucumber integration #40

Merged
merged 12 commits into from
May 19, 2016
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public Observable<JsonObject> asJson() {

@Override
public Observable<String> asString() {
if (value == null) {
return Observable.empty();
}
return Observable.just(value.asString());
}
}
58 changes: 58 additions & 0 deletions cucumber/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.chodavarapu.datamill</groupId>
<artifactId>datamill</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>cucumber</artifactId>
<packaging>jar</packaging>
<name>Data Mill Cucumber Integration</name>

<dependencies>
<dependency>
<groupId>org.chodavarapu.datamill</groupId>
<artifactId>core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.16</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.subethamail</groupId>
<artifactId>subethasmtp</artifactId>
<version>3.1.7</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.3.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package org.chodavarapu.datamill.cucumber;

import com.google.common.io.Files;
import cucumber.api.java.After;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
* @author Ravi Chodavarapu (rchodava@gmail.com)
*/
public class CommandLineSteps {
private static final Logger logger = LoggerFactory.getLogger(CommandLineSteps.class);
static final String TEMPORARY_DIRECTORY = "$$temporaryDirectory";

private final PlaceholderResolver placeholderResolver;
private final PropertyStore propertyStore;

public CommandLineSteps(PropertyStore propertyStore, PlaceholderResolver placeholderResolver) {
this.propertyStore = propertyStore;
this.placeholderResolver = placeholderResolver;
}

@When("^" + Phrases.SUBJECT + " executes \"(.+)\", it should fail")
public void executeCommandExpectingFailure(String command) {
String resolvedCommand = placeholderResolver.resolve(command);
try {
File temporaryDirectory = Files.createTempDir();
propertyStore.put(TEMPORARY_DIRECTORY, temporaryDirectory);
int result = Runtime.getRuntime().exec(resolvedCommand, null, temporaryDirectory).waitFor();
if (result == 0) {
fail("Expected " + resolvedCommand + " to fail but got a zero result!");
}
} catch (InterruptedException | IOException e) {
fail("Error while executing " + resolvedCommand);
}
}


@When("^" + Phrases.SUBJECT + " executes \"(.+)\" from a temporary directory")
public void executeCommand(String command) {
String resolvedCommand = placeholderResolver.resolve(command);
try {
File temporaryDirectory = Files.createTempDir();
propertyStore.put(TEMPORARY_DIRECTORY, temporaryDirectory);
int result = Runtime.getRuntime().exec(resolvedCommand, null, temporaryDirectory).waitFor();
if (result != 0) {
fail("Received result code " + result + " after executing " + resolvedCommand);
}
} catch (InterruptedException | IOException e) {
fail("Error while executing " + resolvedCommand);
}
}

@Then("^the temporary directory should have the files:$")
public void verifyTemporaryDirectoryHasFiles(List<String> names) throws Exception {
File temporaryDirectory = (File) propertyStore.get(TEMPORARY_DIRECTORY);
if (temporaryDirectory != null && temporaryDirectory.isDirectory()) {
for (String name : names) {
String resolved = placeholderResolver.resolve(name);
File resolvedFile = new File(temporaryDirectory, resolved);

assertTrue("Expected file " + resolvedFile.getAbsolutePath() + " does not exist!",
resolvedFile.exists());
}
} else {
if (names.size() > 0) {
fail("A temporary directory was not created to verify the existence of any files!");
}
}
}

private static void delete(File folder) throws IOException {
java.nio.file.Files.walkFileTree(Paths.get(folder.getPath()), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
java.nio.file.Files.delete(file);
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
java.nio.file.Files.delete(dir);
return FileVisitResult.CONTINUE;
}
});
}

@After
public void cleanUp() throws IOException {
File temporaryDirectory = (File) propertyStore.get(TEMPORARY_DIRECTORY);
if (temporaryDirectory != null && temporaryDirectory.isDirectory()) {
logger.debug("Cleaning up temporary directory {}", temporaryDirectory);
delete(temporaryDirectory);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package org.chodavarapu.datamill.cucumber;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import org.chodavarapu.datamill.db.DatabaseClient;
import org.chodavarapu.datamill.db.Row;
import org.json.JSONObject;
import rx.Observable;

import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;

/**
* @author Ravi Chodavarapu (rchodava@gmail.com)
*/
public class DatabaseSteps {

private final static String SQL_SELECT = "SELECT * FROM ";
private final static String SQL_WHERE = " WHERE ";
private final static String SQL_AND = " AND ";

private final PlaceholderResolver placeholderResolver;

public DatabaseSteps(PlaceholderResolver placeholderResolver) {
this.placeholderResolver = placeholderResolver;
}

@Given("^the (.+) table in (.+) contains a row with:$")
public void storeDatabaseRow(String tableName, String databaseUrl, String json) {
String resolvedUrl = placeholderResolver.resolve(databaseUrl);
String resolvedJson = placeholderResolver.resolve(json);

JSONObject rowJson = new JSONObject(resolvedJson);

int count = new DatabaseClient(resolvedUrl).insertInto(tableName).row(builder -> {
for (String propertyName : rowJson.keySet()) {
Object value = rowJson.get(propertyName);
if (value != null) {
if (value == JSONObject.NULL) {
builder.put(propertyName, null);
} else if (value instanceof Number || value instanceof String) {
builder.put(propertyName, value);
}
}
}

return builder.build();
}).count().toBlocking().last();

assertEquals("Failed to insert row into " + tableName, 1, count);
}

@Then("^the (.+) table in (.+) should have (.+) where (.+)$")
public void checkDatabaseRowExists(String tableName, String databaseUrl, String criteriaFragment, String testFragment) {
String resolvedUrl = placeholderResolver.resolve(databaseUrl);
String resolvedCriteriaFragment = placeholderResolver.resolve(criteriaFragment);
String resolvedTestFragment = placeholderResolver.resolve(testFragment);
Row row = executeSelect(resolvedUrl, buildQuery(tableName, resolvedCriteriaFragment, resolvedTestFragment)).toBlocking().lastOrDefault(null);
assertThat(row, notNullValue());
}

@Then("^the (.+) table in (.+) should contain a row where (.+)$")
public void checkDatabaseRowExists(String tableName, String databaseUrl, String testFragment) {
String resolvedUrl = placeholderResolver.resolve(databaseUrl);
String resolvedTestFragment = placeholderResolver.resolve(testFragment);
Row row = executeSelect(resolvedUrl, buildQuery(tableName, resolvedTestFragment, null)).toBlocking().lastOrDefault(null);
assertThat(row, notNullValue());
}

private Observable<Row> executeSelect(String resolvedUrl, String sql, Object... parameters) {
return new DatabaseClient(resolvedUrl).query(sql, parameters);
}

protected String buildQuery(String tableName, String testFragment, String criteriaFragment) {
StringBuilder queryBuilder = new StringBuilder(SQL_SELECT);

queryBuilder.append(tableName);
queryBuilder.append(SQL_WHERE);
queryBuilder.append(testFragment);

if (criteriaFragment != null && !criteriaFragment.isEmpty()) {
queryBuilder.append(SQL_AND);
queryBuilder.append(criteriaFragment);
}
return queryBuilder.toString();
}

}
Loading