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

Commit f4a5d8b

Browse files
author
Rustam Aliyev
committed
Added integration test for counter scrub command and few other tests.
Fixes #21
1 parent b1bc3f5 commit f4a5d8b

File tree

5 files changed

+231
-2
lines changed

5 files changed

+231
-2
lines changed

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

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,107 @@ public void countersTest() throws IOException
722722

723723
logger.info("Counters Test OK");
724724
}
725+
726+
@Test
727+
public void mailboxCountersScrubTest() throws IOException
728+
{
729+
initAccount();
730+
731+
Map<String, UUID> messages = new HashMap<String, UUID>();
732+
LabelCounters inboxCounters = new LabelCounters();
733+
LabelCounters notifCounters = new LabelCounters();
734+
LabelCounters trashCounters = new LabelCounters();
735+
LabelCounters spamCounters = new LabelCounters();
736+
737+
// INBOX: add 5 messages, mark 2 as unread
738+
messages.put("inbox1", addMessage(EMAIL_REGULAR, ReservedLabels.INBOX.getId()));
739+
messages.put("inbox2", addMessage(EMAIL_REGULAR, ReservedLabels.INBOX.getId()));
740+
messages.put("inbox3", addMessage(EMAIL_REGULAR, ReservedLabels.INBOX.getId()));
741+
messages.put("inbox4", addMessage(EMAIL_REGULAR, ReservedLabels.INBOX.getId()));
742+
messages.put("inbox5", addMessage(EMAIL_REGULAR, ReservedLabels.INBOX.getId()));
743+
markAsRead(messages.get("inbox4"));
744+
markAsRead(messages.get("inbox5"));
745+
inboxCounters.setTotalMessages(5L);
746+
inboxCounters.setUnreadMessages(3L);
747+
748+
// NOTIFICATIONS: add 3 messages, mark 1 as read
749+
messages.put("notif1", addMessage(EMAIL_REGULAR, ReservedLabels.NOTIFICATIONS.getId()));
750+
messages.put("notif2", addMessage(EMAIL_REGULAR, ReservedLabels.NOTIFICATIONS.getId()));
751+
messages.put("notif3", addMessage(EMAIL_REGULAR, ReservedLabels.NOTIFICATIONS.getId()));
752+
markAsRead(messages.get("notif2"));
753+
notifCounters.setTotalMessages(3L);
754+
notifCounters.setUnreadMessages(2L);
755+
756+
// SPAM: add 5 messages, keep all unread
757+
messages.put("spam1", addMessage(EMAIL_REGULAR, ReservedLabels.SPAM.getId()));
758+
messages.put("spam2", addMessage(EMAIL_REGULAR, ReservedLabels.SPAM.getId()));
759+
messages.put("spam3", addMessage(EMAIL_REGULAR, ReservedLabels.SPAM.getId()));
760+
messages.put("spam4", addMessage(EMAIL_REGULAR, ReservedLabels.SPAM.getId()));
761+
messages.put("spam5", addMessage(EMAIL_REGULAR, ReservedLabels.SPAM.getId()));
762+
spamCounters.setTotalMessages(5L);
763+
spamCounters.setUnreadMessages(5L);
764+
765+
// TRASH: add 4 messages, mark 2 as read
766+
messages.put("trash1", addMessage(EMAIL_REGULAR, ReservedLabels.TRASH.getId()));
767+
messages.put("trash2", addMessage(EMAIL_REGULAR, ReservedLabels.TRASH.getId()));
768+
messages.put("trash3", addMessage(EMAIL_REGULAR, ReservedLabels.TRASH.getId()));
769+
messages.put("trash4", addMessage(EMAIL_REGULAR, ReservedLabels.TRASH.getId()));
770+
markAsRead(messages.get("trash1"));
771+
markAsRead(messages.get("trash3"));
772+
trashCounters.setTotalMessages(4L);
773+
trashCounters.setUnreadMessages(2L);
774+
775+
// check label counters
776+
expect().
777+
statusCode(200).and().
778+
body("'" + ReservedLabels.INBOX.getId() + "'.total",
779+
equalTo(inboxCounters.getTotalMessages().intValue())).
780+
body("'" + ReservedLabels.INBOX.getId() + "'.unread",
781+
equalTo(inboxCounters.getUnreadMessages().intValue())).
782+
body("'" + ReservedLabels.NOTIFICATIONS.getId() + "'.total",
783+
equalTo(notifCounters.getTotalMessages().intValue())).
784+
body("'" + ReservedLabels.NOTIFICATIONS.getId() + "'.unread",
785+
equalTo(notifCounters.getUnreadMessages().intValue())).
786+
body("'" + ReservedLabels.SPAM.getId() + "'.total",
787+
equalTo(spamCounters.getTotalMessages().intValue())).
788+
body("'" + ReservedLabels.SPAM.getId() + "'.unread",
789+
equalTo(spamCounters.getUnreadMessages().intValue())).
790+
body("'" + ReservedLabels.TRASH.getId() + "'.total",
791+
equalTo(trashCounters.getTotalMessages().intValue())).
792+
body("'" + ReservedLabels.TRASH.getId() + "'.unread",
793+
equalTo(trashCounters.getUnreadMessages().intValue())).
794+
when().
795+
get(REST_PATH + "/mailbox?metadata=true").asString();
796+
797+
// scrub label counters
798+
expect().
799+
statusCode(204).
800+
when().
801+
post(REST_PATH + "/mailbox/scrub/counters");
802+
803+
// check label counters after scrub
804+
expect().
805+
statusCode(200).and().
806+
body("'" + ReservedLabels.INBOX.getId() + "'.total",
807+
equalTo(inboxCounters.getTotalMessages().intValue())).
808+
body("'" + ReservedLabels.INBOX.getId() + "'.unread",
809+
equalTo(inboxCounters.getUnreadMessages().intValue())).
810+
body("'" + ReservedLabels.NOTIFICATIONS.getId() + "'.total",
811+
equalTo(notifCounters.getTotalMessages().intValue())).
812+
body("'" + ReservedLabels.NOTIFICATIONS.getId() + "'.unread",
813+
equalTo(notifCounters.getUnreadMessages().intValue())).
814+
body("'" + ReservedLabels.SPAM.getId() + "'.total",
815+
equalTo(spamCounters.getTotalMessages().intValue())).
816+
body("'" + ReservedLabels.SPAM.getId() + "'.unread",
817+
equalTo(spamCounters.getUnreadMessages().intValue())).
818+
body("'" + ReservedLabels.TRASH.getId() + "'.total",
819+
equalTo(trashCounters.getTotalMessages().intValue())).
820+
body("'" + ReservedLabels.TRASH.getId() + "'.unread",
821+
equalTo(trashCounters.getUnreadMessages().intValue())).
822+
when().
823+
get(REST_PATH + "/mailbox?metadata=true").asString();
824+
825+
}
725826

