Browse code

BM-14999 Fix: set containerUid on sanitizing

Thomas Fricker authored on 28/06/2019 09:41:44
Showing 5 changed files
... ...
@@ -34,5 +34,6 @@ Require-Bundle: org.junit,
34 34
  net.bluemind.authentication.service;bundle-version="4.1.0",
35 35
  org.mockito.mockito-core,
36 36
  org.objenesis,
37
- net.bluemind.core.validator
37
+ net.bluemind.core.validator,
38
+ net.bluemind.core.sanitizer
38 39
 Automatic-Module-Name: net.bluemind.addressbook.service.tests
... ...
@@ -19,11 +19,13 @@
19 19
 package net.bluemind.addressbook.service;
20 20
 
21 21
 import static org.junit.Assert.assertEquals;
22
+import static org.junit.Assert.assertNull;
22 23
 import static org.junit.Assert.fail;
23 24
 
24 25
 import java.util.ArrayList;
25 26
 import java.util.Arrays;
26 27
 import java.util.List;
28
+import java.util.Optional;
27 29
 
28 30
 import org.junit.Test;
29 31
 
... ...
@@ -35,6 +37,7 @@ import net.bluemind.addressbook.api.VCard.Communications.Tel;
35 37
 import net.bluemind.addressbook.api.VCard.DeliveryAddressing;
36 38
 import net.bluemind.addressbook.api.VCard.DeliveryAddressing.Address;
37 39
 import net.bluemind.addressbook.api.VCard.Explanatory.Url;
40
+import net.bluemind.addressbook.api.VCard.Organizational.Member;
38 41
 import net.bluemind.addressbook.api.VCard.Parameter;
39 42
 import net.bluemind.addressbook.service.internal.VCardSanitizer;
40 43
 import net.bluemind.core.api.fault.ServerFault;
