Repository: apache/kafka
File: tools/src/test/java/org/apache/kafka/tools/DelegationTokenCommandTest.java (Java)

Code before:
        String[] args = {"--bootstrap-server", "localhost:9092", "--command-config", "testfile", "--renew", "--hmac", "test-hmac", "--renew-time-period", "604800000"};
        DelegationTokenCommand.DelegationTokenCommandOptions opts = new DelegationTokenCommand.DelegationTokenCommandOptions(args);
        
        opts.checkArgs();
    }

    @Test
    public void testCheckArgsExpireOperation() {
        String[] args = {"--bootstrap-server", "localhost:9092", "--command-config", "testfile", "--expire", "--hmac", "test-hmac", "--expiry-time-period", "604800000"};
        DelegationTokenCommand.DelegationTokenCommandOptions opts = new DelegationTokenCommand.DelegationTokenCommandOptions(args);
        
        opts.checkArgs();
    }

    @Test
    public void testCheckArgsDescribeOperation() {
        String[] args = {"--bootstrap-server", "localhost:9092", "--command-config", "testfile", "--describe"};
        DelegationTokenCommand.DelegationTokenCommandOptions opts = new DelegationTokenCommand.DelegationTokenCommandOptions(args);
        
        opts.checkArgs();
    }

    @Test
    public void testCheckArgsInvalidArgsForCreate() {
        String[] args = {"--bootstrap-server", "localhost:9092", "--command-config", "testfile", "--create", "--max-life-time-period", "604800000", "--hmac", "test-hmac"};
        DelegationTokenCommand.DelegationTokenCommandOptions opts = new DelegationTokenCommand.DelegationTokenCommandOptions(args);
        
        Exit.setExitProcedure((exitCode, message) -> {
            throw new RuntimeException("Exit with code " + exitCode + ": " + message);
        });
        try {
            assertThrows(RuntimeException.class, () -> opts.checkArgs());
        } finally {
            Exit.resetExitProcedure();
        }
    }

    @Test
    public void testCheckArgsInvalidArgsForRenew() {
        String[] args = {"--bootstrap-server", "localhost:9092", "--command-config", "testfile", "--renew", "--hmac", "test-hmac", "--renew-time-period", "604800000", "--max-life-time-period", "604800000"};
        DelegationTokenCommand.DelegationTokenCommandOptions opts = new DelegationTokenCommand.DelegationTokenCommandOptions(args);
        
        Exit.setExitProcedure((exitCode, message) -> {
            throw new RuntimeException("Exit with code " + exitCode + ": " + message);
        });
        try {
            assertThrows(RuntimeException.class, () -> opts.checkArgs());
        } finally {
            Exit.resetExitProcedure();
        }
    }

Review comment (suggestion):
For `--create` command, `--renew-time-period` and `--expiry-time-period` are invalid arguments as well. Could you also add them to the case? We also need similar update for different commands.

