Petals-BC-REST 1.1.0+

compared with
Key
This line was removed.
This word was removed. This word was added.
This line was added.

Changes (1)

View Page History

No dedicated alert is available.

{anchor:Unit_Testing}
h1. Unit testing

The unit testing can occur at several levels in your Rest service unit:
* on the provider side:
** to check the operation definition in the JBI descriptor
** to check the compliance of the JBI descriptor with the WSDL content,
** to unit test your XSL transformations.

A dedicated framework is available as an extension of JUnit providing facilities:
* to validate your WSDL:
** in a WSDL point of view,
** checking the compliance of the JBI descriptor with the WSDL content,
* to validate your JBI descriptor:
** checking syntax of the XPath expressions,
* to verify generated response from HTTP response.

This dedicated framework is provided by the Maven artifact {{org.ow2.petals:petals-bc-rest-junit}}:
{code:xml}
<project>
...
<dependencies>
...
<dependency>
<groupId>org.ow2.petals</groupId>
<artifactId>petals-bc-rest-junit</artifactId>
<version>1.0.0</version>
<scope>test</scope>
</dependency>
...
</dependencies>
...
</project>
{code}

{warning}The version 1.0.0+ of the framework is compliant with the Petals BC REST 1.1.0+.{warning}

h2. Checking the compliance of the JBI descriptor with WSDL content

The unit test framework contains an assertion '{{assertWsdlCompliance}}' to verify easily the compliance of your JBI descriptor with the WSDL content. As the WSDL is parsed, it's syntax is also verified:
{code:lang=java}
import static org.ow2.petals.binding.rest.junit.Assert.assertWsdlCompliance;

public class WsdlValidatorTest {

@Test
public void validateJbiAgainstWsdl() throws Exception {

assertWsdlCompliance();
}

}
{code}

The WSDL file to verify is extracted from the JBI descriptor content.

h2. Unit-testing your operation definitions

The unit test framework contains several assertions to verify transformation of HTTP response:
* {{assertJBIOutResponse}}: The result of the HTTP response is expected as an OUT message similar to the given resource,
* {{assertJBIFault}}: The result of the HTTP response is expected as a fault similar to the given resource.

{code:lang=java}
import static org.ow2.petals.binding.rest.junit.Assert.assertJBIFault;
import static org.ow2.petals.binding.rest.junit.Assert.assertJBIOutResponse;
...
import org.ow2.petals.binding.rest.junit.rule.ServiceOperationUnderTest;
...

public class GetMetadataByJsonOpTest {

@ClassRule
public static final ServiceOperationUnderTest OPERATION_UNDER_TEST = new ServiceOperationUnderTest(
OP_GET_METADATA_BY_JSON);

/**
* Check the JBI response of operation {@value OP_GET_METADATA_BY_JSON} when a REST response is returned with HTTP
* status 200 and contains metadatas.
*/
@Test
public void nominal() throws Exception {
assertJBIOutResponse("get-metadata-expected-nominal-response.xml", OPERATION_UNDER_TEST,
HttpStatus.getCode(HttpStatus.OK_200), "get-metadata-nominal-http-body.json",
MediaType.APPLICATION_JSON_TYPE, null);
}

/**
* Check the JBI response of operation {@value OP_GET_METADATA_BY_JSON} when a REST response is returned with HTTP
* status 404 (unexisting document)
*/
@Test
public void unexistingDocument() throws Exception {

assertJBIFault("get-metadata-expected-fault-unexisting-document.xml", OPERATION_UNDER_TEST,
HttpStatus.getCode(HttpStatus.NOT_FOUND_404), null, null, "get-metadata-by-json-request.xml", null);
}
}
{code}

In the service unit JBI descriptor, the response transformations are described for each operation. The JUnit rule {{ServiceOperationUnderTest}} represents one of these operations, and the response transformation processing is the same than the one of the component. This JUnit rule is used to execute the transformation on a given HTTP response through assertions {{assertJBIOutResponse}} or {{assertJBIFault}}. For each assertion, the transformation result is compared to the content of the given resource. Following part of the HTTP response used as input of the transformation can be set:
* the HTTP status,
* a resource name containing the HTTP body,
* the content type of the HTTP body,
* a resource name containing the JBI IN message payload,
* URI parameters.

See the Javadoc for more details on parameters.