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 org.apache.commons.logging.Log;
|
8
|
import org.apache.commons.logging.LogFactory;
|
9
|
import org.bson.conversions.Bson;
|
10
|
import org.bson.types.ObjectId;
|
11
|
import org.junit.Ignore;
|
12
|
import org.junit.Test;
|
13
|
import org.z3950.zing.cql.CQLParseException;
|
14
|
|
15
|
import static org.junit.Assert.assertEquals;
|
16
|
|
17
|
public class MongoCqlTranslatorTest {
|
18
|
|
19
|
private static final Log log = LogFactory.getLog(MongoCqlTranslatorTest.class); // NOPMD by marko on 11/24/08 5:02 PM
|
20
|
private final MongoCqlTranslator tr = new MongoCqlTranslator();
|
21
|
|
22
|
@Test
|
23
|
public void testParseExact() throws IOException, CQLParseException {
|
24
|
Bson expected = new BasicDBObject("set", "CEDIASManuscripts");
|
25
|
Bson o = tr.toMongo("set exact \"CEDIASManuscripts\"");
|
26
|
assertEquals(expected, o);
|
27
|
}
|
28
|
|
29
|
@Test
|
30
|
public void testParseEq() throws IOException, CQLParseException {
|
31
|
Bson expected = new BasicDBObject("set", "CEDIASManuscripts");
|
32
|
Bson o = tr.toMongo("set = \"CEDIASManuscripts\"");
|
33
|
assertEquals(expected, o);
|
34
|
}
|
35
|
|
36
|
@Test
|
37
|
public void testParseEquivalentExact() throws IOException, CQLParseException {
|
38
|
Bson o = tr.toMongo("set = \"CEDIASManuscripts\"");
|
39
|
Bson oExact = tr.toMongo("set exact \"CEDIASManuscripts\"");
|
40
|
assertEquals(oExact, o);
|
41
|
}
|
42
|
|
43
|
@Test
|
44
|
public void testParseNeq() throws IOException, CQLParseException {
|
45
|
Bson expected = new BasicDBObject("set", new BasicDBObject("$ne", "CEDIASManuscripts"));
|
46
|
Bson o = tr.toMongo("set <> \"CEDIASManuscripts\"");
|
47
|
assertEquals(expected, o);
|
48
|
}
|
49
|
|
50
|
@Test
|
51
|
public void testParseAnd() throws IOException, CQLParseException {
|
52
|
BasicDBObject expected = new BasicDBObject("$and", Lists.newArrayList(new BasicDBObject("set", new BasicDBObject("$ne", "CEDIASManuscripts")),
|
53
|
new BasicDBObject("pippo", new BasicDBObject("$gt", "x"))));
|
54
|
Bson o = tr.toMongo("set <> \"CEDIASManuscripts\" AND pippo > x");
|
55
|
log.info(o);
|
56
|
assertEquals(expected, o);
|
57
|
}
|
58
|
|
59
|
@Test
|
60
|
public void testParseOr() throws IOException, CQLParseException {
|
61
|
BasicDBObject expected = new BasicDBObject("$or", Lists.newArrayList(new BasicDBObject("set", new BasicDBObject("$ne", "CEDIASManuscripts")),
|
62
|
new BasicDBObject("pippo", new BasicDBObject("$gt", "x"))));
|
63
|
Bson o = tr.toMongo("set <> \"CEDIASManuscripts\" OR pippo > x");
|
64
|
log.info(o);
|
65
|
assertEquals(expected, o);
|
66
|
}
|
67
|
|
68
|
@Test
|
69
|
public void testParseNot() throws IOException, CQLParseException {
|
70
|
BasicDBObject expected = new BasicDBObject("$and", Lists.newArrayList(new BasicDBObject("set", "CEDIASManuscripts"), new BasicDBObject("$not",
|
71
|
new BasicDBObject("pippo", new BasicDBObject("$gt", "x")))));
|
72
|
Bson o = tr.toMongo("set = \"CEDIASManuscripts\" NOT pippo > x");
|
73
|
//log.info(o)
|
74
|
assertEquals(expected, o);
|
75
|
}
|
76
|
|
77
|
@Test
|
78
|
public void testParseStar() throws IOException, CQLParseException {
|
79
|
BasicDBObject expected = new BasicDBObject();
|
80
|
Bson o = tr.toMongo("*");
|
81
|
Bson o2 = tr.toMongo("*=*");
|
82
|
assertEquals(expected, o);
|
83
|
assertEquals(expected, o2);
|
84
|
}
|
85
|
|
86
|
@Test
|
87
|
public void testParseStarAnd() throws IOException, CQLParseException {
|
88
|
BasicDBObject expected = new BasicDBObject("$and", Lists.newArrayList(new BasicDBObject(), new BasicDBObject("pippo", new BasicDBObject("$gt", "x"))));
|
89
|
Bson o = tr.toMongo("* AND pippo > x");
|
90
|
Bson o2 = tr.toMongo("*=* AND pippo > x");
|
91
|
assertEquals(expected, o);
|
92
|
assertEquals(expected, o2);
|
93
|
}
|
94
|
|
95
|
@Test
|
96
|
public void testParseIdQuery() throws IOException, CQLParseException {
|
97
|
BasicDBObject expected = new BasicDBObject("_id", new BasicDBObject("$gt", new ObjectId("5225e093aabf055637bf2c65")));
|
98
|
Bson o = tr.toMongo("_id > 5225e093aabf055637bf2c65");
|
99
|
assertEquals(expected, o);
|
100
|
}
|
101
|
|
102
|
@Test
|
103
|
public void testLongQuery() throws IOException, CQLParseException {
|
104
|
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\")";
|
105
|
Bson o = tr.toMongo(q);
|
106
|
log.info(o);
|
107
|
}
|
108
|
|
109
|
@Ignore
|
110
|
@Test
|
111
|
public void testParseWfLoggerQuery() throws IOException, CQLParseException {
|
112
|
BasicDBObject expected = new BasicDBObject("$and",
|
113
|
Lists.newArrayList(
|
114
|
new BasicDBObject("parentDatasourceId", "opendoar____::2294"),
|
115
|
new BasicDBObject("system:profileFamily", "aggregator"),
|
116
|
new BasicDBObject("system:isCompletedSuccessfully", "true")));
|
117
|
|
118
|
Bson o = tr.toMongo("{\"parentDatasourceId\" : \"opendoar____::2294\", \"system:profileFamily\" : \"aggregator\", \"system:isCompletedSuccessfully\" : \"true\" }");
|
119
|
assertEquals(expected, o);
|
120
|
}
|
121
|
|
122
|
}
|