Skip to content

Commit 5dbf182

Browse files
committed
Incorporate changes from 5.0.x and bump Couchbase SDK. (#1287)
Closes #1286. Co-authored-by: mikereiche <[email protected]>
1 parent ef22b3d commit 5dbf182

File tree

3 files changed

+187
-3
lines changed

3 files changed

+187
-3
lines changed

src/main/java/org/springframework/data/couchbase/core/CouchbaseTemplateSupport.java

+6
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ public <T> T decodeEntity(String id, String source, Long cas, Class<T> entityCla
150150
N1qlJoinResolver.handleProperties(persistentEntity, accessor, template.reactive(), id, scope, collection);
151151
return accessor.getBean();
152152
}
153+
CouchbasePersistentEntity couldBePersistentEntity(Class<?> entityClass) {
154+
if (ClassUtils.isPrimitiveOrWrapper(entityClass) || entityClass == String.class) {
155+
return null;
156+
}
157+
return mappingContext.getPersistentEntity(entityClass);
158+
}
153159

154160
CouchbasePersistentEntity couldBePersistentEntity(Class<?> entityClass) {
155161
if (ClassUtils.isPrimitiveOrWrapper(entityClass) || entityClass == String.class) {

src/main/java/org/springframework/data/couchbase/core/query/QueryCriteria.java

-3
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
import java.util.Locale;
2626

2727
import org.springframework.data.couchbase.core.convert.CouchbaseConverter;
28-
import org.springframework.data.couchbase.core.mapping.CouchbaseDocument;
29-
import org.springframework.data.couchbase.core.mapping.CouchbaseList;
3028
import org.springframework.lang.Nullable;
3129
import org.springframework.util.CollectionUtils;
3230

@@ -35,7 +33,6 @@
3533
import com.couchbase.client.java.json.JsonArray;
3634
import com.couchbase.client.java.json.JsonObject;
3735
import com.couchbase.client.java.json.JsonValue;
38-
import org.springframework.util.CollectionUtils;
3936

4037
/**
4138
* @author Michael Nitschinger
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/*
2+
* Copyright 2017-2022 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.couchbase.repository.query;
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+
25+
import org.junit.jupiter.api.BeforeEach;
26+
import org.junit.jupiter.api.Test;
27+
import org.springframework.context.ApplicationContext;
28+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
29+
import org.springframework.context.annotation.Configuration;
30+
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
31+
import org.springframework.data.couchbase.core.CouchbaseTemplate;
32+
import org.springframework.data.couchbase.core.convert.CouchbaseConverter;
33+
import org.springframework.data.couchbase.core.convert.MappingCouchbaseConverter;
34+
import org.springframework.data.couchbase.core.mapping.CouchbaseMappingContext;
35+
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentEntity;
36+
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentProperty;
37+
import org.springframework.data.couchbase.core.query.Query;
38+
import org.springframework.data.couchbase.domain.User;
39+
import org.springframework.data.couchbase.domain.UserRepository;
40+
import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories;
41+
import org.springframework.data.couchbase.util.ClusterAwareIntegrationTests;
42+
import org.springframework.data.mapping.context.MappingContext;
43+
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
44+
import org.springframework.data.repository.core.NamedQueries;
45+
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
46+
import org.springframework.data.repository.core.support.PropertiesBasedNamedQueries;
47+
import org.springframework.data.repository.query.DefaultParameters;
48+
import org.springframework.data.repository.query.ParameterAccessor;
49+
import org.springframework.data.repository.query.Parameters;
50+
import org.springframework.data.repository.query.ParametersParameterAccessor;
51+
import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider;
52+
import org.springframework.expression.spel.standard.SpelExpressionParser;
53+
54+
/**
55+
* @author Michael Nitschinger
56+
* @author Michael Reiche
57+
*/
58+
class StringN1qlQueryCreatorMockedTests extends ClusterAwareIntegrationTests {
59+
60+
MappingContext<? extends CouchbasePersistentEntity<?>, CouchbasePersistentProperty> context;
61+
CouchbaseConverter converter;
62+
CouchbaseTemplate couchbaseTemplate;
63+
static NamedQueries namedQueries = new PropertiesBasedNamedQueries(new Properties());
64+
65+
@BeforeEach
66+
public void beforeEach() {
67+
context = new CouchbaseMappingContext();
68+
converter = new MappingCouchbaseConverter(context);
69+
ApplicationContext ac = new AnnotationConfigApplicationContext(Config.class);
70+
couchbaseTemplate = (CouchbaseTemplate) ac.getBean(COUCHBASE_TEMPLATE);
71+
}
72+
73+
@Test
74+
void createsQueryCorrectly() throws Exception {
75+
String input = "getByFirstnameAndLastname";
76+
Method method = UserRepository.class.getMethod(input, String.class, String.class);
77+
78+
CouchbaseQueryMethod queryMethod = new CouchbaseQueryMethod(method,
79+
new DefaultRepositoryMetadata(UserRepository.class), new SpelAwareProxyProjectionFactory(),
80+
converter.getMappingContext());
81+
82+
StringN1qlQueryCreator creator = new StringN1qlQueryCreator(getAccessor(getParameters(method), "Oliver", "Twist"),
83+
queryMethod, converter, "travel-sample", new SpelExpressionParser(),
84+
QueryMethodEvaluationContextProvider.DEFAULT, namedQueries);
85+
86+
Query query = creator.createQuery();
87+
assertEquals(
88+
"SELECT META(`travel-sample`).id AS __id, META(`travel-sample`).cas AS __cas, `firstname`, `lastname`, `createdBy`, `createdDate`, `lastModifiedBy`, `lastModifiedDate` FROM `travel-sample` where `_class` = \"org.springframework.data.couchbase.domain.User\" and firstname = $1 and lastname = $2",
89+
query.toN1qlSelectString(couchbaseTemplate.reactive(), User.class, false));
90+
}
91+
92+
@Test
93+
void createsQueryCorrectly2() throws Exception {
94+
String input = "getByFirstnameOrLastname";
95+
Method method = UserRepository.class.getMethod(input, String.class, String.class);
96+
97+
CouchbaseQueryMethod queryMethod = new CouchbaseQueryMethod(method,
98+
new DefaultRepositoryMetadata(UserRepository.class), new SpelAwareProxyProjectionFactory(),
99+
converter.getMappingContext());
100+
101+
StringN1qlQueryCreator creator = new StringN1qlQueryCreator(getAccessor(getParameters(method), "Oliver", "Twist"),
102+
queryMethod, converter, "travel-sample", new SpelExpressionParser(),
103+
QueryMethodEvaluationContextProvider.DEFAULT, namedQueries);
104+
105+
Query query = creator.createQuery();
106+
assertEquals(
107+
"SELECT META(`travel-sample`).id AS __id, META(`travel-sample`).cas AS __cas, `firstname`, `lastname`, `createdBy`, `createdDate`, `lastModifiedBy`, `lastModifiedDate` FROM `travel-sample` where `_class` = \"org.springframework.data.couchbase.domain.User\" and (firstname = $first or lastname = $last)",
108+
query.toN1qlSelectString(couchbaseTemplate.reactive(), User.class, false));
109+
}
110+
111+
@Test
112+
void wrongNumberArgs() throws Exception {
113+
String input = "getByFirstnameOrLastname";
114+
Method method = UserRepository.class.getMethod(input, String.class, String.class);
115+
116+
CouchbaseQueryMethod queryMethod = new CouchbaseQueryMethod(method,
117+
new DefaultRepositoryMetadata(UserRepository.class), new SpelAwareProxyProjectionFactory(),
118+
converter.getMappingContext());
119+
120+
try {
121+
StringN1qlQueryCreator creator = new StringN1qlQueryCreator(getAccessor(getParameters(method), "Oliver"),
122+
queryMethod, converter, "travel-sample", new SpelExpressionParser(),
123+
QueryMethodEvaluationContextProvider.DEFAULT, namedQueries);
124+
} catch (IllegalArgumentException e) {
125+
return;
126+
}
127+
fail("should have failed with IllegalArgumentException: Invalid number of parameters given!");
128+
}
129+
130+
@Test
131+
void doesNotHaveAnnotation() throws Exception {
132+
String input = "findByFirstname";
133+
Method method = UserRepository.class.getMethod(input, String.class);
134+
CouchbaseQueryMethod queryMethod = new CouchbaseQueryMethod(method,
135+
new DefaultRepositoryMetadata(UserRepository.class), new SpelAwareProxyProjectionFactory(),
136+
converter.getMappingContext());
137+
138+
try {
139+
StringN1qlQueryCreator creator = new StringN1qlQueryCreator(getAccessor(getParameters(method), "Oliver"),
140+
queryMethod, converter, "travel-sample", new SpelExpressionParser(),
141+
QueryMethodEvaluationContextProvider.DEFAULT, namedQueries);
142+
} catch (IllegalArgumentException e) {
143+
return;
144+
}
145+
fail("should have failed with IllegalArgumentException: query has no inline Query or named Query not found");
146+
}
147+
148+
private ParameterAccessor getAccessor(Parameters<?, ?> params, Object... values) {
149+
return new ParametersParameterAccessor(params, values);
150+
}
151+
152+
private Parameters<?, ?> getParameters(Method method) {
153+
return new DefaultParameters(method);
154+
}
155+
156+
@Configuration
157+
@EnableCouchbaseRepositories("org.springframework.data.couchbase")
158+
static class Config extends AbstractCouchbaseConfiguration {
159+
160+
@Override
161+
public String getConnectionString() {
162+
return connectionString();
163+
}
164+
165+
@Override
166+
public String getUserName() {
167+
return config().adminUsername();
168+
}
169+
170+
@Override
171+
public String getPassword() {
172+
return config().adminPassword();
173+
}
174+
175+
@Override
176+
public String getBucketName() {
177+
return bucketName();
178+
}
179+
180+
}
181+
}

0 commit comments

Comments
 (0)