-
Notifications
You must be signed in to change notification settings - Fork 462
/
MailboxPathV2MigrationTest.java
136 lines (115 loc) · 5.94 KB
/
MailboxPathV2MigrationTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one *
* or more contributor license agreements. See the NOTICE file *
* distributed with this work for additional information *
* regarding copyright ownership. The ASF licenses this file *
* to you under the Apache License, Version 2.0 (the *
* "License"); you may not use this file except in compliance *
* with the License. You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, *
* software distributed under the License is distributed on an *
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
* KIND, either express or implied. See the License for the *
* specific language governing permissions and limitations *
* under the License. *
****************************************************************/
package org.apache.james.mailbox.cassandra.mail.migration;
import static org.assertj.core.api.Assertions.assertThat;
import org.apache.james.backends.cassandra.CassandraCluster;
import org.apache.james.backends.cassandra.DockerCassandraRule;
import org.apache.james.backends.cassandra.init.CassandraConfiguration;
import org.apache.james.backends.cassandra.init.CassandraModuleComposite;
import org.apache.james.backends.cassandra.utils.CassandraUtils;
import org.apache.james.mailbox.cassandra.ids.CassandraId;
import org.apache.james.mailbox.cassandra.mail.CassandraACLMapper;
import org.apache.james.mailbox.cassandra.mail.CassandraIdAndPath;
import org.apache.james.mailbox.cassandra.mail.CassandraMailboxDAO;
import org.apache.james.mailbox.cassandra.mail.CassandraMailboxMapper;
import org.apache.james.mailbox.cassandra.mail.CassandraMailboxPathDAOImpl;
import org.apache.james.mailbox.cassandra.mail.CassandraMailboxPathV2DAO;
import org.apache.james.mailbox.cassandra.mail.CassandraUserMailboxRightsDAO;
import org.apache.james.mailbox.cassandra.modules.CassandraAclModule;
import org.apache.james.mailbox.cassandra.modules.CassandraMailboxModule;
import org.apache.james.mailbox.model.MailboxPath;
import org.apache.james.mailbox.store.mail.model.impl.SimpleMailbox;
import org.assertj.core.api.SoftAssertions;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
public class MailboxPathV2MigrationTest {
private static final MailboxPath MAILBOX_PATH_1 = MailboxPath.forUser("bob", "Important");
private static final int UID_VALIDITY_1 = 452;
private static final SimpleMailbox MAILBOX_1 = new SimpleMailbox(MAILBOX_PATH_1, UID_VALIDITY_1);
private static final CassandraId MAILBOX_ID_1 = CassandraId.timeBased();
@BeforeClass
public static void setUpClass() {
MAILBOX_1.setMailboxId(MAILBOX_ID_1);
}
@ClassRule
public static DockerCassandraRule cassandraServer = new DockerCassandraRule();
private CassandraMailboxPathDAOImpl daoV1;
private CassandraMailboxPathV2DAO daoV2;
private CassandraMailboxMapper mailboxMapper;
private CassandraMailboxDAO mailboxDAO;
@Before
public void setUp() {
CassandraCluster cassandra = CassandraCluster.create(
new CassandraModuleComposite(
new CassandraMailboxModule(),
new CassandraAclModule()),
cassandraServer.getIp(),
cassandraServer.getBindingPort());
daoV1 = new CassandraMailboxPathDAOImpl(
cassandra.getConf(),
cassandra.getTypesProvider(),
CassandraUtils.WITH_DEFAULT_CONFIGURATION);
daoV2 = new CassandraMailboxPathV2DAO(
cassandra.getConf(),
CassandraUtils.WITH_DEFAULT_CONFIGURATION);
CassandraUserMailboxRightsDAO userMailboxRightsDAO = new CassandraUserMailboxRightsDAO(cassandra.getConf(), CassandraUtils.WITH_DEFAULT_CONFIGURATION);
mailboxDAO = new CassandraMailboxDAO(cassandra.getConf(), cassandra.getTypesProvider());
mailboxMapper = new CassandraMailboxMapper(
mailboxDAO,
daoV1,
daoV2,
userMailboxRightsDAO,
new CassandraACLMapper(cassandra.getConf(), userMailboxRightsDAO, CassandraConfiguration.DEFAULT_CONFIGURATION));
}
@Test
public void newValuesShouldBeSavedInMostRecentDAO() throws Exception {
mailboxMapper.save(MAILBOX_1);
assertThat(daoV2.retrieveId(MAILBOX_PATH_1).join())
.contains(new CassandraIdAndPath(MAILBOX_ID_1, MAILBOX_PATH_1));
}
@Test
public void newValuesShouldNotBeSavedInOldDAO() throws Exception {
mailboxMapper.save(MAILBOX_1);
assertThat(daoV1.retrieveId(MAILBOX_PATH_1).join())
.isEmpty();
}
@Test
public void readingOldValuesShouldMigrateThem() throws Exception {
daoV1.save(MAILBOX_PATH_1, MAILBOX_ID_1).join();
mailboxDAO.save(MAILBOX_1).join();
mailboxMapper.findMailboxByPath(MAILBOX_PATH_1);
SoftAssertions softly = new SoftAssertions();
softly.assertThat(daoV1.retrieveId(MAILBOX_PATH_1).join()).isEmpty();
softly.assertThat(daoV2.retrieveId(MAILBOX_PATH_1).join())
.contains(new CassandraIdAndPath(MAILBOX_ID_1, MAILBOX_PATH_1));
softly.assertAll();
}
@Test
public void migrationTaskShouldMoveDataToMostRecentDao() {
daoV1.save(MAILBOX_PATH_1, MAILBOX_ID_1).join();
new MailboxPathV2Migration(daoV1, daoV2).run();
SoftAssertions softly = new SoftAssertions();
softly.assertThat(daoV1.retrieveId(MAILBOX_PATH_1).join()).isEmpty();
softly.assertThat(daoV2.retrieveId(MAILBOX_PATH_1).join())
.contains(new CassandraIdAndPath(MAILBOX_ID_1, MAILBOX_PATH_1));
softly.assertAll();
}
}