Skip to content
This repository has been archived by the owner on Dec 29, 2024. It is now read-only.

Commit

Permalink
fix test, add first naive draft for mangoquery (helun/helun#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
triplem committed Oct 9, 2019
1 parent 85731b6 commit e2ed2d5
Show file tree
Hide file tree
Showing 10 changed files with 393 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/ektorp/Status.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public boolean isOk() {

private Map<String, Object> unknown() {
if (unknownFields == null) {
unknownFields = new HashMap<String, Object>();
unknownFields = new HashMap<>();
}
return unknownFields;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/ektorp/impl/StdCouchDbConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ public <T> Page<T> queryForPage(ViewQuery query, PageRequest pr, Class<T> type)
query.dbPath(dbURI.toString());
LOG.debug("startKey: {}", pr.getStartKey());
LOG.debug("startDocId: {}", pr.getStartKeyDocId());
PageResponseHandler<T> ph = new PageResponseHandler<T>(pr, type, objectMapper,
PageResponseHandler<T> ph = new PageResponseHandler<>(pr, type, objectMapper,
query.isIgnoreNotFound());
query = PageRequest.applyPagingParameters(query, pr);

Expand Down
36 changes: 36 additions & 0 deletions src/main/java/org/ektorp/mongoquery/Expression.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.ektorp.mongoquery;

import com.fasterxml.jackson.annotation.JsonValue;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class Expression implements Serializable {

private String left;
private Object right;

public String getLeft() {
return left;
}

public void setLeft(String left) {
this.left = left;
}

public Object getRight() {
return right;
}

public void setRight(Object right) {
this.right = right;
}

@JsonValue
public Map<String, Object> toJson() {
Map<String, Object> result = new HashMap<>();
result.put(getLeft(), getRight());
return result;
}
}
74 changes: 74 additions & 0 deletions src/main/java/org/ektorp/mongoquery/MangoQuery.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.ektorp.mongoquery;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;

@JsonInclude(Include.NON_NULL)
public class MangoQuery {

public enum Stale {
OK("ok"),
FALSE("false");

private String jsonName;

Stale(String jsonName) {
this.jsonName = jsonName;
}

public String getJsonName() {
return jsonName;
}
}

// object describing criteria used to select documents.
private Expression selector;

// Maximum number of results returned. Default is 25. Optional
private Integer limit;

// Skip the first ‘n’ results, where ‘n’ is the value specified. Optional
private Integer skip;

// contains a list of field name and direction pairs
private List<Sort> sort;

// specifying which fields of each object should be returned. If it is omitted, the entire
// object is returned.
private List<String> fields;

// Instruct a query to use a specific index.
@JsonProperty("use_index")
private String useIndex;

// Read quorum needed for the result. This defaults to 1, in which case the document found in
// the index is returned. If set to a higher value, each document is read from at least that
// many replicas before it is returned in the results. This is likely to take more time than
// using only the document stored locally with the index.
@JsonProperty("r")
private Integer readQuorum;

// A string that enables you to specify which page of results you require. Used for paging
// through result sets. Every query returns an opaque string under the bookmark key that can
// then be passed back in a query to get the next page of results. If any part of the selector
// query changes between requests, the results are undefined.
private String bookmark;

// Whether to update the index prior to returning the result. Default is true.
private Boolean update;

// Whether or not the view results should be returned from a “stable” set of shards
private Boolean stable;

// Combination of update=false and stable=true options. (Default: false)
private Stale stale;

// Include execution statistics in the query response. (Default: false)
@JsonProperty("execution_stats")
private Boolean executionStats;



}
52 changes: 52 additions & 0 deletions src/main/java/org/ektorp/mongoquery/MangoQueryBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.ektorp.mongoquery;

import com.fasterxml.jackson.annotation.JsonValue;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class MangoQueryBuilder {

private Expression expression;

public MangoQueryBuilder or(Operator... operators) {
return combinationOperators("$or", operators);
}

public MangoQueryBuilder and(Operator... operators) {
return combinationOperators("$and", operators);
}

private MangoQueryBuilder combinationOperators(String oper, Operator... operators) {
expression = new Expression();
expression.setLeft(oper);

if (operators.length > 1) {
List<Expression> expressions = new LinkedList<>();
for (Operator operator : operators) {
Expression e = new Expression();
e.setLeft(operator.getFieldName());
e.setRight(operator.getExpression());

expressions.add(e);
}
expression.setRight(expressions);
} else if (operators.length == 1){
Expression e = new Expression();
e.setLeft(operators[0].getFieldName());
e.setRight(operators[0].getExpression());

expression = e;
}

return this;
}

@JsonValue
public Map<String, Object> toJson() {
return this.expression.toJson();
}


}
68 changes: 68 additions & 0 deletions src/main/java/org/ektorp/mongoquery/Operator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.ektorp.mongoquery;

import com.fasterxml.jackson.annotation.JsonValue;

import java.util.HashMap;
import java.util.Map;

public class Operator {

private String fieldName;
private Expression expression;

public void setFieldName(String fieldName) {
this.fieldName = fieldName;
}

public void setExpression(Expression expression) {
this.expression = expression;
}

@JsonValue
public Map<String, Object> toJson() {
Map<String, Object> outerMap = new HashMap<>();
outerMap.put(this.fieldName, this.expression);
return outerMap;
}

public static class SimpleCompareOperator extends Operator {

private String comparator;
private Object value;

public SimpleCompareOperator(String fieldName, String comparator, Object value) {
this.setFieldName(fieldName);
Expression exp = new Expression();
exp.setLeft(comparator);
exp.setRight(value);
this.setExpression(exp);
}
}

public static class EqualOperator extends SimpleCompareOperator {

private static final String COMPARATOR = "$eq";

public EqualOperator(String fieldName, Object value) {
super(fieldName, COMPARATOR, value);
}
}

public static class ExistsOperator extends SimpleCompareOperator {

private static final String COMPARATOR = "$in";

public ExistsOperator(String fieldName, Boolean value) {
super(fieldName, COMPARATOR, value);
}
}


public String getFieldName() {
return fieldName;
}

public Expression getExpression() {
return expression;
}
}
43 changes: 43 additions & 0 deletions src/main/java/org/ektorp/mongoquery/Sort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.ektorp.mongoquery;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class Sort implements Serializable {

public enum SortOrder {
DESC("desc"),
ASC("asc");

private String jsonName;

SortOrder(String jsonName) {
this.jsonName = jsonName;
}

public String getJsonName() {
return this.jsonName;
}
}

private String fieldName;

private SortOrder sortOrder;

public Sort(String fieldName, SortOrder sortOrder) {
this.fieldName = fieldName;
this.sortOrder = sortOrder;
}

public Sort(String fieldName) {
this(fieldName, SortOrder.ASC);
}

public Map<String, String> toJson() {
Map<String, String> result = new HashMap<>();
result.put(fieldName, sortOrder.getJsonName());
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public boolean apply(UpdateHandlers input) {
}

private Map<String, String> createShowFunctions(final Class<?> metaDataClass) {
final Map<String, String> shows = new HashMap<String, String>();
final Map<String, String> shows = new HashMap<>();

ReflectionUtils
.eachAnnotation(metaDataClass, ShowFunction.class, new Predicate<ShowFunction>() {
Expand Down
69 changes: 57 additions & 12 deletions src/test/java/org/ektorp/impl/StdCouchDbInstanceIT.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
package org.ektorp.impl;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Scanner;
import java.util.stream.Stream;
import jdk.internal.util.xml.impl.Input;
import org.ektorp.AttachmentInputStream;
import org.ektorp.CouchDbConnector;
import org.ektorp.CouchDbContainer;
import org.ektorp.CouchDbInstance;
import org.ektorp.DocumentNotFoundException;
import org.ektorp.Options;
import org.ektorp.http.HttpClient;
import org.ektorp.http.StdHttpClient;
import org.ektorp.impl.MembershipInfo;
import org.ektorp.impl.StdCouchDbConnector;
import org.ektorp.impl.StdCouchDbInstance;
import org.ektorp.support.CouchDbDocument;
import org.junit.ClassRule;
import org.junit.Test;
import org.testcontainers.containers.GenericContainer;
Expand All @@ -32,15 +42,50 @@ public void testDescribeCluster() throws Exception {

connector.createDatabaseIfNotExists();

try {
MembershipInfo info = dbInstance.describeCluster();
assertEquals(1, info.getAllNodes().size());
assertEquals("nonode@nodhost", info.getAllNodes().get(0));
assertEquals(1, info.getClusterNodes().size());
assertEquals("nonode@nodhost", info.getClusterNodes().get(0));
} catch (DocumentNotFoundException e) {
e.printStackTrace();
}
MembershipInfo info = dbInstance.describeCluster();
assertEquals(1, info.getAllNodes().size());
assertEquals("nonode@nodhost", info.getAllNodes().get(0));
assertEquals(1, info.getClusterNodes().size());
assertEquals("nonode@nodhost", info.getClusterNodes().get(0));
}

@Test
public void testCreateAttachement() throws Exception {
HttpClient httpClient = new StdHttpClient.Builder()
.url(((CouchDbContainer)couchDb).getCouchDbUrl())
.build();

CouchDbInstance dbInstance = new StdCouchDbInstance(httpClient);
CouchDbConnector connector = new StdCouchDbConnector("test", dbInstance);

connector.createDatabaseIfNotExists();

try(InputStream is = getClass().getResourceAsStream("all_docs_result.json")) {
String contentType = "application/text";

AttachmentInputStream ais = new AttachmentInputStream("1", is, contentType);
String revision = connector.createAttachment("1", ais);
assertNotNull(revision);

Options options = new Options();
options.includeConflicts();
options.includeRevisions();
options.revision(revision);
CouchDbDocument document = connector.get(CouchDbDocument.class, "1", options);
assertNotNull(document);
assertFalse(document.getAttachments().isEmpty());

AttachmentInputStream rs = connector.getAttachment("1",
document.getAttachments().keySet().iterator().next(), revision);
assertNotNull(rs);

String result;
try (Scanner scan = new Scanner(rs, StandardCharsets.UTF_8.name())) {
result = scan.useDelimiter("\\A").next();
}

assertTrue(result.contains("1-2842770487"));
}

}
}
Loading

0 comments on commit e2ed2d5

Please sign in to comment.