PipelineOptionsValidator { public static <T extends PipelineOptions> T validate(Class<T> klass, PipelineOptions options) { return validate(klass, options, false); }  static T validate(Class<T> klass, PipelineOptions options); static T validateCli(Class<T> klass, PipelineOptions options);  }
@Test public void testWhenRequiredOptionIsSet() { Required required = PipelineOptionsFactory.as(Required.class); required.setRunner(CrashingRunner.class); required.setObject("blah"); PipelineOptionsValidator.validate(Required.class, required); }
@Test public void testWhenRequiredOptionIsSetAndCleared() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage( "Missing required value for " + "[public abstract java.lang.String org.apache.beam." + "sdk.options.PipelineOptionsValidatorTest$Required.getObject(), \"Fake Description\"]."); Required required = PipelineOptionsFactory.as(Required.class); required.setObject("blah"); required.setObject(null); PipelineOptionsValidator.validate(Required.class, required); }
@Test public void testWhenRequiredOptionIsNeverSet() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage( "Missing required value for " + "[public abstract java.lang.String org.apache.beam." + "sdk.options.PipelineOptionsValidatorTest$Required.getObject(), \"Fake Description\"]."); Required required = PipelineOptionsFactory.as(Required.class); PipelineOptionsValidator.validate(Required.class, required); }
@Test public void testWhenRequiredOptionIsNeverSetOnSuperInterface() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage( "Missing required value for " + "[public abstract java.lang.String org.apache.beam." + "sdk.options.PipelineOptionsValidatorTest$Required.getObject(), \"Fake Description\"]."); PipelineOptions options = PipelineOptionsFactory.create(); PipelineOptionsValidator.validate(Required.class, options); }
@Test public void testValidationOnOverriddenMethods() throws Exception { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage( "Missing required value for " + "[public abstract java.lang.String org.apache.beam." + "sdk.options.PipelineOptionsValidatorTest$Required.getObject(), \"Fake Description\"]."); SubClassValidation required = PipelineOptionsFactory.as(SubClassValidation.class); PipelineOptionsValidator.validate(Required.class, required); }
@Test public void testWhenOneOfRequiredGroupIsSetIsValid() { GroupRequired groupRequired = PipelineOptionsFactory.as(GroupRequired.class); groupRequired.setFoo("foo"); groupRequired.setBar(null); groupRequired.setRunner(CrashingRunner.class); PipelineOptionsValidator.validate(GroupRequired.class, groupRequired); groupRequired.setFoo(null); groupRequired.setBar("bar"); PipelineOptionsValidator.validate(GroupRequired.class, groupRequired); }
@Test public void testWhenNoneOfRequiredGroupIsSetThrowsException() { GroupRequired groupRequired = PipelineOptionsFactory.as(GroupRequired.class); groupRequired.setRunner(CrashingRunner.class); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Missing required value for group [ham]"); expectedException.expectMessage("properties"); expectedException.expectMessage("getFoo"); expectedException.expectMessage("getBar"); PipelineOptionsValidator.validate(GroupRequired.class, groupRequired); }
@Test public void testWhenOneOfMultipleRequiredGroupsIsSetIsValid() { MultiGroupRequired multiGroupRequired = PipelineOptionsFactory.as(MultiGroupRequired.class); multiGroupRequired.setRunner(CrashingRunner.class); multiGroupRequired.setFoo("eggs"); PipelineOptionsValidator.validate(MultiGroupRequired.class, multiGroupRequired); }
@Test public void testWhenOptionIsDefinedInMultipleSuperInterfacesAndIsNotPresentFailsRequirement() { RightOptions rightOptions = PipelineOptionsFactory.as(RightOptions.class); rightOptions.setBoth("foo"); rightOptions.setRunner(CrashingRunner.class); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Missing required value for group"); expectedException.expectMessage("getFoo"); PipelineOptionsValidator.validate(JoinedOptions.class, rightOptions); }
@Test public void testWhenOptionIsDefinedInMultipleSuperInterfacesMeetsGroupRequirement() { RightOptions rightOpts = PipelineOptionsFactory.as(RightOptions.class); rightOpts.setFoo("true"); rightOpts.setBoth("bar"); LeftOptions leftOpts = PipelineOptionsFactory.as(LeftOptions.class); leftOpts.setFoo("Untrue"); leftOpts.setBoth("Raise the"); rightOpts.setRunner(CrashingRunner.class); leftOpts.setRunner(CrashingRunner.class); PipelineOptionsValidator.validate(JoinedOptions.class, rightOpts); PipelineOptionsValidator.validate(JoinedOptions.class, leftOpts); }
@Test public void testWhenOptionIsDefinedOnOtherOptionsClassMeetsGroupRequirement() { RightOptions rightOpts = PipelineOptionsFactory.as(RightOptions.class); rightOpts.setFoo("true"); rightOpts.setBoth("bar"); LeftOptions leftOpts = PipelineOptionsFactory.as(LeftOptions.class); leftOpts.setFoo("Untrue"); leftOpts.setBoth("Raise the"); rightOpts.setRunner(CrashingRunner.class); leftOpts.setRunner(CrashingRunner.class); PipelineOptionsValidator.validate(RightOptions.class, leftOpts); PipelineOptionsValidator.validate(LeftOptions.class, rightOpts); }
@Test public void testWhenOptionIsDefinedOnMultipleInterfacesOnlyListedOnceWhenNotPresent() { JoinedOptions options = PipelineOptionsFactory.as(JoinedOptions.class); options.setFoo("Hello"); options.setRunner(CrashingRunner.class); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("required value for group [both]"); expectedException.expectMessage("properties [getBoth()]"); PipelineOptionsValidator.validate(JoinedOptions.class, options); }
@Test public void testSuperInterfaceRequiredOptionsAlsoRequiredInSubInterface() { SubOptions subOpts = PipelineOptionsFactory.as(SubOptions.class); subOpts.setFoo("Bar"); subOpts.setRunner(CrashingRunner.class); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("otherSuper"); expectedException.expectMessage("Missing required value"); expectedException.expectMessage("getSuperclassObj"); PipelineOptionsValidator.validate(SubOptions.class, subOpts); }
@Test public void testSuperInterfaceGroupIsInAdditionToSubInterfaceGroupOnlyWhenValidatingSuperInterface() { SubOptions opts = PipelineOptionsFactory.as(SubOptions.class); opts.setFoo("Foo"); opts.setSuperclassObj("Hello world"); opts.setRunner(CrashingRunner.class); PipelineOptionsValidator.validate(SubOptions.class, opts); expectedException.expectMessage("sub"); expectedException.expectMessage("Missing required value"); expectedException.expectMessage("getBar"); PipelineOptionsValidator.validate(SuperOptions.class, opts); }
@Test public void testSuperInterfaceRequiredOptionsSatisfiedBySubInterface() { SubOptions subOpts = PipelineOptionsFactory.as(SubOptions.class); subOpts.setFoo("bar"); subOpts.setBar("bar"); subOpts.setSuperclassObj("SuperDuper"); subOpts.setRunner(CrashingRunner.class); PipelineOptionsValidator.validate(SubOptions.class, subOpts); PipelineOptionsValidator.validate(SuperOptions.class, subOpts); }