blob: 26e1f960ef333e630495c1eb884581748abca450 [file] [log] [blame]
// ==================================================================================
// Copyright (c) 2000-2019 Ericsson Telecom AB AB
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v2.0
// which accompanies this distribution, and is available at
// https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
// ==================================================================================
// Contributors:
// Krisztian Gulyas - initial implementation and initial documentation
//
// File: MongoDBTest.ttcn
// Rev: R1A
// Prodnr: CNL 0
// ==================================================================================
module MongoDBTest
{
// ==============================================================================
//
// Import(s)
//
// ==============================================================================
// mongoDB prototol messages and helper functions
import from MongoDB_Types all;
import from MongoDB_Functions all;
// import basic defitions for TCP/IP communication
import from IPL4asp_Types all;
import from IPL4asp_PortType all;
import from SimpleTCP all;
// ==============================================================================
//
// Module parameter(s)
//
// ==============================================================================
// local and remote TCP host/port
modulepar { SimpleTCP.ConnectionData LocalTCPConnection, RemoteTCPConnection }
// ==============================================================================
//
// Component(s)
//
// ==============================================================================
// test component type definition
type component testComponent
{
port IPL4asp_PT testPort;
}
// ==============================================================================
//
// Additional types and functions
//
// ==============================================================================
// Wrapper function for MongoDB message length function
function IPL4asp_MsgLen(in octetstring stream, inout ro_integer args) return integer {
var integer l := MsgLen(stream)
log (" [::] TCP (mongoDB) message length: ", l);
return l;
}
// ------------------------------------------------------------------------------
// Generic function testing mongoDB wire protocol (using testComponent):
//
// 1) encoding the given mongoDB wire message
// 2) sending message (via TCP)
// 3) if necessary:
// - catching reply message
// - decoding and evaluating it
//
// parameters:
// - Msg mongoDB message template
// - expectResponse expect response message [true/false]
// ------------------------------------------------------------------------------
function testMongoDB (
in template Msg mongo_msg, // mongoDB test message template
boolean expectResponse) // expect response message [true/false]
runs on testComponent
{
var octetstring pduOut, pduIn;
@try {
// trying to encode the outgoing message
pduOut := encMsg(valueof(mongo_msg));
}
@catch(err) {
log("[!!] Unable to encode the outgoing message | error: ", err);
setverdict(fail);
}
// logging the encoded message (encoder updates some fields)
log("[=>] MongoDB message encoded and sent: ", decMsg(pduOut));
if (SimpleTCP.sendReceiveMsg (
testPort,
refers(IPL4asp_MsgLen),
LocalTCPConnection,
RemoteTCPConnection,
pduOut,
pduIn,
expectResponse)) {
if (expectResponse) {
// initalize a mongoDB query message
var Msg mongo_reply_msg;
// trying to decode the incomming message
@try {
mongo_reply_msg := decMsg(pduIn);
var JSONRecords json;
var integer error_code := bsonStream2json(mongo_reply_msg.reply_.documents.octets, json);
// check return value of bson stream conversion
if (error_code != 0) {
setverdict(fail);
}
else {
mongo_reply_msg.reply_.documents.json := json;
}
}
@catch(err) {
log("[!!] Unable to decode incomming message | error: ", err);
setverdict(fail);
}
// compare received message with reply template
if (match(mongo_reply_msg, replyMsgTemplate)) {
log("[<=] MongoDB reply message received: ", mongo_reply_msg);
setverdict(pass);
}
else {
log("[<=] Response does not match.");
setverdict(fail);
}
}
else {
setverdict(pass);
}
}
else {
// TCP communication error
setverdict(fail);
}
}
// ==============================================================================
//
// mongoDB message template definitions (based on the message definitions)
//
// ==============================================================================
// ------------------------------------------------------------------------------
// mongoDB insert template
// ------------------------------------------------------------------------------
template Msg insertMsgTemplate (
universal charstring fullConnectionName_,
integer flag_bytes_,
octetstring documents_
) :=
{
insert := {
header := { messageLength := 0, requestId := 1, responseTo := 0, opCode := OP_UNDEFINED },
flags := { bytes := flag_bytes_ },
fullCollectionName := fullConnectionName_,
documents := documents_
}
}
// ------------------------------------------------------------------------------
// mongoDB update template
// ------------------------------------------------------------------------------
template Msg updateMsgTemplate (
universal charstring fullConnectionName_,
integer flag_bytes_,
octetstring selector_,
octetstring update_
) :=
{
update := {
header := { messageLength := 0, requestId := 1, responseTo := 0, opCode := OP_UNDEFINED },
ZERO := 0,
fullCollectionName := fullConnectionName_,
flags := { bytes := flag_bytes_ },
selector := selector_,
update := update_
}
}
// ------------------------------------------------------------------------------
// mongoDB query template
// ------------------------------------------------------------------------------
template Msg queryMsgTemplate (
universal charstring fullConnectionName_,
integer flag_bytes_,
integer numberToSkip_,
integer numberToReturn_,
octetstring query_
) :=
{
query := {
header := { messageLength := 0, requestId := 1, responseTo := 0, opCode := OP_UNDEFINED},
flags := { bytes := flag_bytes_ },
fullCollectionName := fullConnectionName_,
numberToSkip := numberToSkip_,
numberToReturn := numberToReturn_,
query := query_,
returnFieldsSelector := omit
}
}
// ------------------------------------------------------------------------------
// mongoDB get more template
// ------------------------------------------------------------------------------
template Msg getMoreMsgTemplate (
universal charstring fullConnectionName_,
integer numberToReturn_,
integer cursorID_
) :=
{
getMore := {
header := { messageLength := 0, requestId := 1, responseTo := 0, opCode := OP_UNDEFINED},
ZERO := 0,
fullCollectionName := fullConnectionName_,
numberToReturn := numberToReturn_,
cursorID := cursorID_
}
}
// ------------------------------------------------------------------------------
// mongoDB delete template
// ------------------------------------------------------------------------------
template Msg deleteMsgTemplate (
universal charstring fullConnectionName_,
integer flag_bytes_,
octetstring selector_
) :=
{
delete := {
header := { messageLength := 0, requestId := 1, responseTo := 0, opCode := OP_UNDEFINED },
ZERO := 0,
fullCollectionName := fullConnectionName_,
flags := { bytes := flag_bytes_ },
selector := selector_
}
}
// ------------------------------------------------------------------------------
// mongoDB kill cursor template
// ------------------------------------------------------------------------------
// record of integer
type record of integer roInteger;
template Msg killCursorMsgTemplate (
integer numberOfCursorIDs_,
roInteger cursorID_
) :=
{
killCursor := {
header := { messageLength := 0, requestId := 1, responseTo := 0, opCode := OP_UNDEFINED },
ZERO := 0,
numberOfCursorIDs := numberOfCursorIDs_,
cursorIDs := cursorID_
}
}
// ------------------------------------------------------------------------------
// mongoDB reply message template
// ------------------------------------------------------------------------------
template Msg replyMsgTemplate :=
{
reply_ := {
header := { messageLength := ?, requestId := ?, responseTo := ?, opCode := OP_REPLY },
responseFlags := ?,
cursorID := ?,
startingFrom := ?,
numberReturned := ?,
documents := ?
}
}
// ==============================================================================
//
// Test cases
//
// ==============================================================================
// ------------------------------------------------------------------------------
// Testing MongoDB insert message (no reply message)
// ------------------------------------------------------------------------------
testcase TC_Insert(in universal charstring data) runs on testComponent
{
log("===================================================================================");
log("[::] TC: Insert message test");
map(mtc:testPort, system:testPort);
testMongoDB(
insertMsgTemplate(
"test.ttcn", // dbname.collectionname
1, // insert flags as byte
// one or more BSON documents to insert into the collection
json2bson("{ \"command\": \"insert\", \"data\": " & data & "}")
),
false);
log("");
}
// ------------------------------------------------------------------------------
// Testing MongoDB update message (no reply message)
// ------------------------------------------------------------------------------
testcase TC_Update() runs on testComponent
{
log("===================================================================================");
log("[::] TC Update message test");
map(mtc:testPort, system:testPort);
testMongoDB(
updateMsgTemplate(
"test.ttcn", // dbname.collectionname
1, // update flags as byte
// BSON document that specifies the query for selection of the document to update
json2bson("{}"),
// BSON document that specifies the update to be performed
json2bson("{ \"command\": \"update\", \"data\": \"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. \"}")
),
false);
log("");
}
// ------------------------------------------------------------------------------
// Testing MongoDB query message
// ------------------------------------------------------------------------------
testcase TC_Query() runs on testComponent
{
log("===================================================================================");
log("[::] TC: Query message test");
map(mtc:testPort, system:testPort);
testMongoDB(
queryMsgTemplate(
"test.ttcn", // dbname.collectionname
0, // query flags as byte
0, // number of documents to skip
0, // number of documents to return in the first OP_REPLY batch
// BSON document that represents the query
json2bson("{}")
),
true);
log("");
}
// ------------------------------------------------------------------------------
// Testing MongoDB get more message
// ------------------------------------------------------------------------------
testcase TC_GetMore() runs on testComponent
{
log("===================================================================================");
log("[::] TC: Get More message test");
map(mtc:testPort, system:testPort);
testMongoDB(
getMoreMsgTemplate(
"test.ttcn", // dbname.collectionname
0, // number of documents to return
0 // cursor id from OP_REPLY
),
true);
log("");
}
// ------------------------------------------------------------------------------
// Testing MongoDB delete message (no reply message)
// ------------------------------------------------------------------------------
testcase TC_Delete() runs on testComponent
{
log("===================================================================================");
log("[::] TC: Delete message test");
map(mtc:testPort, system:testPort);
testMongoDB(
deleteMsgTemplate(
"test.ttcn", // dbname.collectionname
0, // query flags as byte
// BSON document that represent the query used to select the documents to be removed.
json2bson("{\"data\": 17}")
),
false);
log("");
}
// ------------------------------------------------------------------------------
// Testing MongoDB delete message (no reply message)
// ------------------------------------------------------------------------------
testcase TC_KillCursor() runs on testComponent
{
log("===================================================================================");
log("[::] TC: KillCursor message test");
map(mtc:testPort, system:testPort);
testMongoDB(valueof(
killCursorMsgTemplate(
1, // number of cursorIDs in message
{1, 2, 3} // sequence of cursorIDs
)),
false);
log("");
}
// ==============================================================================
//
// Run the following testcases
//
// ==============================================================================
control
{
// insert 20 records
for (var integer i := 0; i < 20; i := i + 1) {
execute(TC_Insert(int2str(i)));
}
execute(TC_Update());
execute(TC_Query());
execute(TC_GetMore());
execute(TC_Delete());
execute(TC_KillCursor());
execute(TC_Query());
}
}