mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-28 07:26:56 +00:00
#304 - ElasticSearch integration part 1 / doc store integration
This commit is contained in:
1 parent
cb1fe297d6
commit
8d3b4a0407
209 files changed
+6736
-573
No files matched your search
@@ -9,10 +9,11 @@ import com.avaje.ebeaninternal.api.SpiEbeanServer;
|
||||
import com.avaje.tests.model.basic.EBasic;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
*/
|
||||
public class BeanDescriptorTest {
|
||||
|
||||
@Test
|
||||
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
package com.avaje.ebeaninternal.server.deploy;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebeanservice.docstore.api.mapping.DocPropertyAdapter;
|
||||
import com.avaje.ebeanservice.docstore.api.mapping.DocPropertyMapping;
|
||||
import com.avaje.ebeanservice.docstore.api.mapping.DocumentMapping;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class BeanDescriptor_documentMappingTest extends BaseTestCase {
|
||||
|
||||
|
||||
@Test
|
||||
public void docMapping() {
|
||||
|
||||
BeanDescriptor<Order> desc = getBeanDescriptor(Order.class);
|
||||
|
||||
DocumentMapping documentMapping = desc.getDocMapping();
|
||||
|
||||
DocPropertyMapping properties = documentMapping.getProperties();
|
||||
|
||||
assertThat(properties).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void docMapping_visitor() {
|
||||
|
||||
BeanDescriptor<Order> desc = getBeanDescriptor(Order.class);
|
||||
|
||||
DocumentMapping documentMapping = desc.getDocMapping();
|
||||
|
||||
DocPropertyMapping properties = documentMapping.getProperties();
|
||||
|
||||
assertThat(properties).isNotNull();
|
||||
|
||||
TDVisitor tdVisitor = new TDVisitor();
|
||||
documentMapping.visit(tdVisitor);
|
||||
|
||||
assertThat(tdVisitor.sb.toString()).isEqualTo("{status,orderDate,shipDate, object{customer:id,name,}customerName, nested{details: [id,orderQty,shipQty,unitPrice,cretime,updtime,]}cretime,updtime,}");
|
||||
}
|
||||
|
||||
class TDVisitor extends DocPropertyAdapter {
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
@Override
|
||||
public void visitProperty(DocPropertyMapping property) {
|
||||
sb.append(property.getName()+",");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitBegin() {
|
||||
|
||||
sb.append("{");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitEnd() {
|
||||
sb.append("}");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitBeginObject(DocPropertyMapping property) {
|
||||
sb.append(" object{"+property.getName()+":");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitEndObject(DocPropertyMapping property) {
|
||||
sb.append("}");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitBeginList(DocPropertyMapping property) {
|
||||
sb.append(" nested{"+property.getName()+": [");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitEndList(DocPropertyMapping property) {
|
||||
sb.append("]}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.avaje.ebeaninternal.server.deploy;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.plugin.Property;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class BeanDescriptor_propertiesTest extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void allProperties() {
|
||||
|
||||
BeanDescriptor<Order> desc = getBeanDescriptor(Order.class);
|
||||
Collection<? extends Property> props = desc.allProperties();
|
||||
|
||||
assertThat(props).extracting("name").contains("id", "status", "orderDate", "shipDate");
|
||||
}
|
||||
|
||||
}
|
||||
+4
-4
@@ -21,15 +21,15 @@ public class BeanDescriptor_whenCreatedPropertyTest extends BaseTestCase {
|
||||
|
||||
BeanDescriptor<Customer> desc = server.getBeanDescriptor(Customer.class);
|
||||
|
||||
BeanProperty whenCreatedProperty = desc.findWhenCreatedProperty();
|
||||
BeanProperty whenCreatedProperty = desc.getWhenCreatedProperty();
|
||||
assertEquals("cretime",whenCreatedProperty.getDbColumn());
|
||||
|
||||
BeanProperty whenModifiedProperty = desc.findWhenModifiedProperty();
|
||||
BeanProperty whenModifiedProperty = desc.getWhenModifiedProperty();
|
||||
assertEquals("updtime",whenModifiedProperty.getDbColumn());
|
||||
|
||||
|
||||
BeanDescriptor<EBasic> eBasicDesc = server.getBeanDescriptor(EBasic.class);
|
||||
assertNull(eBasicDesc.findWhenCreatedProperty());
|
||||
assertNull(eBasicDesc.findWhenModifiedProperty());
|
||||
assertNull(eBasicDesc.getWhenCreatedProperty());
|
||||
assertNull(eBasicDesc.getWhenModifiedProperty());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.avaje.ebeaninternal.server.expression;
|
||||
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.fasterxml.jackson.core.JsonFactory;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
|
||||
public abstract class BaseElasticTest extends BaseTestCase {
|
||||
|
||||
public static JsonFactory factory = new JsonFactory();
|
||||
|
||||
public ElasticExpressionContext context(StringWriter sb) throws IOException {
|
||||
|
||||
BeanDescriptor<Order> desc = getBeanDescriptor(Order.class);
|
||||
JsonGenerator gen = factory.createGenerator(sb);
|
||||
return new ElasticExpressionContext(gen, desc);
|
||||
}
|
||||
|
||||
}
|
||||
+3
-3
@@ -54,7 +54,7 @@ public class DefaultExampleExpressionTest extends BaseTestCase {
|
||||
DefaultExampleExpression prepare(DefaultExampleExpression expr) {
|
||||
|
||||
SpiQuery<Customer> query = (SpiQuery<Customer>)spiEbeanServer().find(Customer.class);
|
||||
BeanQueryRequest<?> request = create(query, customerBeanDescriptor());
|
||||
BeanQueryRequest<?> request = create(query);
|
||||
expr.prepareExpression(request);
|
||||
|
||||
return expr;
|
||||
@@ -92,8 +92,8 @@ public class DefaultExampleExpressionTest extends BaseTestCase {
|
||||
|
||||
}
|
||||
|
||||
private <T> OrmQueryRequest<T> create(SpiQuery<T> query, BeanDescriptor<T> desc) {
|
||||
return new OrmQueryRequest<T>(null, null, query, desc, null);
|
||||
private <T> OrmQueryRequest<T> create(SpiQuery<T> query) {
|
||||
return new OrmQueryRequest<T>(null, null, query, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+3
-3
@@ -8,12 +8,12 @@ import java.util.Arrays;
|
||||
|
||||
import static org.assertj.core.api.StrictAssertions.assertThat;
|
||||
|
||||
public class ExistsExpressionTest {
|
||||
public class ExistsQueryExpressionTest {
|
||||
|
||||
|
||||
@NotNull
|
||||
private ExistsExpression exp(boolean not, String sql, Object... bindValues) {
|
||||
return new ExistsExpression(not, sql, Arrays.asList(bindValues));
|
||||
private ExistsQueryExpression exp(boolean not, String sql, Object... bindValues) {
|
||||
return new ExistsQueryExpression(not, sql, Arrays.asList(bindValues));
|
||||
}
|
||||
|
||||
@Test
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.avaje.ebeaninternal.server.expression;
|
||||
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.StringWriter;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class SimpleExpressionElasticTest extends BaseElasticTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void writeElastic() throws Exception {
|
||||
|
||||
SimpleExpression eqExp = new SimpleExpression("name", Op.EQ, "rob");
|
||||
|
||||
StringWriter sb = new StringWriter();
|
||||
ElasticExpressionContext context = context(sb);
|
||||
eqExp.writeElastic(context);
|
||||
context.json().flush();
|
||||
|
||||
String json = sb.toString();
|
||||
|
||||
assertThat(json).isEqualTo("{\"term\":{\"name\":\"rob\"}}");
|
||||
}
|
||||
}
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
package com.avaje.ebeaninternal.server.querydefn;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.ebeaninternal.api.SpiExpressionList;
|
||||
import com.avaje.ebeaninternal.api.SpiQuery;
|
||||
import com.avaje.ebeaninternal.server.expression.BaseElasticTest;
|
||||
import com.avaje.ebeaninternal.server.expression.ElasticExpressionContext;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class DefaultOrmQueryElasticTest extends BaseElasticTest {
|
||||
|
||||
@Test
|
||||
public void writeElastic_on_SpiExpressionList() throws IOException {
|
||||
|
||||
Query<Order> query = Ebean.find(Order.class)
|
||||
.where().eq("customer.name", "Rob")
|
||||
.query();
|
||||
|
||||
SpiQuery<Order> spiQuery = (SpiQuery<Order>)query;
|
||||
|
||||
SpiExpressionList<Order> whereExpressions = spiQuery.getWhereExpressions();
|
||||
|
||||
StringWriter sb = new StringWriter();
|
||||
ElasticExpressionContext context = context(sb);
|
||||
JsonGenerator json = context.json();
|
||||
json.writeStartObject();
|
||||
json.writeFieldName("filter");
|
||||
|
||||
whereExpressions.writeElastic(context);
|
||||
|
||||
json.writeEndObject();
|
||||
context.flush();
|
||||
|
||||
assertThat(sb.toString()).isEqualTo("{\"filter\":{\"term\":{\"customer.name\":\"Rob\"}}}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeElastic() throws IOException {
|
||||
|
||||
Query<Order> query = Ebean.find(Order.class)
|
||||
.select("status, customer.name, details.product.id")
|
||||
.where().eq("customer.name", "Rob")
|
||||
.query();
|
||||
|
||||
SpiQuery<Order> spiQuery = (SpiQuery<Order>)query;
|
||||
|
||||
StringWriter sb = new StringWriter();
|
||||
ElasticExpressionContext context = context(sb);
|
||||
|
||||
spiQuery.writeElastic(context);
|
||||
context.flush();
|
||||
|
||||
assertThat(sb.toString()).isEqualTo("{\"fields\":[\"status\",\"customer.name\",\"details.product.id\"],\"query\":{\"filtered\":{\"filter\":{\"term\":{\"customer.name\":\"Rob\"}}}}}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void asElasticQuery() throws IOException {
|
||||
|
||||
String elasticQuery = Ebean.find(Order.class)
|
||||
.select("status")
|
||||
.where().eq("customer.name", "Rob")
|
||||
.query().asElasticQuery();
|
||||
|
||||
|
||||
assertThat(elasticQuery).isEqualTo("{\"fields\":[\"status\"],\"query\":{\"filtered\":{\"filter\":{\"term\":{\"customer.name\":\"Rob\"}}}}}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void asElasticQuery_firstRowsMaxRows() throws IOException {
|
||||
|
||||
String elasticQuery = Ebean.find(Order.class)
|
||||
.select("status")
|
||||
.setFirstRow(3)
|
||||
.setMaxRows(100)
|
||||
.where().eq("customer.name", "Rob")
|
||||
.query().asElasticQuery();
|
||||
|
||||
assertThat(elasticQuery).isEqualTo("{\"from\":3,\"size\":100,\"fields\":[\"status\"],\"query\":{\"filtered\":{\"filter\":{\"term\":{\"customer.name\":\"Rob\"}}}}}");
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,8 @@ package com.avaje.ebeaninternal.server.querydefn;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.EbeanServer;
|
||||
import com.avaje.ebeaninternal.api.SpiEbeanServer;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import com.avaje.ebeaninternal.server.expression.DefaultExpressionFactory;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import org.junit.Assert;
|
||||
@@ -81,11 +82,13 @@ public class TestQueryLanguage extends BaseTestCase {
|
||||
|
||||
private DefaultOrmQuery<Order> check(String q) {
|
||||
|
||||
EbeanServer server = Ebean.getServer(null);
|
||||
SpiEbeanServer server = (SpiEbeanServer)Ebean.getServer(null);
|
||||
|
||||
OrmQueryDetailParser p = new OrmQueryDetailParser(q);
|
||||
p.parse();
|
||||
DefaultOrmQuery<Order> qry = new DefaultOrmQuery<Order>(Order.class, server,
|
||||
|
||||
BeanDescriptor<Order> desc = server.getBeanDescriptor(Order.class);
|
||||
DefaultOrmQuery<Order> qry = new DefaultOrmQuery<Order>(desc, server,
|
||||
new DefaultExpressionFactory(false), (String) null);
|
||||
p.assign(qry);
|
||||
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.avaje.ebeaninternal.server.text.json;
|
||||
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.bean.EntityBean;
|
||||
import com.avaje.ebeaninternal.api.SpiEbeanServer;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
import com.fasterxml.jackson.core.JsonFactory;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.sql.Date;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class WriteJsonDirtyTest {
|
||||
|
||||
@Test
|
||||
public void test() throws IOException {
|
||||
|
||||
ResetBasicData.reset();
|
||||
List<Customer> customers = Ebean.find(Customer.class).findList();
|
||||
|
||||
Customer customer = Ebean.find(Customer.class).setId(customers.get(0).getId())
|
||||
.setUseCache(false)
|
||||
.findUnique();
|
||||
|
||||
SpiEbeanServer server = (SpiEbeanServer)Ebean.getServer(null);
|
||||
BeanDescriptor<Customer> descriptor = server.getBeanDescriptor(Customer.class);
|
||||
|
||||
customer.setName("dirtyCustName");
|
||||
customer.setAnniversary(new Date(System.currentTimeMillis()));
|
||||
|
||||
EntityBean entityBean = (EntityBean)customer;
|
||||
boolean[] dirtyProperties = entityBean._ebean_getIntercept().getDirtyProperties();
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
JsonFactory jsonFactory = new JsonFactory();
|
||||
JsonGenerator generator = jsonFactory.createGenerator(writer);
|
||||
|
||||
WriteJson writeJson = new WriteJson(server, generator, null, null, null, null);
|
||||
descriptor.jsonWriteDirty(writeJson, entityBean, dirtyProperties);
|
||||
|
||||
generator.flush();
|
||||
generator.close();
|
||||
|
||||
String jsonContent = writer.toString();
|
||||
assertTrue(jsonContent.contains("\"name\":"));
|
||||
assertTrue(jsonContent.contains("\"anniversary\":"));
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package com.avaje.ebeaninternal.server.text.json;
|
||||
|
||||
import com.avaje.ebean.config.JsonConfig;
|
||||
import com.avaje.ebean.text.PathProperties;
|
||||
import com.avaje.ebean.FetchPath;
|
||||
import com.fasterxml.jackson.core.JsonFactory;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import org.junit.Test;
|
||||
@@ -19,8 +20,8 @@ public class WriteJsonTest {
|
||||
JsonFactory jsonFactory = new JsonFactory();
|
||||
JsonGenerator generator = jsonFactory.createGenerator(new StringWriter());
|
||||
|
||||
PathProperties pathProperties = PathProperties.parse("id,status,name,customer(id,name,address(street,city)),orders(qty,product(sku,prodName))");
|
||||
WriteJson writeJson = new WriteJson(null, generator, pathProperties, null, null, JsonConfig.Include.ALL);
|
||||
FetchPath fetchPath = PathProperties.parse("id,status,name,customer(id,name,address(street,city)),orders(qty,product(sku,prodName))");
|
||||
WriteJson writeJson = new WriteJson(null, generator, fetchPath, null, null, JsonConfig.Include.ALL);
|
||||
|
||||
WriteJson.WriteBean rootLevel = writeJson.createWriteBean(null, null);
|
||||
assertTrue(rootLevel.currentIncludeProps.contains("id"));
|
||||
|
||||
@@ -18,9 +18,9 @@ public class TestEnumToBeanType {
|
||||
OrdinalEnum ordinalEnum = new ScalarTypeEnumStandard.OrdinalEnum(Order.Status.class);
|
||||
|
||||
EnumToDbValueMap<?> beanDbMap = EnumToDbValueMap.create(false);
|
||||
beanDbMap.add(Customer.Status.ACTIVE, "A");
|
||||
beanDbMap.add(Customer.Status.NEW, "N");
|
||||
beanDbMap.add(Customer.Status.INACTIVE, "I");
|
||||
beanDbMap.add(Customer.Status.ACTIVE, "A", Customer.Status.ACTIVE.name());
|
||||
beanDbMap.add(Customer.Status.NEW, "N", Customer.Status.NEW.name());
|
||||
beanDbMap.add(Customer.Status.INACTIVE, "I", Customer.Status.INACTIVE.name());
|
||||
|
||||
ScalarTypeEnumWithMapping withMapping = new ScalarTypeEnumWithMapping(beanDbMap, Customer.Status.class, 1);
|
||||
|
||||
|
||||
Reference in new issue
Block a user