Skip to content

Commit

Permalink
Merge pull request #88 from mbehr1/ut_api_volkszaehler
Browse files Browse the repository at this point in the history
Unit tests volkszaehler api_parse_exception
  • Loading branch information
r00t- committed Dec 30, 2014
2 parents 69c589d + 48e8dc0 commit fd9ec3e
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 1 deletion.
3 changes: 2 additions & 1 deletion include/api/Volkszaehler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ namespace vz {
/**
* Parses JSON encoded exception and stores describtion in err
*/
void api_parse_exception(CURLresponse response, char *err, size_t n);
friend class Volkszaehler_Test;
void api_parse_exception(CURLresponse response, char *err, size_t n);


private:
Expand Down
3 changes: 3 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ target_link_libraries(vzlogger_unit_tests
${JSON_LIBRARY}
${SML_LIBRARY}
${LIBUUID}
dl
pthread)
target_link_libraries(vzlogger_unit_tests ${CURL_STATIC_LIBRARIES} ${CURL_LIBRARIES} ${GNUTLS_LIBRARIES})

99 changes: 99 additions & 0 deletions tests/ut_api_volkszaehler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include "gtest/gtest.h"
#include "api/Volkszaehler.hpp"
// #include "api/CurlResponse.hpp"

// dirty hack until we find a better solution:
#include "../src/api/Volkszaehler.cpp"
#include "../src/Buffer.cpp"
#include "../src/Config_Options.cpp"
#include "../src/Channel.cpp"

// another dirty hack to test private functions from Volkszaehler:
namespace vz {
namespace api{
class Volkszaehler_Test
{
public:
static void api_parse_exception(vz::api::Volkszaehler &v, vz::api::CURLresponse &r,
char *&err, size_t &n){ v.api_parse_exception(r, err, n);}
static std::list<Reading> &values(Volkszaehler &v) {return v._values;}
};
}
}

Config_Options options; // this is dangerous! Needs a mocked class as this can throw exceptions!

TEST(api_Volkszaehler, api_parse_exception) {
using namespace vz::api;
CURLresponse resp;
resp.data = 0;
resp.size = 0;
char *err = new char[100];
err[0]=0;
size_t n=100;
std::list<Option> options;
options.push_front(Option("middleware", (char*)"bla_middleware"));
ReadingIdentifier::Ptr pRid;
Channel *ch = new Channel(options, std::string("bla_api"), std::string("bla_uuid"), pRid);
Channel::Ptr chp(ch);
Volkszaehler v(chp, options);

// test using empty data:
// todo bug: this writes in err even if its null!
Volkszaehler_Test::api_parse_exception(v, resp, err, n);
ASSERT_STREQ("continue", err ) << err;

// test using small size for n:
n=5;
Volkszaehler_Test::api_parse_exception(v, resp, err, n);
// todo bug: doesn't zero terminate the string! ASSERT_STREQ("conti", err ) << err;
n=100;

// test using corrupted resp data:
resp.size = 100;
resp.data = 0;
// todo: this crashes! Volkszaehler_Test::api_parse_exception(v, resp, err, n);

// test using empty json resp:
resp.data = (char*)"{}%";
resp.size = strlen(resp.data);
Volkszaehler_Test::api_parse_exception(v, resp, err, n);
ASSERT_STREQ("Missing exception", err);

// test using just exception:
resp.data = (char*)"{exception}%";
resp.size = strlen(resp.data);
Volkszaehler_Test::api_parse_exception(v, resp, err, n);
ASSERT_STREQ("quoted object property name expected", err);

// test using just "exception":
resp.data = (char*)"{\"exception\"}%";
resp.size = strlen(resp.data);
Volkszaehler_Test::api_parse_exception(v, resp, err, n);
ASSERT_STREQ("object property name separator ':' expected", err);

// test using proper "exception":
resp.data = (char*)"{\"exception\":\"1\"}%";
resp.size = strlen(resp.data);
// todo bug throws a c++ exception (null not valid) Volkszaehler_Test::api_parse_exception(v, resp, err, n);
// ASSERT_STREQ("object property name separator ':' expected", err);

// test using proper "exception" but not type/message:
resp.data = (char*)"{\"exception\": { \"type\":\"1\", \"message\":\"2\" } }%";
resp.size = strlen(resp.data);
Volkszaehler_Test::api_parse_exception(v, resp, err, n);
ASSERT_STREQ("'1': '2'", err);

// test type: "UniqueConstraintViolationException" message contains "Duplicate entry"
resp.data = (char*)"{\"exception\": { \"type\":\"UniqueConstraintViolationException\", \"message\":\"2 Duplicate entry\" } }%";
resp.size = strlen(resp.data);
// todo bug: crashes with double free if _values is empty! Volkszaehler_Test::api_parse_exception(v, resp, err, n);
//ASSERT_STREQ("'UniqueConstraintViolationException': '2 Duplicate entry'", err);
Volkszaehler_Test::values(v).push_front(Reading());
Volkszaehler_Test::api_parse_exception(v, resp, err, n);
ASSERT_TRUE(0 == Volkszaehler_Test::values(v).size());
ASSERT_STREQ("'UniqueConstraintViolationException': '2 Duplicate entry'", err);

delete [] err;
}

0 comments on commit fd9ec3e

Please sign in to comment.