Search in sources :

Example 1 with Airport

use of org.springframework.data.couchbase.domain.Airport in project spring-data-couchbase by spring-projects.

the class CouchbaseTemplateQueryIntegrationTests method distinctReactive.

@Test
void distinctReactive() {
    String[] iatas = { "JFK", "IAD", "SFO", "SJC", "SEA", "LAX", "PHX" };
    String[] icaos = { "ic0", "ic1", "ic0", "ic1", "ic0", "ic1", "ic0" };
    try {
        for (int i = 0; i < iatas.length; i++) {
            Airport airport = new Airport("airports::" + iatas[i], iatas[i], /*iata*/
            icaos[i]);
            reactiveCouchbaseTemplate.insertById(Airport.class).one(airport).block();
        }
        // distinct and count(distinct(...)) calls. use as() and consistentWith to verify fluent api
        // as the fluent api for Distinct is tricky
        // distinct icao
        List<Airport> airports1 = reactiveCouchbaseTemplate.findByQuery(Airport.class).distinct(new String[] { "icao" }).as(Airport.class).withConsistency(REQUEST_PLUS).all().collectList().block();
        assertEquals(2, airports1.size());
        // distinct all-fields-in-Airport.class
        List<Airport> airports2 = reactiveCouchbaseTemplate.findByQuery(Airport.class).distinct(new String[] {}).as(Airport.class).withConsistency(REQUEST_PLUS).all().collectList().block();
        assertEquals(7, airports2.size());
        // count( distinct icao )
        Long count1 = reactiveCouchbaseTemplate.findByQuery(Airport.class).distinct(new String[] { "icao" }).as(Airport.class).withConsistency(REQUEST_PLUS).count().block();
        assertEquals(2, count1);
        // count( distinct { icao, iata } )
        Long count2 = reactiveCouchbaseTemplate.findByQuery(Airport.class).distinct(new String[] { "icao", "iata" }).withConsistency(REQUEST_PLUS).count().block();
        assertEquals(7, count2);
    } finally {
        reactiveCouchbaseTemplate.removeById().all(Arrays.stream(iatas).map((iata) -> "airports::" + iata).collect(Collectors.toSet())).collectList().block();
    }
}
Also used : Airport(org.springframework.data.couchbase.domain.Airport) Test(org.junit.jupiter.api.Test)

Example 2 with Airport

use of org.springframework.data.couchbase.domain.Airport in project spring-data-couchbase by spring-projects.

the class CouchbaseTemplateQueryIntegrationTests method sortedTemplate.

@Test
void sortedTemplate() {
    couchbaseTemplate.removeByQuery(Airport.class).withConsistency(REQUEST_PLUS).all();
    String[] iatas = { "JFK", "IAD", "SFO", "SJC", "SEA", "LAX", "PHX" };
    try {
        couchbaseTemplate.insertById(Airport.class).all(Arrays.stream(iatas).map((iata) -> new Airport("airports::" + iata, iata, iata.toLowerCase(Locale.ROOT))).collect(Collectors.toSet()));
        org.springframework.data.couchbase.core.query.Query query = org.springframework.data.couchbase.core.query.Query.query(QueryCriteria.where("iata").isNotNull());
        Pageable pageableWithSort = PageRequest.of(0, 7, Sort.by("iata"));
        query.with(pageableWithSort);
        List<Airport> airports = couchbaseTemplate.findByQuery(Airport.class).withConsistency(REQUEST_PLUS).matching(query).all();
        String[] sortedIatas = iatas.clone();
        System.out.println("" + iatas.length + " " + sortedIatas.length);
        Arrays.sort(sortedIatas);
        for (int i = 0; i < pageableWithSort.getPageSize(); i++) {
            System.out.println(airports.get(i).getIata());
            assertEquals(sortedIatas[i], airports.get(i).getIata());
        }
    } finally {
        couchbaseTemplate.removeById(Airport.class).all(Arrays.stream(iatas).map((iata) -> "airports::" + iata).collect(Collectors.toSet()));
    }
}
Also used : Pageable(org.springframework.data.domain.Pageable) Airport(org.springframework.data.couchbase.domain.Airport) Query(org.springframework.data.couchbase.core.query.Query) Test(org.junit.jupiter.api.Test)

Example 3 with Airport

use of org.springframework.data.couchbase.domain.Airport in project spring-data-couchbase by spring-projects.

the class CouchbaseTemplateQueryCollectionIntegrationTests method findByAnalytics.

@Test
// needs analytics data set
@Disabled
public void findByAnalytics() {
    // 2
    AnalyticsOptions options = AnalyticsOptions.analyticsOptions().timeout(Duration.ofSeconds(10));
    Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(scopeName).inCollection(collectionName).one(vie.withIcao("413"));
    try {
        List<Airport> found = couchbaseTemplate.findByAnalytics(Airport.class).inScope(scopeName).inCollection(collectionName).withOptions(options).all();
        assertEquals(saved, found);
    } finally {
        couchbaseTemplate.removeById().inScope(scopeName).inCollection(collectionName).one(saved.getId());
    }
}
Also used : Airport(org.springframework.data.couchbase.domain.Airport) AnalyticsOptions(com.couchbase.client.java.analytics.AnalyticsOptions) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 4 with Airport

