Browse code

BM-13061 Fix: MailboxStoreService.allUids now returns uids linked to a mailbox instead of all domain's items uids

Vincent Vignaud authored on 13/03/2019 14:37:59
Showing 3 changed files
... ...
@@ -67,7 +67,6 @@ public class MailboxStoreTests {
67 67
 	public void before() throws Exception {
68 68
 		JdbcTestHelper.getInstance().beforeTest();
69 69
 
70
-		
71 70
 		SecurityContext securityContext = SecurityContext.ANONYMOUS;
72 71
 
73 72
 		ContainerStore containerStore = new ContainerStore(JdbcTestHelper.getInstance().getDataSource(),
... ...
@@ -263,4 +262,37 @@ public class MailboxStoreTests {
263 262
 		assertEquals(1, uids.size());
264 263
 		assertEquals(uid, uids.get(0));
265 264
 	}
265
+
266
+	/**
267
+	 * {@link MailboxStore#allUids()} should only return uids of items linked to a
268
+	 * {@link Mailbox}.
269
+	 */
270
+	@Test
271
+	public void testAllUids() throws Exception {
272
+		// create 4 non-mailbox items
273
+		itemStore.create(Item.create("testAllUids001", null));
274
+		itemStore.create(Item.create("testAllUids002", null));
275
+		itemStore.create(Item.create("testAllUids003", null));
276
+		itemStore.create(Item.create("testAllUids004", null));
277
+
278
+		// create 1 mailbox item
279
+		itemStore.create(Item.create(uid, null));
280
+		Item item = itemStore.get(uid);
281
+		Mailbox mb = getDefaultMailbox();
282
+		mailboxStore.create(item, mb);
283
+
284
+		// should retrieve 1 uid when calling mailboxStore.allUids()
285
+		final List<String> allMailboxUids = mailboxStore.allUids();
286
+		assertNotNull(allMailboxUids);
287
+		assertEquals(1, allMailboxUids.size());
288
+
289
+		// should retrieve 5 uids when calling itemStore.allItemUids()
290
+		final List<String> allItemUids = itemStore.allItemUids();
291
+		assertNotNull(allItemUids);
292
+		assertEquals(5, allItemUids.size());
293
+
294
+		// itemStore.allItemUids should containe uids in mailboxStore.allUids
295
+		assertTrue(allItemUids.contains(allMailboxUids.get(0)));
296
+
297
+	}
266 298
 }
... ...
@@ -36,6 +36,7 @@ import net.bluemind.core.api.Email;
36 36
 import net.bluemind.core.container.model.Container;
37 37
 import net.bluemind.core.container.model.Item;
38 38
 import net.bluemind.core.container.persistance.AbstractItemValueStore;
39
+import net.bluemind.core.container.persistance.StringCreator;
39 40
 import net.bluemind.mailbox.api.Mailbox;
40 41
 import net.bluemind.mailbox.api.Mailbox.Routing;
41 42
 import net.bluemind.mailbox.api.Mailbox.Type;
... ...
@@ -60,12 +61,6 @@ public class MailboxStore extends AbstractItemValueStore<Mailbox> {
60 61
 		}
61 62
 	};
62 63
 
63
-	private static final Creator<Email> EMAIL_CREATOR = new Creator<Email>() {
64
-		public Email create(ResultSet con) throws SQLException {
65
-			return new Email();
66
-		}
67
-	};
68
-
69 64
 	public MailboxStore(DataSource pool, Container container) {
70 65
 		super(pool);
71 66
 		this.container = container;
... ...
@@ -176,14 +171,8 @@ public class MailboxStore extends AbstractItemValueStore<Mailbox> {
176 171
 				+ "  )" //
177 172
 				+ " AND item.container_id = ?";
178 173
 
179
-		String mailboxUid = unique(query, new Creator<String>() {
180
-
181
-			@Override
182
-			public String create(ResultSet rs) throws SQLException {
183
-				return rs.getString(1);
184
-			}
185
-
186
-		}, Collections.<EntityPopulator<String>>emptyList(), new Object[] { email, leftPart, leftPart, container.id });
174
+		String mailboxUid = unique(query, StringCreator.FIRST, Collections.<EntityPopulator<String>>emptyList(),
175
+				new Object[] { email, leftPart, leftPart, container.id });
187 176
 
188 177
 		return mailboxUid;
189 178
 	}
... ...
@@ -201,14 +190,8 @@ public class MailboxStore extends AbstractItemValueStore<Mailbox> {
201 190
 				+ " WHERE m.type = ?::enum_mailbox_type" //
202 191
 				+ " AND item.container_id = ?";
203 192
 
204
-		List<String> mailboxUids = select(query, new Creator<String>() {
205
-
206
-			@Override
207
-			public String create(ResultSet rs) throws SQLException {
208
-				return rs.getString(1);
209
-			}
210
-
211
-		}, Collections.<EntityPopulator<String>>emptyList(), new Object[] { type.name(), container.id });
193
+		List<String> mailboxUids = select(query, StringCreator.FIRST, Collections.<EntityPopulator<String>>emptyList(),
194
+				new Object[] { type.name(), container.id });
212 195
 
213 196
 		return mailboxUids;
214 197
 	}
... ...
@@ -226,14 +209,8 @@ public class MailboxStore extends AbstractItemValueStore<Mailbox> {
226 209
 				+ " WHERE e.name = ?" //
227 210
 				+ " AND item.container_id = ?";
228 211
 
229
-		String mailboxUid = unique(query, new Creator<String>() {
230
-
231
-			@Override
232
-			public String create(ResultSet rs) throws SQLException {
233
-				return rs.getString(1);
234
-			}
235
-
236
-		}, Collections.<EntityPopulator<String>>emptyList(), new Object[] { name, container.id });
212
+		String mailboxUid = unique(query, StringCreator.FIRST, Collections.<EntityPopulator<String>>emptyList(),
213
+				new Object[] { name, container.id });
237 214
 
238 215
 		return mailboxUid;
239 216
 	}
... ...
@@ -324,13 +301,8 @@ public class MailboxStore extends AbstractItemValueStore<Mailbox> {
324 301
 		String query = "SELECT item.uid FROM t_mailbox "
325 302
 				+ " INNER JOIN t_container_item item ON item_id = item.id WHERE container_id = ? AND quota > 0";
326 303
 
327
-		List<String> uids = select(query, new Creator<String>() {
328
-			@Override
329
-			public String create(ResultSet rs) throws SQLException {
330
-				return rs.getString(1);
331
-			}
332
-
333
-		}, Collections.<EntityPopulator<String>>emptyList(), new Object[] { container.id });
304
+		List<String> uids = select(query, StringCreator.FIRST, Collections.<EntityPopulator<String>>emptyList(),
305
+				new Object[] { container.id });
334 306
 
335 307
 		return uids;
336 308
 	}
... ...
@@ -342,14 +314,8 @@ public class MailboxStore extends AbstractItemValueStore<Mailbox> {
342 314
 				+ " WHERE m.routing = ?::enum_mailbox_routing" //
343 315
 				+ " AND item.container_id = ?";
344 316
 
345
-		List<String> mailboxUids = select(query, new Creator<String>() {
346
-
347
-			@Override
348
-			public String create(ResultSet rs) throws SQLException {
349
-				return rs.getString(1);
350
-			}
351
-
352
-		}, Collections.<EntityPopulator<String>>emptyList(), new Object[] { routing.name(), container.id });
317
+		List<String> mailboxUids = select(query, StringCreator.FIRST, Collections.<EntityPopulator<String>>emptyList(),
318
+				new Object[] { routing.name(), container.id });
353 319
 
354 320
 		return mailboxUids;
355 321
 	}
... ...
@@ -359,4 +325,11 @@ public class MailboxStore extends AbstractItemValueStore<Mailbox> {
359 325
 		delete(query, new Object[] { alias });
360 326
 	}
361 327
 
328
+	public List<String> allUids() throws SQLException {
329
+		final String query = "SELECT i.uid FROM t_container_item i JOIN t_mailbox m ON m.item_id = i.id WHERE i.container_id = ?";
330
+
331
+		return select(query.toString(), StringCreator.FIRST, Collections.<EntityPopulator<String>>emptyList(),
332
+				new Object[] { container.id });
333
+	}
334
+
362 335
 }
... ...
@@ -42,12 +42,14 @@ public class MailboxStoreService extends ContainerStoreService<Mailbox> {
42 42
 	private final MailFilterStore mailFilterStore;
43 43
 	private final String origin;
44 44
 	private MailboxIdentityStore identityStore;
45
+	private MailboxStore mailboxStore;
45 46
 
46 47
 	public MailboxStoreService(DataSource pool, SecurityContext securityContext, Container container) {
47 48
 		super(pool, securityContext, container, MailboxContainerType.TYPE, new MailboxStore(pool, container));
48 49
 		this.mailFilterStore = new MailFilterStore(pool, container);
49 50
 		this.origin = securityContext.getOrigin();
50 51
 		this.identityStore = new MailboxIdentityStore(pool);
52
+		this.mailboxStore = new MailboxStore(pool, container);
51 53
 	}
52 54
 
53 55
 	@Override
... ...
@@ -130,4 +132,13 @@ public class MailboxStoreService extends ContainerStoreService<Mailbox> {
130 132
 		}
131 133
 	}
132 134
 
135
+	@Override
136
+	public List<String> allUids() throws ServerFault {
137
+		try {
138
+			return this.mailboxStore.allUids();
139
+		} catch (SQLException e) {
140
+			throw ServerFault.sqlFault(e);
141
+		}
142
+	}
143
+
133 144
 }