Java Code Examples for com.bazaarvoice.jolt.JsonUtils#customJsonUtil()

The following examples show how to use com.bazaarvoice.jolt.JsonUtils#customJsonUtil() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MappingTest2.java    From jolt with Apache License 2.0 4 votes vote down vote up
@Test
public void testPolymorphicJacksonSerializationAndDeserialization()
{
    ObjectMapper mapper = new ObjectMapper();

    SimpleModule testModule = new SimpleModule("testModule", new Version(1, 0, 0, null, null, null))
            .addDeserializer( QueryFilter.class, new QueryFilterDeserializer() )
            .addSerializer( LogicalFilter2.class, new LogicalFilter2Serializer() );

    mapper.registerModule(testModule);

    // Verifying that we can pass in a custom Mapper and create a new JsonUtil
    JsonUtil jsonUtil = JsonUtils.customJsonUtil( mapper );

    String testFixture = "/jsonUtils/testdomain/two/queryFilter-realAndLogical2.json";

    // TEST JsonUtil and our deserialization logic
    QueryFilter queryFilter = jsonUtil.classpathToType( testFixture, new TypeReference<QueryFilter>() {} );

    // Make sure the hydrated QFilter looks right
    Assert.assertTrue( queryFilter instanceof LogicalFilter2 );
    Assert.assertEquals( QueryParam.AND, queryFilter.getQueryParam() );
    Assert.assertTrue( queryFilter.isLogical() );
    Assert.assertEquals( 3, queryFilter.getFilters().size() );
    Assert.assertNotNull( queryFilter.getFilters().get( QueryParam.OR ) );

    // Make sure one of the top level RealFilters looks right
    QueryFilter productIdFilter = queryFilter.getFilters().get( QueryParam.PRODUCTID );
    Assert.assertTrue( productIdFilter.isReal() );
    Assert.assertEquals( QueryParam.PRODUCTID, productIdFilter.getQueryParam() );
    Assert.assertEquals( "Acme-1234", productIdFilter.getValue() );

    // Make sure the nested OR looks right
    QueryFilter orFilter = queryFilter.getFilters().get( QueryParam.OR );
    Assert.assertTrue( orFilter.isLogical() );
    Assert.assertEquals( QueryParam.OR, orFilter.getQueryParam() );
    Assert.assertEquals( 2, orFilter.getFilters().size() );

    // Make sure nested AND looks right
    QueryFilter nestedAndFilter = orFilter.getFilters().get( QueryParam.AND );
    Assert.assertTrue( nestedAndFilter.isLogical() );
    Assert.assertEquals( QueryParam.AND, nestedAndFilter.getQueryParam() );
    Assert.assertEquals( 2, nestedAndFilter.getFilters().size() );


    // SERIALIZE TO STRING to test serialization logic
    String unitTestString = jsonUtil.toJsonString( queryFilter );

    // LOAD and Diffy the plain vanilla JSON versions of the documents
    Map<String, Object> actual   = JsonUtils.jsonToMap( unitTestString );
    Map<String, Object> expected = JsonUtils.classpathToMap( testFixture );

    // Diffy the vanilla versions
    Diffy.Result result = diffy.diff( expected, actual );
    if (!result.isEmpty()) {
        Assert.fail( "Failed.\nhere is a diff:\nexpected: " + JsonUtils.toJsonString( result.expected ) + "\n  actual: " + JsonUtils.toJsonString( result.actual ) );
    }
}
 
Example 2
Source File: MappingTest4.java    From jolt with Apache License 2.0 4 votes vote down vote up
@Test
public void testPolymorphicJacksonSerializationAndDeserialization()
{
    ObjectMapper mapper = new ObjectMapper();

    SimpleModule testModule = new SimpleModule("testModule", new Version(1, 0, 0, null, null, null))
            .addDeserializer( QueryFilter4.class, new QueryFilter4Deserializer() );

    mapper.registerModule(testModule);

    // Verifying that we can pass in a custom Mapper and create a new JsonUtil
    JsonUtil jsonUtil = JsonUtils.customJsonUtil( mapper );

    String testFixture = "/jsonUtils/testdomain/four/queryFilter-realAndLogical4.json";

    // TEST JsonUtil and our deserialization logic
    QueryFilter4 queryFilter = jsonUtil.classpathToType( testFixture, new TypeReference<QueryFilter4>() {} );

    // Make sure the hydrated QFilter looks right
    Assert.assertTrue( queryFilter instanceof LogicalFilter4);
    Assert.assertEquals( QueryParam.AND, queryFilter.getQueryParam() );
    Assert.assertTrue( queryFilter.isLogical() );
    Assert.assertEquals( 3, queryFilter.getFilters().size() );
    Assert.assertNotNull( queryFilter.getFilters().get( QueryParam.OR ) );

    // Make sure one of the top level RealFilters looks right
    QueryFilter4 productIdFilter = queryFilter.getFilters().get( QueryParam.PRODUCTID );
    Assert.assertTrue( productIdFilter.isReal() );
    Assert.assertTrue( productIdFilter instanceof StringRealFilter4);
    StringRealFilter4 stringRealProductIdFilter = (StringRealFilter4) productIdFilter;
    Assert.assertEquals( QueryParam.PRODUCTID, stringRealProductIdFilter.getQueryParam() );
    Assert.assertEquals( "Acme-1234", stringRealProductIdFilter.getValue() );

    // Make sure the nested OR looks right
    QueryFilter4 orFilter = queryFilter.getFilters().get( QueryParam.OR );
    Assert.assertTrue( orFilter.isLogical() );
    Assert.assertEquals( QueryParam.OR, orFilter.getQueryParam() );
    Assert.assertEquals( 2, orFilter.getFilters().size() );

    // Make sure nested AND looks right
    QueryFilter4 nestedAndFilter = orFilter.getFilters().get( QueryParam.AND );
    Assert.assertTrue( nestedAndFilter.isLogical() );
    Assert.assertEquals( QueryParam.AND, nestedAndFilter.getQueryParam() );
    Assert.assertEquals( 2, nestedAndFilter.getFilters().size() );


    // SERIALIZE TO STRING to test serialization logic
    String unitTestString = jsonUtil.toJsonString( queryFilter );

    // LOAD and Diffy the plain vanilla JSON versions of the documents
    Map<String, Object> actual   = JsonUtils.jsonToMap( unitTestString );
    Map<String, Object> expected = JsonUtils.classpathToMap( testFixture );

    // Diffy the vanilla versions
    Diffy.Result result = diffy.diff( expected, actual );
    if (!result.isEmpty()) {
        Assert.fail( "Failed.\nhere is a diff:\nexpected: " + JsonUtils.toJsonString( result.expected ) + "\n  actual: " + JsonUtils.toJsonString( result.actual ) );
    }
}
 