... ...
@@ -49,7 +52,7 @@ public class BasicVCardSanitizerTest {
49 52
 		card.organizational.org.company = null;
50 53
 
51 54
 		try {
52
-			getSanitizer().sanitize(card);
55
+			getSanitizer().sanitize(card, Optional.empty());
53 56
 		} catch (ServerFault e) {
54 57
 			e.printStackTrace();
55 58
 			fail(e.getMessage());
... ...
@@ -67,7 +70,7 @@ public class BasicVCardSanitizerTest {
67 70
 		card.organizational.org.company = company;
68 71
 
69 72
 		try {
70
-			getSanitizer().sanitize(card);
73
+			getSanitizer().sanitize(card, Optional.empty());
71 74
 		} catch (ServerFault e) {
72 75
 			e.printStackTrace();
73 76
 			fail(e.getMessage());
... ...
@@ -89,7 +92,7 @@ public class BasicVCardSanitizerTest {
89 92
 		card.communications.emails = emails;
90 93
 
91 94
 		try {
92
-			getSanitizer().sanitize(card);
95
+			getSanitizer().sanitize(card, Optional.empty());
93 96
 		} catch (ServerFault e) {
94 97
 			e.printStackTrace();
95 98
 			fail(e.getMessage());
... ...
@@ -100,6 +103,54 @@ public class BasicVCardSanitizerTest {
100 103
 	}
101 104
 
102 105
 	@Test
106
+	public void testSanitizerShouldInsertMissingContainerUid() {
107
+		String email = "test@test.com";
108
+		VCard card = defaultVCard();
109
+		card.identification.name.givenNames = null;
110
+		card.identification.name.familyNames = null;
111
+		card.organizational.org.company = null;
112
+		card.organizational.member = new ArrayList<VCard.Organizational.Member>();
113
+		VCard.Organizational.Member m = new VCard.Organizational.Member();
114
+		m.commonName = "air conditioning";
115
+		m.mailto = "killmy@planet.org";
116
+		m.itemUid = "1234";
117
+		VCard.Organizational.Member m2 = new VCard.Organizational.Member();
118
+		m2.commonName = "wind";
119
+		m2.mailto = "wind@ofchange.org";
120
+		m2.itemUid = "4321";
121
+		m2.containerUid = "alreadythere";
122
+		VCard.Organizational.Member m3 = new VCard.Organizational.Member();
123
+		m2.commonName = "ext";
124
+		m2.mailto = "ext@ofchange.org";
125
+		card.organizational.member.add(m);
126
+		card.organizational.member.add(m2);
127
+		card.organizational.member.add(m3);
128
+
129
+		List<Email> emails = new ArrayList<>();
130
+		emails.add(Email.create(email));
131
+		card.communications.emails = emails;
132
+
133
+		try {
134
+			getSanitizer().sanitize(card, Optional.of("mycontainer"));
135
+		} catch (ServerFault e) {
136
+			e.printStackTrace();
137
+			fail(e.getMessage());
138
+		}
139
+
140
+		assertEquals(3, card.organizational.member.size());
141
+
142
+		for (Member member : card.organizational.member) {
143
+			if (member.itemUid == null) {
144
+				assertNull(member.containerUid);
145
+			} else if (member.itemUid.equals("1234")) {
146
+				assertEquals("mycontainer", member.containerUid);
147
+			} else if (member.itemUid.equals("4321")) {
148
+				assertEquals("alreadythere", member.containerUid);
149
+			}
150
+		}
151
+	}
152
+
153
+	@Test
103 154
 	public void testSanitizerShouldRemoveEmptyBasicAttributes() {
104 155
 		VCard card = defaultVCard();
105 156
 		card.identification.formatedName.parameters = getEmptyAttributeTestList();
... ...
@@ -133,7 +184,7 @@ public class BasicVCardSanitizerTest {
133 184
 		card.communications.langs = Arrays.asList(lang1, lang2);
134 185
 
135 186
 		try {
136
-			getSanitizer().sanitize(card);
187
+			getSanitizer().sanitize(card, Optional.empty());
137 188
 		} catch (ServerFault e) {
138 189
 			e.printStackTrace();
139 190
 			fail(e.getMessage());
... ...
@@ -232,7 +283,7 @@ public class BasicVCardSanitizerTest {
232 283
 		card.communications.langs = Arrays.asList(lang1, lang2);
233 284
 
234 285
 		try {
235
-			getSanitizer().sanitize(card);
286
+			getSanitizer().sanitize(card, Optional.empty());
236 287
 		} catch (ServerFault e) {
237 288
 			e.printStackTrace();
238 289
 			fail(e.getMessage());
... ...
@@ -24,6 +24,7 @@ import static org.junit.Assert.fail;
24 24
 
25 25
 import java.sql.SQLException;
26 26
 import java.util.Arrays;
27
+import java.util.Optional;
27 28
 import java.util.UUID;
28 29
 
29 30
 import org.junit.Before;
... ...
@@ -117,16 +118,17 @@ public class VCardSanitizerTests extends AbstractServiceTests {
117 118
 				VCard.Organizational.Member.create(book1.uid, uid1, "fakeName", "fakemailto@fake.com"),
118 119
 				VCard.Organizational.Member.create(book2.uid, uid2, "fakeName", "fakemailto@fake.com"),
119 120
 				VCard.Organizational.Member.create(book2.uid, uid3, "fakeName", "fakemailto@fake.com"),
120
-				VCard.Organizational.Member.create("fakeUid", "fakeUid", "dontchange", "immutable@freeze.com"));
121
+				VCard.Organizational.Member.create("fakeUid", "fakeUid", "dontchange", "immutable@freeze.com"),
122
+				VCard.Organizational.Member.create(null, "fakeUid2", "fakeuid2", "fakeuid2@freeze.com"));
121 123
 
122 124
 		try {
123
-			new VCardSanitizer(testContext).sanitize(testCard);
125
+			new VCardSanitizer(testContext).sanitize(testCard, Optional.of("bookUid"));
124 126
 		} catch (ServerFault e) {
125 127
 			e.printStackTrace();
126 128
 			fail(e.getMessage());
127 129
 		}
128 130
 
129
-		assertEquals(4, testCard.organizational.member.size());
131
+		assertEquals(5, testCard.organizational.member.size());
130 132
 		VCard.Organizational.Member m = testCard.organizational.member.get(0);
131 133
 
132 134
 		// check info of user1 was updated from container item
... ...
@@ -149,6 +151,10 @@ public class VCardSanitizerTests extends AbstractServiceTests {
149 151
 		assertEquals("dontchange", m.commonName);
150 152
 		assertEquals("immutable@freeze.com", m.mailto);
151 153
 		assertNull(m.containerUid);
154
+
155
+		m = testCard.organizational.member.get(4);
156
+		assertEquals("fakeuid2", m.commonName);
157
+		assertEquals("bookUid", m.containerUid);
152 158
 	}
153 159
 
154 160
 	@Test
... ...
@@ -157,7 +163,7 @@ public class VCardSanitizerTests extends AbstractServiceTests {
157 163
 		card.identification.name = VCard.Identification.Name.create("familyNames  ", "givenNames  ",
158 164
 				" additionnalNames  ", " prefix ", "suffix ", Arrays.<VCard.Parameter>asList());
159 165
 		try {
160
-			new VCardSanitizer(testContext).sanitize(card);
166
+			new VCardSanitizer(testContext).sanitize(card, Optional.empty());
161 167
 		} catch (ServerFault e) {
162 168
 			fail(e.getMessage());
163 169
 		}
... ...
@@ -175,7 +181,7 @@ public class VCardSanitizerTests extends AbstractServiceTests {
175 181
 		VCard card = defaultVCard();
176 182
 		card.communications.emails = Arrays.asList(VCard.Communications.Email.create("  this.is.calendar@bm.lan    "));
177 183
 		try {
178
-			new VCardSanitizer(testContext).sanitize(card);
184
+			new VCardSanitizer(testContext).sanitize(card, Optional.empty());
179 185
 		} catch (ServerFault e) {
180 186
 			fail(e.getMessage());
181 187
 		}
... ...
@@ -21,6 +21,7 @@ package net.bluemind.addressbook.service.internal;
21 21
 import java.sql.SQLException;
22 22
 import java.util.ArrayList;
23 23
 import java.util.Collection;
24
+import java.util.HashMap;
24 25
 import java.util.List;
25 26
 import java.util.Map;
26 27
 import java.util.function.Consumer;
... ...
@@ -147,9 +148,14 @@ public class AddressBookService implements IInCoreAddressBook {
147 148
 		return Ack.create(version);
148 149
 	}
149 150
 
151
+	@SuppressWarnings("serial")
150 152
 	private long doCreate(String uid, Long internalId, VCard card, byte[] photo) throws ServerFault {
151 153
 		// ext point sanitizer
152
-		extSanitizer.create(card);
154
+		extSanitizer.create(card, new HashMap<String, String>() {
155
+			{
156
+				put("containerUid", container.uid);
157
+			}
158
+		});
153 159
 
154 160
 		extValidator.create(card, ImmutableMap.of("containerUid", container.uid));
155 161
 
... ...
@@ -240,6 +246,7 @@ public class AddressBookService implements IInCoreAddressBook {
240 246
 	 * @return
241 247
 	 * @throws ServerFault
242 248
 	 */
249
+	@SuppressWarnings("serial")
243 250
 	private Updated doUpdate(String givenUid, Long itemId, VCard card, byte[] photo) throws ServerFault {
244 251
 
245 252
 		ItemValue<VCard> previousItemValue = itemId == null ? storeService.get(givenUid, null)
... ...
@@ -256,8 +263,11 @@ public class AddressBookService implements IInCoreAddressBook {
256 263
 				: getDisplayName(card).equals(getDisplayName(previousItemValue.value));
257 264
 		boolean directoryValueChanged = !displayNameEquals || !emailEquals;
258 265
 
259
-		// ext point sanitizer
260
-		extSanitizer.update(previousItemValue.value, card);
266
+		extSanitizer.update(previousItemValue.value, card, new HashMap<String, String>() {
267
+			{
268
+				put("containerUid", container.uid);
269
+			}
270
+		});
261 271
 
262 272
 		extValidator.update(previousItemValue.value, card, ImmutableMap.of("containerUid", container.uid));
263 273
 
... ...
@@ -21,6 +21,8 @@ package net.bluemind.addressbook.service.internal;
21 21
 import java.util.ArrayList;
22 22
 import java.util.Collections;
23 23
 import java.util.List;
24
+import java.util.Map;
25
+import java.util.Optional;
24 26
 import java.util.stream.Collectors;
25 27
 
26 28
 import org.apache.commons.lang.StringUtils;
... ...
@@ -62,15 +64,26 @@ public class VCardSanitizer implements ISanitizer<VCard> {
62 64
 		this.tagsSanitizer = new TagsSanitizer(context);
63 65
 	}
64 66
 
65
-	public void sanitize(VCard card) throws ServerFault {
67
+	public void sanitize(VCard card, Optional<String> containerUid) throws ServerFault {
66 68
 		sanitizeDefault(card);
67 69
 		sanitizeFormattedName(card);
68 70
 		sanitizeCnAndMailto(card);
69 71
 		sanitizeParameterValues(card);
70 72
 		sanitizeEmails(card);
73
+		sanitizeMembers(card, containerUid);
71 74
 		tagsSanitizer.sanitize(card.explanatory.categories);
72 75
 	}
73 76
 
77
+	private void sanitizeMembers(VCard card, Optional<String> containerUid) {
78
+		containerUid.ifPresent(container -> {
79
+			card.organizational.member.forEach(member -> {
80
+				if (member.itemUid != null && member.containerUid == null) {
81
+					member.containerUid = container;
82
+				}
83
+			});
84
+		});
85
+	}
86
+
74 87
 	private void sanitizeEmails(VCard card) {
75 88
 		card.communications.emails = card.communications.emails.stream().filter(e -> e.value != null).map(e -> {
76 89
 			e.value = e.value.toLowerCase().trim();
... ...
@@ -251,11 +264,22 @@ public class VCardSanitizer implements ISanitizer<VCard> {
251 264
 
252 265
 	@Override
253 266
 	public void create(VCard obj) throws ServerFault {
254
-		sanitize(obj);
267
+		create(obj, Collections.emptyMap());
255 268
 	}
256 269
 
257 270
 	@Override
258 271
 	public void update(VCard current, VCard obj) throws ServerFault {
259
-		sanitize(obj);
272
+		update(current, obj, Collections.emptyMap());
273
+	}
274
+
275
+	@Override
276
+	public void create(VCard entity, Map<String, String> params) {
277
+		sanitize(entity, Optional.ofNullable(params.get("containerUid")));
260 278
 	}
279
+
280
+	@Override
281
+	public void update(VCard current, VCard entity, Map<String, String> params) {
282
+		sanitize(entity, Optional.ofNullable(params.get("containerUid")));
283
+	}
284
+
261 285
 }