@@ -814,14 +814,14 @@ public static void testFunctionInfo() {
814814 @ AfterClass
815815 public static void testFunctionLicenseChecks () throws Exception {
816816 Class <?> testClass = getTestClass ();
817- DocsV3Support .LicenseRequirementChecker licenseChecker = new DocsV3Support .LicenseRequirementChecker (testClass );
818817 Logger log = LogManager .getLogger (testClass );
819818 FunctionDefinition definition = definition (functionName ());
820819 if (definition == null ) {
821820 log .info ("Skipping function info checks because the function isn't registered" );
822821 return ;
823822 }
824823 log .info ("Running function license checks" );
824+ DocsV3Support .LicenseRequirementChecker licenseChecker = new DocsV3Support .LicenseRequirementChecker (testClass );
825825 License .OperationMode functionLicense = licenseChecker .invoke (null );
826826 Constructor <?> ctor = constructorWithFunctionInfo (definition .clazz ());
827827 if (LicenseAware .class .isAssignableFrom (definition .clazz ()) == false ) {
@@ -834,9 +834,7 @@ public static void testFunctionLicenseChecks() throws Exception {
834834 return ;
835835 }
836836 // For classes with LicenseAware, we need to check that the license is correct
837- XPackLicenseState basicLicense = makeLicenseState (License .OperationMode .BASIC );
838- XPackLicenseState platinumLicense = makeLicenseState (License .OperationMode .PLATINUM );
839- XPackLicenseState enterpriseLicense = makeLicenseState (License .OperationMode .ENTERPRISE );
837+ TestCheckLicense checkLicense = new TestCheckLicense ();
840838
841839 // Go through all signatures and assert that the license is as expected
842840 signatures (testClass ).forEach ((signature , returnType ) -> {
@@ -854,23 +852,10 @@ public static void testFunctionLicenseChecks() throws Exception {
854852 // Check that object implements the LicenseAware interface
855853 if (LicenseAware .class .isAssignableFrom (instance .getClass ())) {
856854 LicenseAware licenseAware = (LicenseAware ) instance ;
857- boolean basic = licenseAware .licenseCheck (basicLicense );
858- boolean platinum = licenseAware .licenseCheck (platinumLicense );
859- boolean enterprise = licenseAware .licenseCheck (enterpriseLicense );
860- if (license == License .OperationMode .BASIC ) {
861- assertTrue ("Basic license should be accepted for " + signature , basic );
862- assertTrue ("Platinum license should be accepted for " + signature , platinum );
863- assertTrue ("Enterprise license should be accepted for " + signature , enterprise );
864- }
865- if (license == License .OperationMode .PLATINUM ) {
866- assertFalse ("Basic license should not be accepted for " + signature , basic );
867- assertTrue ("Platinum license should be accepted for " + signature , platinum );
868- assertTrue ("Enterprise license should be accepted for " + signature , enterprise );
869- }
870- if (license == License .OperationMode .ENTERPRISE ) {
871- assertFalse ("Basic license should not be accepted for " + signature , basic );
872- assertFalse ("Platinum license should not be accepted for " + signature , platinum );
873- assertTrue ("Enterprise license should be accepted for " + signature , enterprise );
855+ switch (license ) {
856+ case BASIC -> checkLicense .assertLicenseCheck (licenseAware , signature , true , true , true );
857+ case PLATINUM -> checkLicense .assertLicenseCheck (licenseAware , signature , false , true , true );
858+ case ENTERPRISE -> checkLicense .assertLicenseCheck (licenseAware , signature , false , false , true );
874859 }
875860 } else {
876861 fail ("Function " + definition .name () + " does not implement LicenseAware" );
@@ -881,6 +866,31 @@ public static void testFunctionLicenseChecks() throws Exception {
881866 });
882867 }
883868
869+ private static class TestCheckLicense {
870+ XPackLicenseState basicLicense = makeLicenseState (License .OperationMode .BASIC );
871+ XPackLicenseState platinumLicense = makeLicenseState (License .OperationMode .PLATINUM );
872+ XPackLicenseState enterpriseLicense = makeLicenseState (License .OperationMode .ENTERPRISE );
873+
874+ private void assertLicenseCheck (
875+ LicenseAware licenseAware ,
876+ List <DataType > signature ,
877+ boolean allowsBasic ,
878+ boolean allowsPlatinum ,
879+ boolean allowsEnterprise
880+ ) {
881+ boolean basic = licenseAware .licenseCheck (basicLicense );
882+ boolean platinum = licenseAware .licenseCheck (platinumLicense );
883+ boolean enterprise = licenseAware .licenseCheck (enterpriseLicense );
884+ assertThat ("Basic license should be accepted for " + signature , basic , equalTo (allowsBasic ));
885+ assertThat ("Platinum license should be accepted for " + signature , platinum , equalTo (allowsPlatinum ));
886+ assertThat ("Enterprise license should be accepted for " + signature , enterprise , equalTo (allowsEnterprise ));
887+ }
888+
889+ private void assertLicenseCheck (List <DataType > signature , boolean allowed , boolean expected ) {
890+ assertThat ("Basic license should " + (expected ? "" : "not " ) + "be accepted for " + signature , allowed , equalTo (expected ));
891+ }
892+ }
893+
884894 private static XPackLicenseState makeLicenseState (License .OperationMode mode ) {
885895 return new XPackLicenseState (System ::currentTimeMillis , new XPackLicenseStatus (mode , true , "" ));
886896 }
0 commit comments