726827
/**
727828
* Adds message throught REST API and returns new message UUID
@@ -754,6 +855,23 @@ protected static UUID addMessage(String messageFile, Integer labelId) throws IOE
754855

755856
return UUID.fromString( with(response.asString()).getString("id") );
756857
}
858+
859+
/**
860+
* Mark given message as read (add SEEN marker)
861+
*
862+
* @param messageId
863+
*/
864+
private static void markAsRead(UUID messageId)
865+
{
866+
// assign labels and marker to the message
867+
given().
868+
pathParam("messageId", messageId.toString()).
869+
pathParam("seenMarker", Marker.SEEN.toString().toLowerCase()).
870+
expect().
871+
statusCode(204).
872+
when().
873+
put(REST_PATH + "/mailbox/message/{messageId}?addmarker={seenMarker}");
874+
}
757875

758876
/**
759877
* Adds labels through REST API and returns new label ID

modules/core/src/main/java/com/elasticinbox/core/cassandra/CassandraMessageDAO.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,14 +468,16 @@ public Labels calculateCounters(final Mailbox mailbox)
468468
}
469469
while (purgeIndex.size() >= BatchConstants.BATCH_READS);
470470

471+
logger.debug("Found {} messages pending purge. Will exclude them from calculations.", deletedMessages.size());
472+
471473
// reset start, read messages and calculate label counters
472474
start = TimeUUIDUtils.getUniqueTimeUUIDinMillis();
473475
do {
474476
messages = MessagePersistence.getRange(
475477
mailbox.getId(), start, BatchConstants.BATCH_READS);
476478

477479
for (UUID messageId : messages.keySet())
478-
{
480+
{
479481
start = messageId; // shift next query start
480482

481483
// skip messages from purge queue
@@ -487,6 +489,8 @@ public Labels calculateCounters(final Mailbox mailbox)
487489
for (int labelId : message.getLabels()) {
488490
labels.incrementCounters(labelId, message.getLabelCounters());
489491
}
492+
493+
logger.debug("Counters state after message {} is {}", messageId, labels.toString());
490494
}
491495
}
492496
while (messages.size() >= BatchConstants.BATCH_READS);

modules/core/src/main/java/com/elasticinbox/core/model/Labels.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,14 @@ public void setCounters(Map<Integer, LabelCounters> counters) {
123123
*/
124124
public void incrementCounters(final int labelId, LabelCounters counters)
125125
{
126-
if (!this.counters.containsKey(labelId)) {
126+
if (!this.counters.containsKey(labelId))
127+
{
127128
this.counters.put(labelId, counters);
129+
130+
// also initialize label with empty name
131+
if (!this.labels.containsKey(labelId)) {
132+
this.labels.put(labelId, "");
133+
}
128134
} else {
129135
this.counters.get(labelId).add(counters);
130136
}
@@ -225,4 +231,10 @@ public Map<Integer, Map<String, Object>> toJson()
225231

226232
return metadata;
227233
}
234+
235+
@Override
236+
public String toString()
237+
{
238+
return toJson().toString();
239+
}
228240
}

