Skip to content

Commit

Permalink
JAMES-1983 Write CLI integration tests on the printed output
Browse files Browse the repository at this point in the history
  • Loading branch information
chibenwa committed Apr 3, 2017
1 parent e854211 commit c994be0
Show file tree
Hide file tree
Showing 7 changed files with 315 additions and 81 deletions.
Expand Up @@ -24,32 +24,27 @@

import java.util.AbstractMap;

import javax.inject.Singleton;

import org.apache.james.GuiceJamesServer;
import org.apache.james.MemoryJmapTestRule;
import org.apache.james.mailbox.inmemory.InMemoryMailboxManager;
import org.apache.james.cli.util.OutputCapture;
import org.apache.james.mailbox.store.search.ListeningMessageSearchIndex;
import org.apache.james.modules.server.JMXServerModule;
import org.apache.james.rrt.lib.MappingImpl;
import org.apache.james.rrt.lib.Mappings;
import org.apache.james.rrt.lib.MappingsImpl;
import org.apache.james.utils.DataProbeImpl;
import org.apache.james.utils.MailboxManagerDefinition;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import com.google.inject.Inject;
import com.google.inject.multibindings.Multibinder;

public class DataCommandsIntegrationTest {

public static final String DOMAIN_COM = "domain.com";
public static final String USER = "user";
public static final String MAIL_ADDRESS = USER + "@" + DOMAIN_COM;
public static final String DOMAIN = "domain.com";
public static final String USER = "chibenwa";
public static final String MAIL_ADDRESS = USER + "@" + DOMAIN;
public static final String PASSWORD = "12345";
private OutputCapture outputCapture;

@Rule
public MemoryJmapTestRule memoryJmap = new MemoryJmapTestRule();
Expand All @@ -62,6 +57,7 @@ public void setUp() throws Exception {
binder -> binder.bind(ListeningMessageSearchIndex.class).toInstance(mock(ListeningMessageSearchIndex.class)));
guiceJamesServer.start();
dataProbe = guiceJamesServer.getProbe(DataProbeImpl.class);
outputCapture = new OutputCapture();
}

@After
Expand All @@ -71,23 +67,43 @@ public void tearDown() {

@Test
public void addDomainShouldWork() throws Exception {
ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "ADDDOMAIN", DOMAIN_COM});
ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "ADDDOMAIN", DOMAIN});

assertThat(dataProbe.containsDomain(DOMAIN_COM)).isTrue();
assertThat(dataProbe.containsDomain(DOMAIN)).isTrue();
}

@Test
public void removeDomainShouldWork() throws Exception {
dataProbe.addDomain(DOMAIN_COM);
dataProbe.addDomain(DOMAIN);

ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "REMOVEDOMAIN", DOMAIN});

assertThat(dataProbe.containsDomain(DOMAIN)).isFalse();
}

@Test
public void listDomainsShouldWork() throws Exception {
dataProbe.addDomain(DOMAIN);

ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "REMOVEDOMAIN", DOMAIN_COM});
ServerCmd.executeAndOutputToStream(new String[] {"-h", "127.0.0.1", "-p", "9999", "listdomains"}, outputCapture.getPrintStream());

assertThat(dataProbe.containsDomain(DOMAIN_COM)).isFalse();
assertThat(outputCapture.getContent()).contains(DOMAIN);
}

@Test
public void containsDomainShouldWork() throws Exception {
dataProbe.addDomain(DOMAIN);

ServerCmd.executeAndOutputToStream(new String[] {"-h", "127.0.0.1", "-p", "9999", "containsdomain", DOMAIN},
outputCapture.getPrintStream());

assertThat(outputCapture.getContent())
.containsOnlyOnce(DOMAIN + " exists");
}

@Test
public void addUserShouldWork() throws Exception {
dataProbe.addDomain(DOMAIN_COM);
dataProbe.addDomain(DOMAIN);

ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "ADDUSER", MAIL_ADDRESS, PASSWORD});

