Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add some tests #1

Merged
merged 1 commit into from

3 participants

@ElPicador

Add some tests to QuickSearchService.scala, method lookup.

Thanks to Arnaud Bailly, Alexis Agahi, Rand Hindi, Jonathan Clark, Nicolas Perron and Nicolas Charles

@Normation-Gitmerger
Collaborator
@fanf fanf merged commit d7637d8 into from
@fanf fanf referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 27, 2012
  1. @ElPicador

    First level of refactoring

    ElPicador authored
This page is out of date. Refresh to see the latest.
View
41 rudder-core/src/main/scala/com/normation/rudder/services/nodes/QuickSearchService.scala
@@ -95,19 +95,28 @@ class QuickSearchServiceImpl(
*/
def lookup(in:String, maxResult:Int) : Box[Seq[NodeInfo]] = {
- if(validateInput(in)) {
- for {
+ if(validateInput(in))
+ searchNodesInLdap(in, maxResult)
+ else
+ Full(Seq())
+ }
+
+ def searchNodesInLdap(in : String, maxResult: Int) : Box[Seq[NodeInfo]] = {
+ for {
con <- ldap
+
nodeEntries <- search(con, nodeDit.NODES.dn, maxResult, nodeFilter(in), nodeInfoAttributes)
nodePairs <- { sequence(nodeEntries) { e =>
nodeDit.NODES.NODE.idFromDn(e.dn).map(id => (id,e))
} }.map( _.toMap )
nodeIdSet1 = nodePairs.keySet
+
serverEntries <- search(con, inventoryDit.NODES.dn, maxResult, serverFilter(in, nodeIdSet1), nodeInfoAttributes)
serverPairs <- { sequence(serverEntries) { e =>
e(A_NODE_UUID).map(id => (NodeId(id),e))
} }.map( _.toMap )
nodeIdSet2 = serverPairs.keySet
+
//now, we have to find back entries missing from nodes but in server
filter = OR( (nodeIdSet2 -- nodeIdSet1).map(id => EQ(A_NODE_UUID,id.value) ).toSeq:_* )
missingNodeEntries <- search(con, nodeDit.NODES.dn, 0, filter, nodeInfoAttributes)
@@ -116,21 +125,27 @@ class QuickSearchServiceImpl(
} }.map( _.toMap )
//now, map to node info
val allNodePairs = nodePairs ++ missingNodePairs
- nodeInfos <- sequence( serverPairs.toSeq ) { case(id,serverEntry) =>
- for {
- nodeEntry <- Box(allNodePairs.get(id)) ?~! "Missing required node entry with id %s".format(id)
- nodeInfos <- mapper.convertEntriesToNodeInfos(nodeEntry, serverEntry)
- } yield {
- nodeInfos
- }
- }
+
+ nodeInfos <- matchNodeInfoPairs( serverPairs, allNodePairs )
} yield {
nodeInfos
}
- } else Full(Seq())
}
-
+
+def matchNodeInfoPairs( serverPairs:Map[NodeId,LDAPEntry], allNodePairs:Map[NodeId,LDAPEntry] ): Box[Seq[NodeInfo]] = {
+ sequence( allNodePairs.toSeq ) {
+ case(id,nodeEntry) =>
+ for {
+ serverEntry <- Box(serverPairs.get(id)) ?~! "Missing required node entry with id %s".format(id)
+ nodeInfos <- mapper.convertEntriesToNodeInfos(nodeEntry, serverEntry)
+ } yield {
+ nodeInfos
+ }
+ }
+}
+
+
//filter for ou=nodes based on attributes to look for in quicksearch
private[this] def nodeFilter(in:String) = OR( nodeAttributes.map(attr => SUB(attr, null, Array(in), null) ) :_* )
//filter for ou=severs in inventory based on attributes to look for in quicksearch
@@ -182,7 +197,7 @@ class QuickSearchServiceImpl(
*/
private[this] def validateInput(in:String) : Boolean = {
in.trim match {
- case null | "" => false
+ case "" => false
case s if(s.length < 2) => false
case _ => true
}
View
41 rudder-core/src/test/scala/com/normation/rudder/services/nodes/TestQuickSearchService.scala
@@ -82,22 +82,37 @@ class TestQuickSearchService extends QuickSearchServiceSpec {
*
*/
-// "yield 8 results" in {
-//
-// quickSearch.lookup("node", 100) match {
-// case eb:EmptyBox =>
-// val e = eb ?~! "test1 failed"
-// e.exceptionChain.foreach( t => logger.debug(t) )
-// failure(e.messageChain)
-// case Full(res) => res must have size(8)
-// }
-//
-// }
-
+ "ignore superfluous server entries" in {
+
+ quickSearch.lookup("node", 100) match {
+ case eb:EmptyBox =>
+ val e = eb ?~! "test1 failed"
+ e.exceptionChain.foreach( t => logger.debug(t) )
+ failure(e.messageChain)
+ case Full(res) => res must have size(8)
+ }
+
+ }
+ "not matchsuperfluous server entries" in {
+
+ quickSearch.lookup("node0_0", 100) match {
+ case Full(res) => res must have size(0)
+ }
+
+ }
+
+
}
- "test2: todo" should { "succeed" in success }
+ "" should { "succeed" in success }
+
+ "when entry is invalid" should {
+ "return an empty sequence" in {
+ quickSearch.lookup("", 100) must beEqualTo(Full(Seq()))
+ }
+
+ }
}
Something went wrong with that request. Please try again.