Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Titan » Using JSON in TTCN-3 and Titan Part II: Simple REST/JSON test framework(Using a generic JSON representation in TTCN-3)
Using JSON in TTCN-3 and Titan Part II: Simple REST/JSON test framework [message #1704963] Tue, 11 August 2015 07:10
Elemer Lelik is currently offline Elemer LelikFriend
Messages: 1120
Registered: January 2015
Senior Member
It has been mentioned in the beginning (see part I) that, TTCN-3 being strictly typed, in order to decode JSON we need to create the type which is
the equivalent of the JSON schema that could generate the JSON instance in question.

The JSON protocol module ( https://github.com/eclipse/titan.ProtocolModules.JSON_v07_2006 ) tries to circumvent this problem by describing a generic JSON structure which matches any JSON content. The protocol module uses the TEXT codec to implement the ECMA standard (see http://json.org/) which is at the basis of JSON.
This permits the following trick: the expected JSON response can be specified in the configuration file in JSON format;
both the real response received and the expected response configured are converted into TTCN-3 using the JSON protocol module and compared;
comparison can benefit from TTCN-3 's matching mechanism. The drawback is that (as JSON has no concept of optional elements)
optionality cannot be specified in the configuration file; the benefit is that the user does not have to have any TTCN-3 knowledge, yet can describe a test case only using JSON.
In the attached code of the simple REST/JSON framework the cells of the ConfigurationElements-type test suite parameter are scanned;
each cell contains information regarding the HTTP message to be sent and received in response:

{
name := "test case name",
description := "test case description",
method := "GET",
endpoint := "/title/ipsum/content/blah",
body := "",
headers := {
{
header_name := "Content-Type",
header_value := "application/json"
}
},
http_response_code := 200,
json_response := "{\"errno\": 2000, \"error\":\"Not found\"}"
}

These pieces of information are: HTTP method to be used, REST endpoint, HTTP body to be sent, expected HTTP answer code, expected JSON response, and header modifiers. The expected JSON
response can be "omit", in which case verification is disabled, an empty string "" signifying an unstructured string, or else a string with an internal JSON structure.

A default HTTP header is specified ,
header := {
{
header_name := "Host",
header_value := tsp_url&":"&int2str(tsp_HttpPort)
},
{
header_name := "Content-Length",
header_value := "0"
},
{
header_name := "Accept",
header_value := "*/*"
},
{
header_name := "User-Agent",
header_value := "curl/7.19.0 (x86_64-suse-linux-gnu) libcurl/7.19.0 OpenSSL/0.9.8h zlib/1.2.3 libidn/1.10"
}
}

which is then modified by the values entered in configuration. If the headers field in the configuration is set to {}, the default header will not be modified.
The code scans the configuration file and executes each cell one by one; sends the specified message, receives the JSON response, converts it , together with the configured response, into the internal TTCN-3 structure, performs matching and decides if the two match ; if yes, declares a pass; if no, declares a fail. Details of execution can be seen from the log. When testing different REST services, only the config file has to be adapted; there is no need to change the TTCN-3 code. If a more tailored solution is needed, the principles detailed in part I. can be used.

How to use the code:
Unpack it to a directory REST.

cd REST
cd bin../src/install.script
make

(The Makefile was generated with the options makefilegen -s -e HTTP *.ttcn *.cc *.hh)
./HTTP HTTP_jsonplaceholder.cfg


A number of sample .cfg files are attached. A separate preamble/postamble can be run before/after the test cases themselves.


Have fun

Best regards

Elemer
  • Attachment: REST.tgz
    (Size: 1.18MB, Downloaded 168 times)
Previous Topic:Protocol Buffers support in TTCN-3 Titan
Next Topic:Titan on Windows 7 with MSVC??
Goto Forum:
  


Current Time: Thu Apr 25 20:42:03 GMT 2024

Powered by FUDForum. Page generated in 0.03066 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top