modules/core/src/test/java/com/elasticinbox/core/message/MimeParserTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1+
/**
2+
* Copyright (c) 2011-2013 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+
129
package com.elasticinbox.core.message;
230

331
import static org.junit.Assert.*;
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Copyright (c) 2011-2013 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.core.model;
30+
31+
import static org.junit.Assert.*;
32+
33+
import org.junit.AfterClass;
34+
import org.junit.Test;
35+
36+
public class LabelsTest {
37+
38+
@AfterClass
39+
public static void tearDownAfterClass() throws Exception {
40+
}
41+
42+
@Test
43+
public void testIncrementCounters()
44+
{
45+
Labels labels = new Labels();
46+
47+
LabelCounters lc = new LabelCounters();
48+
lc.setTotalMessages(120L);
49+
lc.setTotalBytes(1024000L);
50+
lc.setUnreadMessages(32L);
51+
52+
LabelCounters diff = new LabelCounters();
53+
diff.setTotalMessages(19L);
54+
diff.setTotalBytes(24000L);
55+
diff.setUnreadMessages(5L);
56+
57+
// increment initialized label
58+
labels.add(1, ReservedLabels.INBOX.getName());
59+
labels.setCounters(1, lc);
60+
labels.incrementCounters(1, diff);
61+
62+
assertEquals(labels.getLabelCounters(1).getTotalMessages().intValue(), 120+19);
63+
assertEquals(labels.getLabelCounters(1).getTotalBytes().intValue(), 1024000+24000);
64+
assertEquals(labels.getLabelCounters(1).getUnreadMessages().intValue(), 32+5);
65+
}
66+
67+
}

0 commit comments

Comments
 (0)