Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.

Commit 235fd3d

Browse files
author
Rustam Aliyev
committed
Added POP3 integration tests
1 parent 1456346 commit 235fd3d

File tree

5 files changed

+288
-11
lines changed

5 files changed

+288
-11
lines changed

itests/pom.xml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@
384384
<scope>test</scope>
385385
</dependency>
386386

387-
<!-- LMTP -->
387+
<!-- JAMES Protocols (LMTP, POP3) -->
388388
<dependency>
389389
<groupId>org.apache.james.protocols</groupId>
390390
<artifactId>protocols-netty</artifactId>
@@ -406,6 +406,13 @@
406406
<type>bundle</type>
407407
<scope>provided</scope>
408408
</dependency>
409+
<dependency>
410+
<groupId>org.apache.james.protocols</groupId>
411+
<artifactId>protocols-pop3</artifactId>
412+
<version>${bundle.james-protocols.version}</version>
413+
<type>bundle</type>
414+
<scope>provided</scope>
415+
</dependency>
409416
<dependency>
410417
<groupId>org.apache.james.protocols</groupId>
411418
<artifactId>protocols-lmtp</artifactId>
@@ -426,6 +433,12 @@
426433
<version>0.1.2</version>
427434
<scope>test</scope>
428435
</dependency>
436+
<dependency>
437+
<groupId>commons-net</groupId>
438+
<artifactId>commons-net</artifactId>
439+
<version>3.2</version>
440+
<scope>test</scope>
441+
</dependency>
429442
</dependencies>
430443

431444
<repositories>

