Browse code

SUDO-119 Fix: delete domain filters on domain deletion

Thomas Fricker authored on 13/04/2018 14:44:06
Showing 5 changed files
... ...
@@ -34,5 +34,6 @@ Require-Bundle: org.junit,
34 34
  net.bluemind.authentication.handler;bundle-version="3.1.0",
35 35
  net.bluemind.authentication.persistence;bundle-version="3.1.0",
36 36
  net.bluemind.authentication.service;bundle-version="3.1.0",
37
- net.bluemind.authentication.sqlschema;bundle-version="3.1.0"
37
+ net.bluemind.authentication.sqlschema;bundle-version="3.1.0",
38
+ net.bluemind.core.elasticsearch.testshelper
38 39
 
... ...
@@ -4,6 +4,8 @@
4 4
     "name":"bluemind/imap-role"
5 5
             },{
6 6
     "name":"bluemind/smtp-role"
7
-        },{
7
+        },{ 
8
+    "name":"bluemind/elasticsearch-tests" 
9
+		},{
8 10
     "name":"bluemind/node-tests"
9 11
 }]
10 12
new file mode 100644
... ...
@@ -0,0 +1,121 @@
0
+/* BEGIN LICENSE
1
+ * Copyright © Blue Mind SAS, 2012-2018
2
+ *
3
+ * This file is part of BlueMind. BlueMind is a messaging and collaborative
4
+ * solution.
5
+ *
6
+ * This program is free software; you can redistribute it and/or modify
7
+ * it under the terms of either the GNU Affero General Public License as
8
+ * published by the Free Software Foundation (version 3 of the License).
9
+ *
10
+ *
11
+ * This program is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
+ *
15
+ * See LICENSE.txt
16
+ * END LICENSE
17
+ */
18
+package net.bluemind.mailbox.service.tests;
19
+
20
+import static org.junit.Assert.assertEquals;
21
+
22
+import java.util.Arrays;
23
+import java.util.concurrent.CountDownLatch;
24
+
25
+import org.junit.Before;
26
+import org.junit.Test;
27
+import org.vertx.java.core.AsyncResult;
28
+import org.vertx.java.core.Handler;
29
+
30
+import com.google.common.collect.Lists;
31
+
32
+import net.bluemind.core.api.fault.ServerFault;
33
+import net.bluemind.core.context.SecurityContext;
34
+import net.bluemind.core.elasticsearch.ElasticsearchTestHelper;
35
+import net.bluemind.core.jdbc.JdbcActivator;
36
+import net.bluemind.core.jdbc.JdbcTestHelper;
37
+import net.bluemind.core.rest.ServerSideServiceProvider;
38
+import net.bluemind.core.task.api.ITask;
39
+import net.bluemind.core.task.api.TaskRef;
40
+import net.bluemind.core.task.api.TaskStatus;
41
+import net.bluemind.core.task.api.TaskStatus.State;
42
+import net.bluemind.domain.api.IDomains;
43
+import net.bluemind.lib.vertx.VertxPlatform;
44
+import net.bluemind.mailbox.api.IMailboxes;
45
+import net.bluemind.mailbox.api.MailFilter;
46
+import net.bluemind.pool.impl.BmConfIni;
47
+import net.bluemind.server.api.Server;
48
+import net.bluemind.tests.defaultdata.PopulateHelper;
49
+
50
+public class DomainFilterHookTests {
51
+
52
+	protected String domainUid;
53
+
54
+	@Before
55
+	public void before() throws Exception {
56
+		JdbcTestHelper.getInstance().beforeTest();
57
+		JdbcTestHelper.getInstance().getDbSchemaService().initialize();
58
+		JdbcActivator.getInstance().setDataSource(JdbcTestHelper.getInstance().getDataSource());
59
+		ElasticsearchTestHelper.getInstance().beforeTest();
60
+		final CountDownLatch launched = new CountDownLatch(1);
61
+		VertxPlatform.spawnVerticles(new Handler<AsyncResult<Void>>() {
62
+			@Override
63
+			public void handle(AsyncResult<Void> event) {
64
+				launched.countDown();
65
+			}
66
+		});
67
+		launched.await();
68
+
69
+		domainUid = "bm.lan";
70
+
71
+		Server imapServer = new Server();
72
+		imapServer.ip = new BmConfIni().get("imap-role");
73
+		imapServer.tags = Lists.newArrayList("mail/imap");
74
+
75
+		Server esServer = new Server();
76
+		esServer.ip = ElasticsearchTestHelper.getInstance().getHost();
77
+		esServer.tags = Lists.newArrayList("bm/es");
78
+
79
+		PopulateHelper.initGlobalVirt(imapServer, esServer);
80
+
81
+		PopulateHelper.createTestDomain(domainUid, imapServer, esServer);
82
+	}
83
+
84
+	@Test
85
+	public void testDomainDeletionShouldDeleteAllMailfilters() {
86
+		IMailboxes domainMailboxesService = ServerSideServiceProvider.getProvider(SecurityContext.SYSTEM)
87
+				.instance(IMailboxes.class, domainUid);
88
+
89
+		MailFilter filter = new MailFilter();
90
+		MailFilter.Rule rule = new MailFilter.Rule();
91
+		rule.criteria = "blub";
92
+		rule.active = false;
93
+		filter.rules = Arrays.asList(rule);
94
+		domainMailboxesService.setDomainFilter(filter);
95
+
96
+		assertEquals(1, domainMailboxesService.getDomainFilter().rules.size());
97
+
98
+		// would fail if filter persists
99
+		IDomains service = ServerSideServiceProvider.getProvider(SecurityContext.SYSTEM).instance(IDomains.class);
100
+		TaskRef deleteDomainItems = service.deleteDomainItems(domainUid);
101
+		waitFor(deleteDomainItems);
102
+		service.delete(domainUid);
103
+	}
104
+
105
+	private void waitFor(TaskRef taskRef) throws ServerFault {
106
+		ITask task = ServerSideServiceProvider.getProvider(SecurityContext.SYSTEM).instance(ITask.class, taskRef.id);
107
+		while (!task.status().state.ended) {
108
+			try {
109
+				Thread.sleep(100);
110
+			} catch (InterruptedException e) {
111
+			}
112
+		}
113
+
114
+		TaskStatus status = task.status();
115
+		if (status.state == State.InError) {
116
+			throw new ServerFault("error");
117
+		}
118
+	}
119
+
120
+}
... ...
@@ -80,4 +80,10 @@
80 80
                impl="net.bluemind.mailbox.service.cache.UnreadMessagesCacheRegistry">