use of org.springframework.data.couchbase.domain.Airport in project spring-data-couchbase by spring-projects.

the class CouchbaseTemplateQueryCollectionIntegrationTests method distinctReactive.

@Test
void distinctReactive() {
    String[] iatas = { "JFK", "IAD", "SFO", "SJC", "SEA", "LAX", "PHX" };
    String[] icaos = { "ic0", "ic1", "ic0", "ic1", "ic0", "ic1", "ic0" };
    try {
        for (int i = 0; i < iatas.length; i++) {
            Airport airport = new Airport("airports::" + iatas[i], iatas[i], /*iata*/
            icaos[i]);
            reactiveCouchbaseTemplate.insertById(Airport.class).inCollection(collectionName).one(airport).block();
        }
        // distinct and count(distinct(...)) calls. use as() and consistentWith to verify fluent api
        // as the fluent api for Distinct is tricky
        // distinct icao
        List<Airport> airports1 = reactiveCouchbaseTemplate.findByQuery(Airport.class).distinct(new String[] { "icao" }).as(Airport.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).inCollection(collectionName).all().collectList().block();
        assertEquals(2, airports1.size());
        // distinct all-fields-in-Airport.class
        List<Airport> airports2 = reactiveCouchbaseTemplate.findByQuery(Airport.class).distinct(new String[] {}).as(Airport.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).inCollection(collectionName).all().collectList().block();
        assertEquals(7, airports2.size());
        // count( distinct icao )
        Long count1 = reactiveCouchbaseTemplate.findByQuery(Airport.class).distinct(new String[] { "icao" }).as(Airport.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).inCollection(collectionName).count().block();
        assertEquals(2, count1);
        // count (distinct { iata, icao } )
        Long count2 = reactiveCouchbaseTemplate.findByQuery(Airport.class).distinct(new String[] { "iata", "icao" }).withConsistency(QueryScanConsistency.REQUEST_PLUS).inCollection(collectionName).count().block();
        assertEquals(7, count2);
    } finally {
        reactiveCouchbaseTemplate.removeById().inCollection(collectionName).all(Arrays.stream(iatas).map((iata) -> "airports::" + iata).collect(Collectors.toSet())).collectList().block();
    }
}
Also used : Airport(org.springframework.data.couchbase.domain.Airport) Test(org.junit.jupiter.api.Test)

Example 5 with Airport

use of org.springframework.data.couchbase.domain.Airport in project spring-data-couchbase by spring-projects.

the class CouchbaseTemplateQueryCollectionIntegrationTests method findFromReplicasByIdOptions.

@Test
public void findFromReplicasByIdOptions() {
    // 5
    GetAnyReplicaOptions options = GetAnyReplicaOptions.getAnyReplicaOptions().timeout(Duration.ofNanos(1000));
    Airport saved = couchbaseTemplate.insertById(Airport.class).inScope(otherScope).inCollection(otherCollection).one(vie.withIcao("723"));
    try {
        Airport found = couchbaseTemplate.findFromReplicasById(Airport.class).inScope(otherScope).inCollection(otherCollection).withOptions(options).any(saved.getId());
        assertNull(found, "should not have found document in short timeout");
    } finally {
        couchbaseTemplate.removeById().inScope(otherScope).inCollection(otherCollection).one(saved.getId());
    }
}
Also used : Airport(org.springframework.data.couchbase.domain.Airport) GetAnyReplicaOptions(com.couchbase.client.java.kv.GetAnyReplicaOptions) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)93 Airport (org.springframework.data.couchbase.domain.Airport)93 AirportRepositoryScanConsistencyTest (org.springframework.data.couchbase.domain.AirportRepositoryScanConsistencyTest)27 GetOptions (com.couchbase.client.java.kv.GetOptions)20 DataRetrievalFailureException (org.springframework.dao.DataRetrievalFailureException)11 InsertOptions (com.couchbase.client.java.kv.InsertOptions)9 QueryOptions (com.couchbase.client.java.query.QueryOptions)9 IndexFailureException (com.couchbase.client.core.error.IndexFailureException)7 RemoveResult (org.springframework.data.couchbase.core.RemoveResult)7 IgnoreWhen (org.springframework.data.couchbase.util.IgnoreWhen)7 GetAnyReplicaOptions (com.couchbase.client.java.kv.GetAnyReplicaOptions)6 RemoveOptions (com.couchbase.client.java.kv.RemoveOptions)6 JsonArray (com.couchbase.client.java.json.JsonArray)5 Callable (java.util.concurrent.Callable)5 ExecutorService (java.util.concurrent.ExecutorService)5 Future (java.util.concurrent.Future)5 AirportRepository (org.springframework.data.couchbase.domain.AirportRepository)5 ReactiveAirportRepository (org.springframework.data.couchbase.domain.ReactiveAirportRepository)5 ExistsOptions (com.couchbase.client.java.kv.ExistsOptions)4 ReplaceOptions (com.couchbase.client.java.kv.ReplaceOptions)4