Expand All @@ -96,18 +112,29 @@ public void addUserShouldWork() throws Exception {

@Test
public void removeUserShouldWork() throws Exception {
dataProbe.addDomain(DOMAIN_COM);
dataProbe.addDomain(DOMAIN);
dataProbe.addUser(MAIL_ADDRESS, PASSWORD);

ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "REMOVEUSER", MAIL_ADDRESS});

assertThat(dataProbe.listUsers()).doesNotContain(MAIL_ADDRESS);
}

@Test
public void listUsersShouldWork() throws Exception {
dataProbe.addDomain(DOMAIN);
dataProbe.addUser(MAIL_ADDRESS, PASSWORD);

ServerCmd.executeAndOutputToStream(new String[] {"-h", "127.0.0.1", "-p", "9999", "listusers"}, outputCapture.getPrintStream());

assertThat(outputCapture.getContent())
.containsOnlyOnce(USER);
}

@Test
public void addAddressMappingShouldWork() throws Exception {
String redirectionAddress = "redirect@apache.org";
ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "addaddressmapping", USER, DOMAIN_COM, redirectionAddress});
ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "addaddressmapping", USER, DOMAIN, redirectionAddress});

assertThat(dataProbe.listMappings())
.containsOnly(
Expand All @@ -118,12 +145,36 @@ public void addAddressMappingShouldWork() throws Exception {
.build()));
}

@Test
public void listMappingsShouldWork() throws Exception {
String redirectionAddress = "redirect@apache.org";
ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "addaddressmapping", USER, DOMAIN, redirectionAddress});

ServerCmd.executeAndOutputToStream(new String[] {"-h", "127.0.0.1", "-p", "9999", "listmappings"},
outputCapture.getPrintStream());

assertThat(outputCapture.getContent())
.containsOnlyOnce("chibenwa@domain.com=redirect@apache.org");
}

@Test
public void listUsersDomainMappingShouldWork() throws Exception {
String redirectionAddress = "redirect@apache.org";
ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "addaddressmapping", USER, DOMAIN, redirectionAddress});

ServerCmd.executeAndOutputToStream(new String[] {"-h", "127.0.0.1", "-p", "9999", "listuserdomainmappings", USER, DOMAIN},
outputCapture.getPrintStream());

assertThat(outputCapture.getContent())
.containsOnlyOnce("redirect@apache.org");
}

@Test
public void removeAddressMappingShouldWork() throws Exception {
String redirectionAddress = "redirect@apache.org";
ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "addaddressmapping", USER, DOMAIN_COM, redirectionAddress});
ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "addaddressmapping", USER, DOMAIN, redirectionAddress});

ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "removeaddressmapping", USER, DOMAIN_COM, redirectionAddress});
ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "removeaddressmapping", USER, DOMAIN, redirectionAddress});

assertThat(dataProbe.listMappings())
.isNull();
Expand All @@ -132,7 +183,7 @@ public void removeAddressMappingShouldWork() throws Exception {
@Test
public void addRegexMappingShouldWork() throws Exception {
String regex = "regex";
ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "addregexmapping", USER, DOMAIN_COM, regex});
ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "addregexmapping", USER, DOMAIN, regex});

assertThat(dataProbe.listMappings())
.containsOnly(
Expand All @@ -146,9 +197,9 @@ public void addRegexMappingShouldWork() throws Exception {
@Test
public void removeRegexMappingShouldWork() throws Exception {
String regex = "regex";
ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "addregexmapping", USER, DOMAIN_COM, regex});
ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "addregexmapping", USER, DOMAIN, regex});

ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "removeregexmapping", USER, DOMAIN_COM, regex});
ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "removeregexmapping", USER, DOMAIN, regex});

assertThat(dataProbe.listMappings())
.isNull();
Expand Down
Expand Up @@ -22,40 +22,34 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

import javax.inject.Singleton;

