Java tutorial
/* * Copyright (C) 2007-2013 Crafter Software Corporation. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package org.craftercms.commerce.server; import static org.junit.Assert.assertEquals; import junit.framework.Assert; import org.junit.Test; import org.craftercms.commerce.server.solr.MockSolrIndex; import org.springframework.data.mongodb.core.query.Query; import com.mongodb.BasicDBObject; import com.mongodb.DBObject; /** * Test the query converter to assure all the syntax it is capable of handling * * @author Michiel Verkaik (mverkaik@rivetlogic.com) * */ public class QueryConverterTest { private QueryConverter queryConverter; public QueryConverterTest() { queryConverter = new QueryConverter(); queryConverter.setSolrIndex(new MockSolrIndex()); } @Test public void testToSolrQuery() { String ccQuery; String solrQuery; // comparisonOperator: = ccQuery = "name = 'goldmember'"; solrQuery = queryConverter.toSolrQuery(ccQuery); assertEquals("name:\"goldmember\"", solrQuery); // comparisonOperator: > ccQuery = "age > 21"; solrQuery = queryConverter.toSolrQuery(ccQuery); assertEquals("age:[22 TO *]", solrQuery); // comparisonOperator: < ccQuery = "age < 21"; solrQuery = queryConverter.toSolrQuery(ccQuery); assertEquals("age:[* TO 20]", solrQuery); // comparisonOperator: <> ccQuery = "color <> 'blue'"; solrQuery = queryConverter.toSolrQuery(ccQuery); assertEquals("-color:\"blue\"", solrQuery); } @Test public void testToMongoQuery() { DBObject dbObj; String json; // comparisonOperator: = dbObj = dbObject("name = 'goldmember'"); Assert.assertNotNull(dbObj); Assert.assertNotNull(dbObj.get("name")); Assert.assertEquals("goldmember", dbObj.get("name")); // comparisonOperator: > dbObj = dbObject("age > 21"); Assert.assertNotNull(dbObj); Assert.assertNotNull(dbObj.get("age")); json = ((BasicDBObject) dbObj.get("age")).toString(); Assert.assertEquals("{ \"$gt\" : \"21\"}", json); // comparisonOperator: < dbObj = dbObject("age < 21"); Assert.assertNotNull(dbObj); Assert.assertNotNull(dbObj.get("age")); json = ((BasicDBObject) dbObj.get("age")).toString(); Assert.assertEquals("{ \"$lt\" : \"21\"}", json); // comparisonOperator: <> dbObj = dbObject("color <> 'blue'"); Assert.assertNotNull(dbObj); Assert.assertNotNull(dbObj.get("color")); json = ((BasicDBObject) dbObj.get("color")).toString(); Assert.assertEquals("{ \"$ne\" : \"blue\"}", json); Assert.assertEquals(true, true); // The ultimate optimist } // ---------- Utility methods ---------- private DBObject dbObject(String ccQuery) { Query springMongoQuery = queryConverter.toSpringMongoQuery(ccQuery); return springMongoQuery.getQueryObject(); } }