/* **************************************************************************** * * json_ok - */ TEST(NotifyContextRequest, json_ok) { ParseData reqData; ConnectionInfo ci("", "POST", "1.1"); NotifyContextRequest* ncrP = &reqData.ncr.res; const char* infile = "notifyContextRequest_ok.json"; const char* outfile = "ngsi10.notifyContextRequest_ok.expected1.valid.json"; std::string rendered; utInit(); ci.outMimeType = JSON; EXPECT_EQ("OK", testDataFromFile(testBuf, sizeof(testBuf), infile)) << "Error getting test data from '" << infile << "'"; ci.inMimeType = JSON; ci.outMimeType = JSON; lmTraceLevelSet(LmtDump, true); std::string result = jsonTreat(testBuf, &ci, &reqData, NotifyContext, NULL); EXPECT_EQ("OK", result); lmTraceLevelSet(LmtDump, false); // // With the data obtained, render, present and release methods are exercised // std::vector<std::string> emptyV; EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile)) << "Error getting test data from '" << outfile << "'"; rendered = ncrP->toJsonV1(false, emptyV, false, emptyV); EXPECT_STREQ(expectedBuf, rendered.c_str()); ncrP->release(); utExit(); }
/* **************************************************************************** * * xml_ok - */ TEST(NotifyContextRequest, xml_ok) { ParseData reqData; ConnectionInfo ci("", "POST", "1.1"); std::string rendered; const char* infile = "ngsi10.notifyContextRequest.ok.valid.xml"; const char* outfile = "ngsi10.notifyContextResponse.ok.valid.xml"; NotifyContextRequest* ncrP = &reqData.ncr.res; utInit(); ci.outFormat = XML; EXPECT_EQ("OK", testDataFromFile(testBuf, sizeof(testBuf), infile)) << "Error getting test data from '" << infile << "'"; lmTraceLevelSet(LmtDump, true); std::string result = xmlTreat(testBuf, &ci, &reqData, NotifyContext, "notifyContextRequest", NULL); EXPECT_EQ("OK", result); lmTraceLevelSet(LmtDump, false); ncrP->present(""); rendered = ncrP->render(&ci, NotifyContext, ""); EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile)) << "Error getting test data from '" << outfile << "'"; EXPECT_STREQ(expectedBuf, rendered.c_str()); ncrP->present(""); ncrP->release(); utExit(); }
/* **************************************************************************** * * predetectedError - */ TEST(NotifyContextRequest, predetectedError) { NotifyContextRequest ncr; const char* outfile = "ngsi10.notifyContextResponse.predetectedError.valid.xml"; std::string out; ConnectionInfo ci(XML); utInit(); EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), outfile)) << "Error getting test data from '" << outfile << "'"; out = ncr.check(&ci, NotifyContext, "", "predetected error", 0); EXPECT_STREQ(expectedBuf, out.c_str()); utExit(); }
/* **************************************************************************** * * json_render - */ TEST(NotifyContextRequest, json_render) { const char* filename1 = "ngsi10.notifyContextRequest.jsonRender1.valid.json"; const char* filename2 = "ngsi10.notifyContextRequest.jsonRender2.valid.json"; const char* filename3 = "ngsi10.notifyContextRequest.jsonRender3.valid.json"; NotifyContextRequest* ncrP; ContextElementResponse* cerP; std::string rendered; utInit(); // Preparation ncrP = new NotifyContextRequest(); ncrP->subscriptionId.set("012345678901234567890123"); ncrP->originator.set("http://www.tid.es/NotifyContextRequestUnitTest"); std::vector<std::string> emptyV; // 1. Without ContextResponseList EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), filename1)) << "Error getting test data from '" << filename1 << "'"; rendered = ncrP->toJsonV1(false, emptyV, false, emptyV); EXPECT_STREQ(expectedBuf, rendered.c_str()); // 2. With ContextResponseList cerP = new ContextElementResponse(); cerP->entity.fill("E01", "EType", "false"); ncrP->contextElementResponseVector.push_back(cerP); cerP->statusCode.fill(SccOk); EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), filename2)) << "Error getting test data from '" << filename2 << "'"; rendered = ncrP->toJsonV1(false, emptyV, false, emptyV); EXPECT_STREQ(expectedBuf, rendered.c_str()); // 3. ContextResponseList with two instances cerP = new ContextElementResponse(); cerP->entity.fill("E02", "EType", "false"); ncrP->contextElementResponseVector.push_back(cerP); cerP->statusCode.fill(SccOk); EXPECT_EQ("OK", testDataFromFile(expectedBuf, sizeof(expectedBuf), filename3)) << "Error getting test data from '" << filename3 << "'"; rendered = ncrP->toJsonV1(false, emptyV, false, emptyV); EXPECT_STREQ(expectedBuf, rendered.c_str()); utExit(); }
/* **************************************************************************** * * templateNotify - * * This function performs the necessary substitutions according to the template of * subscription to form the desired notification and send it to the endpoint specified * in the subscription. * * */ static bool templateNotify ( const SubscriptionId& subscriptionId, const ContextElement& ce, const ngsiv2::HttpInfo& httpInfo, const std::string& tenant, const std::string& xauthToken, const std::string& fiwareCorrelator, RenderFormat renderFormat, const std::vector<std::string>& attrsOrder ) { Verb verb = httpInfo.verb; std::string method; std::string url; std::string payload; std::string mimeType; std::map<std::string, std::string> qs; std::map<std::string, std::string> headers; // // 1. Verb/Method // if (verb == NOVERB) { // Default verb/method is POST verb = POST; } method = verbName(verb); // // 2. URL // macroSubstitute(&url, httpInfo.url, ce); // // 3. Payload // if (httpInfo.payload == "") { NotifyContextRequest ncr; ContextElementResponse cer; cer.contextElement = ce; ncr.subscriptionId = subscriptionId; ncr.contextElementResponseVector.push_back(&cer); payload = ncr.toJson(renderFormat, attrsOrder); mimeType = "application/json"; } else { macroSubstitute(&payload, httpInfo.payload, ce); char* pload = curl_unescape(payload.c_str(), payload.length()); payload = pload; renderFormat = NGSI_V2_CUSTOM; mimeType = "text/plain"; // May be overridden by 'Content-Type' in 'headers' free(pload); } // // 4. URI Params (Query Strings) // for (std::map<std::string, std::string>::const_iterator it = httpInfo.qs.begin(); it != httpInfo.qs.end(); ++it) { std::string key = it->first; std::string value = it->second; macroSubstitute(&key, it->first, ce); macroSubstitute(&value, it->second, ce); if ((value == "") || (key == "")) { // To avoid e.g '?a=&b=&c=' continue; } qs[key] = value; } // // 5. HTTP Headers // for (std::map<std::string, std::string>::const_iterator it = httpInfo.headers.begin(); it != httpInfo.headers.end(); ++it) { std::string key = it->first; std::string value = it->second; macroSubstitute(&key, it->first, ce); macroSubstitute(&value, it->second, ce); if (key == "") { // To avoid empty header name continue; } headers[key] = value; } // // 6. Split URI in protocol, host, port and path // std::string protocol; std::string host; int port; std::string uriPath; if (!parseUrl(url, host, port, uriPath, protocol)) { LM_E(("Runtime Error (not sending NotifyContextRequest: malformed URL: '%s')", httpInfo.url.c_str())); return false; } // // 7. Add URI params from template to uriPath // std::string uri = uriPath; if (qs.size() != 0) { uri += "?"; int ix = 0; for (std::map<std::string, std::string>::iterator it = qs.begin(); it != qs.end(); ++it) { if (ix != 0) { uri += "&"; } uri += it->first + "=" + it->second; ++ix; } } // // 8. Send the request // // NOTE: the HTTP headers are sent to httpRequestSend via parameter 'extraHeaders' // std::string out; int r; r = httpRequestSend(host, port, protocol, method, tenant, ce.entityId.servicePath, xauthToken, uri, mimeType, payload, fiwareCorrelator, renderFormatToString(renderFormat), true, // Use Rush if CLI '--rush' allows it true, // wait for response &out, headers, "application/json", // Accept Format -1); // Timeout in milliseconds, depends on CLI '-httpTimeout' if (r == 0) { statisticsUpdate(NotifyContextSent, JSON); alarmMgr.notificationErrorReset(url); return true; } return false; }