1
+ /*
2
+ * Copyright 2016-2024 the original author or authors.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * https://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ package org .springframework .data .ldap .repository .query ;
17
+
18
+ import java .util .List ;
19
+
20
+ import org .junit .jupiter .api .Test ;
21
+ import org .mockito .Mockito ;
22
+
23
+ import org .springframework .data .ldap .core .mapping .LdapMappingContext ;
24
+ import org .springframework .data .ldap .repository .LdapRepository ;
25
+ import org .springframework .data .ldap .repository .Query ;
26
+ import org .springframework .data .mapping .model .EntityInstantiators ;
27
+ import org .springframework .data .projection .SpelAwareProxyProjectionFactory ;
28
+ import org .springframework .data .repository .core .support .DefaultRepositoryMetadata ;
29
+ import org .springframework .data .repository .query .Param ;
30
+ import org .springframework .data .repository .query .ValueExpressionDelegate ;
31
+ import org .springframework .ldap .core .LdapOperations ;
32
+ import org .springframework .ldap .query .LdapQuery ;
33
+
34
+ import static org .assertj .core .api .Assertions .assertThat ;
35
+
36
+ class AnnotatedLdapRepositoryQueryTests {
37
+
38
+ LdapOperations ldapOperations = Mockito .mock ();
39
+
40
+ ValueExpressionDelegate valueExpressionDelegate = ValueExpressionDelegate .create ();
41
+
42
+ @ Test
43
+ void shouldEncodeQuery () throws NoSuchMethodException {
44
+ LdapQueryMethod method = queryMethod ("namedParameters" );
45
+ AnnotatedLdapRepositoryQuery query = repositoryQuery (method );
46
+
47
+ LdapQuery ldapQuery = query .createQuery (
48
+ new LdapParametersParameterAccessor (method , new Object [] { "John)(cn=Doe)" , "foo" }));
49
+
50
+ assertThat (ldapQuery .filter ().encode ()).isEqualTo ("(cn=John\\ 29\\ 28cn=Doe\\ 29)" );
51
+ }
52
+
53
+ @ Test
54
+ void shouldEncodeBase () throws NoSuchMethodException {
55
+ LdapQueryMethod method = queryMethod ("baseNamedParameters" );
56
+ AnnotatedLdapRepositoryQuery query = repositoryQuery (method );
57
+
58
+ LdapQuery ldapQuery = query .createQuery (
59
+ new LdapParametersParameterAccessor (method , new Object [] { "foo" , "cn=John)" }));
60
+
61
+ assertThat (ldapQuery .base ()).hasToString ("cn=John\\ 29" );
62
+ }
63
+
64
+ private LdapQueryMethod queryMethod (String methodName ) throws NoSuchMethodException {
65
+ return new LdapQueryMethod (QueryRepository .class .getMethod (methodName , String .class , String .class ),
66
+ new DefaultRepositoryMetadata (QueryRepository .class ), new SpelAwareProxyProjectionFactory ());
67
+ }
68
+
69
+ private AnnotatedLdapRepositoryQuery repositoryQuery (LdapQueryMethod method ) {
70
+ return new AnnotatedLdapRepositoryQuery (method , SchemaEntry .class , ldapOperations , new LdapMappingContext (),
71
+ new EntityInstantiators (), valueExpressionDelegate );
72
+ }
73
+
74
+ interface QueryRepository extends LdapRepository <SchemaEntry > {
75
+
76
+ @ Query (value = "(cn=:fullName)" )
77
+ List <SchemaEntry > namedParameters (@ Param ("fullName" ) String fullName , @ Param ("lastName" ) String lastName );
78
+
79
+ @ Query (base = ":dc" , value = "(cn=:fullName)" )
80
+ List <SchemaEntry > baseNamedParameters (@ Param ("fullName" ) String fullName , @ Param ("dc" ) String dc );
81
+
82
+ }
83
+ }
0 commit comments