如果在switch语句中不存在xml节点,如何通过assert使groovy脚本失败

时间:2016-09-01 13:30:01

标签: xml groovy switch-statement soapui

这是我的剧本,

import groovy.xml.*

def xml = '''<ROOT>
<Profile>12</Profile>
<Rate>3072</Rate>
<AudioRate>0</AudioRate>
<GPS>15</GPS>
</ROOT>'''

def Xmlnodes = new XmlParser().parseText(xml)

Xmlnodes.each { setting -> 

switch (setting.name()) {
    case 'Profile': break;
    case 'Rate': break;
    case 'GPS': break;
    default: log.info "Unknown Unit Config Element: " + setting.name()
          }   
}

如何在switch语句

中不存在AudioRate节点时,如何使此脚本失败

2 个答案:

答案 0 :(得分:2)

如果要验证 XML 至少包含一个<AudioRate/>元素,则无需在switch内执行此操作。

您可以添加assert来检查 XML 中是否至少存在一个<AudioRate/>元素,如下所示:

assert Xmlnodes.'**'.findAll { it.name() == 'AudioRate' }.size() > 0

如果您有一个必需的节点名称列表,如果 XML 中没有其中一些名称,您可以使用以下代码使测试失败:

// required node names
['Profile','Rate','AudioRate','GPS'].each { nodeName ->
    assert Xmlnodes.'**'.findAll { it.name() == nodeName }.size() > 0
}

<强>更新

我认为我误解了您的要求,而不是检查您的 XML 是否至少包含所有必需的元素,而是要检查以确认 XML 仅包含允许的元素。如果是这种情况,只需在switch中添加assert false即可使用default进行操作。

Xmlnodes.each { setting ->
  switch (setting.name()) {
    case 'Profile': break;
    case 'Rate': break;
    case 'GPS': break;
    default: 
         log.info "Unknown Unit Config Element: " + setting.name()
         assert false
  }   
}

或者以更多 Groovy 方式,检查所有 XML 节点名称是否在允许列表中:

assert ['Profile','Rate','GPS'].containsAll(Xmlnodes*.name())​

更新2:

根据您在聊天中发表的评论,如果您想首先检查<Screen>的所有子节点名称(如果至少存在的话)都在允许列表中,然后检查允许的名称是否在<ROOT>节点,您可以执行以下操作:

def xml = '''<ROOT> 
    <Profile>12</Profile> 
    <Rate>3072</Rate> 
    <AudioRate>0</AudioRate> 
    <GPS>15</GPS> 
    <Screen> 
        <Video>12</Video> 
        <Rate>600</Rate> 
        <Camera>1</Camera> 
    </Screen> 
    <Screen> 
        <Devices> 
            <audioDevice>1</audioDevice> 
        </Devices> 
    </Screen>
</ROOT>'''

def Xmlnodes = new XmlParser().parseText(xml)

// <Screen> nodes exists...
if(Xmlnodes.Screen){
    // [Video, Rate, Camera, Devices] --> [Video,Rate,Camera] are from first <Screen> [Devices] are from the second <Screen>
    println Xmlnodes.Screen*.collect { it.name() }.sum() 

    // get all childnode names for all the possible <Screen> elements in one list and
    // check that all names are allowed
    assert ['Video', 'Rate', 'Camera', 'Devices'].containsAll(Xmlnodes.Screen*.collect { it.name() }.sum() )
}

// check that only allowed names are in xml
assert ['Profile','Rate','GPS','AudioRate','Screen'].containsAll(Xmlnodes*.name())

更新3:

由于您在上次评论中告诉您还要知道 XML 上未知标记的名称,因此您可以从预定义的预定义列表中执行-操作节点名称,这样其余的都是未知元素;这是我的最后一次尝试:

def Xmlnodes = new XmlParser().parseText(xml)

def screenUnknown = []

// <Screen> nodes exists...
if(Xmlnodes.Screen){
    // get all unknown elements
    screenUnknown = Xmlnodes.Screen*.collect { it.name() }.sum() - ['Video', 'Rate', 'Camera', 'Devices']
}

// get all unknown nodes in root
def rootUnknown = Xmlnodes*.name() -  ['Profile','Rate','GPS','AudioRate','Screen']

println "unknown in <Root>: ${rootUnknown}"
println "unknown in <Screen>: ${screenUnknown}"

// optionally you can make an assert to check
// that there aren't unknown elements
assert rootUnknown.size() == 0, 'THE ARE UNKNOWN ELEMENTS IN <ROOT>'
assert screenUnknown.size()  == 0,  'THE ARE UNKNOWN ELEMENTS IN <SCREEN>'

希望它有所帮助,

答案 1 :(得分:1)

也许是这样的?

...
def Xmlnodes = new XmlParser().parseText(xml)

Xmlnodes.each { setting -> 
    assert setting.name() in ['Profile','Rate','GPS'] : "Unknown Unit Config Element: ${setting.name()}"
}