|
20 | 20 |
|
21 | 21 | import javax.naming.NamingException;
|
22 | 22 | import javax.naming.directory.DirContext;
|
| 23 | +import javax.naming.ldap.LdapContext; |
| 24 | +import javax.naming.ldap.StartTlsRequest; |
23 | 25 |
|
24 | 26 | import io.micrometer.observation.tck.TestObservationRegistry;
|
25 | 27 | import io.micrometer.observation.tck.TestObservationRegistryAssert;
|
26 |
| -import org.junit.Test; |
| 28 | +import org.junit.jupiter.api.Test; |
27 | 29 |
|
| 30 | +import org.springframework.ldap.core.DirContextOperations; |
| 31 | + |
| 32 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 33 | +import static org.mockito.BDDMockito.given; |
28 | 34 | import static org.mockito.Mockito.mock;
|
29 | 35 |
|
| 36 | +/** |
| 37 | + * Tests for {@link ObservationContextSource} |
| 38 | + */ |
30 | 39 | public class ObservationContextSourceTests {
|
31 | 40 |
|
32 | 41 | private final TestObservationRegistry registry = TestObservationRegistry.create();
|
33 | 42 |
|
| 43 | + private final ObservationContextSource contextSource = new ObservationContextSource(new TestContextSource(), |
| 44 | + this.registry); |
| 45 | + |
34 | 46 | @Test
|
35 |
| - public void dirContextGetAttributesWhenObservingThenObserves() throws Exception { |
36 |
| - TestContextSource observed = new TestContextSource(); |
37 |
| - ObservationContextSource observing = new ObservationContextSource(observed, this.registry); |
38 |
| - observing.getReadOnlyContext().getAttributes("ou=user,ou=people"); |
| 47 | + void dirContextGetAttributesWhenObservingThenObserves() throws Exception { |
| 48 | + this.contextSource.getReadOnlyContext().getAttributes("ou=user,ou=people"); |
39 | 49 | // @formatter:off
|
40 | 50 | TestObservationRegistryAssert.assertThat(this.registry)
|
41 | 51 | .hasObservationWithNameEqualTo("spring.ldap.dir.context.operations").that()
|
42 | 52 | .hasContextualNameEqualTo("perform get.attributes");
|
43 | 53 | // @formatter:on
|
44 | 54 | }
|
45 | 55 |
|
46 |
| - private final class TestContextSource extends AbstractContextSource { |
| 56 | + @Test |
| 57 | + void dirContextGetAttributesByNameWhenObservingThenObserves() throws Exception { |
| 58 | + this.contextSource.getReadOnlyContext().getAttributes("ou=user,ou=people", new String[] { "id" }); |
| 59 | + // @formatter:off |
| 60 | + TestObservationRegistryAssert.assertThat(this.registry) |
| 61 | + .hasObservationWithNameEqualTo("spring.ldap.dir.context.operations").that() |
| 62 | + .hasContextualNameEqualTo("perform get.attributes") |
| 63 | + .hasHighCardinalityKeyValue("attribute.ids", "[id]"); |
| 64 | + // @formatter:on |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void dirContextRenameWhenObservingThenObserves() throws Exception { |
| 69 | + this.contextSource.getReadOnlyContext().rename("ou=user,ou=people", "ou=carrot,ou=people"); |
| 70 | + // @formatter:off |
| 71 | + TestObservationRegistryAssert.assertThat(this.registry) |
| 72 | + .hasObservationWithNameEqualTo("spring.ldap.dir.context.operations").that() |
| 73 | + .hasContextualNameEqualTo("perform rename"); |
| 74 | + // @formatter:on |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void dirContextWhenDirContextOperationsThenDoesNotObserve() throws Exception { |
| 79 | + BaseLdapPathContextSource contextSource = mock(BaseLdapPathContextSource.class); |
| 80 | + ObservationContextSource observing = new ObservationContextSource(contextSource, this.registry); |
| 81 | + DirContextOperations operations = mock(DirContextOperations.class); |
| 82 | + given(contextSource.getReadOnlyContext()).willReturn(operations); |
| 83 | + observing.getReadOnlyContext().getAttributes("ou=user,ou=people"); |
| 84 | + // @formatter:off |
| 85 | + TestObservationRegistryAssert.assertThat(this.registry) |
| 86 | + .hasNumberOfObservationsEqualTo(0); |
| 87 | + // @formatter:on |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void ldapContextWhenExtendedOperationThenObserves() throws Exception { |
| 92 | + ObservationContextSource observing = new ObservationContextSource(new TestContextSource(true), this.registry); |
| 93 | + ((LdapContext) observing.getReadOnlyContext()).extendedOperation(new StartTlsRequest()); |
| 94 | + // @formatter:off |
| 95 | + TestObservationRegistryAssert.assertThat(this.registry) |
| 96 | + .hasObservationWithNameEqualTo("spring.ldap.dir.context.operations").that() |
| 97 | + .hasContextualNameEqualTo("perform extended.operation"); |
| 98 | + // @formatter:on |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void constructorWhenObservationContextSourceThenIllegalArgument() { |
| 103 | + assertThatExceptionOfType(IllegalArgumentException.class) |
| 104 | + .isThrownBy(() -> new ObservationContextSource(this.contextSource, this.registry)); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + void constructorWhenNullParametersThenIllegalArgumment() { |
| 109 | + assertThatExceptionOfType(IllegalArgumentException.class) |
| 110 | + .isThrownBy(() -> new ObservationContextSource(null, this.registry)); |
| 111 | + assertThatExceptionOfType(IllegalArgumentException.class) |
| 112 | + .isThrownBy(() -> new ObservationContextSource(new TestContextSource(), null)); |
| 113 | + } |
| 114 | + |
| 115 | + private static final class TestContextSource extends AbstractContextSource { |
| 116 | + |
| 117 | + private final boolean ldapContext; |
47 | 118 |
|
48 | 119 | TestContextSource() {
|
| 120 | + this(false); |
| 121 | + } |
| 122 | + |
| 123 | + TestContextSource(boolean ldapContext) { |
| 124 | + this.ldapContext = ldapContext; |
49 | 125 | setUrls(new String[] { "ldap://localhost:1234" });
|
50 | 126 | setBase("dc=example,dc=org");
|
51 | 127 | afterPropertiesSet();
|
52 | 128 | }
|
53 | 129 |
|
54 | 130 | @Override
|
55 | 131 | protected DirContext getDirContextInstance(Hashtable<String, Object> environment) throws NamingException {
|
56 |
| - return mock(DirContext.class); |
| 132 | + return this.ldapContext ? mock(LdapContext.class) : mock(DirContext.class); |
57 | 133 | }
|
58 | 134 |
|
59 | 135 | }
|
|
0 commit comments