This repository has been archived by the owner on Mar 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
229 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 108 additions & 4 deletions
112
src/main/scala/de/retest/guistatemachine/api/neo4j/ActionConverter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,113 @@ | ||
package de.retest.guistatemachine.api.neo4j | ||
|
||
import de.retest.surili.commons.actions.{Action, NavigateRefreshAction} | ||
import de.retest.recheck.ui.descriptors.{Element, SutState} | ||
import de.retest.surili.commons.actions._ | ||
import org.neo4j.ogm.typeconversion.AttributeConverter | ||
|
||
class ActionConverter extends AttributeConverter[Action, String] { | ||
def toGraphProperty(value: Action): String = value.toString // TODO #19 convert to XML with element | ||
def toEntityAttribute(value: String): Action = new NavigateRefreshAction // TODO #19 convert from XML to an action | ||
import scala.xml._ | ||
|
||
/** | ||
* We do not want to store the whole target element as XML again. Hence, we store its retest ID which is unique and | ||
* matches the element in the SUT state and only the additional attributes. | ||
*/ | ||
class ActionConverter(val sutState: Option[SutState]) extends AttributeConverter[Action, String] { | ||
|
||
def this() = this(None) | ||
|
||
def toGraphProperty(value: Action): String = { | ||
val nodeBuffer = new NodeBuffer | ||
|
||
nodeBuffer += <type>{value.getClass.getSimpleName}</type> | ||
|
||
if (value.getTargetElement.isPresent) { | ||
val retestId = value.getTargetElement.get().getRetestId | ||
nodeBuffer += <retestId>{retestId}</retestId> | ||
} | ||
|
||
value match { | ||
case a: ChangeValueOfAction => | ||
val sequences = a.getKeysToSend map { sequence => | ||
<sequence>{sequence.toString}</sequence> | ||
} | ||
nodeBuffer += <keys>{sequences}</keys> | ||
case a: NavigateToAction => | ||
nodeBuffer += <url>{a.getUrl}</url> | ||
case a: SwitchToWindowAction => | ||
nodeBuffer += <window>{a.getWindowName}</window> | ||
case _ => | ||
} | ||
|
||
val topLevelNode = <action>{nodeBuffer}</action> | ||
val stringBuilder = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") | ||
val prettyPrinter = new PrettyPrinter(0, 2) | ||
prettyPrinter.formatNodes(topLevelNode, TopScope, stringBuilder) | ||
stringBuilder.toString() | ||
} | ||
def toEntityAttribute(value: String): Action = sutState match { | ||
case Some(state) => | ||
val node = scala.xml.XML.loadString(value) | ||
val typeNode = getNodeByTag(node, "type") | ||
typeNode.text match { | ||
case "ChangeValueOfAction" => | ||
val element = getElement(node, state) | ||
val keys = getNodeByTag(node, "keys") | ||
val sequences = keys.child map { c => | ||
c.text | ||
} | ||
new ChangeValueOfAction(element, sequences.toArray) | ||
case "ClickOnAction" => | ||
val element = getElement(node, state) | ||
new ClickOnAction(element) | ||
case "NavigateToAction" => | ||
val urlNode = getNodeByTag(node, "url") | ||
new NavigateToAction(urlNode.text) | ||
case "NavigateBackAction" => new NavigateBackAction | ||
case "NavigateForwardAction" => new NavigateForwardAction | ||
case "NavigateRefreshAction" => new NavigateRefreshAction | ||
case "SwitchToWindowAction" => | ||
val windowNode = getNodeByTag(node, "window") | ||
new SwitchToWindowAction(windowNode.text) | ||
case _ => throw new RuntimeException("Unknown type.") | ||
} | ||
|
||
case None => throw new RuntimeException("We need the SutState to reconstruct the action") | ||
} | ||
|
||
private def getNodeByTag(node: Node, tag: String): Node = { | ||
val matchingNodes = node \\ tag | ||
if (matchingNodes.isEmpty) { | ||
throw new RuntimeException(s"Missing node with tag $tag.") | ||
} else { | ||
matchingNodes.head | ||
} | ||
} | ||
|
||
private def getElement(node: Node, sutState: SutState): Element = { | ||
val retestId = getNodeByTag(node, "retestId").text | ||
getElementByRetestId(retestId, sutState) match { | ||
case Some(element) => element | ||
case None => throw new RuntimeException(s"Missing element with retestId $retestId") | ||
} | ||
} | ||
|
||
private def getElementByRetestId(retestId: String, sutState: SutState): Option[Element] = { | ||
val elements = scala.collection.mutable.Set[Element]() | ||
val iterator = sutState.getRootElements.iterator() | ||
var result: Option[Element] = None | ||
|
||
while (iterator.hasNext && result.isEmpty) { | ||
val element = iterator.next() | ||
result = if (element.getRetestId == retestId) { | ||
Some(element) | ||
} else { None } | ||
|
||
val nestedIterator = element.getContainedElements.iterator() | ||
|
||
while (nestedIterator.hasNext) { | ||
elements += nestedIterator.next() | ||
} | ||
} | ||
|
||
result | ||
} | ||
} |
11 changes: 7 additions & 4 deletions
11
src/main/scala/de/retest/guistatemachine/api/neo4j/ActionTransitionEntity.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
src/test/scala/de/retest/guistatemachine/api/neo4j/ActionConverterSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package de.retest.guistatemachine.api.neo4j | ||
|
||
import java.util | ||
|
||
import de.retest.guistatemachine.api.AbstractApiSpec | ||
import de.retest.surili.commons.actions._ | ||
import org.scalatest.BeforeAndAfterEach | ||
|
||
class ActionConverterSpec extends AbstractApiSpec with BeforeAndAfterEach { | ||
private val rootElement = getRootElement("a", 0) | ||
private val sutState = createSutState(rootElement) | ||
private val cut = new ActionConverter(Some(sutState)) | ||
|
||
"ActionConverter" should { | ||
|
||
"convert ChangeValueOfAction" in { | ||
val list = util.Arrays.asList("foo", "bar", "waa") | ||
val cs = list.toArray(new Array[CharSequence](list.size)) | ||
val action = new ChangeValueOfAction(rootElement, cs) | ||
|
||
val result = cut.toGraphProperty(action) | ||
result shouldEqual | ||
"""<?xml version="1.0" encoding="UTF-8"?> | ||
|<action><type>ChangeValueOfAction</type><retestId>retestId</retestId><keys><sequence>foo</sequence><sequence>bar</sequence><sequence>waa</sequence></keys></action> | ||
|""".stripMargin | ||
|
||
val loadedAction = cut.toEntityAttribute(result) | ||
loadedAction shouldEqual action | ||
} | ||
|
||
"convert ClickOnAction" in { | ||
val action = new ClickOnAction(rootElement) | ||
|
||
val result = cut.toGraphProperty(action) | ||
result shouldEqual | ||
"""<?xml version="1.0" encoding="UTF-8"?> | ||
|<action><type>ClickOnAction</type><retestId>retestId</retestId></action> | ||
|""".stripMargin | ||
|
||
val loadedAction = cut.toEntityAttribute(result) | ||
loadedAction shouldEqual action | ||
} | ||
|
||
"convert NavigateToAction" in { | ||
val action = new NavigateToAction("http://google.com") | ||
|
||
val result = cut.toGraphProperty(action) | ||
result shouldEqual | ||
"""<?xml version="1.0" encoding="UTF-8"?> | ||
|<action><type>NavigateToAction</type><url>http://google.com</url></action> | ||
|""".stripMargin | ||
|
||
val loadedAction = cut.toEntityAttribute(result) | ||
loadedAction shouldEqual action | ||
} | ||
|
||
"convert NavigateBackAction" in { | ||
val action = new NavigateBackAction() | ||
|
||
val result = cut.toGraphProperty(action) | ||
result shouldEqual | ||
"""<?xml version="1.0" encoding="UTF-8"?> | ||
|<action><type>NavigateBackAction</type></action> | ||
|""".stripMargin | ||
|
||
val loadedAction = cut.toEntityAttribute(result) | ||
loadedAction shouldEqual action | ||
} | ||
|
||
"convert NavigateForwardAction" in { | ||
val action = new NavigateForwardAction() | ||
|
||
val result = cut.toGraphProperty(action) | ||
result shouldEqual | ||
"""<?xml version="1.0" encoding="UTF-8"?> | ||
|<action><type>NavigateForwardAction</type></action> | ||
|""".stripMargin | ||
|
||
val loadedAction = cut.toEntityAttribute(result) | ||
loadedAction shouldEqual action | ||
} | ||
|
||
"convert NavigateRefreshAction" in { | ||
val action = new NavigateRefreshAction() | ||
|
||
val result = cut.toGraphProperty(action) | ||
result shouldEqual | ||
"""<?xml version="1.0" encoding="UTF-8"?> | ||
|<action><type>NavigateRefreshAction</type></action> | ||
|""".stripMargin | ||
|
||
val loadedAction = cut.toEntityAttribute(result) | ||
loadedAction shouldEqual action | ||
} | ||
|
||
"convert SwitchToWindowAction" in { | ||
val action = new SwitchToWindowAction("test") | ||
|
||
val result = cut.toGraphProperty(action) | ||
result shouldEqual | ||
"""<?xml version="1.0" encoding="UTF-8"?> | ||
|<action><type>SwitchToWindowAction</type><window>test</window></action> | ||
|""".stripMargin | ||
|
||
val loadedAction = cut.toEntityAttribute(result) | ||
loadedAction shouldEqual action | ||
} | ||
} | ||
} |