Search in sources :

Example 1 with RemoveResult

use of org.springframework.data.couchbase.core.RemoveResult in project spring-data-couchbase by spring-projects.

the class CouchbaseRepositoryQueryIntegrationTests method saveNotBounded.

@Test
public void saveNotBounded() {
    // save() followed by query with NOT_BOUNDED will result in not finding the document
    Airport vie = new Airport("airports::vie", "vie", "low9");
    Airport airport2 = null;
    for (int i = 1; i <= 100; i++) {
        // set version == 0 so save() will be an upsert, not a replace
        Airport saved = airportRepository.save(vie.clearVersion());
        try {
            airport2 = airportRepository.iata(saved.getIata());
            if (airport2 == null) {
                break;
            }
        } catch (DataRetrievalFailureException drfe) {
            // 
            airport2 = null;
        } finally {
            // airportRepository.delete(vie);
            // instead of delete, use removeResult to test QueryOptions.consistentWith()
            RemoveResult removeResult = couchbaseTemplate.removeById().one(vie.getId());
            assertEquals(vie.getId(), removeResult.getId());
            assertTrue(removeResult.getCas() != 0);
            assertTrue(removeResult.getMutationToken().isPresent());
            Airport airport3 = airportRepository.iata(vie.getIata());
            assertNull(airport3, "should have been removed");
        }
    }
    assertNull(airport2, "airport2 should have likely been null at least once");
    Airport saved = airportRepository.save(vie.clearVersion());
    couchbaseTemplate.findByQuery(Airport.class).withConsistency(REQUEST_PLUS).all();
    airport2 = airportRepository.iata(vie.getIata());
    RemoveResult removeResult = couchbaseTemplate.removeById().one(saved.getId());
    assertNotNull(airport2, "airport2 should have been found");
}
Also used : RemoveResult(org.springframework.data.couchbase.core.RemoveResult) Airport(org.springframework.data.couchbase.domain.Airport) DataRetrievalFailureException(org.springframework.dao.DataRetrievalFailureException) AirportRepositoryScanConsistencyTest(org.springframework.data.couchbase.domain.AirportRepositoryScanConsistencyTest) Test(org.junit.jupiter.api.Test)

Example 2 with RemoveResult

use of org.springframework.data.couchbase.core.RemoveResult in project spring-data-couchbase by spring-projects.

the class CouchbaseRepositoryQueryIntegrationTests method saveNotBoundedRequestPlus.

@Test
public void saveNotBoundedRequestPlus() {
    airportRepository.withOptions(QueryOptions.queryOptions().scanConsistency(REQUEST_PLUS)).deleteAll();
    ApplicationContext ac = new AnnotationConfigApplicationContext(ConfigRequestPlus.class);
    // the Config class has been modified, these need to be loaded again
    CouchbaseTemplate couchbaseTemplateRP = (CouchbaseTemplate) ac.getBean(COUCHBASE_TEMPLATE);
    AirportRepository airportRepositoryRP = (AirportRepository) ac.getBean("airportRepository");
    // save() followed by query with NOT_BOUNDED will result in not finding the document
    Airport vie = new Airport("airports::vie", "vie", "low9");
    Airport airport2 = null;
    for (int i = 1; i <= 100; i++) {
        // set version == 0 so save() will be an upsert, not a replace
        Airport saved = airportRepositoryRP.save(vie.clearVersion());
        try {
            airport2 = airportRepositoryRP.iata(saved.getIata());
            if (airport2 == null) {
                break;
            }
        } catch (DataRetrievalFailureException drfe) {
            // 
            airport2 = null;
        } finally {
            // airportRepository.delete(vie);
            // instead of delete, use removeResult to test QueryOptions.consistentWith()
            RemoveResult removeResult = couchbaseTemplateRP.removeById().one(vie.getId());
            assertEquals(vie.getId(), removeResult.getId());
            assertTrue(removeResult.getCas() != 0);
            assertTrue(removeResult.getMutationToken().isPresent());
            Airport airport3 = airportRepositoryRP.iata(vie.getIata());
            assertNull(airport3, "should have been removed");
        }
    }
    assertNotNull(airport2, "airport2 should have never been null");
    Airport saved = airportRepositoryRP.save(vie.clearVersion());
    List<Airport> airports = couchbaseTemplateRP.findByQuery(Airport.class).withConsistency(NOT_BOUNDED).all();
    RemoveResult removeResult = couchbaseTemplateRP.removeById().one(saved.getId());
    assertFalse(!airports.isEmpty(), "airports should have been empty");
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) RemoveResult(org.springframework.data.couchbase.core.RemoveResult) Airport(org.springframework.data.couchbase.domain.Airport) DataRetrievalFailureException(org.springframework.dao.DataRetrievalFailureException) AirportRepository(org.springframework.data.couchbase.domain.AirportRepository) CouchbaseTemplate(org.springframework.data.couchbase.core.CouchbaseTemplate) AirportRepositoryScanConsistencyTest(org.springframework.data.couchbase.domain.AirportRepositoryScanConsistencyTest) Test(org.junit.jupiter.api.Test)

