Skip to content

Commit

Permalink
Merge pull request #49 from snkinard/issue-15
Browse files Browse the repository at this point in the history
Created a new jolt-complete module. Created a chainr factory
  • Loading branch information
Milo Simpson committed Sep 4, 2013
2 parents 107976e + c8c804e commit 1f00174
Show file tree
Hide file tree
Showing 7 changed files with 353 additions and 8 deletions.
36 changes: 36 additions & 0 deletions complete/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.bazaarvoice.jolt</groupId>
<artifactId>jolt-parent</artifactId>
<version>0.0.9-SNAPSHOT</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>

<artifactId>jolt-complete</artifactId>
<name>Jolt Complete</name>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>com.bazaarvoice.jolt</groupId>
<artifactId>jolt-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.bazaarvoice.jolt</groupId>
<artifactId>json-utils</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
132 changes: 132 additions & 0 deletions complete/src/main/java/com/bazarvoice/jolt/ChainrFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Copyright 2013 Bazaarvoice, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.bazarvoice.jolt;

import com.bazaarvoice.jolt.Chainr;
import com.bazaarvoice.jolt.JsonUtils;
import com.bazaarvoice.jolt.chainr.instantiator.ChainrInstantiator;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
* A factory class with various static methods that return instances of Chainr.
*/
public class ChainrFactory {

/**
* Builds a Chainr instance using the spec described in the data via the class path that is passed in.
*
* @param chainrSpecClassPath The class path that points to the chainr spec.
* @return a Chainr instance
*/
public static Chainr fromClassPath( String chainrSpecClassPath ) {
return fromClassPath( chainrSpecClassPath, null );
}

/**
* Builds a Chainr instance using the spec described in the data via the class path that is passed in.
*
* @param chainrSpecClassPath The class path that points to the chainr spec.
* @param chainrInstantiator the ChainrInstantiator to use to initialze the Chainr instance
* @return a Chainr instance
*/
public static Chainr fromClassPath( String chainrSpecClassPath, ChainrInstantiator chainrInstantiator ) {
InputStream inputStream = Object.class.getResourceAsStream( chainrSpecClassPath );
return getChainr( chainrInstantiator, inputStream, chainrSpecClassPath );
}

/**
* Builds a Chainr instance using the spec described in the data via the file path that is passed in.
*
* @param chainrSpecFilePath The file path that points to the chainr spec.
* @return a Chainr instance
*/
public static Chainr fromFileSystem( String chainrSpecFilePath ) {
return fromFileSystem( chainrSpecFilePath, null );
}

/**
* Builds a Chainr instance using the spec described in the data via the file path that is passed in.
*
* @param chainrSpecFilePath The file path that points to the chainr spec.
* @param chainrInstantiator the ChainrInstantiator to use to initialze the Chainr instance
* @return a Chainr instance
*/
public static Chainr fromFileSystem( String chainrSpecFilePath, ChainrInstantiator chainrInstantiator ) {
FileInputStream fileInputStream;
try {
fileInputStream = new FileInputStream( chainrSpecFilePath );
} catch ( FileNotFoundException e ) {
throw new RuntimeException( "Unable to load chainr spec file " + chainrSpecFilePath );
}
return getChainr( chainrInstantiator, fileInputStream, chainrSpecFilePath );
}

/**
* Builds a Chainr instance using the spec described in the File that is passed in.
*
* @param chainrSpecFile The File which contains the chainr spec.
* @return a Chainr instance
*/
public static Chainr fromFile( File chainrSpecFile ) {
return fromFile( chainrSpecFile, null );
}

/**
* Builds a Chainr instance using the spec described in the File that is passed in.
*
* @param chainrSpecFile The File which contains the chainr spec.
* @param chainrInstantiator the ChainrInstantiator to use to initialze the Chainr instance
* @return a Chainr instance
*/
public static Chainr fromFile( File chainrSpecFile, ChainrInstantiator chainrInstantiator ) {
FileInputStream fileInputStream;
try {
fileInputStream = new FileInputStream( chainrSpecFile );
} catch ( FileNotFoundException e ) {
throw new RuntimeException( "Unable to load chainr spec file " + chainrSpecFile.getAbsolutePath() );
}
return getChainr( chainrInstantiator, fileInputStream, chainrSpecFile.getAbsolutePath() );
}

/**
* The main engine in ChainrFactory for building a Chainr Instance.
*
* @param chainrInstantiator The ChainrInstantiator to use. If null it will not be used.
* @param inputStream The input stream to read the json data from
* @param filePath The path to the file
* @return the Chainr instance created from the chainrInstantiator and inputStream
*/
private static Chainr getChainr( ChainrInstantiator chainrInstantiator, InputStream inputStream, String filePath ) {
Chainr chainr;
try {
Object chainrSpec = JsonUtils.jsonToObject( inputStream );
if (chainrInstantiator == null ) {
chainr = Chainr.fromSpec( chainrSpec );
}
else {
chainr = Chainr.fromSpec( chainrSpec, chainrInstantiator );
}
} catch ( IOException e ) {
throw new RuntimeException( "Unable to load chainr spec file " + filePath );
}
return chainr;
}
}
139 changes: 139 additions & 0 deletions complete/src/test/java/com/bazaarvoice/jolt/ChainrFactoryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* Copyright 2013 Bazaarvoice, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.bazaarvoice.jolt;

