Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[improve][cli]New tests for operations on namespaces in pulsar-client-tools #22761

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,87 @@
*/
package org.apache.pulsar.admin.cli;

import com.google.common.collect.Lists;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
import lombok.Cleanup;
import org.apache.pulsar.client.admin.Namespaces;
import org.apache.pulsar.client.admin.PulsarAdmin;
import org.apache.pulsar.common.policies.data.Policies;
import org.apache.pulsar.common.policies.data.RetentionPolicies;
import org.mockito.ArgumentCaptor;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.io.IOException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class TestCmdNamespaces {

private PulsarAdmin pulsarAdmin;
private CmdNamespaces cmdNamespaces;
private Namespaces namespaces;

@BeforeMethod
public void setup() throws Exception {
namespaces = mock(Namespaces.class);
pulsarAdmin = mock(PulsarAdmin.class);
when(pulsarAdmin.namespaces()).thenReturn(namespaces);
cmdNamespaces = new CmdNamespaces(() -> pulsarAdmin);
}

@AfterMethod(alwaysRun = true)
public void cleanup() throws IOException {
//NOTHING FOR NOW
}


@Test
public void testSetRetentionCmd() throws Exception {
Namespaces namespaces = mock(Namespaces.class);
public void testListCmd() throws Exception {
List<String> namespaceList = Lists.newArrayList("namespace1", "namespace2", "namespace3");
doReturn(namespaceList).when(namespaces).getNamespaces(anyString());
@Cleanup
StringWriter stringWriter = new StringWriter();
@Cleanup
PrintWriter printWriter = new PrintWriter(stringWriter);
cmdNamespaces.getCommander().setOut(printWriter);
cmdNamespaces.run("list tenant1".split("\\s+"));
Assert.assertEquals(stringWriter.toString(), String.join("\n", namespaceList) + "\n");
}

PulsarAdmin admin = mock(PulsarAdmin.class);
when(admin.namespaces()).thenReturn(namespaces);
@Test
public void testCreateCmd() throws Exception {
ArgumentCaptor<String> namespaceCapture = ArgumentCaptor.forClass(String.class);
doNothing().when(namespaces).createNamespace(namespaceCapture.capture(), any(Policies.class));
cmdNamespaces.run("create tenant1/namespace1".split("\\s+"));
verify(namespaces, times(1))
.createNamespace(anyString(), any(Policies.class));
Assert.assertEquals(namespaceCapture.getValue(), "tenant1/namespace1");
}

CmdNamespaces cmd = new CmdNamespaces(() -> admin);
@Test
public void testDeleteCmd() throws Exception {
ArgumentCaptor<String> namespaceCapture = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<Boolean> forceCapture = ArgumentCaptor.forClass(Boolean.class);
doNothing().when(namespaces).deleteNamespace(namespaceCapture.capture(), forceCapture.capture());
cmdNamespaces.run("delete tenant1/namespace1".split("\\s+"));
verify(namespaces, times(1))
.deleteNamespace(namespaceCapture.getValue(), forceCapture.getValue());
Assert.assertEquals(namespaceCapture.getValue(), "tenant1/namespace1");
Assert.assertEquals(forceCapture.getValue(), false);
}

cmd.run("set-retention public/default -s 2T -t 2h".split("\\s+"));
@Test
public void testSetRetentionCmd() throws Exception {
cmdNamespaces.run("set-retention public/default -s 2T -t 2h".split("\\s+"));
verify(namespaces, times(1)).setRetention("public/default", new RetentionPolicies(120, 2 * 1024 * 1024));
}
}
}