import org.apache.james.GuiceJamesServer;
import org.apache.james.MemoryJmapTestRule;
import org.apache.james.mailbox.inmemory.InMemoryMailboxManager;
import org.apache.james.cli.util.OutputCapture;
import org.apache.james.mailbox.model.MailboxConstants;
import org.apache.james.mailbox.model.QuotaRoot;
import org.apache.james.mailbox.store.quota.QuotaRootImpl;
import org.apache.james.mailbox.store.search.ListeningMessageSearchIndex;
import org.apache.james.modules.MailboxProbeImpl;
import org.apache.james.modules.server.JMXServerModule;
import org.apache.james.utils.MailboxManagerDefinition;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import com.google.inject.Inject;
import com.google.inject.multibindings.Multibinder;

public class MailboxCommandsIntegrationTest {
public static final String USER = "user";
public static final String MAILBOX = "mailbox";
public static final String MAILBOX = "mailboxExampleName";

@Rule
public MemoryJmapTestRule memoryJmap = new MemoryJmapTestRule();
private GuiceJamesServer guiceJamesServer;
private MailboxProbeImpl mailboxProbe;
private OutputCapture outputCapture;

@Before
public void setUp() throws Exception {
guiceJamesServer = memoryJmap.jmapServer(new JMXServerModule(),
binder -> binder.bind(ListeningMessageSearchIndex.class).toInstance(mock(ListeningMessageSearchIndex.class)));
guiceJamesServer.start();
outputCapture = new OutputCapture();
mailboxProbe = guiceJamesServer.getProbe(MailboxProbeImpl.class);
}

Expand All @@ -80,6 +74,17 @@ public void deleteUserMailboxesShouldWork() throws Exception {
assertThat(mailboxProbe.listUserMailboxes(USER)).isEmpty();
}

@Test
public void listUserMailboxesShouldWork() throws Exception {
ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "createmailbox", MailboxConstants.USER_NAMESPACE, USER, MAILBOX});

ServerCmd.executeAndOutputToStream(new String[] {"-h", "127.0.0.1", "-p", "9999", "listusermailboxes", USER},
outputCapture.getPrintStream());

assertThat(outputCapture.getContent())
.containsOnlyOnce(MAILBOX);
}

@Test
public void deleteMailboxeShouldWork() throws Exception {
ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "createmailbox", MailboxConstants.USER_NAMESPACE, USER, MAILBOX});
Expand Down
Expand Up @@ -22,36 +22,23 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

import javax.inject.Singleton;

import org.apache.james.GuiceJamesServer;
import org.apache.james.MemoryJmapTestRule;
import org.apache.james.mailbox.inmemory.InMemoryMailboxManager;
import org.apache.james.cli.util.OutputCapture;
import org.apache.james.mailbox.model.QuotaRoot;
import org.apache.james.mailbox.store.quota.QuotaRootImpl;
import org.apache.james.mailbox.store.search.ListeningMessageSearchIndex;
import org.apache.james.modules.QuotaProbesImpl;
import org.apache.james.modules.server.JMXServerModule;
import org.apache.james.utils.MailboxManagerDefinition;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import com.google.inject.Inject;
import com.google.inject.multibindings.Multibinder;

public class QuotaCommandsIntegrationTest {
public static final String USER = "user";
public static final QuotaRoot QUOTA_ROOT = QuotaRootImpl.quotaRoot("#private&" + USER);

@Singleton
private static class MemoryMailboxManagerDefinition extends MailboxManagerDefinition {
@Inject
private MemoryMailboxManagerDefinition(InMemoryMailboxManager manager) {
super("memory-mailboxmanager", manager);
}
}
private OutputCapture outputCapture;

@Rule
public MemoryJmapTestRule memoryJmap = new MemoryJmapTestRule();
Expand All @@ -64,6 +51,7 @@ public void setUp() throws Exception {
binder -> binder.bind(ListeningMessageSearchIndex.class).toInstance(mock(ListeningMessageSearchIndex.class)));
guiceJamesServer.start();
quotaProbe = guiceJamesServer.getProbe(QuotaProbesImpl.class);
outputCapture = new OutputCapture();
}

