1
|
package eu.dnetlib.functionality.cql.mongo;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
|
5
|
import com.google.common.collect.Lists;
|
6
|
import com.mongodb.BasicDBObject;
|
7
|
import com.mongodb.MongoClient;
|
8
|
import org.apache.commons.logging.Log;
|
9
|
import org.apache.commons.logging.LogFactory;
|
10
|
import org.bson.BsonDocument;
|
11
|
import org.bson.conversions.Bson;
|
12
|
import org.bson.types.ObjectId;
|
13
|
import org.junit.Ignore;
|
14
|
import org.junit.Test;
|
15
|
import org.z3950.zing.cql.CQLParseException;
|
16
|
|
17
|
import static com.mongodb.client.model.Filters.gt;
|
18
|
import static org.junit.Assert.assertEquals;
|
19
|
|
20
|
public class MongoCqlTranslatorTest {
|
21
|
|
22
|
private static final Log log = LogFactory.getLog(MongoCqlTranslatorTest.class); // NOPMD by marko on 11/24/08 5:02 PM
|
23
|
private final MongoCqlTranslator tr = new MongoCqlTranslator();
|
24
|
|
25
|
@Test
|
26
|
public void testParseExact() throws IOException, CQLParseException {
|
27
|
Bson expected = new BasicDBObject("set", "CEDIASManuscripts");
|
28
|
Bson o = tr.toMongo("set exact \"CEDIASManuscripts\"");
|
29
|
assertEquals(expected, o);
|
30
|
}
|
31
|
|
32
|
@Test
|
33
|
public void testParseEq() throws IOException, CQLParseException {
|
34
|
Bson expected = new BasicDBObject("set", "CEDIASManuscripts");
|
35
|
Bson o = tr.toMongo("set = \"CEDIASManuscripts\"");
|
36
|
assertEquals(expected, o);
|
37
|
}
|
38
|
|
39
|
@Test
|
40
|
public void testParseEquivalentExact() throws IOException, CQLParseException {
|
41
|
Bson o = tr.toMongo("set = \"CEDIASManuscripts\"");
|
42
|
Bson oExact = tr.toMongo("set exact \"CEDIASManuscripts\"");
|
43
|
assertEquals(oExact, o);
|
44
|
}
|
45
|
|
46
|
@Test
|
47
|
public void testParseNeq() throws IOException, CQLParseException {
|
48
|
Bson expected = new BasicDBObject("set", new BasicDBObject("$ne", "CEDIASManuscripts"));
|
49
|
Bson o = tr.toMongo("set <> \"CEDIASManuscripts\"");
|
50
|
assertEquals(expected, o);
|
51
|
}
|
52
|
|
53
|
@Test
|
54
|
public void testParseAnd() throws IOException, CQLParseException {
|
55
|
BasicDBObject expected = new BasicDBObject("$and", Lists.newArrayList(new BasicDBObject("set", new BasicDBObject("$ne", "CEDIASManuscripts")),
|
56
|
new BasicDBObject("pippo", new BasicDBObject("$gt", "x"))));
|
57
|
Bson o = tr.toMongo("set <> \"CEDIASManuscripts\" AND pippo > x");
|
58
|
log.info(o);
|
59
|
assertEquals(expected, o);
|
60
|
}
|
61
|
|
62
|
@Test
|
63
|
public void testParseOr() throws IOException, CQLParseException {
|
64
|
BasicDBObject expected = new BasicDBObject("$or", Lists.newArrayList(new BasicDBObject("set", new BasicDBObject("$ne", "CEDIASManuscripts")),
|
65
|
new BasicDBObject("pippo", new BasicDBObject("$gt", "x"))));
|
66
|
Bson o = tr.toMongo("set <> \"CEDIASManuscripts\" OR pippo > x");
|
67
|
log.info(o);
|
68
|
assertEquals(expected, o);
|
69
|
}
|
70
|
|
71
|
@Test
|
72
|
public void testParseNot() throws IOException, CQLParseException {
|
73
|
BasicDBObject expected = new BasicDBObject("$and", Lists.newArrayList(new BasicDBObject("set", "CEDIASManuscripts"), new BasicDBObject("$not",
|
74
|
new BasicDBObject("pippo", new BasicDBObject("$gt", "x")))));
|
75
|
Bson o = tr.toMongo("set = \"CEDIASManuscripts\" NOT pippo > x");
|
76
|
//log.info(o)
|
77
|
assertEquals(expected, o);
|
78
|
}
|
79
|
|
80
|
@Test
|
81
|
public void testParseStar() throws IOException, CQLParseException {
|
82
|
BasicDBObject expected = new BasicDBObject();
|
83
|
Bson o = tr.toMongo("*");
|
84
|
Bson o2 = tr.toMongo("*=*");
|
85
|
assertEquals(expected, o);
|
86
|
assertEquals(expected, o2);
|
87
|
}
|
88
|
|
89
|
@Test
|
90
|
public void testParseStarAnd() throws IOException, CQLParseException {
|
91
|
BasicDBObject expected = new BasicDBObject("$and", Lists.newArrayList(new BasicDBObject(), new BasicDBObject("pippo", new BasicDBObject("$gt", "x"))));
|
92
|
Bson o = tr.toMongo("* AND pippo > x");
|
93
|
Bson o2 = tr.toMongo("*=* AND pippo > x");
|
94
|
assertEquals(expected, o);
|
95
|
assertEquals(expected, o2);
|
96
|
}
|
97
|
|
98
|
@Test
|
99
|
public void testParseIdQuery() throws IOException, CQLParseException {
|
100
|
BasicDBObject expected = new BasicDBObject("_id", new BasicDBObject("$gt", new ObjectId("5225e093aabf055637bf2c65")));
|
101
|
Bson o = tr.toMongo("_id > 5225e093aabf055637bf2c65");
|
102
|
assertEquals(expected, o);
|
103
|
}
|
104
|
|
105
|
@Test
|
106
|
public void testLongQuery() throws IOException, CQLParseException {
|
107
|
String q = "oaftype=\"result\" AND resulttypeid=\"publication\" AND (set=\"All_Ireland_Public_Health_Repository\" OR set=\"ARROW_DIT\" OR set=\"Cork_Open_Research_Archive\" OR set=\"DCU_Online_Research_Access_Service\" OR set=\"e-publications_RCSI\" OR set=\"Marine_Institute_Open_Access_Repository__OAR\" OR set=\"Maynooth_University_ePrints___eTheses_Archive\" OR set=\"Research_Repository_UCD\" OR set=\"STOR\" OR set=\"Trinity_s_Access_to_Research_Archive\" OR set=\"UCD_Digital_Library\" OR set=\"University_of_Limerick_Institutional_Repository\" OR set=\"Waterford_Institute_of_Technology_Repository\")";
|
108
|
Bson o = tr.toMongo(q);
|
109
|
log.info(o);
|
110
|
}
|
111
|
|
112
|
@Ignore
|
113
|
@Test
|
114
|
public void testParseWfLoggerQuery() throws IOException, CQLParseException {
|
115
|
BasicDBObject expected = new BasicDBObject("$and",
|
116
|
Lists.newArrayList(
|
117
|
new BasicDBObject("parentDatasourceId", "opendoar____::2294"),
|
118
|
new BasicDBObject("system:profileFamily", "aggregator"),
|
119
|
new BasicDBObject("system:isCompletedSuccessfully", "true")));
|
120
|
|
121
|
Bson o = tr.toMongo("{\"parentDatasourceId\" : \"opendoar____::2294\", \"system:profileFamily\" : \"aggregator\", \"system:isCompletedSuccessfully\" : \"true\" }");
|
122
|
assertEquals(expected, o);
|
123
|
}
|
124
|
|
125
|
@Test
|
126
|
public void testParseTimestamp() throws IOException, CQLParseException {
|
127
|
Long l = new Long("1494945927504");
|
128
|
BasicDBObject expected = new BasicDBObject("timestamp", new BasicDBObject("$gt",l));
|
129
|
Bson filter = gt("timestamp", l);
|
130
|
assertEquals(expected.toBsonDocument(BsonDocument.class, MongoClient.getDefaultCodecRegistry()), filter.toBsonDocument(BsonDocument.class, MongoClient.getDefaultCodecRegistry()));
|
131
|
}
|
132
|
|
133
|
}
|