-
Notifications
You must be signed in to change notification settings - Fork 331
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from snkinard/issue-15
Created a new jolt-complete module. Created a chainr factory
- Loading branch information
Showing
7 changed files
with
353 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
132
complete/src/main/java/com/bazarvoice/jolt/ChainrFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
139
complete/src/test/java/com/bazaarvoice/jolt/ChainrFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters