JobParamsAdapter extends XmlAdapter<JobParams, Map<String, Serializable>> { public JobParams marshal( Map<String, Serializable> v ) throws Exception { Object variables = v.get( VARIABLES ); Object parameters = v.get( PARAMETERS ); if ( parameters != null && parameters instanceof Map && variables != null && variables instanceof Map ) { Map<String, String> paramMap = (Map) parameters; Map<String, String> variableMap = (Map) variables; if ( !paramMap.isEmpty() && !variableMap.isEmpty() ) { for ( Map.Entry<String, String> paramEntry : paramMap.entrySet() ) { if ( variableMap.containsKey( paramEntry.getKey() ) && paramEntry.getValue() != null ) { variableMap.remove( paramEntry.getKey() ); } } } } ArrayList<JobParam> params = new ArrayList<JobParam>(); for ( Map.Entry<String, Serializable> entry : v.entrySet() ) { if ( entry != null && entry.getKey() != null && entry.getValue() != null ) { if ( entry.getValue() instanceof Collection ) { for ( Object iValue : (Collection<?>) entry.getValue() ) { if ( iValue != null ) { JobParam jobParam = new JobParam(); jobParam.name = entry.getKey(); jobParam.value = iValue.toString(); params.add( jobParam ); } } } else if ( entry.getValue() instanceof Map ) { ( (Map<String, Serializable>) entry.getValue() ).forEach( ( key, value ) -> { if ( value != null ) { JobParam jobParam = new JobParam(); jobParam.name = key; jobParam.value = value.toString(); params.add( jobParam ); } } ); } else { JobParam jobParam = new JobParam(); jobParam.name = entry.getKey(); jobParam.value = entry.getValue().toString(); params.add( jobParam ); } } } JobParams jobParams = new JobParams(); jobParams.jobParams = params.toArray( new JobParam[0] ); return jobParams; }  JobParams marshal( Map<String, Serializable> v ); Map<String, Serializable> unmarshal( JobParams v );  }
@Test public void testMarshal() throws Exception { JobParamsAdapter adapter = new JobParamsAdapter(); Map<String, Serializable> dataMap = new HashMap<String, Serializable>(); dataMap.put( "a", "A" ); dataMap.put( "bb", "[B]" ); dataMap.put( "ccc", "[C].[CCC]" ); dataMap.put( "dddd", "[D].[DDD,ddd]" ); dataMap.put( "eeeee", null ); dataMap.put( null, "FFFFFF" ); JobParams expectedJobParams = createJobParams( new JobParam[]{ createJobParam( "a", "A" ), createJobParam( "bb", "[B]" ), createJobParam( "ccc", "[C].[CCC]" ), createJobParam( "dddd", "[D].[DDD,ddd]" ) } ); final JobParams resultJobParams = adapter.marshal( dataMap ); assertNotNull( resultJobParams ); assertNotNull( resultJobParams.jobParams ); java.util.Arrays.sort( resultJobParams.jobParams, new JobParamWholeComparator() ); assertJobParamArrayEquals( "", expectedJobParams.jobParams, resultJobParams.jobParams ); }
@Test public void testMarshalMultiValue() throws Exception { JobParamsAdapter adapter = new JobParamsAdapter(); Map<String, Serializable> dataMap = new HashMap<String, Serializable>(); dataMap.put( "a", "A" ); dataMap.put( "bb", "[B]" ); ArrayList<String> cValue = castAsArrayList( new String[] { "[C].[CCC]", "[D].[DDD,ddd]" } ); dataMap.put( "ccc", cValue ); ArrayList<String> eValue = castAsArrayList( new String[] { null, "FFFFFF" } ); dataMap.put( "eeeee", eValue ); JobParams expectedJobParams = createJobParams( new JobParam[]{ createJobParam( "a", "A" ), createJobParam( "bb", "[B]" ), createJobParam( "ccc", "[C].[CCC]" ), createJobParam( "ccc", "[D].[DDD,ddd]" ), createJobParam( "eeeee", "FFFFFF" ) } ); final JobParams resultJobParams = adapter.marshal( dataMap ); assertNotNull( resultJobParams ); assertNotNull( resultJobParams.jobParams ); Arrays.sort( resultJobParams.jobParams, new JobParamWholeComparator() ); assertJobParamArrayEquals( "", expectedJobParams.jobParams, resultJobParams.jobParams ); }
@Test public void testMarshalRemovesVariableDuplicate() throws Exception { JobParamsAdapter adapter = new JobParamsAdapter(); Map<String, Serializable> dataMap = new HashMap<String, Serializable>(); HashMap<String, String> variables = new HashMap<>(); variables.put( "test1", "val1" ); variables.put( "test2", "val2" ); HashMap<String, String> parameters = new HashMap<>(); parameters.put( "test2", "val2Updated" ); dataMap.put( "variables", variables ); dataMap.put( "parameters", parameters ); dataMap.put( "test3", "val3" ); JobParams expectedJobParams = createJobParams( new JobParam[]{ createJobParam( "test1", "val1" ), createJobParam( "test2", "val2Updated" ), createJobParam( "test3", "val3" ) } ); final JobParams resultJobParams = adapter.marshal( dataMap ); assertNotNull( resultJobParams ); assertNotNull( resultJobParams.jobParams ); Arrays.sort( resultJobParams.jobParams, new JobParamWholeComparator() ); assertJobParamArrayEquals( "", expectedJobParams.jobParams, resultJobParams.jobParams ); }