clolov commented on code in PR #13172:
URL: https://github.com/apache/kafka/pull/13172#discussion_r1094714174


##########
tools/src/test/java/org/apache/kafka/tools/DelegationTokenCommandTest.java:
##########
@@ -0,0 +1,105 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.tools;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.ExecutionException;
+import org.apache.kafka.clients.admin.Admin;
+import org.apache.kafka.clients.admin.MockAdminClient;
+import org.apache.kafka.common.security.token.delegation.DelegationToken;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class DelegationTokenCommandTest {
+
+    @Test
+    public void testDelegationTokenRequests() throws ExecutionException, 
InterruptedException {
+        Admin adminClient = new MockAdminClient.Builder().build();
+
+        String renewer1 = "User:renewer1";
+        String renewer2 = "User:renewer2";
+
+        // create token1 with renewer1
+        DelegationToken tokenCreated = 
DelegationTokenCommand.createToken(adminClient, getCreateOpts(renewer1));
+
+        List<DelegationToken> tokens = 
DelegationTokenCommand.describeToken(adminClient, getDescribeOpts(""));
+        assertEquals(1, tokens.size());
+        DelegationToken token1 = tokens.get(0);
+        assertEquals(token1, tokenCreated);
+
+        // create token2 with renewer2
+        DelegationToken token2 = 
DelegationTokenCommand.createToken(adminClient, getCreateOpts(renewer2));
+
+        tokens = DelegationTokenCommand.describeToken(adminClient, 
getDescribeOpts(""));
+        assertEquals(2, tokens.size());
+        assertEquals(Arrays.asList(token1, token2), tokens);
+
+        //get tokens for renewer2
+        tokens = DelegationTokenCommand.describeToken(adminClient, 
getDescribeOpts(renewer2));
+        assertEquals(1, tokens.size());
+        assertEquals(Collections.singletonList(token2), tokens);
+
+        //test renewing tokens
+        Long expiryTimestamp = DelegationTokenCommand.renewToken(adminClient, 
getRenewOpts(token1.hmacAsBase64String()));
+        DelegationToken renewedToken = 
DelegationTokenCommand.describeToken(adminClient, 
getDescribeOpts(renewer1)).get(0);
+        assertEquals(expiryTimestamp, 
renewedToken.tokenInfo().expiryTimestamp());
+
+        //test expire tokens
+        DelegationTokenCommand.expireToken(adminClient, 
getExpireOpts(token1.hmacAsBase64String()));
+        DelegationTokenCommand.expireToken(adminClient, 
getExpireOpts(token2.hmacAsBase64String()));
+
+        tokens = DelegationTokenCommand.describeToken(adminClient, 
getDescribeOpts(""));
+        assertEquals(0, tokens.size());
+
+        //create token with invalid renewer principal type
+        assertThrows(ExecutionException.class, () -> 
DelegationTokenCommand.createToken(adminClient, 
getCreateOpts("Group:Renewer3")));
+
+        // try describing tokens for unknown owner
+        assertTrue(DelegationTokenCommand.describeToken(adminClient, 
getDescribeOpts("User:Unknown")).isEmpty());
+
+    }
+
+    private DelegationTokenCommand.DelegationTokenCommandOptions 
getCreateOpts(String renewer) {
+        String[] args = {"--bootstrap-server", "localhost:9092", 
"--max-life-time-period", "-1", "--command-config", "testfile", "--create", 
"--renewer-principal", renewer};
+        return new DelegationTokenCommand.DelegationTokenCommandOptions(args);
+    }
+
+    private DelegationTokenCommand.DelegationTokenCommandOptions 
getDescribeOpts(String owner) {
+        String[] args;
+        if (!owner.equals("")) {
+            args = new String[] {"--bootstrap-server", "localhost:9092", 
"--command-config", "testfile", "--describe","--owner-principal", owner};

Review Comment:
   ```suggestion
               args = new String[] {"--bootstrap-server", "localhost:9092", 
"--command-config", "testfile", "--describe", "--owner-principal", owner};
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to