Example 3
Source File: MappingTest3.java    From jolt with Apache License 2.0 4 votes vote down vote up
@Test
public void testPolymorphicJacksonSerializationAndDeserialization()
{
    ObjectMapper mapper = new ObjectMapper();

    SimpleModule testModule = new SimpleModule("testModule", new Version(1, 0, 0, null, null, null))
            .addDeserializer( QueryFilter.class, new QueryFilterDeserializer() );

    mapper.registerModule(testModule);

    // Verifying that we can pass in a custom Mapper and create a new JsonUtil
    JsonUtil jsonUtil = JsonUtils.customJsonUtil( mapper );

    String testFixture = "/jsonUtils/testdomain/two/queryFilter-realAndLogical2.json";

    // TEST JsonUtil and our deserialization logic
    QueryFilter queryFilter = jsonUtil.classpathToType( testFixture, new TypeReference<QueryFilter>() {} );

    // Make sure the hydrated QFilter looks right
    Assert.assertTrue( queryFilter instanceof LogicalFilter3 );
    Assert.assertEquals( QueryParam.AND, queryFilter.getQueryParam() );
    Assert.assertTrue( queryFilter.isLogical() );
    Assert.assertEquals( 3, queryFilter.getFilters().size() );
    Assert.assertNotNull( queryFilter.getFilters().get( QueryParam.OR ) );

    // Make sure one of the top level RealFilters looks right
    QueryFilter productIdFilter = queryFilter.getFilters().get( QueryParam.PRODUCTID );
    Assert.assertTrue( productIdFilter.isReal() );
    Assert.assertEquals( QueryParam.PRODUCTID, productIdFilter.getQueryParam() );
    Assert.assertEquals( "Acme-1234", productIdFilter.getValue() );

    // Make sure the nested OR looks right
    QueryFilter orFilter = queryFilter.getFilters().get( QueryParam.OR );
    Assert.assertTrue( orFilter.isLogical() );
    Assert.assertEquals( QueryParam.OR, orFilter.getQueryParam() );
    Assert.assertEquals( 2, orFilter.getFilters().size() );

    // Make sure nested AND looks right
    QueryFilter nestedAndFilter = orFilter.getFilters().get( QueryParam.AND );
    Assert.assertTrue( nestedAndFilter.isLogical() );
    Assert.assertEquals( QueryParam.AND, nestedAndFilter.getQueryParam() );
    Assert.assertEquals( 2, nestedAndFilter.getFilters().size() );


    // SERIALIZE TO STRING to test serialization logic
    String unitTestString = jsonUtil.toJsonString( queryFilter );

    // LOAD and Diffy the plain vanilla JSON versions of the documents
    Map<String, Object> actual   = JsonUtils.jsonToMap( unitTestString );
    Map<String, Object> expected = JsonUtils.classpathToMap( testFixture );

    // Diffy the vanilla versions
    Diffy.Result result = diffy.diff( expected, actual );
    if (!result.isEmpty()) {
        Assert.fail( "Failed.\nhere is a diff:\nexpected: " + JsonUtils.toJsonString( result.expected ) + "\n  actual: " + JsonUtils.toJsonString( result.actual ) );
    }
}
 
Example 4
Source File: MappingTest5.java    From jolt with Apache License 2.0 4 votes vote down vote up
@Test
public void testPolymorphicJacksonSerializationAndDeserialization()
{
    ObjectMapper mapper = new ObjectMapper();

    SimpleModule testModule = new SimpleModule("testModule", new Version(1, 0, 0, null, null, null))
            .addDeserializer( QueryFilter5.class, new QueryFilter5Deserializer() );

    mapper.registerModule(testModule);

    // Verifying that we can pass in a custom Mapper and create a new JsonUtil
    JsonUtil jsonUtil = JsonUtils.customJsonUtil( mapper );

    String testFixture = "/jsonUtils/testdomain/five/queryFilter-realAndLogical5.json";

    // TEST JsonUtil and our deserialization logic
    QueryFilter5 queryFilter = jsonUtil.classpathToType( testFixture, new TypeReference<QueryFilter5>() {} );

    // Make sure the hydrated QFilter looks right
    Assert.assertTrue( queryFilter instanceof LogicalFilter5);
    LogicalFilter5 andFilter = (LogicalFilter5) queryFilter;
    Assert.assertEquals( Operator.AND, andFilter.getOperator() );
    Assert.assertNotNull(andFilter.getValues());
    Assert.assertEquals(3, andFilter.getValues().size());

    // Make sure one of the top level RealFilters looks right
    QueryFilter5 productIdFilter = andFilter.getValues().get(1);
    Assert.assertTrue( productIdFilter instanceof StringRealFilter5);
    StringRealFilter5 stringRealProductIdFilter = (StringRealFilter5) productIdFilter;
    Assert.assertEquals( Field.PRODUCTID, stringRealProductIdFilter.getField() );
    Assert.assertEquals( Operator.EQ, stringRealProductIdFilter.getOperator() );
    Assert.assertEquals( "Acme-1234", stringRealProductIdFilter.getValues().get(0) );

    // Make sure the nested OR looks right
    QueryFilter5 orFilter = andFilter.getValues().get(2);
    Assert.assertTrue( orFilter instanceof LogicalFilter5 );
    LogicalFilter5 realOrFilter = (LogicalFilter5) orFilter;
    Assert.assertEquals( Operator.OR, realOrFilter.getOperator() );
    Assert.assertEquals( 2, realOrFilter.getValues().size() );

    // Make sure nested AND looks right
    QueryFilter5 nestedAndFilter = realOrFilter.getValues().get(1);
    Assert.assertTrue( nestedAndFilter instanceof LogicalFilter5 );
    Assert.assertEquals( Operator.AND, nestedAndFilter.getOperator() );
    Assert.assertEquals( 3, nestedAndFilter.getValues().size() );


    // SERIALIZE TO STRING to test serialization logic
    String unitTestString = jsonUtil.toJsonString( queryFilter );

    // LOAD and Diffy the plain vanilla JSON versions of the documents
    Map<String, Object> actual   = JsonUtils.jsonToMap( unitTestString );
    Map<String, Object> expected = JsonUtils.classpathToMap( testFixture );

    // Diffy the vanilla versions
    Diffy.Result result = diffy.diff( expected, actual );
    if (!result.isEmpty()) {
        Assert.fail( "Failed.\nhere is a diff:\nexpected: " + JsonUtils.toJsonString( result.expected ) + "\n  actual: " + JsonUtils.toJsonString( result.actual ) );
    }
}
 
