|
23 | 23 | import javax.naming.Name; |
24 | 24 | import javax.naming.NamingEnumeration; |
25 | 25 | import javax.naming.NamingException; |
| 26 | +import javax.naming.directory.Attribute; |
26 | 27 | import javax.naming.directory.BasicAttributes; |
27 | 28 | import javax.naming.directory.DirContext; |
28 | 29 | import javax.naming.directory.ModificationItem; |
|
60 | 61 | import static org.mockito.BDDMockito.never; |
61 | 62 | import static org.mockito.BDDMockito.times; |
62 | 63 | import static org.mockito.BDDMockito.verify; |
| 64 | +import static org.mockito.BDDMockito.willAnswer; |
63 | 65 | import static org.mockito.BDDMockito.willDoNothing; |
64 | 66 | import static org.mockito.BDDMockito.willThrow; |
65 | 67 |
|
@@ -1151,56 +1153,68 @@ public void testCreateWithNoIdAvailableThrows() throws NamingException { |
1151 | 1153 |
|
1152 | 1154 | @Test |
1153 | 1155 | public void testUpdateWithIdSpecified() throws NamingException { |
1154 | | - given(this.contextSourceMock.getReadOnlyContext()).willReturn(this.dirContextMock); |
1155 | | - given(this.contextSourceMock.getReadWriteContext()).willReturn(this.dirContextMock); |
| 1156 | + MockDirContext dirContext = new MockDirContext(); |
1156 | 1157 | LdapName expectedName = LdapUtils.newLdapName("ou=someOu"); |
1157 | | - |
1158 | | - ModificationItem[] expectedModificationItems = new ModificationItem[0]; |
1159 | | - DirContextOperations ctxMock = mock(DirContextOperations.class); |
1160 | | - given(ctxMock.getDn()).willReturn(expectedName); |
1161 | | - given(ctxMock.isUpdateMode()).willReturn(true); |
1162 | | - given(ctxMock.getModificationItems()).willReturn(expectedModificationItems); |
1163 | | - |
| 1158 | + dirContext.bind(expectedName, TestDirContextAdapters.forUpdate(expectedName)); |
1164 | 1159 | Object expectedObject = new Object(); |
| 1160 | + Attribute added = TestNameAwareAttributes.attribute("someName", "someValue"); |
| 1161 | + ModificationItem[] expectedModificationItems = { TestModificationItems.add(added) }; |
| 1162 | + |
| 1163 | + ArgumentCaptor<LdapDataEntry> entryCaptor = ArgumentCaptor.forClass(LdapDataEntry.class); |
| 1164 | + given(this.contextSourceMock.getReadOnlyContext()).willReturn(dirContext); |
| 1165 | + given(this.contextSourceMock.getReadWriteContext()).willReturn(dirContext); |
1165 | 1166 | given(this.odmMock.getId(expectedObject)).willReturn(expectedName); |
1166 | 1167 | given(this.odmMock.getCalculatedId(expectedObject)).willReturn(null); |
1167 | | - |
1168 | | - given(this.dirContextMock.lookup(expectedName)).willReturn(ctxMock); |
| 1168 | + given(this.odmMock.manageClass(Object.class)).willReturn(new String[0]); |
| 1169 | + willAnswer((invocation) -> { |
| 1170 | + LdapDataEntry entry = invocation.getArgument(1); |
| 1171 | + entry.addAttributeValue(added.getID(), added.get()); |
| 1172 | + return null; |
| 1173 | + }).given(this.odmMock).mapToLdapDataEntry(eq(expectedObject), entryCaptor.capture()); |
1169 | 1174 |
|
1170 | 1175 | this.tested.update(expectedObject); |
1171 | 1176 |
|
1172 | | - verify(this.odmMock, never()).setId(expectedObject, expectedName); |
1173 | | - verify(this.odmMock).mapToLdapDataEntry(expectedObject, ctxMock); |
1174 | | - verify(this.dirContextMock).modifyAttributes(expectedName, expectedModificationItems); |
1175 | | - |
1176 | | - verify(this.dirContextMock, times(2)).close(); |
| 1177 | + verify(this.odmMock, never()).setId(any(), any()); |
| 1178 | + DirContextOperations operations = (DirContextOperations) entryCaptor.getValue(); |
| 1179 | + assertEqualModificationItems(operations.getModificationItems(), expectedModificationItems); |
| 1180 | + assertThat(dirContext.isClosed()).isTrue(); |
| 1181 | + assertThat(dirContext.getAttributes(expectedName).size()).isEqualTo(2); |
1177 | 1182 | } |
1178 | 1183 |
|
1179 | 1184 | @Test |
1180 | 1185 | public void testUpdateWithIdCalculated() throws NamingException { |
1181 | | - given(this.contextSourceMock.getReadOnlyContext()).willReturn(this.dirContextMock); |
1182 | | - given(this.contextSourceMock.getReadWriteContext()).willReturn(this.dirContextMock); |
| 1186 | + MockDirContext dirContext = new MockDirContext(); |
1183 | 1187 | LdapName expectedName = LdapUtils.newLdapName("ou=someOu"); |
1184 | | - |
1185 | | - ModificationItem[] expectedModificationItems = new ModificationItem[0]; |
1186 | | - DirContextOperations ctxMock = mock(DirContextOperations.class); |
1187 | | - given(ctxMock.getDn()).willReturn(expectedName); |
1188 | | - given(ctxMock.isUpdateMode()).willReturn(true); |
1189 | | - given(ctxMock.getModificationItems()).willReturn(expectedModificationItems); |
1190 | | - |
| 1188 | + dirContext.bind(expectedName, TestDirContextAdapters.forUpdate(expectedName)); |
1191 | 1189 | Object expectedObject = new Object(); |
| 1190 | + Attribute added = TestNameAwareAttributes.attribute("someName", "someValue"); |
| 1191 | + ModificationItem[] expectedModificationItems = { TestModificationItems.add(added) }; |
| 1192 | + |
| 1193 | + ArgumentCaptor<DirContextOperations> entryCaptor = ArgumentCaptor.forClass(DirContextOperations.class); |
| 1194 | + given(this.contextSourceMock.getReadOnlyContext()).willReturn(dirContext); |
| 1195 | + given(this.contextSourceMock.getReadWriteContext()).willReturn(dirContext); |
1192 | 1196 | given(this.odmMock.getId(expectedObject)).willReturn(null); |
1193 | 1197 | given(this.odmMock.getCalculatedId(expectedObject)).willReturn(expectedName); |
1194 | | - |
1195 | | - given(this.dirContextMock.lookup(expectedName)).willReturn(ctxMock); |
| 1198 | + given(this.odmMock.manageClass(Object.class)).willReturn(new String[0]); |
| 1199 | + willAnswer((invocation) -> { |
| 1200 | + LdapDataEntry entry = invocation.getArgument(1); |
| 1201 | + entry.addAttributeValue(added.getID(), added.get()); |
| 1202 | + return null; |
| 1203 | + }).given(this.odmMock).mapToLdapDataEntry(eq(expectedObject), entryCaptor.capture()); |
1196 | 1204 |
|
1197 | 1205 | this.tested.update(expectedObject); |
1198 | 1206 |
|
1199 | 1207 | verify(this.odmMock).setId(expectedObject, expectedName); |
1200 | | - verify(this.odmMock).mapToLdapDataEntry(expectedObject, ctxMock); |
1201 | | - verify(this.dirContextMock).modifyAttributes(expectedName, expectedModificationItems); |
| 1208 | + assertEqualModificationItems(entryCaptor.getValue().getModificationItems(), expectedModificationItems); |
| 1209 | + assertThat(dirContext.isClosed()).isTrue(); |
| 1210 | + assertThat(dirContext.getAttributes(expectedName).size()).isEqualTo(2); |
| 1211 | + } |
1202 | 1212 |
|
1203 | | - verify(this.dirContextMock, times(2)).close(); |
| 1213 | + private static void assertEqualModificationItems(ModificationItem[] actual, ModificationItem[] expected) { |
| 1214 | + assertThat(actual).hasSize(expected.length); |
| 1215 | + for (int i = 0; i < actual.length; i++) { |
| 1216 | + TestModificationItems.assertEquals(actual[i], expected[i]); |
| 1217 | + } |
1204 | 1218 | } |
1205 | 1219 |
|
1206 | 1220 | @Test |
|
0 commit comments