Code after:

            String[] argsCreateMissingConfig = {"--bootstrap-server", "localhost:9092", "--create", "--max-life-time-period", "604800000"};
            DelegationTokenCommand.DelegationTokenCommandOptions optsCreateMissingConfig = new DelegationTokenCommand.DelegationTokenCommandOptions(argsCreateMissingConfig);
            assertThrows(RuntimeException.class, optsCreateMissingConfig::checkArgs);

            String[] argsCreateMissingMaxLife = {"--bootstrap-server", "localhost:9092", "--command-config", "testfile", "--create"};
            DelegationTokenCommand.DelegationTokenCommandOptions optsCreateMissingMaxLife = new DelegationTokenCommand.DelegationTokenCommandOptions(argsCreateMissingMaxLife);
            assertThrows(RuntimeException.class, optsCreateMissingMaxLife::checkArgs);

            String[] argsRenewMissingBootstrap = {"--command-config", "testfile", "--renew", "--hmac", "test-hmac", "--renew-time-period", "604800000"};
            DelegationTokenCommand.DelegationTokenCommandOptions optsRenewMissingBootstrap = new DelegationTokenCommand.DelegationTokenCommandOptions(argsRenewMissingBootstrap);
            assertThrows(RuntimeException.class, optsRenewMissingBootstrap::checkArgs);

            String[] argsRenewMissingConfig = {"--bootstrap-server", "localhost:9092", "--renew", "--hmac", "test-hmac", "--renew-time-period", "604800000"};
            DelegationTokenCommand.DelegationTokenCommandOptions optsRenewMissingConfig = new DelegationTokenCommand.DelegationTokenCommandOptions(argsRenewMissingConfig);
            assertThrows(RuntimeException.class, optsRenewMissingConfig::checkArgs);

            String[] argsRenewMissingHmac = {"--bootstrap-server", "localhost:9092", "--command-config", "testfile", "--renew", "--renew-time-period", "604800000"};
            DelegationTokenCommand.DelegationTokenCommandOptions optsRenewMissingHmac = new DelegationTokenCommand.DelegationTokenCommandOptions(argsRenewMissingHmac);
            assertThrows(RuntimeException.class, optsRenewMissingHmac::checkArgs);

            String[] argsRenewMissingRenewTime = {"--bootstrap-server", "localhost:9092", "--command-config", "testfile", "--renew", "--hmac", "test-hmac"};
            DelegationTokenCommand.DelegationTokenCommandOptions optsRenewMissingRenewTime = new DelegationTokenCommand.DelegationTokenCommandOptions(argsRenewMissingRenewTime);
            assertThrows(RuntimeException.class, optsRenewMissingRenewTime::checkArgs);

            String[] argsExpireMissingBootstrap = {"--command-config", "testfile", "--expire", "--hmac", "test-hmac", "--expiry-time-period", "604800000"};
            DelegationTokenCommand.DelegationTokenCommandOptions optsExpireMissingBootstrap = new DelegationTokenCommand.DelegationTokenCommandOptions(argsExpireMissingBootstrap);
            assertThrows(RuntimeException.class, optsExpireMissingBootstrap::checkArgs);

            String[] argsExpireMissingConfig = {"--bootstrap-server", "localhost:9092", "--expire", "--hmac", "test-hmac", "--expiry-time-period", "604800000"};
            DelegationTokenCommand.DelegationTokenCommandOptions optsExpireMissingConfig = new DelegationTokenCommand.DelegationTokenCommandOptions(argsExpireMissingConfig);
            assertThrows(RuntimeException.class, optsExpireMissingConfig::checkArgs);

            String[] argsExpireMissingHmac = {"--bootstrap-server", "localhost:9092", "--command-config", "testfile", "--expire", "--expiry-time-period", "604800000"};
            DelegationTokenCommand.DelegationTokenCommandOptions optsExpireMissingHmac = new DelegationTokenCommand.DelegationTokenCommandOptions(argsExpireMissingHmac);
            assertThrows(RuntimeException.class, optsExpireMissingHmac::checkArgs);

            String[] argsExpireMissingExpiryTime = {"--bootstrap-server", "localhost:9092", "--command-config", "testfile", "--expire", "--hmac", "test-hmac"};
            DelegationTokenCommand.DelegationTokenCommandOptions optsExpireMissingExpiryTime = new DelegationTokenCommand.DelegationTokenCommandOptions(argsExpireMissingExpiryTime);
            assertThrows(RuntimeException.class, optsExpireMissingExpiryTime::checkArgs);

            String[] argsDescribeMissingBootstrap = {"--command-config", "testfile", "--describe"};
            DelegationTokenCommand.DelegationTokenCommandOptions optsDescribeMissingBootstrap = new DelegationTokenCommand.DelegationTokenCommandOptions(argsDescribeMissingBootstrap);
            assertThrows(RuntimeException.class, optsDescribeMissingBootstrap::checkArgs);

            String[] argsDescribeMissingConfig = {"--bootstrap-server", "localhost:9092", "--describe"};
            DelegationTokenCommand.DelegationTokenCommandOptions optsDescribeMissingConfig = new DelegationTokenCommand.DelegationTokenCommandOptions(argsDescribeMissingConfig);
            assertThrows(RuntimeException.class, optsDescribeMissingConfig::checkArgs);
        } finally {
            Exit.resetExitProcedure();
        }