Skip to content
This repository was archived by the owner on Apr 21, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 19 additions & 30 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,24 @@
<version>1.0</version>

<properties>
<scala.version>2.10.2</scala.version>
<scala.version>2.12.0</scala.version>
<junit.version>4.11</junit.version>
</properties>

<dependencies>
<dependency>
<groupId>org.specs2</groupId>
<artifactId>specs2_2.10</artifactId>
<version>2.0</version>
<artifactId>specs2-core_2.12</artifactId>
<version>3.8.6</version>
<scope>test</scope>
</dependency>


<dependency>
<groupId>org.specs2</groupId>
<artifactId>specs2-junit_2.12</artifactId>
<version>3.8.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand All @@ -32,23 +37,25 @@
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-compiler</artifactId>
<version>2.10.2</version>
<version>2.12.0</version>
</dependency>


<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.10.1</version>
<version>3.12.1</version>
<!--<version>3.12.1</version>-->
</dependency>


<dependency>
<groupId>org.mongodb</groupId>
<artifactId>casbah-core_2.10</artifactId>
<version>2.6.1</version>
<artifactId>casbah-core_2.12</artifactId>
<version>3.1.1</version>
</dependency>


</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
Expand All @@ -58,19 +65,22 @@
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>4.3.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.15</version>
<version>2.22.2</version>
<configuration>
<includes>
<include>**/*Suite.class</include>
Expand All @@ -81,27 +91,6 @@
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<executions>
<execution>
<id>scala-compile-first</id>
<phase>process-resources</phase>
<goals>
<goal>add-source</goal>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>scala-test-compile</id>
<phase>process-test-resources</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
10 changes: 8 additions & 2 deletions src/main/scala/com/ee/dynamicmongoquery/MongoQuery.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class MongoQuery(val queryTypes: List[Query], val collectionName: String) {

protected def executeAggregate(aggregationQuery: AggregateQuery, collection: DBCollection): Array[DBObject] = {
val aggregationPipeline = aggregationQuery.aggregationPipeline
var aggregateOP = collection.aggregate(aggregationPipeline.head, aggregationPipeline.tail: _*)
aggregateOP.results().toTraversable.toArray
var cursor = collection.aggregate(aggregationPipeline, AggregationOptions.builder().batchSize(100).allowDiskUse(true).build())
cursor.toArray
}


Expand All @@ -78,5 +78,11 @@ class MongoQuery(val queryTypes: List[Query], val collectionName: String) {
case _ => false
}
}
// Overriding toString method
override def toString() : String = {

return "[MongoQuery : " + this.collectionName +
", Types: = " + this.queryTypes+"]";
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class PlaceholderLookup(params: Map[String, String]) {
}
case s: String => {
val resolvedValue = placeholderResolver.resolve(s)
basicDBObject.put(key, resolvedValue)
basicDBObject.put(key, resolvedValue.asInstanceOf[AnyRef])
}
case _ => {
basicDBObject.put(key, value)
Expand Down
16 changes: 8 additions & 8 deletions src/test/scala/com/ee/dynamicmongoquery/IntegrationTest.scala
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package com.ee.dynamicmongoquery

import com.mongodb.casbah.{MongoConnection, MongoCollection}
import com.mongodb.casbah.{Imports, MongoClient, MongoCollection, MongoConnection}
import com.mongodb.util.JSON
import com.mongodb.{BasicDBList, BasicDBObject}
import com.mongodb.{BasicDBList, BasicDBObject, DBCollection}
import org.specs2.mutable.{BeforeAfter, Specification}
import org.specs2.matcher.MatchResult
import org.specs2.specification.BeforeAfterEach


class IntegrationTest extends Specification {
class IntegrationTest extends Specification with BeforeAfterEach {

lazy val mongoConn = MongoConnection()
lazy val mongoDB = mongoConn("dynamic-query-test-db")
lazy val userCollection: MongoCollection = mongoDB("users")
lazy val mongoConn = MongoClient()
lazy val mongoDB = mongoConn.getDB("dynamic-query-test-db")
lazy val userCollection: DBCollection = mongoDB.getCollection("users")

lazy val parser: MongoQueryParser = new MongoQueryParser()
sequential

implicit val myContext = new BeforeAfter {

def clearCollections {
userCollection.drop()
Expand All @@ -34,7 +35,6 @@ class IntegrationTest extends Specification {
def after = {
clearCollections
}
}


def readJsonFile(config: String): String = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class MongoQueryParserTest extends Specification {

"find query with params and datatypes" in {
//Given
val query: String = "db.users.find({'name':'name#String', 'rank' : 'givenRank#Integer' , 'employed':'isEmployed#Boolean', 'salary':'sal#Long','weight' :'weight#Double'})"
val query: String = "db.users.find({'name':'name#String', 'rank' : 'givenRank#Integer' , 'employed':'isEmployed#Boolean', 'salary':'sal#Integer','weight' :'weight#Double'})"
val params: Map[String, String] = Map("name" -> "leena", "givenRank" -> "5", "isEmployed" -> "true", "sal" -> "1000", "weight" -> "2.5")
val lookup: PlaceholderLookup = new PlaceholderLookup(params)

Expand Down Expand Up @@ -151,7 +151,7 @@ class MongoQueryParserTest extends Specification {

"aggregation query " in {
//Given
val query: String = "db.users.aggregate([{'$match':{'salary' : {$gte:'from#Long',$lte:'to#Long'}}}, { $group: {_id:'$role', 'role': {$sum: '$age' } } }, { '$sort': { 'age': -1 } }, { '$limit': 5 }])"
val query: String = "db.users.aggregate([{'$match':{'salary' : {$gte:'from#Integer',$lte:'to#Integer'}}}, { $group: {_id:'$role', 'role': {$sum: '$age' } } }, { '$sort': { 'age': -1 } }, { '$limit': 5 }])"
val params: Map[String, String] = Map("from"->"1000","to" -> "2000")
val lookup: PlaceholderLookup = new PlaceholderLookup(params)

Expand All @@ -166,7 +166,7 @@ class MongoQueryParserTest extends Specification {

"aggregation query without array " in {
//Given
val query: String = "db.users.aggregate( { '$match':{'salary' : {$gte:'from#Long',$lte:'to#Long'}}}, { $group: {_id:'$role', 'role': {$sum: '$age' } } }, { '$sort': { 'age': -1 } }, { '$limit': 5 } )"
val query: String = "db.users.aggregate( { '$match':{'salary' : {$gte:'from#Integer',$lte:'to#Integer'}}}, { $group: {_id:'$role', 'role': {$sum: '$age' } } }, { '$sort': { 'age': -1 } }, { '$limit': 5 } )"
val params: Map[String, String] = Map("from"->"1000","to" -> "2000")
val lookup: PlaceholderLookup = new PlaceholderLookup(params)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class PlaceholderLookupTest extends Specification {
val lookup: PlaceholderLookup = new PlaceholderLookup(params)

val address = new BasicDBObject()
address.put("lane", 5)
address.put("lane", 5:Integer)
address.put("city", "Pune")
address.put("country", "country")
address.put("zipcode", "zip#Integer")
Expand All @@ -23,10 +23,10 @@ class PlaceholderLookupTest extends Specification {
person.put("dontreplace", "dontreplace#Long")

val expectedAddress = new BasicDBObject()
expectedAddress.put("lane", 5)
expectedAddress.put("lane", 5:Integer)
expectedAddress.put("city", "Pune")
expectedAddress.put("country", "India")
expectedAddress.put("zipcode", 411037)
expectedAddress.put("zipcode", 411037:Integer)
val expectedPerson = new BasicDBObject()
expectedPerson.put("name", "leena")
expectedPerson.put("address", address)
Expand Down
21 changes: 5 additions & 16 deletions src/test/scala/com/ee/dynamicmongoquery/QueryTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package com.ee.dynamicmongoquery

import org.specs2.mutable.Specification
import com.mongodb.BasicDBObject

import collection.JavaConversions._
import org.junit.runner.RunWith
import org.specs2.runner.JUnitRunner

import scala.collection.immutable.SortedMap
import scala.collection.mutable

@RunWith(classOf[JUnitRunner])
class QueryTest extends Specification {
"Find Queries" should {
Expand Down Expand Up @@ -62,22 +66,7 @@ class QueryTest extends Specification {
val findQuery = FindQuery(query, lookup)

//Then
val criteriaMap = Map("name" -> "leena", "rank" -> 5, "employed" -> true, "salary" -> 1000, "weight" -> 2.5)
findQuery.criteria === new BasicDBObject(criteriaMap)
findQuery.projection === new BasicDBObject()
}

"find query with params and datatypes" in {
//Given
val query: String = "{'name':'name#String', 'rank' : 'givenRank#Integer' , 'employed':'isEmployed#Boolean', 'salary':'sal#Long','weight' :'weight#Double'}"
val params: Map[String, String] = Map("name" -> "leena", "givenRank" -> "5", "isEmployed" -> "true", "sal" -> "1000", "weight" -> "2.5")
val lookup: PlaceholderLookup = new PlaceholderLookup(params)

//When
val findQuery = FindQuery(query, lookup)

//Then
val criteriaMap = Map("name" -> "leena", "rank" -> 5, "employed" -> true, "salary" -> 1000, "weight" -> 2.5)
val criteriaMap = mutable.LinkedHashMap("name" -> "leena", "rank" -> 5, "employed" -> true, "salary" -> 1000l, "weight" -> 2.5)
findQuery.criteria === new BasicDBObject(criteriaMap)
findQuery.projection === new BasicDBObject()
}
Expand Down