/
DocumentContainsTest.java
127 lines (109 loc) · 3.74 KB
/
DocumentContainsTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import static play.test.Helpers.GET;
import static play.test.Helpers.POST;
import static play.test.Helpers.routeAndCall;
import static play.test.Helpers.running;
import java.io.IOException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import play.mvc.Http.Status;
import play.mvc.Result;
import play.test.FakeRequest;
import core.AbstractDocumentTest;
import core.TestConfig;
public class DocumentContainsTest extends AbstractDocumentTest {
private Object json = null;
private JsonNode document1;
private JsonNode jsonForUpdate;
private String sFakeCollection;
@Override
public String getRouteAddress() {
return SERVICE_ROUTE + TestConfig.TEST_COLLECTION_NAME;
}
@Override
public String getMethod() {
return GET;
}
@Override
protected void assertContent(String s) {
json = toJSON(s);
assertJSON(json, "@version");
}
@Before
public void setDocument() throws JsonProcessingException, IOException{
document1 = new ObjectMapper().readTree(
"{\"players\": ["+
"{"+
"\"username\": \"XXXX@XXXX.COM\","+
"\"name\": \"john doe\","+
"\"points\": 0"+
"},"+
"{"+
"\"username\": \"YYYY1@YYYY.COM\","+
"\"name\": \"jane doe\","+
"\"department\": \"one\","+
"\"points\": 0"+
"},"+
"{"+
"\"username\": \"ZZZZ@ZZZZ1.COM\","+
"\"name\": \"dana scully\","+
"\"department\": \"two\","+
"\"points\": 0"+
"}"+
"],"+
"\"status\": \"open\","+
"\"player_turn\": 0,"+
"\"id\": \"ff342a9b-79cd-4494-983e-6ec4a1a47c8a\","+
"\"creationdate\": \"2014-02-15T13:49:51.051+0100\","+
"\"_author\": \"admin\""
+ "}");
}
@Test
public void testContains(){
running
(
getFakeApplication(),
new Runnable() {
public void run() {
continueOnFail(false);
sFakeCollection = new AdminCollectionFunctionalTest().routeCreateCollection();
//create document
FakeRequest request = new FakeRequest(POST, getRouteAddress(sFakeCollection));
request = request.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
request = request.withHeader(TestConfig.KEY_AUTH, TestConfig.AUTH_ADMIN_ENC);
request = request.withJsonBody(document1,POST);
Result result = routeAndCall(request);
assertRoute(result, "testContains CREATE 1", 200, null, true);
String id1=getUuid();
//read the doc
request = new FakeRequest(GET, "/document/" + sFakeCollection + "/" + id1);
request = request.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
request = request.withHeader(TestConfig.KEY_AUTH, TestConfig.AUTH_ADMIN_ENC);
result = routeAndCall(request);
assertRoute(result, "testContains load document 1", Status.OK, "\"name\":\"john doe\"", true);
//select it using the `contains`function
request = new FakeRequest("GET", "/document/" + sFakeCollection + "?where=players%20contains%20(username%3D%27XXXX%40XXXX.COM%27)");
request = request.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
request = request.withHeader(TestConfig.KEY_AUTH, TestConfig.AUTH_ADMIN_ENC);
result = routeAndCall(request);
assertRoute(result, "testContains load document 1 using contains", 200, "\"name\":\"john doe\"", true);
}
}
);
}
private String getUuid() {
String sUuid = null;
try {
JSONObject jo = (JSONObject)json;
sUuid = jo.getJSONObject("data").getString("id");
}
catch (Exception ex) {
Assert.fail("Cannot get UUID (id) value: " + ex.getMessage() + "\n The json object is: \n" + json);
}
return sUuid;
}
}