31
31
import org .junit .jupiter .api .BeforeEach ;
32
32
import org .junit .jupiter .api .Test ;
33
33
34
- import org .springframework .ldap .core .AttributesMapper ;
35
34
import org .springframework .ldap .core .LdapTemplate ;
36
35
import org .springframework .ldap .core .support .LdapContextSource ;
37
36
import org .springframework .ldap .query .LdapQueryBuilder ;
38
37
39
38
import static org .assertj .core .api .Assertions .assertThat ;
40
39
import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
41
40
42
- public class EmbeddedLdapServerTests {
41
+ class EmbeddedLdapServerTests {
43
42
44
43
private int port ;
45
44
46
45
@ BeforeEach
47
- public void setUp () throws IOException {
46
+ void setUp () throws IOException {
48
47
this .port = getFreePort ();
49
48
}
50
49
51
50
@ Test
52
- public void shouldStartAndCloseServer () throws Exception {
51
+ void shouldStartAndCloseServer () throws Exception {
53
52
assertPortIsFree (this .port );
54
53
55
54
EmbeddedLdapServer server = EmbeddedLdapServer .newEmbeddedServer ("jayway" , "dc=jayway,dc=se" , this .port );
@@ -60,7 +59,7 @@ public void shouldStartAndCloseServer() throws Exception {
60
59
}
61
60
62
61
@ Test
63
- public void shouldStartAndAutoCloseServer () throws Exception {
62
+ void shouldStartAndAutoCloseServer () throws Exception {
64
63
assertPortIsFree (this .port );
65
64
66
65
try (EmbeddedLdapServer ignored = EmbeddedLdapServer .newEmbeddedServer ("jayway" , "dc=jayway,dc=se" ,
@@ -71,7 +70,7 @@ public void shouldStartAndAutoCloseServer() throws Exception {
71
70
}
72
71
73
72
@ Test
74
- public void shouldStartAndCloseServerViaLdapTestUtils () throws Exception {
73
+ void shouldStartAndCloseServerViaLdapTestUtils () throws Exception {
75
74
assertPortIsFree (this .port );
76
75
77
76
LdapTestUtils .startEmbeddedServer (this .port , "dc=jayway,dc=se" , "jayway" );
@@ -82,13 +81,13 @@ public void shouldStartAndCloseServerViaLdapTestUtils() throws Exception {
82
81
}
83
82
84
83
@ Test
85
- public void startWhenNewEmbeddedServerThenException () throws Exception {
84
+ void startWhenNewEmbeddedServerThenException () throws Exception {
86
85
EmbeddedLdapServer server = EmbeddedLdapServer .newEmbeddedServer ("jayway" , "dc=jayway,dc=se" , this .port );
87
86
assertThatExceptionOfType (IllegalArgumentException .class ).isThrownBy (server ::start );
88
87
}
89
88
90
89
@ Test
91
- public void startWhenUnstartedThenWorks () throws Exception {
90
+ void startWhenUnstartedThenWorks () throws Exception {
92
91
InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig ("dc=jayway,dc=se" );
93
92
config .setListenerConfigs (InMemoryListenerConfig .createLDAPConfig ("LDAP" , this .port ));
94
93
InMemoryDirectoryServer ds = new InMemoryDirectoryServer (config );
@@ -99,7 +98,7 @@ public void startWhenUnstartedThenWorks() throws Exception {
99
98
}
100
99
101
100
@ Test
102
- public void startWhenAlreadyStartedThenFails () throws Exception {
101
+ void startWhenAlreadyStartedThenFails () throws Exception {
103
102
InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig ("dc=jayway,dc=se" );
104
103
config .setListenerConfigs (InMemoryListenerConfig .createLDAPConfig ("LDAP" , this .port ));
105
104
InMemoryDirectoryServer ds = new InMemoryDirectoryServer (config );
@@ -111,13 +110,13 @@ public void startWhenAlreadyStartedThenFails() throws Exception {
111
110
}
112
111
113
112
@ Test
114
- public void shouldBuildButNotStartTheServer () {
113
+ void shouldBuildButNotStartTheServer () {
115
114
EmbeddedLdapServer .withPartitionSuffix ("dc=jayway,dc=se" ).port (this .port ).build ();
116
115
assertPortIsFree (this .port );
117
116
}
118
117
119
118
@ Test
120
- public void shouldBuildTheServerWithCustomPort () {
119
+ void shouldBuildTheServerWithCustomPort () {
121
120
EmbeddedLdapServer .Builder serverBuilder = EmbeddedLdapServer .withPartitionSuffix ("dc=jayway,dc=se" )
122
121
.port (this .port );
123
122
@@ -129,7 +128,7 @@ public void shouldBuildTheServerWithCustomPort() {
129
128
}
130
129
131
130
@ Test
132
- public void shouldBuildLdapServerAndApplyCustomConfiguration () throws IOException {
131
+ void shouldBuildLdapServerAndApplyCustomConfiguration () throws IOException {
133
132
String tempLogFile = Files .createTempFile ("ldap-log-" , ".txt" ).toAbsolutePath ().toString ();
134
133
135
134
EmbeddedLdapServer .Builder serverBuilder = EmbeddedLdapServer .withPartitionSuffix ("dc=jayway,dc=se" )
@@ -140,18 +139,18 @@ public void shouldBuildLdapServerAndApplyCustomConfiguration() throws IOExceptio
140
139
server .start ();
141
140
142
141
ldapTemplate ("dc=jayway,dc=se" , this .port )
143
- .search (LdapQueryBuilder .query ().where ("objectclass" ).is ("person" ), new AttributesMapper <>() {
144
- public String mapFromAttributes (Attributes attrs ) throws NamingException {
145
- return (String ) attrs .get ("cn" ).get ();
146
- }
147
- });
142
+ .search (LdapQueryBuilder .query ().where ("objectclass" ).is ("person" ), this ::commonNameAttribute );
148
143
}
149
144
150
145
assertThat (Path .of (tempLogFile ))
151
146
.as ("Applying the custom configuration should create a log file and populate it with the request" )
152
147
.isNotEmptyFile ();
153
148
}
154
149
150
+ private String commonNameAttribute (Attributes attrs ) throws NamingException {
151
+ return (String ) attrs .get ("cn" ).get ();
152
+ }
153
+
155
154
static void assertPortIsFree (int port ) {
156
155
assertThat (isPortOpen (port )).isFalse ();
157
156
}
0 commit comments