You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by bt...@apache.org on 2020/12/18 07:27:37 UTC

[james-project] 09/13: JAMES 3400 Add mailbox exist command

This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit afa7e121ffc7f1db1e24b1055628cecf8e6594f2
Author: quanth <hq...@linagora.com>
AuthorDate: Mon Nov 30 13:45:11 2020 +0700

    JAMES 3400 Add mailbox exist command
---
 .../apache/james/cli/mailbox/MailboxCommand.java   |  3 +-
 .../james/cli/mailbox/MailboxCreateCommand.java    |  2 +-
 ...CreateCommand.java => MailboxExistCommand.java} | 34 +++++----
 .../org/apache/james/httpclient/MailboxClient.java |  3 +
 .../org/apache/james/cli/MailboxManageTest.java    | 85 +++++++++++++++++++++-
 5 files changed, 108 insertions(+), 19 deletions(-)

diff --git a/server/protocols/webadmin-cli/src/main/java/org/apache/james/cli/mailbox/MailboxCommand.java b/server/protocols/webadmin-cli/src/main/java/org/apache/james/cli/mailbox/MailboxCommand.java
index 908e7dc..6494db2 100644
--- a/server/protocols/webadmin-cli/src/main/java/org/apache/james/cli/mailbox/MailboxCommand.java
+++ b/server/protocols/webadmin-cli/src/main/java/org/apache/james/cli/mailbox/MailboxCommand.java
@@ -31,7 +31,8 @@ import picocli.CommandLine;
     name = "mailbox",
     description = "Manage Mailboxes",
     subcommands = {
-        MailboxCreateCommand.class
+        MailboxCreateCommand.class,
+        MailboxExistCommand.class
     })
 public class MailboxCommand implements Callable<Integer> {
 
diff --git a/server/protocols/webadmin-cli/src/main/java/org/apache/james/cli/mailbox/MailboxCreateCommand.java b/server/protocols/webadmin-cli/src/main/java/org/apache/james/cli/mailbox/MailboxCreateCommand.java
index 46a7806..950880d 100644
--- a/server/protocols/webadmin-cli/src/main/java/org/apache/james/cli/mailbox/MailboxCreateCommand.java
+++ b/server/protocols/webadmin-cli/src/main/java/org/apache/james/cli/mailbox/MailboxCreateCommand.java
@@ -50,7 +50,7 @@ public class MailboxCreateCommand implements Callable<Integer> {
             MailboxClient mailboxClient = mailboxCommand.fullyQualifiedURL("/users");
             Response rs = mailboxClient.createAMailbox(userName, mailboxName);
             if (rs.status() == CREATED_CODE) {
-                mailboxCommand.out.println("The mailbox was created successfully.");
+                mailboxCommand.out.println("The mailbox now exists on the server.");
                 return WebAdminCli.CLI_FINISHED_SUCCEED;
             } else if (rs.status() == BAD_REQUEST_CODE) {
                 mailboxCommand.err.println(rs.body());
diff --git a/server/protocols/webadmin-cli/src/main/java/org/apache/james/cli/mailbox/MailboxCreateCommand.java b/server/protocols/webadmin-cli/src/main/java/org/apache/james/cli/mailbox/MailboxExistCommand.java
similarity index 71%
copy from server/protocols/webadmin-cli/src/main/java/org/apache/james/cli/mailbox/MailboxCreateCommand.java
copy to server/protocols/webadmin-cli/src/main/java/org/apache/james/cli/mailbox/MailboxExistCommand.java
index 46a7806..bc46953 100644
--- a/server/protocols/webadmin-cli/src/main/java/org/apache/james/cli/mailbox/MailboxCreateCommand.java
+++ b/server/protocols/webadmin-cli/src/main/java/org/apache/james/cli/mailbox/MailboxExistCommand.java
@@ -28,36 +28,36 @@ import feign.Response;
 import picocli.CommandLine;
 
 @CommandLine.Command(
-    name = "create",
-    description = "Create a new mailbox")
-public class MailboxCreateCommand implements Callable<Integer> {
+    name = "exist",
+    description = "Check if a mailbox exists")
+public class MailboxExistCommand implements Callable<Integer> {
 
-    public static final int CREATED_CODE = 204;
-    public static final int BAD_REQUEST_CODE = 400;
-    public static final int USERNAME_NOT_EXIST_CODE = 404;
+    public static final int EXISTED_CODE = 204;
+    public static final int INVALID_MAILBOX_NAME_CODE = 400;
+    public static final int NOT_EXISTED_CODE = 404;
 
     @CommandLine.ParentCommand MailboxCommand mailboxCommand;
 
-    @CommandLine.Parameters(description = "Username")
+    @CommandLine.Parameters(description = "Username to be checked")
     String userName;
 
-    @CommandLine.Parameters(description = "Mailbox's name to be created")
+    @CommandLine.Parameters(description = "Mailbox's name to be tested existence")
     String mailboxName;
 
     @Override
     public Integer call() {
         try {
             MailboxClient mailboxClient = mailboxCommand.fullyQualifiedURL("/users");
-            Response rs = mailboxClient.createAMailbox(userName, mailboxName);
-            if (rs.status() == CREATED_CODE) {
-                mailboxCommand.out.println("The mailbox was created successfully.");
+            Response rs = mailboxClient.doesExist(userName, mailboxName);
+            if (rs.status() == EXISTED_CODE) {
+                mailboxCommand.out.println("The mailbox exists.");
                 return WebAdminCli.CLI_FINISHED_SUCCEED;
-            } else if (rs.status() == BAD_REQUEST_CODE) {
-                mailboxCommand.err.println(rs.body());
-                return WebAdminCli.CLI_FINISHED_FAILED;
-            } else if (rs.status() == USERNAME_NOT_EXIST_CODE) {
+            } else if (rs.status() == INVALID_MAILBOX_NAME_CODE) {
                 mailboxCommand.err.println(rs.body());
                 return WebAdminCli.CLI_FINISHED_FAILED;
+            } else if (rs.status() == NOT_EXISTED_CODE) {
+                mailboxCommand.out.println("Either the user name or the mailbox does not exist.");
+                return WebAdminCli.CLI_FINISHED_SUCCEED;
             }
             return WebAdminCli.CLI_FINISHED_FAILED;
         } catch (Exception e) {
@@ -66,4 +66,6 @@ public class MailboxCreateCommand implements Callable<Integer> {
         }
     }
 
-}
\ No newline at end of file
+}
+
+
diff --git a/server/protocols/webadmin-cli/src/main/java/org/apache/james/httpclient/MailboxClient.java b/server/protocols/webadmin-cli/src/main/java/org/apache/james/httpclient/MailboxClient.java
index ab781dc..0e77cf2 100644
--- a/server/protocols/webadmin-cli/src/main/java/org/apache/james/httpclient/MailboxClient.java
+++ b/server/protocols/webadmin-cli/src/main/java/org/apache/james/httpclient/MailboxClient.java
@@ -28,4 +28,7 @@ public interface MailboxClient {
     @RequestLine("PUT /{userNameToBeUsed}/mailboxes/{mailboxNameToBeCreated}")
     Response createAMailbox(@Param("userNameToBeUsed") String userName, @Param("mailboxNameToBeCreated") String mailboxName);
 
+    @RequestLine("GET /{usernameToBeUsed}/mailboxes/{mailboxNameToBeTested}")
+    Response doesExist(@Param("usernameToBeUsed") String userName, @Param("mailboxNameToBeTested") String mailboxName);
+
 }
diff --git a/server/protocols/webadmin-cli/src/test/java/org/apache/james/cli/MailboxManageTest.java b/server/protocols/webadmin-cli/src/test/java/org/apache/james/cli/MailboxManageTest.java
index 8452d0f..4e59b84 100644
--- a/server/protocols/webadmin-cli/src/test/java/org/apache/james/cli/MailboxManageTest.java
+++ b/server/protocols/webadmin-cli/src/test/java/org/apache/james/cli/MailboxManageTest.java
@@ -62,7 +62,7 @@ public class MailboxManageTest {
             "--url", "http://127.0.0.1:" + port.getValue(), "mailbox", "exist", "hqtran@linagora.com", "INBOX");
 
         assertThat(exitCode).isEqualTo(0);
-        assertThat(outputStreamCaptor.toString().trim()).isEqualTo("The mailbox was created successfully.\n" +
+        assertThat(outputStreamCaptor.toString().trim()).isEqualTo("The mailbox now exists on the server.\n" +
             "The mailbox exists.");
     }
 
@@ -87,4 +87,87 @@ public class MailboxManageTest {
         assertThat(errorStreamCaptor.toString()).contains("404");
     }
 
+    @Test
+    void mailboxCreateWithAlreadyExistingMailboxShouldSucceed() throws Exception {
+        dataProbe.fluent().addDomain("linagora.com")
+            .addUser("hqtran@linagora.com", "123456");
+
+        int exitCode = WebAdminCli.executeFluent(new PrintStream(new ByteArrayOutputStream()), new PrintStream(new ByteArrayOutputStream()),
+            "--url", "http://127.0.0.1:" + port.getValue(), "mailbox", "create", "hqtran@linagora.com", "INBOX");
+
+        int exitCode2 = WebAdminCli.executeFluent(new PrintStream(outputStreamCaptor), new PrintStream(errorStreamCaptor),
+            "--url", "http://127.0.0.1:" + port.getValue(), "mailbox", "create", "hqtran@linagora.com", "INBOX");
+
+        assertThat(exitCode).isEqualTo(0);
+        assertThat(exitCode2).isEqualTo(0);
+        assertThat(outputStreamCaptor.toString().trim()).isEqualTo("The mailbox now exists on the server.");
+    }
+
+    @Test
+    void mailboxCreateSubMailboxesShouldSucceed() throws Exception {
+        dataProbe.fluent().addDomain("linagora.com")
+            .addUser("hqtran@linagora.com", "123456");
+
+        int exitCode = WebAdminCli.executeFluent(new PrintStream(new ByteArrayOutputStream()), new PrintStream(new ByteArrayOutputStream()),
+            "--url", "http://127.0.0.1:" + port.getValue(), "mailbox", "create", "hqtran@linagora.com", "INBOX.1");
+
+        int exitCode2 = WebAdminCli.executeFluent(new PrintStream(new ByteArrayOutputStream()), new PrintStream(new ByteArrayOutputStream()),
+            "--url", "http://127.0.0.1:" + port.getValue(), "mailbox", "create", "hqtran@linagora.com", "INBOX.2");
+
+        WebAdminCli.executeFluent(new PrintStream(outputStreamCaptor), new PrintStream(errorStreamCaptor),
+            "--url", "http://127.0.0.1:" + port.getValue(), "mailbox", "list", "hqtran@linagora.com");
+
+        assertThat(exitCode).isEqualTo(0);
+        assertThat(exitCode2).isEqualTo(0);
+        assertThat(outputStreamCaptor.toString()).isEqualTo("INBOX\nINBOX.1\nINBOX.2\n");
+    }
+
+    @Test
+    void mailboxExistWithExistedUsernameAndExistedMailboxNameShouldSucceed() throws Exception {
+        dataProbe.fluent().addDomain("linagora.com")
+            .addUser("hqtran@linagora.com", "123456");
+
+        WebAdminCli.executeFluent(new PrintStream(new ByteArrayOutputStream()), new PrintStream(new ByteArrayOutputStream()),
+            "--url", "http://127.0.0.1:" + port.getValue(), "mailbox", "create", "hqtran@linagora.com", "INBOX");
+
+        int exitCode = WebAdminCli.executeFluent(new PrintStream(outputStreamCaptor), new PrintStream(errorStreamCaptor),
+            "--url", "http://127.0.0.1:" + port.getValue(), "mailbox", "exist", "hqtran@linagora.com", "INBOX");
+
+        assertThat(exitCode).isEqualTo(0);
+        assertThat(outputStreamCaptor.toString().trim()).isEqualTo("The mailbox exists.");
+    }
+
+    @Test
+    void mailboxExistWithInvalidMailboxNameShouldFail() throws Exception {
+        dataProbe.fluent().addDomain("linagora.com")
+            .addUser("hqtran@linagora.com", "123456");
+
+        int exitCode = WebAdminCli.executeFluent(new PrintStream(outputStreamCaptor), new PrintStream(errorStreamCaptor),
+            "--url", "http://127.0.0.1:" + port.getValue(), "mailbox", "exist", "hqtran@linagora.com", "#INBOX");
+
+        assertThat(exitCode).isEqualTo(1);
+        assertThat(errorStreamCaptor.toString()).contains("400");
+    }
+
+    @Test
+    void mailboxExistWithExistedUserAndNonExistingMailboxNameShouldFail() throws Exception {
+        dataProbe.fluent().addDomain("linagora.com")
+            .addUser("hqtran@linagora.com", "123456");
+
+        int exitCode = WebAdminCli.executeFluent(new PrintStream(outputStreamCaptor), new PrintStream(errorStreamCaptor),
+            "--url", "http://127.0.0.1:" + port.getValue(), "mailbox", "exist", "hqtran@linagora.com", "INBOX");
+
+        assertThat(exitCode).isEqualTo(0);
+        assertThat(outputStreamCaptor.toString().trim()).isEqualTo("Either the user name or the mailbox does not exist.");
+    }
+
+    @Test
+    void mailboxExistWithNonExistingUserAndNonExistingMailboxNameShouldFail() {
+        int exitCode = WebAdminCli.executeFluent(new PrintStream(outputStreamCaptor), new PrintStream(errorStreamCaptor),
+            "--url", "http://127.0.0.1:" + port.getValue(), "mailbox", "exist", "hqtran@linagora.com", "INBOX");
+
+        assertThat(exitCode).isEqualTo(0);
+        assertThat(outputStreamCaptor.toString().trim()).isEqualTo("Either the user name or the mailbox does not exist.");
+    }
+
 }
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org