Project

General

Profile

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 testParseStarAnd2() throws IOException, CQLParseException {
100
		BasicDBObject expected = new BasicDBObject("$and", Lists.newArrayList(new BasicDBObject("resulttypeid", "publication"), new BasicDBObject("funder", new BasicDBObject("$exists", true))));
101
		Bson o = tr.toMongo("(resulttypeid exact \"publication\" and funder =*)");
102
		assertEquals(expected, o);
103
	}
104

    
105
	@Test
106
	public void testParseIdQuery() throws IOException, CQLParseException {
107
		BasicDBObject expected = new BasicDBObject("_id", new BasicDBObject("$gt", new ObjectId("5225e093aabf055637bf2c65")));
108
		Bson o = tr.toMongo("_id > 5225e093aabf055637bf2c65");
109
		assertEquals(expected, o);
110
	}
111

    
112
	@Test
113
	public void testLongQuery() throws IOException, CQLParseException {
114
		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\")";
115
		Bson o = tr.toMongo(q);
116
		log.info(o);
117
	}
118

    
119
	@Ignore
120
	@Test
121
	public void testParseWfLoggerQuery() throws IOException, CQLParseException {
122
		BasicDBObject expected = new BasicDBObject("$and",
123
				Lists.newArrayList(
124
						new BasicDBObject("parentDatasourceId", "opendoar____::2294"),
125
						new BasicDBObject("system:profileFamily", "aggregator"),
126
						new BasicDBObject("system:isCompletedSuccessfully", "true")));
127

    
128
		Bson o = tr.toMongo("{\"parentDatasourceId\" : \"opendoar____::2294\", \"system:profileFamily\" : \"aggregator\", \"system:isCompletedSuccessfully\" : \"true\" }");
129
		assertEquals(expected, o);
130
	}
131

    
132
	@Test
133
	public void testParseTimestamp() throws IOException, CQLParseException {
134
		Long l = new Long("1494945927504");
135
		BasicDBObject expected = new BasicDBObject("timestamp", new BasicDBObject("$gt",l));
136
		Bson filter =  gt("timestamp", l);
137
		assertEquals(expected.toBsonDocument(BsonDocument.class, MongoClient.getDefaultCodecRegistry()), filter.toBsonDocument(BsonDocument.class, MongoClient.getDefaultCodecRegistry()));
138
	}
139

    
140
	@Test
141
	public void testPM_PMCIDQueryNotinPubMedCentral() throws IOException, CQLParseException {
142
		String q = "((pidtype = \"pmc\" or pidtype = \"pmid\") and set <> \"PubMed_Central\" )";
143
		BasicDBObject expected = new BasicDBObject("$and", Lists.newArrayList(new BasicDBObject("$or", Lists.newArrayList(new BasicDBObject("pidtype", "pmc"), new BasicDBObject("pidtype", "pmid"))), new BasicDBObject("set", new BasicDBObject("$ne", "PubMed_Central"))));
144
		Bson o = tr.toMongo(q);
145
		assertEquals(expected, o);
146
	}
147

    
148
}
    (1-1/1)