OptionObjectBundleHook extends AbstractObjectBundleHook { @Override public <T extends IdentifiableObject> void preCreate( T object, ObjectBundle bundle ) { if ( !(object instanceof Option) ) { return; } final Option option = (Option) object; if ( option.getOptionSet() != null && !bundle.containsObject( option.getOptionSet() ) ) { OptionSet optionSet = bundle.getPreheat().get( bundle.getPreheatIdentifier(), OptionSet.class, option.getOptionSet() ); if ( optionSet != null ) { optionSet.addOption( option ); } } }  @Override List<ErrorReport> validate( T object, ObjectBundle bundle ); @Override void preCreate( T object, ObjectBundle bundle );  }
@Test public void preCreate() { OptionSet optionSet = new OptionSet(); optionSet.setUid( "jadhjSHdhs" ); Option option = new Option(); option.setOptionSet( optionSet ); OptionSet persistedOptionSet = new OptionSet(); persistedOptionSet.setUid( "jadhjSHdhs" ); preheat.put( PreheatIdentifier.UID, persistedOptionSet ); ObjectBundleParams objectBundleParams = new ObjectBundleParams(); objectBundleParams.setPreheatIdentifier( PreheatIdentifier.UID ); ObjectBundle bundle = new ObjectBundle( objectBundleParams, preheat, Collections.emptyMap() ); hook.preCreate( option, bundle ); Assert.assertEquals( 1, persistedOptionSet.getOptions().size() ); Assert.assertSame( option, persistedOptionSet.getOptions().get( 0 ) ); }
@Test public void preCreateOptionSetAvailable() { OptionSet optionSet = new OptionSet(); optionSet.setUid( "jadhjSHdhs" ); Option option = new Option(); option.setOptionSet( optionSet ); OptionSet persistedOptionSet = new OptionSet(); persistedOptionSet.setUid( "jadhjSHdhs" ); preheat.put( PreheatIdentifier.UID, persistedOptionSet ); ObjectBundleParams objectBundleParams = new ObjectBundleParams(); objectBundleParams.setPreheatIdentifier( PreheatIdentifier.UID ); ObjectBundle bundle = new ObjectBundle( objectBundleParams, preheat, Collections.singletonMap( OptionSet.class, Collections.singletonList( optionSet ) ) ); hook.preCreate( option, bundle ); Assert.assertEquals( 0, persistedOptionSet.getOptions().size() ); }