Skip to content

Commit

Permalink
Merge pull request #358 from paveljandejsek/emptyNode
Browse files Browse the repository at this point in the history
Added option to send empty node in WS steps.
  • Loading branch information
paveljandejsek authored Apr 22, 2020
2 parents a0ee61c + f664a56 commit 734cf9f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
20 changes: 20 additions & 0 deletions jbehave-support-core/docs/Web-service.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ When [ClientRequest] is sent to [MYAPP] with fault:
```
Note the special `faultCode` and `faultReason` parameters that can be checked (either one of them or both can be checked).

### Sending NIL values
A special `NIL` command can be used for sending nil values:
> ```
> [$request] data for [$application]:
> | name | data |
> | Foo.validTo | {NIL} |
> ```
The above will result in something like `<Foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><validTo xsi:nil="true"/></Foo>`.
### Sending Empty values
An empty node (`<node/>`) can be sent by se setting the required node using the `NULL` command:
> ```
> [$request] data for [$application]:
> | name | data |
> | Foo.validTo | {NULL} |
> ```
The above will result in something like `<Foo><validTo/></Foo>`.
When using the `NULL` command the node gets sent with empty value, when not specifying any value at all (not specifying the key in the table) the node does not get sent at all.


---
### Custom type converters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static java.util.stream.Collectors.toList;
import static org.apache.commons.lang3.ArrayUtils.isNotEmpty;
import static org.apache.commons.lang3.ClassUtils.isAssignable;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.util.StringUtils.isEmpty;

import java.beans.PropertyDescriptor;
Expand Down Expand Up @@ -292,14 +293,14 @@ private Object resolveValue(TestContext testContext, String key, Class type) {
if (metaType != null) {
return instantiateClass(metaType);
}
if (value == null) {
value = null;
} else if (isAssignable(type, JAXBElement.class)) { //Very tricky to extract to converter
if (isAssignable(type, JAXBElement.class)) { //Very tricky to extract to converter
value = resolveJaxbElement(testContext, key, value);
} else if (value == null) {
value = instantiateClass(type);
} else if (conversionService.canConvert(value.getClass(), type)) {
value = conversionService.convert(value, type);
}
Assert.isTrue(!(value == null && isAbstract(type)), "Please specify type for " + key);
assertThat(value == null && isAbstract(type)).withFailMessage("Please specify type for " + key).isFalse();
return value;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.jbehavesupport.core.ws

import org.jbehave.core.configuration.MostUsefulConfiguration
import org.jbehave.core.model.ExamplesTable
import org.jbehave.core.steps.ParameterConverters
import org.jbehavesupport.core.TestConfig
import org.jbehavesupport.core.test.app.domain.Address
import org.jbehavesupport.core.internal.parameterconverters.ExamplesEvaluationTableConverter
import org.jbehavesupport.core.internal.parameterconverters.NullStringConverter
import org.jbehavesupport.core.test.app.oxm.AddressInfo
import org.jbehavesupport.core.test.app.oxm.NameRequest
import org.jbehavesupport.core.test.app.oxm.NameResponse
import org.junit.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.test.context.ContextConfiguration
Expand All @@ -17,6 +19,12 @@ class WebServiceTestIT extends Specification{
@Autowired
private WebServiceHandler webServiceHandler

@Autowired
private ExamplesEvaluationTableConverter converter

@Autowired
private NullStringConverter nullStringConverter

private NameRequest request

@Test
Expand Down Expand Up @@ -50,4 +58,28 @@ class WebServiceTestIT extends Specification{
addressInfoList[1].details[0] == "10"
addressInfoList[1].details[1] == "11"
}

@Test
void canFillEmptyElement() {
given:
def paramsConverters = new ParameterConverters().addConverters(nullStringConverter)
converter.setConfiguration(new MostUsefulConfiguration().useParameterConverters(paramsConverters))

def tableData =
"| name | data |\n" +
"| addressList.addressInfo.0.city | Praha |\n" +
"| cell | {NULL} |"
ExamplesTable data = converter.convertValue(tableData, ExamplesTable.class)

when:
webServiceHandler.setRequestData("NameRequest", data)
request = webServiceHandler.createRequest(NameRequest.class, null)

then:
noExceptionThrown()
request.addressList.addressInfo[0].city == "Praha"
request.cell != null
request.name == null
}

}

0 comments on commit 734cf9f

Please sign in to comment.