Skip to content

Commit 5cd916b

Browse files
committed
DATACOUCH-577 - Fix tests that run only when using UNMANAGED couchbase server.
Some of the tests that run as UNMANAGED and pass when ran from intellij were failing when ran from the command-line mvn, or when ran together will other tests.
1 parent 3c6f245 commit 5cd916b

File tree

5 files changed

+111
-81
lines changed

5 files changed

+111
-81
lines changed

src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateKeyValueIntegrationTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ void upsertAndFindById() {
8282

8383
@Test
8484
void findDocWhichDoesNotExist() {
85-
assertThrows(DataRetrievalFailureException.class,
86-
() -> couchbaseTemplate.findById(User.class).one(UUID.randomUUID().toString()));
85+
assertNull(couchbaseTemplate.findById(User.class).one(UUID.randomUUID().toString()));
8786
}
8887

8988
@Test
@@ -113,7 +112,7 @@ void upsertAndRemoveById() {
113112
assertTrue(removeResult.getCas() != 0);
114113
assertTrue(removeResult.getMutationToken().isPresent());
115114

116-
assertThrows(DataRetrievalFailureException.class, () -> couchbaseTemplate.findById(User.class).one(user.getId()));
115+
assertNull(couchbaseTemplate.findById(User.class).one(user.getId()));
117116
}
118117

119118
@Test

src/test/java/org/springframework/data/couchbase/domain/AirportRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,6 @@ public interface AirportRepository extends PagingAndSortingRepository<Airport, S
4747

4848
long countByIcaoAndIataIn(String icao, String... iata);
4949

50+
long countByIcaoOrIataIn(String icao, String... iata);
51+
5052
}

src/test/java/org/springframework/data/couchbase/repository/CouchbaseRepositoryQueryIntegrationTests.java

Lines changed: 79 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
import static org.junit.jupiter.api.Assertions.*;
2020