81 81
          </reg>
82 82
       </extension>
83
+      <extension
84
+            point="net.bluemind.domain.domainHook">
85
+         <hook
86
+               class="net.bluemind.mailbox.service.hook.DomainMailFilterHook">
87
+         </hook>
88
+      </extension>
83 89
 </plugin>
84 90
new file mode 100644
... ...
@@ -0,0 +1,42 @@
0
+/* BEGIN LICENSE
1
+ * Copyright © Blue Mind SAS, 2012-2018
2
+ *
3
+ * This file is part of BlueMind. BlueMind is a messaging and collaborative
4
+ * solution.
5
+ *
6
+ * This program is free software; you can redistribute it and/or modify
7
+ * it under the terms of either the GNU Affero General Public License as
8
+ * published by the Free Software Foundation (version 3 of the License).
9
+ *
10
+ *
11
+ * This program is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
+ *
15
+ * See LICENSE.txt
16
+ * END LICENSE
17
+ */
18
+package net.bluemind.mailbox.service.hook;
19
+
20
+import org.slf4j.Logger;
21
+import org.slf4j.LoggerFactory;
22
+
23
+import net.bluemind.core.api.fault.ServerFault;
24
+import net.bluemind.core.container.model.ItemValue;
25
+import net.bluemind.core.rest.BmContext;
26
+import net.bluemind.domain.api.Domain;
27
+import net.bluemind.domain.hook.DomainHookAdapter;
28
+import net.bluemind.mailbox.api.IMailboxes;
29
+import net.bluemind.mailbox.api.MailFilter;
30
+
31
+public class DomainMailFilterHook extends DomainHookAdapter {
32
+
33
+	public static final Logger logger = LoggerFactory.getLogger(DomainMailFilterHook.class);
34
+
35
+	@Override
36
+	public void onBeforeDelete(BmContext context, ItemValue<Domain> domain) throws ServerFault {
37
+		logger.info("Deleting domain mail filters of domain {}", domain.uid);
38
+		context.su().provider().instance(IMailboxes.class, domain.uid).setDomainFilter(new MailFilter());
39
+	}
40
+
41
+}