Skip to content

Commit

Permalink
[DCS-722]Feature/add exists path (#53)
Browse files Browse the repository at this point in the history
* Updated curator version to be compatible with zookeeper 3.5.1-alpha (docker)

* Added existsPath to check if main path of the DAO exists

* Suggestions by @dvallejo, from the friendly people of DataGov

* Fixed
  • Loading branch information
pianista215 authored Feb 7, 2017
1 parent fe98bd3 commit e5d4098
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ trait DAOComponent[K, V, M] {
def count(): Try[Long] =
repository.count(entity)

def existsPath: Try[Boolean] =
repository.existsPath(entity)

def exists(id: K): Try[Boolean] =
repository.exists(entity, id)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ trait RepositoryComponent[K, V] {

def count(entity: String): Try[Long]

def existsPath(entity: String): Try[Boolean]

def exists(entity: String, id: K): Try[Boolean]

def create(entity: String, id: K, element: V): Try[V]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ trait ZookeeperRepositoryComponent extends RepositoryComponent[String, Array[Byt
)
}


def getNodes(entity: String): Try[Seq[String]] =
Try(curatorClient
.getChildren
Expand All @@ -68,12 +67,16 @@ trait ZookeeperRepositoryComponent extends RepositoryComponent[String, Array[Byt
.getChildren
.forPath(s"/$entity").size.toLong)

def exists(entity: String, id: String): Try[Boolean] =

override def existsPath(entity: String): Try[Boolean] =
Try(Option(curatorClient
.checkExists()
.forPath(s"/$entity/$id"))
.forPath(s"/$entity"))
).map(_.isDefined)

def exists(entity: String, id: String): Try[Boolean] =
existsPath(s"$entity/$id")

def create(entity: String, id: String, element: Array[Byte]): Try[Array[Byte]] =
Try(
curatorClient
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,103 +17,123 @@ package com.stratio.common.utils.components.dao

import com.stratio.common.utils.components.repository.DummyRepositoryComponent
import org.json4s.DefaultFormats
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.{Matchers, WordSpec}

import scala.util
import scala.util.{Success, Try}

@RunWith(classOf[JUnitRunner])
class DaoComponentTest extends WordSpec with Matchers {

trait DummyDAOComponentContext extends DummyDAOComponent {
override val dao : DAO = new GenericDAO(Option("dummy"))
override val dao: DAO = new GenericDAO(Option("dummy"))
}

trait DummyInvalidPathDAOComponentContext extends DummyDAOComponent {
override val dao: DAO = new GenericDAO(Option("invalid"))
}

"A dao component" when {
"get a value" should {

"return an option with the value if the value exists" in new DummyDAOComponentContext {
dao.get("key1") should be(Some(DummyModel("value1")))
dao.get("key1") should be(Success(Some(DummyModel("value1"))))
dao.count()
}
"return None if the value doesn't exist" in new DummyDAOComponentContext {
dao.get("keyNotFound") should be(None)
dao.get("keyNotFound") should be(Success(None))
}
}

"check the path exists" should {

"return true if the path exists" in new DummyDAOComponentContext {
dao.existsPath should matchPattern { case Success(true) => }
}

"return false if the path doesn't exists" in new DummyInvalidPathDAOComponentContext {
dao.existsPath should matchPattern { case Success(false) => }
}

}

"check if a value exists" should {

"return true if the value exists" in new DummyDAOComponentContext {
dao.exists("key1") should matchPattern {case Success(true) => }
dao.exists("key1") should matchPattern { case Success(true) => }
}

"return false if the value doesn't exist" in new DummyDAOComponentContext {
dao.exists("keyNotFound") should matchPattern {case Success(false) => }
dao.exists("keyNotFound") should matchPattern { case Success(false) => }
}
}

"create a new value" should {

"return Model if the operation is successful" in new DummyDAOComponentContext {
dao.get("keyNotFound") should be(None)
dao.create("keyNotFound", DummyModel("newValue")) should be(DummyModel("newValue"))
dao.get("keyNotFound") should be(Some(DummyModel("newValue")))
dao.get("keyNotFound") should be(Success(None))
dao.create("keyNotFound", DummyModel("newValue")) should be(Success(DummyModel("newValue")))
dao.get("keyNotFound") should be(Success(Some(DummyModel("newValue"))))
}

"return false if the operation is not successful" in new DummyDAOComponentContext {
dao.create("key1", new DummyModel("newValue")) should be(DummyModel("newValue"))
dao.create("key1", new DummyModel("newValue")) should be(Success(DummyModel("newValue")))
dao.getAll()
}
}

"remove a value" should {

"return true if the operation is successful" in new DummyDAOComponentContext {
dao.get("key1") should be(Some(DummyModel("value1")))
dao.get("key1") should be(Success(Some(DummyModel("value1"))))
dao.delete("key1")
dao.get("key1") should be(None)
dao.get("key1") should be(Success(None))
}

"return false if the operation is not successful" in new DummyDAOComponentContext {
dao.delete("keyNotFound")
dao.getAll().map(_.size) should matchPattern {case Success(3) => }
dao.getAll().map(_.size) should matchPattern { case Success(3) => }
}
}

"update a value" should {

"return true if the operation is successful" in new DummyDAOComponentContext {
dao.get("key1") should be(Some(DummyModel("value1")))
dao.get("key1") should be(Success(Some(DummyModel("value1"))))
dao.update("key1", DummyModel("newValue"))
dao.get("key1") should be(Some(DummyModel("newValue")))
dao.get("key1") should be(Success(Some(DummyModel("newValue"))))
}

"return false if the operation is not successful" in new DummyDAOComponentContext {
dao.update("keyNotFound", DummyModel("newValue"))
dao.getAll().map(_.size) should matchPattern {case Success(3) => }
dao.getAll().map(_.size) should matchPattern { case Success(3) => }
}
}

"upsert a value" should {

"return true if the operation is successful" in new DummyDAOComponentContext {
dao.upsert("key1", DummyModel("newValue"))
dao.get("key1") should be(Some(DummyModel("newValue")))
dao.get("key1") should be(Success(Some(DummyModel("newValue"))))
dao.upsert("key1", DummyModel("newValue2"))
dao.get("key1") should be(Some(DummyModel("newValue2")))
dao.get("key2") should be(None)
dao.get("key1") should be(Success(Some(DummyModel("newValue2"))))
dao.get("key2") should be(Success(None))
dao.upsert("key2", DummyModel("newValue"))
dao.get("key2") should be(Some(DummyModel("newValue")))
dao.get("key2") should be(Success(Some(DummyModel("newValue"))))
}

"return false if the operation is not successful" in new DummyDAOComponentContext {
dao.update("keyNotFound", DummyModel("newValue"))
dao.getAll().map(_.size) should matchPattern {case Success(3) => }
dao.getAll().map(_.size) should matchPattern { case Success(3) => }
}
}

"getall" should {

"return a list with all the data in the table" in new DummyDAOComponentContext {
dao.getAll() should be(List(DummyModel("value1"), DummyModel("value2"), DummyModel("value3")))
dao.getAll() should be(Success(List(DummyModel("value1"), DummyModel("value2"), DummyModel("value3"))))
}
}
}
Expand All @@ -128,14 +148,15 @@ trait DummyDAOComponent extends DAOComponent[String, String, DummyModel] with Du
implicit val formats = DefaultFormats

def entity: String = {
if(key.isEmpty || key.get.trim.isEmpty) throw new IllegalStateException("EntityName in the DAO must be defined")
if (key.isEmpty || key.get.trim.isEmpty) throw new IllegalStateException("EntityName in the DAO must be defined")
else key.get
}

override def fromVtoM[TM >: DummyModel <: DummyModel : Manifest](v: String): TM = new DummyModel(v)

override def fromMtoV[TM <: DummyModel : Manifest](m: TM): String = m.property
}

}

case class DummyModel(property: String)
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ trait DummyRepositoryComponent extends RepositoryComponent[String, String] {
def count(entity: String): Try[Long] =
Try(memoryMap.get(entity).map(_.size).getOrElse(0).toLong)


def existsPath(entity: String): Try[Boolean] =
Try(memoryMap.exists { case (key, _) =>
key == entity
})

def exists(entity: String, id: String): Try[Boolean] =
Try(memoryMap.exists { case (key, entityMap) =>
key == entity && entityMap.keys.toList.contains(id)
Expand Down

0 comments on commit e5d4098

Please sign in to comment.