import com.bazaarvoice.jolt.chainr.instantiator.DefaultChainrInstantiator;
import com.bazarvoice.jolt.ChainrFactory;
import org.testng.AssertJUnit;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.File;

public class ChainrFactoryTest {

private static final String MALFORMED_INPUT_FILENAME = "malformed-input.json";
private static final String WELLFORMED_INPUT_FILENAME = "wellformed-input.json";

private static final String CLASSPATH = "/json/";
private String fileSystemPath;
private File wellformedFile;
private File malformedFile;

@BeforeClass
public void setup() {
fileSystemPath = getFileSystemPath();
wellformedFile = new File( fileSystemPath + WELLFORMED_INPUT_FILENAME );
malformedFile = new File( fileSystemPath + MALFORMED_INPUT_FILENAME );
}

@Test
public void testGetChainrInstanceFromClassPath_success()
throws Exception {
Object result = ChainrFactory.fromClassPath( CLASSPATH + WELLFORMED_INPUT_FILENAME );
AssertJUnit.assertTrue( "ChainrFactory did not return an instance of Chainr.", result instanceof Chainr );
}

@Test( expectedExceptions = RuntimeException.class, expectedExceptionsMessageRegExp = "Unable to load chainr spec file.*" )
public void testGetChainrInstanceFromClassPath_error()
throws Exception {
ChainrFactory.fromClassPath( CLASSPATH + MALFORMED_INPUT_FILENAME );
}

@Test
public void testGetChainrInstanceFromClassPathWithInstantiator_success()
throws Exception {
Object result = ChainrFactory.fromClassPath( CLASSPATH + WELLFORMED_INPUT_FILENAME, new DefaultChainrInstantiator() );
AssertJUnit.assertTrue( "ChainrFactory did not return an instance of Chainr.", result instanceof Chainr );
}

@Test( expectedExceptions = RuntimeException.class, expectedExceptionsMessageRegExp = "Unable to load chainr spec file.*" )
public void testGetChainrInstanceFromClassPathWithInstantiator_error()
throws Exception {
ChainrFactory.fromClassPath( CLASSPATH + MALFORMED_INPUT_FILENAME, new DefaultChainrInstantiator() );
}

@Test
public void testGetChainrInstanceFromFileSystem_success()
throws Exception {
Object result = ChainrFactory.fromFileSystem( fileSystemPath + WELLFORMED_INPUT_FILENAME );
AssertJUnit.assertTrue( "ChainrFactory did not return an instance of Chainr.", result instanceof Chainr );
}

@Test( expectedExceptions = RuntimeException.class, expectedExceptionsMessageRegExp = "Unable to load chainr spec file.*" )
public void testGetChainrInstanceFromFileSystem_error()
throws Exception {
ChainrFactory.fromFileSystem( fileSystemPath + MALFORMED_INPUT_FILENAME );
}

@Test
public void testGetChainrInstanceFromFileSystemWithInstantiator_success()
throws Exception {
Object result = ChainrFactory.fromFileSystem( fileSystemPath + WELLFORMED_INPUT_FILENAME, new DefaultChainrInstantiator() );
AssertJUnit.assertTrue( "ChainrFactory did not return an instance of Chainr.", result instanceof Chainr );
}

@Test(expectedExceptions = RuntimeException.class, expectedExceptionsMessageRegExp = "Unable to load chainr spec file.*")
public void testGetChainrInstanceFromFileSystemWithInstantiator_error()
throws Exception {
ChainrFactory.fromFileSystem( fileSystemPath + MALFORMED_INPUT_FILENAME, new DefaultChainrInstantiator() );
}

@Test
public void testGetChainrInstanceFromFile_success()
throws Exception {
Object result = ChainrFactory.fromFile( wellformedFile );
AssertJUnit.assertTrue( "ChainrFactory did not return an instance of Chainr.", result instanceof Chainr );
}

@Test( expectedExceptions = RuntimeException.class, expectedExceptionsMessageRegExp = "Unable to load chainr spec file.*" )
public void testGetChainrInstanceFromFile_error()
throws Exception {
ChainrFactory.fromFile( malformedFile );
}

@Test
public void testGetChainrInstanceFromFileWithInstantiator_success()
throws Exception {
Object result = ChainrFactory.fromFile( wellformedFile, new DefaultChainrInstantiator() );
AssertJUnit.assertTrue( "ChainrFactory did not return an instance of Chainr.", result instanceof Chainr );
}

@Test(expectedExceptions = RuntimeException.class, expectedExceptionsMessageRegExp = "Unable to load chainr spec file.*")
public void testGetChainrInstanceFromFileWithInstantiator_error()
throws Exception {
ChainrFactory.fromFile( malformedFile, new DefaultChainrInstantiator() );
}

private String getFileSystemPath() {
// The horrifying code you see below chooses the "correct" path to the test files to feed to the CLI.
// When the test is run by maven the working directory is $JOLT_CHECKOUT/tools/. The code figures this out
// by examining the last directory in the file path for the working directory. If it's 'tools' then it
// chooses the path to the resource files copied by maven into the target/ directory. Obviously, this assumes
// that you did not name $JOLT_CHECKOUT 'tools'. If that check fails then the path is chosen with the assumption
// that the test is running in an IDE (Intellij IDEA in my case). Your mileage with other IDE's may very.
String path = System.getProperty( "user.dir" );
if ( path.endsWith( "complete" ) ) {
// This test is being run by maven
path += "//target//test-classes//json//";
} else {
// This test is being run in an IDE (IntelliJ IDEA)
path += "//complete//src//test//resources//json//";
}
return path;
}

}
22 changes: 22 additions & 0 deletions complete/src/test/resources/json/malformed-input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"operation" "cardinality",
"spec": {
"facets": {
"statistics": {
"_type":"MANY"
}
}
}
},
{
"operation": "default",
"spec": {
"facets": {
"statistics": {
"id":"abc123"
}
}
}
}
]
22 changes: 22 additions & 0 deletions complete/src/test/resources/json/wellformed-input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"operation": "cardinality",
"spec": {
"facets": {
"statistics": {
"_type":"MANY"
}
}
}
},
{
"operation": "default",
"spec": {
"facets": {
"statistics": {
"id":"abc123"
}
}
}
}
]
3 changes: 2 additions & 1 deletion json-utils/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<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/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

Expand Down
7 changes: 0 additions & 7 deletions json-utils/src/test/java/com/bazaarvoice/jolt/DiffyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@
import java.util.HashMap;
import java.util.List;

/**
* Created by IntelliJ IDEA.
* User: nate.forman
* Date: 8/3/12
* Time: 11:47 PM
* To change this template use File | Settings | File Templates.
*/
public class DiffyTest {

private Diffy unit;
Expand Down

0 comments on commit 1f00174

Please sign in to comment.