Example 5
Source File: MappingTest1.java    From jolt with Apache License 2.0 4 votes vote down vote up
@Test
public void testPolymorphicJacksonSerializationAndDeserialization()
{
    ObjectMapper mapper = new ObjectMapper();

    SimpleModule testModule = new SimpleModule("testModule", new Version(1, 0, 0, null, null, null))
            .addDeserializer( QueryFilter.class, new QueryFilter1Deserializer() );

    mapper.registerModule(testModule);

    // Verifying that we can pass in a custom Mapper and create a new JsonUtil
    JsonUtil jsonUtil = JsonUtils.customJsonUtil( mapper );

    String testFixture = "/jsonUtils/testdomain/one/queryFilter-realAndLogical.json";

    // TEST JsonUtil and our deserialization logic
    QueryFilter queryFilter = jsonUtil.classpathToType( testFixture, new TypeReference<QueryFilter>() {} );

    // Make sure the hydrated queryFilter looks right
    Assert.assertTrue( queryFilter instanceof LogicalFilter1 );
    Assert.assertEquals( QueryParam.AND, queryFilter.getQueryParam() );
    Assert.assertTrue( queryFilter.isLogical() );
    Assert.assertEquals( 3, queryFilter.getFilters().size() );
    Assert.assertNotNull( queryFilter.getFilters().get( QueryParam.OR ) );

    // Make sure one of the top level RealFilters looks right
    QueryFilter productIdFilter = queryFilter.getFilters().get( QueryParam.PRODUCTID );
    Assert.assertTrue( productIdFilter.isReal() );
    Assert.assertEquals( QueryParam.PRODUCTID, productIdFilter.getQueryParam() );
    Assert.assertEquals( "Acme-1234", productIdFilter.getValue() );

    // Make sure the nested OR looks right
    QueryFilter orFilter = queryFilter.getFilters().get( QueryParam.OR );
    Assert.assertTrue( orFilter.isLogical() );
    Assert.assertEquals( QueryParam.OR, orFilter.getQueryParam() );
    Assert.assertEquals( 2, orFilter.getFilters().size() );

    // Make sure nested AND looks right
    QueryFilter nestedAndFilter = orFilter.getFilters().get( QueryParam.AND );
    Assert.assertTrue( nestedAndFilter.isLogical() );
    Assert.assertEquals( QueryParam.AND, nestedAndFilter.getQueryParam() );
    Assert.assertEquals( 2, nestedAndFilter.getFilters().size() );


    // SERIALIZE TO STRING to test serialization logic
    String unitTestString = jsonUtil.toJsonString( queryFilter );

    // LOAD and Diffy the plain vanilla JSON versions of the documents
    Map<String, Object> actual   = JsonUtils.jsonToMap( unitTestString );
    Map<String, Object> expected = JsonUtils.classpathToMap( testFixture );

    // Diffy the vanilla versions
    Diffy.Result result = diffy.diff( expected, actual );
    if (!result.isEmpty()) {
        Assert.fail( "Failed.\nhere is a diff:\nexpected: " + JsonUtils.toJsonString( result.expected ) + "\n  actual: " + JsonUtils.toJsonString( result.actual ) );
    }
}