|
15 | 15 | */
|
16 | 16 | package org.springframework.data.couchbase.repository.query;
|
17 | 17 |
|
| 18 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 19 | +import static org.junit.jupiter.api.Assertions.fail; |
| 20 | +import static org.springframework.data.couchbase.config.BeanNames.COUCHBASE_TEMPLATE; |
| 21 | + |
| 22 | +import java.lang.reflect.Method; |
| 23 | +import java.util.Properties; |
| 24 | + |
18 | 25 | import org.junit.jupiter.api.BeforeEach;
|
19 | 26 | import org.junit.jupiter.api.Test;
|
20 | 27 | import org.springframework.context.ApplicationContext;
|
21 | 28 | import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
22 | 29 | import org.springframework.context.annotation.Configuration;
|
23 | 30 | import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
|
24 |
| -import org.springframework.data.couchbase.core.CouchbaseTemplate; |
25 | 31 | import org.springframework.data.couchbase.core.convert.CouchbaseConverter;
|
26 | 32 | import org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter;
|
27 | 33 | import org.springframework.data.couchbase.core.mapping.CouchbaseMappingContext;
|
|
37 | 43 | import org.springframework.data.repository.core.NamedQueries;
|
38 | 44 | import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
|
39 | 45 | import org.springframework.data.repository.core.support.PropertiesBasedNamedQueries;
|
40 |
| -import org.springframework.data.repository.query.*; |
41 |
| - |
42 |
| -import java.lang.reflect.Method; |
43 |
| -import java.util.Properties; |
44 |
| - |
45 |
| -import static org.junit.jupiter.api.Assertions.assertEquals; |
46 |
| -import static org.junit.jupiter.api.Assertions.fail; |
47 |
| -import static org.springframework.data.couchbase.config.BeanNames.COUCHBASE_TEMPLATE; |
| 46 | +import org.springframework.data.repository.query.DefaultParameters; |
| 47 | +import org.springframework.data.repository.query.ParameterAccessor; |
| 48 | +import org.springframework.data.repository.query.Parameters; |
| 49 | +import org.springframework.data.repository.query.ParametersParameterAccessor; |
| 50 | +import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider; |
| 51 | +import org.springframework.expression.spel.standard.SpelExpressionParser; |
48 | 52 |
|
49 | 53 | /**
|
50 | 54 | * @author Michael Nitschinger
|
51 | 55 | * @author Michael Reiche
|
52 | 56 | */
|
53 |
| -class StringN1qlQueryCreatorMockedTests extends ClusterAwareIntegrationTests { |
| 57 | +class StringN1qlQueryCreatorMockedTests { |
54 | 58 |
|
55 | 59 | MappingContext<? extends CouchbasePersistentEntity<?>, CouchbasePersistentProperty> context;
|
56 | 60 | CouchbaseConverter converter;
|
57 |
| - CouchbaseTemplate couchbaseTemplate; |
58 | 61 | static NamedQueries namedQueries = new PropertiesBasedNamedQueries(new Properties());
|
59 | 62 |
|
60 | 63 | @BeforeEach
|
61 | 64 | public void beforeEach() {
|
62 | 65 | context = new CouchbaseMappingContext();
|
63 | 66 | converter = new MappingCouchbaseConverter(context);
|
64 |
| - ApplicationContext ac = new AnnotationConfigApplicationContext(Config.class); |
65 |
| - couchbaseTemplate = (CouchbaseTemplate) ac.getBean(COUCHBASE_TEMPLATE); |
66 |
| - } |
67 |
| - |
68 |
| - @Test |
69 |
| - void createsQueryCorrectly() throws Exception { |
70 |
| - String input = "getByFirstnameAndLastname"; |
71 |
| - Method method = UserRepository.class.getMethod(input, String.class, String.class); |
72 |
| - |
73 |
| - CouchbaseQueryMethod queryMethod = new CouchbaseQueryMethod(method, |
74 |
| - new DefaultRepositoryMetadata(UserRepository.class), new SpelAwareProxyProjectionFactory(), |
75 |
| - converter.getMappingContext()); |
76 |
| - |
77 |
| - StringN1qlQueryCreator creator = new StringN1qlQueryCreator(getAccessor(getParameters(method), "Oliver", "Twist"), |
78 |
| - queryMethod, converter, "travel-sample", QueryMethodEvaluationContextProvider.DEFAULT, namedQueries); |
79 |
| - |
80 |
| - Query query = creator.createQuery(); |
81 |
| - assertEquals( |
82 |
| - "SELECT META(`travel-sample`).id AS __id, META(`travel-sample`).cas AS __cas, `travel-sample`.* FROM `travel-sample` where `_class` = \"org.springframework.data.couchbase.domain.User\" and firstname = $1 and lastname = $2", |
83 |
| - query.toN1qlSelectString(couchbaseTemplate.reactive(), User.class, false)); |
84 |
| - } |
85 |
| - |
86 |
| - @Test |
87 |
| - void createsQueryCorrectly2() throws Exception { |
88 |
| - String input = "getByFirstnameOrLastname"; |
89 |
| - Method method = UserRepository.class.getMethod(input, String.class, String.class); |
90 |
| - |
91 |
| - CouchbaseQueryMethod queryMethod = new CouchbaseQueryMethod(method, |
92 |
| - new DefaultRepositoryMetadata(UserRepository.class), new SpelAwareProxyProjectionFactory(), |
93 |
| - converter.getMappingContext()); |
94 |
| - |
95 |
| - StringN1qlQueryCreator creator = new StringN1qlQueryCreator(getAccessor(getParameters(method), "Oliver", "Twist"), |
96 |
| - queryMethod, converter, "travel-sample", QueryMethodEvaluationContextProvider.DEFAULT, namedQueries); |
97 |
| - |
98 |
| - Query query = creator.createQuery(); |
99 |
| - assertEquals( |
100 |
| - "SELECT META(`travel-sample`).id AS __id, META(`travel-sample`).cas AS __cas, `travel-sample`.* FROM `travel-sample` where `_class` = \"org.springframework.data.couchbase.domain.User\" and (firstname = $first or lastname = $last)", |
101 |
| - query.toN1qlSelectString(couchbaseTemplate.reactive(), User.class, false)); |
102 | 67 | }
|
103 | 68 |
|
104 | 69 | @Test
|
@@ -144,29 +109,4 @@ private ParameterAccessor getAccessor(Parameters<?, ?> params, Object... values)
|
144 | 109 | return new DefaultParameters(method);
|
145 | 110 | }
|
146 | 111 |
|
147 |
| - @Configuration |
148 |
| - @EnableCouchbaseRepositories("org.springframework.data.couchbase") |
149 |
| - static class Config extends AbstractCouchbaseConfiguration { |
150 |
| - |
151 |
| - @Override |
152 |
| - public String getConnectionString() { |
153 |
| - return connectionString(); |
154 |
| - } |
155 |
| - |
156 |
| - @Override |
157 |
| - public String getUserName() { |
158 |
| - return config().adminUsername(); |
159 |
| - } |
160 |
| - |
161 |
| - @Override |
162 |
| - public String getPassword() { |
163 |
| - return config().adminPassword(); |
164 |
| - } |
165 |
| - |
166 |
| - @Override |
167 |
| - public String getBucketName() { |
168 |
| - return bucketName(); |
169 |
| - } |
170 |
| - |
171 |
| - } |
172 | 112 | }
|
0 commit comments