@After
Expand All @@ -78,25 +66,90 @@ public void setDefaultMaxStorageShouldWork() throws Exception {
assertThat(quotaProbe.getDefaultMaxStorage()).isEqualTo(36);
}

@Test
public void getDefaultMaxStorageShouldWork() throws Exception {
ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "setdefaultmaxstoragequota", "36M"});

ServerCmd.executeAndOutputToStream(new String[] {"-h", "127.0.0.1", "-p", "9999", "getdefaultmaxstoragequota"},
outputCapture.getPrintStream());

assertThat(outputCapture.getContent())
.containsOnlyOnce("Default Maximum Storage Quota: 36 MB");
}

@Test
public void setDefaultMaxMessageCountShouldWork() throws Exception {
ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "setdefaultmaxmessagecountquota", "36"});

assertThat(quotaProbe.getDefaultMaxMessageCount()).isEqualTo(36);
}

@Test
public void getDefaultMaxMessageCountShouldWork() throws Exception {
ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "setdefaultmaxmessagecountquota", "36"});

ServerCmd.executeAndOutputToStream(new String[] {"-h", "127.0.0.1", "-p", "9999", "getdefaultmaxmessagecountquota"},
outputCapture.getPrintStream());

assertThat(outputCapture.getContent())
.containsOnlyOnce("Default Maximum message count Quota: 36");
}

@Test
public void setMaxStorageShouldWork() throws Exception {
ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "setmaxstoragequota", QUOTA_ROOT.getValue(), "36"});

assertThat(quotaProbe.getMaxStorage(QUOTA_ROOT.getValue())).isEqualTo(36);
}

@Test
public void getMaxStorageShouldWork() throws Exception {
ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "setmaxstoragequota", QUOTA_ROOT.getValue(), "1g"});

ServerCmd.executeAndOutputToStream(new String[] {"-h", "127.0.0.1", "-p", "9999", "getmaxstoragequota", QUOTA_ROOT.getValue()},
outputCapture.getPrintStream());

assertThat(outputCapture.getContent())
.containsOnlyOnce("Storage space allowed for Quota Root #private&user: 1 GB");
}

@Test
public void setMaxMessageCountShouldWork() throws Exception {
ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "setmaxmessagecountquota", QUOTA_ROOT.getValue(), "36"});

assertThat(quotaProbe.getMaxMessageCount(QUOTA_ROOT.getValue())).isEqualTo(36);
}

@Test
public void getMaxMessageCountShouldWork() throws Exception {
ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "setmaxmessagecountquota", QUOTA_ROOT.getValue(), "36"});

ServerCmd.executeAndOutputToStream(new String[] {"-h", "127.0.0.1", "-p", "9999", "getmaxmessagecountquota", QUOTA_ROOT.getValue()},
outputCapture.getPrintStream());

assertThat(outputCapture.getContent())
.containsOnlyOnce("MailboxMessage count allowed for Quota Root #private&user: 36");
}

@Test
public void getStorageQuotaShouldWork() throws Exception {
ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "setmaxstoragequota", QUOTA_ROOT.getValue(), "36"});

ServerCmd.executeAndOutputToStream(new String[] {"-h", "127.0.0.1", "-p", "9999", "getstoragequota", QUOTA_ROOT.getValue()},
outputCapture.getPrintStream());

assertThat(outputCapture.getContent())
.containsOnlyOnce("Storage quota for #private&user is: 0 bytes / 36 bytes");
}

@Test
public void getMessageCountQuotaShouldWork() throws Exception {
ServerCmd.doMain(new String[] {"-h", "127.0.0.1", "-p", "9999", "setmaxmessagecountquota", QUOTA_ROOT.getValue(), "36"});

ServerCmd.executeAndOutputToStream(new String[] {"-h", "127.0.0.1", "-p", "9999", "getmessagecountquota", QUOTA_ROOT.getValue()},
outputCapture.getPrintStream());

assertThat(outputCapture.getContent())
.containsOnlyOnce("MailboxMessage count quota for #private&user is: 0 / 36");
}
}

0 comments on commit c994be0

Please sign in to comment.