1616
1717import com .couchbase .client .core .error .DocumentNotFoundException ;
1818import com .couchbase .client .java .json .JsonObject ;
19+ import org .eclipse .jnosql .communication .Settings ;
1920import org .eclipse .jnosql .communication .TypeReference ;
2021import org .eclipse .jnosql .communication .document .Document ;
2122import org .eclipse .jnosql .communication .document .DocumentDeleteQuery ;
2223import org .eclipse .jnosql .communication .document .DocumentEntity ;
2324import org .eclipse .jnosql .communication .document .DocumentQuery ;
24- import org .eclipse .jnosql .communication .keyvalue .BucketManager ;
2525import org .eclipse .jnosql .communication .document .Documents ;
26+ import org .eclipse .jnosql .communication .keyvalue .BucketManager ;
2627import org .junit .jupiter .api .AfterEach ;
28+ import org .junit .jupiter .api .BeforeEach ;
2729import org .junit .jupiter .api .Test ;
2830import org .junit .jupiter .api .condition .EnabledIfSystemProperty ;
2931
3638import java .util .Set ;
3739import java .util .stream .Collectors ;
3840
39- import static org .eclipse .jnosql .communication .document .DocumentDeleteQuery .delete ;
40- import static org .eclipse .jnosql .communication .document .DocumentQuery .select ;
4141import static java .util .Arrays .asList ;
4242import static org .assertj .core .api .Assertions .assertThat ;
43- import static org .eclipse .jnosql .communication .driver .IntegrationTest .NAMED ;
43+ import static org .awaitility .Awaitility .await ;
44+ import static org .eclipse .jnosql .communication .document .DocumentDeleteQuery .delete ;
45+ import static org .eclipse .jnosql .communication .document .DocumentQuery .select ;
4446import static org .eclipse .jnosql .communication .driver .IntegrationTest .MATCHES ;
47+ import static org .eclipse .jnosql .communication .driver .IntegrationTest .NAMED ;
4548import static org .junit .jupiter .api .Assertions .assertEquals ;
46- import static org .junit .jupiter .api .Assertions .assertFalse ;
4749import static org .junit .jupiter .api .Assertions .assertNotNull ;
4850import static org .junit .jupiter .api .Assertions .assertTrue ;
4951
@@ -53,35 +55,39 @@ public class CouchbaseDocumentManagerTest {
5355
5456 public static final String COLLECTION_PERSON_NAME = "person" ;
5557 public static final String COLLECTION_APP_NAME = "AppointmentBook" ;
56- private CouchbaseDocumentManager entityManager ;
58+ private static Settings settings ;
59+ private static CouchbaseDocumentManager entityManager ;
60+ private static BucketManager keyValueEntityManagerForPerson ;
61+ private static BucketManager keyValueEntityManagerForAppointmentBook ;
5762
58- {
63+ static {
64+ settings = Database .INSTANCE .getSettings ();
5965 CouchbaseDocumentConfiguration configuration = Database .INSTANCE .getDocumentConfiguration ();
60-
61- CouchbaseDocumentManagerFactory managerFactory = configuration .apply (CouchbaseUtil .getSettings ());
66+ CouchbaseDocumentManagerFactory managerFactory = configuration .apply (settings );
6267 entityManager = managerFactory .apply (CouchbaseUtil .BUCKET_NAME );
68+
69+ CouchbaseKeyValueConfiguration keyValueConfiguration = Database .INSTANCE .getKeyValueConfiguration ();
70+ CouchbaseBucketManagerFactory keyValueEntityManagerFactory = keyValueConfiguration .apply (settings );
71+ keyValueEntityManagerForPerson = keyValueEntityManagerFactory
72+ .getBucketManager (CouchbaseUtil .BUCKET_NAME , COLLECTION_PERSON_NAME );
73+ keyValueEntityManagerForAppointmentBook = keyValueEntityManagerFactory
74+ .getBucketManager (CouchbaseUtil .BUCKET_NAME , COLLECTION_APP_NAME );
6375 }
6476
77+ @ BeforeEach
6578 @ AfterEach
66- public void afterEach () {
67- CouchbaseKeyValueConfiguration configuration = Database .INSTANCE .getKeyValueConfiguration ();
68- CouchbaseBucketManagerFactory keyValueEntityManagerFactory = configuration .apply (CouchbaseUtil .getSettings ());
69-
79+ public void cleanUpData () throws InterruptedException {
7080 try {
71- BucketManager keyValueEntityManager = keyValueEntityManagerFactory
72- .getBucketManager (CouchbaseUtil .BUCKET_NAME , COLLECTION_PERSON_NAME );
73- keyValueEntityManager .delete ("id" );
81+ keyValueEntityManagerForPerson .delete ("id" );
7482 } catch (DocumentNotFoundException exp ) {
7583 //IGNORE
7684 }
77-
7885 try {
79- BucketManager keyValueEntityManager = keyValueEntityManagerFactory
80- .getBucketManager (CouchbaseUtil .BUCKET_NAME , COLLECTION_APP_NAME );
81- keyValueEntityManager .delete ("ids" );
86+ keyValueEntityManagerForAppointmentBook .delete ("ids" );
8287 } catch (DocumentNotFoundException exp ) {
8388 //IGNORE
8489 }
90+ Thread .sleep (1_000L );
8591 }
8692
8793 @ Test
@@ -128,7 +134,6 @@ public void shouldSaveSubDocument() {
128134 entity .add (Document .of ("phones" , Document .of ("mobile" , "1231231" )));
129135 DocumentEntity entitySaved = entityManager .insert (entity );
130136 Document id = entitySaved .find ("_id" ).get ();
131-
132137 DocumentQuery query = select ().from (COLLECTION_PERSON_NAME ).where (id .name ()).eq (id .get ()).build ();
133138 DocumentEntity entityFound = entityManager .select (query ).collect (Collectors .toList ()).get (0 );
134139 Document subDocument = entityFound .find ("phones" ).get ();
@@ -217,19 +222,25 @@ private DocumentEntity createSubdocumentList() {
217222 public void shouldRunN1Ql () {
218223 DocumentEntity entity = getEntity ();
219224 entityManager .insert (entity );
220- List <DocumentEntity > entities = entityManager .n1qlQuery ("select * from jnosql._default.person" ).collect (Collectors .toList ());
221- assertFalse (entities .isEmpty ());
225+ await ().until (() ->
226+ !entityManager
227+ .n1qlQuery ("select * from `jnosql`._default.person" )
228+ .collect (Collectors .toList ()).isEmpty ()
229+ );
222230 }
223231
224232 @ Test
225233 public void shouldRunN1QlParameters () {
226234 DocumentEntity entity = getEntity ();
227235 entityManager .insert (entity );
228- JsonObject params = JsonObject .create ().put ("name" , "Poliana" );
229- List <DocumentEntity > entities = entityManager .n1qlQuery ("select * from jnosql._default.person where name = $name" ,
230- params ).collect (Collectors .toList ());
231- assertNotNull (entities );
232- assertFalse (entities .isEmpty ());
236+
237+ JsonObject params = JsonObject .create ().put ("name" , entity .find ("name" , String .class ).orElse (null ));
238+
239+ await ().until (() ->
240+ !entityManager
241+ .n1qlQuery ("select * from `jnosql`._default.person where name = $name" , params )
242+ .collect (Collectors .toList ()).isEmpty ()
243+ );
233244 }
234245
235246
0 commit comments