21+
import java.util.ArrayList;
2122
import java.util.List;
23+
import java.util.Locale;
2224
import java.util.concurrent.Callable;
2325
import java.util.concurrent.ExecutorService;
2426
import java.util.concurrent.Executors;
@@ -68,21 +70,32 @@ void beforeEach() {
6870

6971
@Test
7072
void shouldSaveAndFindAll() {
71-
Airport vie = new Airport("airports::vie", "vie", "loww");
72-
airportRepository.save(vie);
73-
74-
List<Airport> all = StreamSupport.stream(airportRepository.findAll().spliterator(), false)
75-
.collect(Collectors.toList());
76-
77-
assertFalse(all.isEmpty());
78-
assertTrue(all.stream().anyMatch(a -> a.getId().equals("airports::vie")));
73+
Airport vie = null;
74+
try {
75+
vie = new Airport("airports::vie", "vie", "loww");
76+
airportRepository.save(vie);
77+
List<Airport> all = new ArrayList<>();
78+
airportRepository.findAll().forEach(all::add);
79+
assertFalse(all.isEmpty());
80+
assertTrue(all.stream().anyMatch(a -> a.getId().equals("airports::vie")));
81+
} finally {
82+
airportRepository.delete(vie);
83+
}
7984
}
8085

8186
@Test
8287
void findBySimpleProperty() {
83-
List<Airport> airports = airportRepository.findAllByIata("vie");
84-
// TODO
85-
System.err.println(airports);
88+
Airport vie = null;
89+
try {
90+
vie = new Airport("airports::vie", "vie", "loww");
91+
airportRepository.save(vie);
92+
sleep(1000);
93+
List<Airport> airports = airportRepository.findAllByIata("vie");
94+
assertEquals(vie.getId(), airports.get(0).getId());
95+
} finally {
96+
airportRepository.delete(vie);
97+
}
98+
8699
}
87100

88101
@Test
@@ -94,12 +107,11 @@ void count() {
94107
try {
95108
Callable<Boolean>[] suppliers = new Callable[iatas.length];
96109
for (int i = 0; i < iatas.length; i++) {
97-
Airport airport = new Airport("airports::" + iatas[i], iatas[i] /*iata*/, iatas[i].toLowerCase() /* lcao */);
110+
Airport airport = new Airport("airports::" + iatas[i], iatas[i] /*iata*/,
111+
iatas[i].toLowerCase(Locale.ROOT) /* lcao */);
98112
airportRepository.save(airport);
99113
}
100-
try {
101-
Thread.sleep(1000);
102-
} catch (InterruptedException ie) {}
114+
sleep(1000);
103115
long airportCount = 0;
104116
airportCount = airportRepository.count();
105117
assertEquals(7, airportCount);
@@ -110,6 +122,9 @@ void count() {
110122
airportCount = airportRepository.countByIcaoAndIataIn("jfk", "JFK", "IAD", "SFO", "XXX");
111123
assertEquals(1, airportCount);
112124

125+
airportCount = airportRepository.countByIcaoOrIataIn("jfk", "LAX", "IAD", "SFO");
126+
assertEquals(4, airportCount);
127+
113128
airportCount = airportRepository.countByIataIn("XXX");
114129
assertEquals(0, airportCount);
115130

@@ -128,29 +143,30 @@ void threadSafeParametersTest() throws Exception {
128143
ExecutorService executorService = Executors.newFixedThreadPool(iatas.length);
129144

130145
try {
131-
Callable<Boolean>[] suppliers = new Callable[iatas.length];
132146
for (int i = 0; i < iatas.length; i++) {
133-
Airport airport = new Airport("airports::" + iatas[i], iatas[i] /*iata*/, iatas[i].toLowerCase() /* lcao */);
147+
Airport airport = new Airport("airports::" + iatas[i], iatas[i] /*iata*/,
148+
iatas[i].toLowerCase(Locale.ROOT) /* lcao */);
134149
airportRepository.save(airport);
135-
final int idx = i;
136-
suppliers[i] = () -> {
137-
System.out.println(Thread.currentThread() + " " + iatas[idx] + " ->");
138-
try {
139-
Thread.sleep(iatas.length - idx); // so they are executed out-of-order
140-
} catch (InterruptedException ie) {
141-
;
142-
}
143-
String foundName = airportRepository.findAllByIata(iatas[idx]).get(0).getIata();
144-
System.out.println(Thread.currentThread() + " " + iatas[idx] + " <- ");
145-
assertEquals(iatas[idx], foundName);
146-
return iatas[idx].equals(foundName);
147-
};
148150
}
149-
for (int i = 0; i < iatas.length; i++) {
150-
future[i] = executorService.submit(suppliers[i]);
151-
}
152-
for (int i = 0; i < iatas.length; i++) {
153-
future[i].get();
151+
sleep(1000);
152+
for (int k = 0; k < 50; k++) {
153+
Callable<Boolean>[] suppliers = new Callable[iatas.length];
154+
for (int i = 0; i < iatas.length; i++) {
155+
final int idx = i;
156+
suppliers[i] = () -> {
157+
sleep(iatas.length - idx); // so they are executed out-of-order
158+
List<Airport> airports = airportRepository.findAllByIata(iatas[idx]);
159+
String foundName = airportRepository.findAllByIata(iatas[idx]).get(0).getIata();
160+
assertEquals(iatas[idx], foundName);
161+
return iatas[idx].equals(foundName);
162+
};
163+
}
164+
for (int i = 0; i < iatas.length; i++) {
165+
future[i] = executorService.submit(suppliers[i]);
166+
}
167+
for (int i = 0; i < iatas.length; i++) {
168+
future[i].get(); // check is done in Callable
169+
}
154170
}
155171

156172
} finally {
@@ -163,35 +179,34 @@ void threadSafeParametersTest() throws Exception {
163179
}
164180

165181
@Test
166-
void threadSafeStringParametersTest() throws Exception {
182+
void threadSafeStringParametersTest() throws Exception {
167183
String[] iatas = { "JFK", "IAD", "SFO", "SJC", "SEA", "LAX", "PHX" };
168184
Future[] future = new Future[iatas.length];
169185
ExecutorService executorService = Executors.newFixedThreadPool(iatas.length);
170186

171187
try {
172-
Callable<Boolean>[] suppliers = new Callable[iatas.length];
173188
for (int i = 0; i < iatas.length; i++) {
174-
Airport airport = new Airport("airports::" + iatas[i], iatas[i] /*iata*/, iatas[i] /* lcao */);
189+
Airport airport = new Airport("airports::" + iatas[i], iatas[i] /*iata*/, iatas[i].toLowerCase() /* lcao */);
175190
airportRepository.save(airport);
176-
final int idx = i;
177-
suppliers[i] = () -> {
178-
System.out.println(Thread.currentThread() + " " + iatas[idx] + " ->");
179-
try {
180-
Thread.sleep(iatas.length - idx); // so they are executed out-of-order
181-
} catch (InterruptedException ie) {
182-
;
183-
}
184-
String foundName = airportRepository.getAllByIata(iatas[idx]).get(0).getIata();
185-
System.out.println(Thread.currentThread() + " " + iatas[idx] + " <- ");
186-
assertEquals(iatas[idx], foundName);
187-
return iatas[idx].equals(foundName);
188-
};
189191
}
190-
for (int i = 0; i < iatas.length; i++) {
191-
future[i] = executorService.submit(suppliers[i]);
192-
}
193-
for (int i = 0; i < iatas.length; i++) {
194-
future[i].get();
192+
sleep(1000);
193+
for (int k = 0; k < 100; k++) {
194+
Callable<Boolean>[] suppliers = new Callable[iatas.length];
195+
for (int i = 0; i < iatas.length; i++) {
196+
final int idx = i;
197+
suppliers[i] = () -> {
198+
sleep(iatas.length - idx); // so they are executed out-of-order
199+
String foundName = airportRepository.getAllByIata(iatas[idx]).get(0).getIata();
200+
assertEquals(iatas[idx], foundName);
201+
return iatas[idx].equals(foundName);
202+
};
203+
}
204+
for (int i = 0; i < iatas.length; i++) {
205+
future[i] = executorService.submit(suppliers[i]);
206+
}
207+
for (int i = 0; i < iatas.length; i++) {
208+
future[i].get(); // check is done in Callable
209+
}
195210
}
196211
} finally {
197212
executorService.shutdown();
@@ -202,6 +217,14 @@ void threadSafeStringParametersTest() throws Exception {
202217
}
203218
}
204219

220+
private void sleep(int millis) {
221+
try {
222+
Thread.sleep(millis); // so they are executed out-of-order
223+
} catch (InterruptedException ie) {
224+
;
225+
}
226+
}
227+
205228
@Configuration
206229
@EnableCouchbaseRepositories("org.springframework.data.couchbase")
207230
static class Config extends AbstractCouchbaseConfiguration {

src/test/java/org/springframework/data/couchbase/repository/ReactiveCouchbaseRepositoryQueryIntegrationTests.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class ReactiveCouchbaseRepositoryQueryIntegrationTests extends ClusterAwa
5454

5555
@Autowired CouchbaseClientFactory clientFactory;
5656

57-
@Autowired ReactiveAirportRepository airportRepository;
57+
@Autowired ReactiveAirportRepository airportRepository; // intellij flags "Could not Autowire", but it runs ok.
5858

5959
@BeforeEach
6060
void beforeEach() {
@@ -67,20 +67,32 @@ void beforeEach() {
6767

6868
@Test
6969
void shouldSaveAndFindAll() {
70-
Airport vie = new Airport("airports::vie", "vie", "loww");
71-
airportRepository.save(vie).block();
70+
Airport vie = null;
71+
try {
72+
vie = new Airport("airports::vie", "vie", "loww");
73+
airportRepository.save(vie).block();
7274

73-
List<Airport> all = airportRepository.findAll().toStream().collect(Collectors.toList());
75+
List<Airport> all = airportRepository.findAll().toStream().collect(Collectors.toList());
7476

75-
assertFalse(all.isEmpty());
76-
assertTrue(all.stream().anyMatch(a -> a.getId().equals("airports::vie")));
77+
assertFalse(all.isEmpty());
78+
assertTrue(all.stream().anyMatch(a -> a.getId().equals("airports::vie")));
79+
} finally {
80+
airportRepository.delete(vie).block();
81+
}
7782
}
7883

7984
@Test
8085
void findBySimpleProperty() {
81-
List<Airport> airports = airportRepository.findAllByIata("vie").collectList().block();
82-
// TODO
83-
System.err.println(airports);
86+
Airport vie = null;
87+
try {
88+
vie = new Airport("airports::vie", "vie", "loww");
89+
airportRepository.save(vie).block();
90+
List<Airport> airports = airportRepository.findAllByIata("vie").collectList().block();
91+
// TODO
92+
System.err.println(airports);
93+
} finally {
94+
airportRepository.delete(vie).block();
95+
}
8496
}
8597

8698
@Test
@@ -114,7 +126,7 @@ void count() {
114126
} finally {
115127
for (int i = 0; i < iatas.length; i++) {
116128
Airport airport = new Airport("airports::" + iatas[i], iatas[i] /*iata*/, iatas[i] /* lcao */);
117-
airportRepository.delete(airport);
129+
airportRepository.delete(airport).block();
118130
}
119131
}
120132
}

src/test/java/org/springframework/data/couchbase/repository/query/StringN1qlQueryCreatorMockedTests.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
* @author Michael Nitschinger
5959
* @author Michael Reiche
6060
*/
61-
@IgnoreWhen(clusterTypes = ClusterType.UNMANAGED)
6261
class StringN1qlQueryCreatorMockedTests extends ClusterAwareIntegrationTests {
6362

6463
MappingContext<? extends CouchbasePersistentEntity<?>, CouchbasePersistentProperty> context;
@@ -83,9 +82,8 @@ void createsQueryCorrectly() throws Exception {
8382
new DefaultRepositoryMetadata(UserRepository.class), new SpelAwareProxyProjectionFactory(),
8483
converter.getMappingContext());
8584

86-
StringN1qlQueryCreator creator = new StringN1qlQueryCreator(
87-
getAccessor(getParameters(method), "Oliver", "Twist"), queryMethod, converter, "travel-sample",
88-
QueryMethodEvaluationContextProvider.DEFAULT, namedQueries);
85+
StringN1qlQueryCreator creator = new StringN1qlQueryCreator(getAccessor(getParameters(method), "Oliver", "Twist"),
86+
queryMethod, converter, "travel-sample", QueryMethodEvaluationContextProvider.DEFAULT, namedQueries);
8987

9088
Query query = creator.createQuery();
9189
assertEquals(
@@ -102,9 +100,8 @@ void createsQueryCorrectly2() throws Exception {
102100
new DefaultRepositoryMetadata(UserRepository.class), new SpelAwareProxyProjectionFactory(),
103101
converter.getMappingContext());
104102

105-
StringN1qlQueryCreator creator = new StringN1qlQueryCreator(
106-
getAccessor(getParameters(method), "Oliver", "Twist"), queryMethod, converter, "travel-sample",
107-
QueryMethodEvaluationContextProvider.DEFAULT, namedQueries);
103+
StringN1qlQueryCreator creator = new StringN1qlQueryCreator(getAccessor(getParameters(method), "Oliver", "Twist"),
104+
queryMethod, converter, "travel-sample", QueryMethodEvaluationContextProvider.DEFAULT, namedQueries);
108105

109106
Query query = creator.createQuery();
110107
assertEquals(
@@ -123,8 +120,7 @@ void wrongNumberArgs() throws Exception {
123120

124121
try {
125122
StringN1qlQueryCreator creator = new StringN1qlQueryCreator(getAccessor(getParameters(method), "Oliver"),
126-
queryMethod, converter, "travel-sample", QueryMethodEvaluationContextProvider.DEFAULT,
127-
namedQueries);
123+
queryMethod, converter, "travel-sample", QueryMethodEvaluationContextProvider.DEFAULT, namedQueries);
128124
} catch (IllegalArgumentException e) {
129125
return;
130126
}
@@ -141,15 +137,13 @@ void doesNotHaveAnnotation() throws Exception {
141137

142138
try {
143139
StringN1qlQueryCreator creator = new StringN1qlQueryCreator(getAccessor(getParameters(method), "Oliver"),
144-
queryMethod, converter, "travel-sample", QueryMethodEvaluationContextProvider.DEFAULT,
145-
namedQueries);
140+
queryMethod, converter, "travel-sample", QueryMethodEvaluationContextProvider.DEFAULT, namedQueries);
146141
} catch (IllegalArgumentException e) {
147142
return;
148143
}
149144
fail("should have failed with IllegalArgumentException: query has no inline Query or named Query not found");
150145
}
151146

152-
153147
private ParameterAccessor getAccessor(Parameters<?, ?> params, Object... values) {
154148
return new ParametersParameterAccessor(params, values);
155149
}

0 commit comments

Comments
 (0)