Skip to content

Commit

Permalink
test: add CouchbaseTemplateIntegrationTest
Browse files Browse the repository at this point in the history
Signed-off-by: Maximillian Arruda <dearrudam@gmail.com>
  • Loading branch information
dearrudam committed May 23, 2023
1 parent b170bb5 commit d4c4dc8
Showing 1 changed file with 152 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Maximillian Arruda
*/
package org.eclipse.jnosql.databases.couchbase.integration;


import com.couchbase.client.core.env.TimeoutConfig;
import com.couchbase.client.java.json.JsonObject;
import jakarta.inject.Inject;
import org.eclipse.jnosql.databases.couchbase.communication.CouchbaseUtil;
import org.eclipse.jnosql.databases.couchbase.mapping.CouchbaseTemplate;
import org.eclipse.jnosql.mapping.Convert;
import org.eclipse.jnosql.mapping.document.DocumentEntityConverter;
import org.eclipse.jnosql.mapping.document.spi.DocumentExtension;
import org.eclipse.jnosql.mapping.reflection.EntityMetadataExtension;
import org.jboss.weld.junit5.auto.AddExtensions;
import org.jboss.weld.junit5.auto.AddPackages;
import org.jboss.weld.junit5.auto.EnableAutoWeld;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;

import java.util.Map;
import java.util.Optional;

import static java.util.UUID.randomUUID;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.SoftAssertions.assertSoftly;
import static org.awaitility.Awaitility.await;
import static org.eclipse.jnosql.communication.driver.IntegrationTest.MATCHES;
import static org.eclipse.jnosql.communication.driver.IntegrationTest.NAMED;

@EnableAutoWeld
@AddPackages(value = {Convert.class, DocumentEntityConverter.class})
@AddPackages(Book.class)
@AddPackages(CouchbaseTemplate.class)
@AddExtensions({EntityMetadataExtension.class,
DocumentExtension.class})
@EnabledIfSystemProperty(named = NAMED, matches = MATCHES)
class CouchbaseTemplateIntegrationTest {

@Inject
private CouchbaseTemplate template;

static {
CouchbaseUtil.systemPropertySetup();
}

@BeforeEach
@AfterEach
public void cleanUp(){
template.deleteAll(Book.class);
}

@Test
public void shouldInsert() {
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
template.insert(book);
Optional<Book> optional = template.find(Book.class, book.id());
assertThat(optional).isNotNull().isNotEmpty()
.get().isEqualTo(book);
}

@Test
public void shouldUpdate() throws InterruptedException {
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
assertThat(template.insert(book))
.isNotNull()
.isEqualTo(book);

Book updated = new Book(book.id(), book.title() + " updated", 2);

assertThat(template.update(updated))
.isNotNull()
.isNotEqualTo(book);

await().atLeast(TimeoutConfig.DEFAULT_KV_DURABLE_TIMEOUT);

assertThat(template.find(Book.class, book.id()))
.isNotNull().get().isEqualTo(updated);

}

@Test
public void shouldFindById() {
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
assertThat(template.insert(book))
.isNotNull()
.isEqualTo(book);

assertThat(template.find(Book.class, book.id()))
.isNotNull().get().isEqualTo(book);
}

@Test
public void shouldFindByN1qlWithParams() {
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
assertThat(template.insert(book))
.isNotNull()
.isEqualTo(book);

var data = template.n1qlQuery("select * from " + CouchbaseUtil.BUCKET_NAME + "._default.Book where title = $title",
JsonObject.from(Map.of("title", book.title()))).toList();

assertSoftly(softly -> {
softly.assertThat(data).as("query result is a non-null instance").isNotNull();
softly.assertThat(data.size()).as("query result size is correct").isEqualTo(1);
softly.assertThat(data.get(0)).as("returned data is correct").isEqualTo(book);
});
}

@Test
public void shouldFindByN1qlWithoutParams() {
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
assertThat(template.insert(book))
.isNotNull()
.isEqualTo(book);

var data = template.n1qlQuery("select * from " + CouchbaseUtil.BUCKET_NAME + "._default.Book").toList();
assertSoftly(softly -> {
softly.assertThat(data).as("query result is a non-null instance").isNotNull();
softly.assertThat(data.size()).as("query result size is correct").isEqualTo(1);
softly.assertThat(data.get(0)).as("returned data is correct").isEqualTo(book);
});
}

@Test
public void shouldDelete() {
Book book = new Book(randomUUID().toString(), "Effective Java", 1);
assertThat(template.insert(book))
.isNotNull()
.isEqualTo(book);

template.delete(Book.class, book.id());
assertThat(template.find(Book.class, book.id()))
.isNotNull().isEmpty();
}


}

0 comments on commit d4c4dc8

Please sign in to comment.