I have tried to build a set of CRUD REST APIs using Spring Boot. I have created the project via Spring Initializr with the dependency spring-boot-starter-data-couchbase.
I have uploaded the project here https://github.com/RosarioB/spring-boot-rest-api-couchbase-crud/tree/basic_crud The test class is called CustomerRepositoryTest.
I have implemented some Repository methods using the CouchbaseTemplate. For example:
@Override
public List<Customer> findAll() {
List<JsonObject> jsonObjects = couchbaseTemplate.getCouchbaseClientFactory().getScope()
.query(String.format("SELECT * FROM %1$s ", keySpace)).rowsAsObject();
return jsonObjects.stream().map(this::mapJsonToCustomer).collect(Collectors.toList());
}
And i have also implemented a test for the above method (with Testcontainers) :
@Test
public void testFindAll() {
Customer alex = new Customer("customer1", "Alex", "Stone");
Customer jack = new Customer("customer2", "Jack", "Sparrow");
List<Customer> customerList = List.of(alex, jack);
Transactions transactions = couchbaseTemplate.getCouchbaseClientFactory().getCluster().transactions();
customerList.forEach(customer ->
transactions.run(ctx -> ctx.insert(collection, customer.getId(), customer)
)
);
List<Customer> customers = customerRepository.findAll();
Assertions.assertEquals(customerList, customers);
}
My problem is that even if I perform an insert with 2 items in the database with collection.insert, when I try to recover the items with customerRepository.findAll some times the test fails because it finds just one item of the two.
If I run the test in debug it works. I thought there were some synchronization issue and I've tried to synchronize the methods but I have not solved the problem.
What am I doing wrong?