Example 3 with RemoveResult

use of org.springframework.data.couchbase.core.RemoveResult in project spring-data-couchbase by spring-projects.

the class ReactiveCouchbaseTemplateQueryCollectionIntegrationTests method removeByQueryAll.

@Test
void removeByQueryAll() {
    User user1 = new User(UUID.randomUUID().toString(), "user1", "user1");
    User user2 = new User(UUID.randomUUID().toString(), "user2", "user2");
    couchbaseTemplate.upsertById(User.class).inScope(scopeName).inCollection(collectionName).all(Arrays.asList(user1, user2));
    assertTrue(couchbaseTemplate.existsById().inScope(scopeName).inCollection(collectionName).one(user1.getId()));
    assertTrue(couchbaseTemplate.existsById().inScope(scopeName).inCollection(collectionName).one(user2.getId()));
    List<RemoveResult> result = couchbaseTemplate.removeByQuery(User.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).inCollection(collectionName).all();
    assertEquals(2, result.size(), "should have deleted user1 and user2");
    assertNull(couchbaseTemplate.findById(User.class).inScope(scopeName).inCollection(collectionName).one(user1.getId()));
    assertNull(couchbaseTemplate.findById(User.class).inScope(scopeName).inCollection(collectionName).one(user2.getId()));
}
Also used : User(org.springframework.data.couchbase.domain.User) RemoveResult(org.springframework.data.couchbase.core.RemoveResult) Test(org.junit.jupiter.api.Test)

Example 4 with RemoveResult

use of org.springframework.data.couchbase.core.RemoveResult in project spring-data-couchbase by spring-projects.

the class ReactiveCouchbaseTemplateQueryCollectionIntegrationTests method removeByQuery.

@Test
public void removeByQuery() {
    // 8
    QueryOptions options = QueryOptions.queryOptions().timeout(Duration.ofSeconds(10));
    Airport saved = template.insertById(Airport.class).inScope(scopeName).inCollection(collectionName).one(vie.withIcao("lowe")).block();
    List<RemoveResult> removeResults = template.removeByQuery(Airport.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).inScope(scopeName).inCollection(collectionName).withOptions(options).matching(Query.query(QueryCriteria.where("iata").is(vie.getIata()))).all().collectList().block();
    assertEquals(saved.getId(), removeResults.get(0).getId());
}
Also used : RemoveResult(org.springframework.data.couchbase.core.RemoveResult) Airport(org.springframework.data.couchbase.domain.Airport) QueryOptions(com.couchbase.client.java.query.QueryOptions) Test(org.junit.jupiter.api.Test)

Example 5 with RemoveResult

use of org.springframework.data.couchbase.core.RemoveResult in project spring-data-couchbase by spring-projects.

the class ReactiveCouchbaseTemplateQueryCollectionIntegrationTests method removeByQueryOther.

@Test
public void removeByQueryOther() {
    // 8
    QueryOptions options = QueryOptions.queryOptions().timeout(Duration.ofSeconds(10));
    Airport saved = template.insertById(Airport.class).inScope(otherScope).inCollection(otherCollection).one(vie.withIcao("lown")).block();
    List<RemoveResult> removeResults = template.removeByQuery(Airport.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).inScope(otherScope).inCollection(otherCollection).withOptions(options).matching(Query.query(QueryCriteria.where("iata").is(vie.getIata()))).all().collectList().block();
    assertEquals(saved.getId(), removeResults.get(0).getId());
}
Also used : RemoveResult(org.springframework.data.couchbase.core.RemoveResult) Airport(org.springframework.data.couchbase.domain.Airport) QueryOptions(com.couchbase.client.java.query.QueryOptions) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)8 RemoveResult (org.springframework.data.couchbase.core.RemoveResult)8 Airport (org.springframework.data.couchbase.domain.Airport)6 QueryOptions (com.couchbase.client.java.query.QueryOptions)3 RemoveOptions (com.couchbase.client.java.kv.RemoveOptions)2 ApplicationContext (org.springframework.context.ApplicationContext)2 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)2 DataRetrievalFailureException (org.springframework.dao.DataRetrievalFailureException)2 CouchbaseTemplate (org.springframework.data.couchbase.core.CouchbaseTemplate)2 AirportRepositoryScanConsistencyTest (org.springframework.data.couchbase.domain.AirportRepositoryScanConsistencyTest)2 Collection (com.couchbase.client.java.Collection)1 ReactiveCollection (com.couchbase.client.java.ReactiveCollection)1 JsonObject (com.couchbase.client.java.json.JsonObject)1 GetResult (com.couchbase.client.java.kv.GetResult)1 QueryProfile (com.couchbase.client.java.query.QueryProfile)1 QueryResult (com.couchbase.client.java.query.QueryResult)1 QueryScanConsistency (com.couchbase.client.java.query.QueryScanConsistency)1 Arrays (java.util.Arrays)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1