itests/src/test/java/com/elasticinbox/itests/AbstractIntegrationTest.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,21 @@ public BundleContext getBundleContext() {
127127
* and @AfterClass. However PaxExam does not support it at this moment (see PAXEXAM-288).
128128
*
129129
* This method should be called at the beginning of each test manually.
130+
* @throws InterruptedException
130131
*/
131132
public static synchronized void initAccount()
132133
{
133-
if (!initialized) {
134-
// delete account
135-
expect().statusCode(204).when().delete(REST_PATH);
136-
137-
// create account
138-
expect().statusCode(201).when().post(REST_PATH);
139-
140-
initialized = true;
134+
// delete account
135+
expect().statusCode(204).when().delete(REST_PATH);
136+
137+
try {
138+
Thread.sleep(1000);
139+
} catch (InterruptedException e) {
140+
// ignore
141141
}
142+
143+
// create account
144+
expect().statusCode(201).when().post(REST_PATH);
142145
}
143146

144147
//@Test
Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
/**
2+
* Copyright (c) 2011-2012 Optimax Software Ltd.
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* * Redistributions of source code must retain the above copyright notice,
9+
* this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation
12+
* and/or other materials provided with the distribution.
13+
* * Neither the name of Optimax Software, ElasticInbox, nor the names
14+
* of its contributors may be used to endorse or promote products derived
15+
* from this software without specific prior written permission.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
21+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+
*/
28+
29+
package com.elasticinbox.itests;
30+
31+
import static org.ops4j.pax.exam.CoreOptions.*;
32+
33+
import static org.hamcrest.MatcherAssert.assertThat;
34+
import static org.hamcrest.Matchers.*;
35+
36+
import java.io.IOException;
37+
import java.util.HashMap;
38+
import java.util.Map;
39+
import java.util.UUID;
40+
41+
import org.apache.commons.net.pop3.POP3Client;
42+
import org.apache.commons.net.pop3.POP3MessageInfo;
43+
import org.junit.Test;
44+
import org.junit.runner.RunWith;
45+
import org.ops4j.pax.exam.Option;
46+
import org.ops4j.pax.exam.junit.Configuration;
47+
import org.ops4j.pax.exam.junit.ExamReactorStrategy;
48+
import org.ops4j.pax.exam.junit.JUnit4TestRunner;
49+
import org.ops4j.pax.exam.spi.reactors.EagerSingleStagedReactorFactory;
50+
51+
import com.elasticinbox.core.model.ReservedLabels;
52+
import com.elasticinbox.core.utils.Base64UUIDUtils;
53+
import com.google.common.collect.ObjectArrays;
54+
55+
/**
56+
* Integration test for POP3
57+
*
58+
* @author Rustam Aliyev
59+
*/
60+
@RunWith(JUnit4TestRunner.class)
61+
@ExamReactorStrategy(EagerSingleStagedReactorFactory.class)
62+
public class Pop3IT extends AbstractIntegrationTest
63+
{
64+
private final static int POP3_PORT = 2110;
65+
private final static String POP3_HOST = "localhost";
66+
67+
/**
68+
* Append POP3 Specific config options
69+
*
70+
* @return
71+
*/
72+
@Configuration()
73+
public Option[] config()
74+
{
75+
return ObjectArrays.concat(super.config(), options(
76+
// POP3 Test Bundles
77+
mavenBundle().groupId("commons-net").artifactId("commons-net").versionAsInProject(),
78+
mavenBundle().groupId("org.apache.james.protocols").artifactId("protocols-netty").versionAsInProject(),
79+
mavenBundle().groupId("org.apache.james.protocols").artifactId("protocols-api").versionAsInProject(),
80+
mavenBundle().groupId("org.apache.james.protocols").artifactId("protocols-pop3").versionAsInProject(),
81+
mavenBundle().groupId("io.netty").artifactId("netty").versionAsInProject(),
82+
83+
// ElasticInbox Bundles
84+
scanDir("../modules/pop3/target/")
85+
), Option.class);
86+
}
87+
88+
@Test
89+
public void testListUidl() throws IOException
90+
{
91+
initAccount();
92+
93+
// load messages with POP3 label
94+
long mailSizeRegular = getResourceSize(EMAIL_REGULAR);
95+
long mailSizeAttach = getResourceSize(EMAIL_LARGE_ATT);
96+
97+
Map<String, UUID> messages = new HashMap<String, UUID>(2);
98+
Integer labelId = ReservedLabels.POP3.getId();
99+
100+
messages.put("headers", RestV2IT.addMessage(EMAIL_REGULAR, labelId));
101+
messages.put("attach", RestV2IT.addMessage(EMAIL_LARGE_ATT, labelId));
102+
103+
// initialize POP3 client
104+
POP3Client client = new POP3Client();
105+
client.connect(POP3_HOST, POP3_PORT);
106+
107+
boolean loginSuccess = client.login(TEST_ACCOUNT, "valid");
108+
assertThat(loginSuccess, is(true));
109+
110+
// LIST all messages
111+
POP3MessageInfo[] info = client.listMessages();
112+
assertThat(info.length, equalTo(2));
113+
assertThat((int) mailSizeAttach, equalTo(info[0].size));
114+
assertThat((int) mailSizeRegular, equalTo(info[1].size));
115+
assertThat(info[0].number, equalTo(1));
116+
assertThat(info[1].number, equalTo(2));
117+
118+
// LIST one message
119+
POP3MessageInfo msgInfo = client.listMessage(1);
120+
assertThat((int) mailSizeAttach, equalTo(msgInfo.size));
121+
assertThat(msgInfo.number, equalTo(1));
122+
123+
// LIST message that does not exist
124+
msgInfo = client.listMessage(10);
125+
assertThat(msgInfo, nullValue());
126+
127+
// UIDL all messages
128+
info = client.listUniqueIdentifiers();
129+
assertThat(info.length, equalTo(2));
130+
assertThat(info[0].identifier,
131+
equalTo(Base64UUIDUtils.encode(messages.get("attach"))));
132+
assertThat(info[1].identifier,
133+
equalTo(Base64UUIDUtils.encode(messages.get("headers"))));
134+
assertThat(info[0].number, equalTo(1));
135+
assertThat(info[1].number, equalTo(2));
136+
137+
// UIDL one message
138+
msgInfo = client.listUniqueIdentifier(1);
139+
assertThat(msgInfo.identifier,
140+
equalTo(Base64UUIDUtils.encode(messages.get("attach"))));
141+
assertThat(msgInfo.number, equalTo(1));
142+
143+
// UIDL message that does not exist
144+
msgInfo = client.listUniqueIdentifier(10);
145+
assertThat(msgInfo, nullValue());
146+
147+
boolean logoutSuccess = client.logout();
148+
assertThat(logoutSuccess, is(true));
149+
}
150+
151+
@Test
152+
public void testDele() throws IOException
153+
{
154+
initAccount();
155+
156+
Map<String, UUID> messages = new HashMap<String, UUID>(2);
157+
Integer labelId = ReservedLabels.POP3.getId();
158+
159+
messages.put("headers", RestV2IT.addMessage(EMAIL_REGULAR, labelId));
160+
messages.put("attach", RestV2IT.addMessage(EMAIL_LARGE_ATT, labelId));
161+
162+
// initialize POP3 client
163+
POP3Client client = new POP3Client();
164+
client.connect(POP3_HOST, POP3_PORT);
165+
166+
// Login
167+
boolean loginSuccess = client.login(TEST_ACCOUNT, "valid");
168+
assertThat(loginSuccess, is(true));
169+
170+
// LIST all messages
171+
POP3MessageInfo[] info = client.listMessages();
172+
assertThat(info.length, equalTo(2));
173+
174+
// DELE message 1
175+
boolean deleteResult = client.deleteMessage(1);
176+
assertThat(deleteResult, is(true));
177+
178+
// LIST remaining
179+
info = client.listMessages();
180+
assertThat(info.length, equalTo(1));
181+
182+
// DELE message 1 again
183+
deleteResult = client.deleteMessage(1);
184+
assertThat(deleteResult, is(false));
185+
186+
// LIST remaining
187+
info = client.listMessages();
188+
assertThat(info.length, equalTo(1));
189+
190+
// DELE message 2
191+
deleteResult = client.deleteMessage(2);
192+
assertThat(deleteResult, is(true));
193+
194+
info = client.listMessages();
195+
assertThat(info.length, equalTo(0));
196+
197+
// QUIT so the messages get expunged
198+
boolean logoutSuccess = client.logout();
199+
assertThat(logoutSuccess, is(true));
200+
201+
// reconnect
202+
client.connect(POP3_HOST, POP3_PORT);
203+
204+
// Login
205+
loginSuccess = client.login(TEST_ACCOUNT, "valid");
206+
assertThat(loginSuccess, is(true));
207+
208+
info = client.listMessages();
209+
assertThat(info.length, equalTo(0));
210+
211+
logoutSuccess = client.logout();
212+
assertThat(logoutSuccess, is(true));
213+
}
214+
215+
@Test
216+
public void testRset() throws IOException
217+
{
218+
initAccount();
219+
220+
Integer labelId = ReservedLabels.POP3.getId();
221+
RestV2IT.addMessage(EMAIL_REGULAR, labelId);
222+
223+
// initialize POP3 client
224+
POP3Client client = new POP3Client();
225+
client.connect(POP3_HOST, POP3_PORT);
226+
227+
// Login
228+
boolean loginSuccess = client.login(TEST_ACCOUNT, "valid");
229+
assertThat(loginSuccess, is(true));
230+
231+
// LIST all messages
232+
POP3MessageInfo[] info = client.listMessages();
233+
assertThat(info.length, equalTo(1));
234+
235+
// DELE message 1
236+
boolean deleteResult = client.deleteMessage(1);
237+
assertThat(deleteResult, is(true));
238+
239+
// LIST remaining
240+
info = client.listMessages();
241+
assertThat(info.length, equalTo(0));
242+
243+
// RSET. After this the deleted mark should be removed again
244+
boolean resetRestult = client.reset();
245+
assertThat(resetRestult, is(true));
246+
247+
// LIST all messages
248+
info = client.listMessages();
249+
assertThat(info.length, equalTo(1));
250+
251+
// Logout
252+
boolean logoutSuccess = client.logout();
253+
assertThat(logoutSuccess, is(true));
254+
}
255+
}

itests/src/test/java/com/elasticinbox/itests/RestV2IT.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public void reservedLabelsTest()
8181
body("'" + ReservedLabels.IMPORTANT.getId() + "'", equalTo(ReservedLabels.IMPORTANT.getName())).
8282
body("'" + ReservedLabels.NOTIFICATIONS.getId() + "'", equalTo(ReservedLabels.NOTIFICATIONS.getName())).
8383
body("'" + ReservedLabels.ATTACHMENTS.getId() + "'", equalTo(ReservedLabels.ATTACHMENTS.getName())).
84+
body("'" + ReservedLabels.POP3.getId() + "'", equalTo(ReservedLabels.POP3.getName())).
8485
when().
8586
get(REST_PATH + "/mailbox");
8687
}
@@ -730,13 +731,13 @@ public void countersTest() throws IOException
730731
* @return
731732
* @throws IOException
732733
*/
733-
private UUID addMessage(String messageFile, Integer labelId) throws IOException
734+
protected static UUID addMessage(String messageFile, Integer labelId) throws IOException
734735
{
735736
InputStream in = null;
736737
byte[] messageBytes;
737738

738739
try {
739-
in = this.getClass().getResourceAsStream(messageFile);
740+
in = RestV2IT.class.getResourceAsStream(messageFile);
740741
messageBytes = ByteStreams.toByteArray(in);
741742
} finally {
742743
in.close();

itests/src/test/resources/elasticinbox.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ performance_counters_interval: 180
1919
lmtp_port: 2400
2020
lmtp_max_connections: 20
2121

22+
### POP3 settings
23+
# Set port number and maximum concurrent connections.
24+
pop3_port: 2110
25+
pop3_max_connections: 20
26+
2227
### Metadata storage driver
2328
# currently only "cassandra" supported
2429
metadata_storage_driver: cassandra

0 commit comments

Comments
 (0)