DRDE/AusRegCliever/server/mdJSON.cpp

154 lines
3.9 KiB
C++
Raw Normal View History

2014-01-28 18:16:48 +00:00
/*
* mdJSON.cpp
*
* Created on: Jan 28, 2014
* Author: jdaugherty
*/
2014-01-29 00:02:24 +00:00
#define MD_JSON
2014-01-28 18:16:48 +00:00
#include "cliever-md.h"
2014-01-28 19:23:02 +00:00
#include <algorithm> // sort
#include <json/json.h>
#include <stdio.h>
2014-01-29 18:10:26 +00:00
#include "mdJSON.hpp"
2014-01-28 19:23:02 +00:00
2014-01-29 18:10:26 +00:00
namespace AC_OTE {
extern testCases theseCases;
extern testFuncs theseFuncs;
}
2014-01-29 00:02:24 +00:00
using namespace std;
2014-01-28 19:23:02 +00:00
static std::string
readInputTestFile( const char *path )
{
FILE *file = fopen( path, "rb" );
if ( !file )
return std::string("");
fseek( file, 0, SEEK_END );
long size = ftell( file );
fseek( file, 0, SEEK_SET );
std::string text;
char *buffer = new char[size+1];
buffer[size] = 0;
if ( fread( buffer, 1, size, file ) == (unsigned long)size )
text = buffer;
fclose( file );
delete[] buffer;
return text;
}
2014-01-29 18:10:26 +00:00
bool mdJSON::run()
{
2014-01-29 23:57:01 +00:00
bool done=false;
int i=0, nCases =0, nThings = 0, debug=1000;
2014-01-29 18:10:26 +00:00
const Json::Value suite = root["testSuite00"];
if (!suite) {
theseLogs->logN(0,"No 'testSuite00' suite root, can't run.");
return true;
}
2014-01-29 23:57:01 +00:00
if (!suite.isObject()) {
theseLogs->logN(0,"'testSuite00' isn't an object, can't run.");
return true;
}
if (suite.empty()) {
theseLogs->logN(0,"'testSuite00' contains nothing, can't run.");
return true;
}
theseLogs->logN(1,"'testSuite00' - binding %d fields and case objects.",suite.size());
Json::Value::Members itemNames = suite.getMemberNames();
for ( i = 0; i < itemNames.size(); ++i ) { const char *thisItem;
thisItem = itemNames[i].c_str();
if (debug > 100)
theseLogs->logN(1,"item %s.",thisItem);
2014-01-29 18:10:26 +00:00
if (strncmp(thisItem,"case",4)) continue;
if (strlen(thisItem) != 6) continue;
2014-01-29 23:57:01 +00:00
if (debug > 100)
theseLogs->logN(1,"case %s.",thisItem);
2014-01-29 18:10:26 +00:00
if (!AC_OTE::theseFuncs[thisItem]) {
2014-01-29 23:57:01 +00:00
theseLogs->logN(1,"No logic to bind to '%s', need it.",thisItem);
2014-01-29 18:10:26 +00:00
return false;
}
2014-01-29 23:57:01 +00:00
if (!suite[i].isObject()) {
theseLogs->logN(1,"'%s' isn't an object, can't use.",thisItem);
continue;
}
2014-01-29 18:10:26 +00:00
AC_OTE::theseCases[nCases].parms = &suite[i];
AC_OTE::theseCases[nCases].fBody = AC_OTE::theseFuncs[thisItem];
AC_OTE::theseCases[nCases++].caseName = thisItem;
}
2014-01-29 23:57:01 +00:00
theseLogs->logN(1,"%d cases bound.",nCases);
2014-01-29 18:10:26 +00:00
2014-01-29 23:57:01 +00:00
for (i=0;i<AC_OTE::theseCases.size();i++) {
try{
2014-01-29 18:10:26 +00:00
theseLogs->logN(2,"%d (%s) Begin ",i,AC_OTE::theseCases[i].caseName );
AC_OTE::theseCases[i].fBody();
theseLogs->logN(2,"%d (%s) End ",i,AC_OTE::theseCases[i].caseName );
}
2014-01-29 23:57:01 +00:00
catch (...)
{
theseLogs->logN(1,"Inner JSON Exception in %s ",AC_OTE::theseCases[i].caseName );
}
}
theseLogs->logN(0,"Suite 'testSuite00' end execution.");
2014-01-29 18:10:26 +00:00
}
2014-01-28 19:23:02 +00:00
static bool
2014-01-29 00:02:24 +00:00
parseValueTree( const std::string &input, const std::string &kind, Json::Value &root, const Json::Features &features)
2014-01-28 19:23:02 +00:00
{
Json::Reader reader( features );
bool parsingSuccessful = reader.parse( input, root );
if ( !parsingSuccessful )
{
2014-01-29 23:57:01 +00:00
theseLogs->logN(2, "Failed to parse %s file: %s",
2014-01-28 19:23:02 +00:00
kind.c_str(),
reader.getFormattedErrorMessages().c_str() );
return true;
}
return false;
2014-01-28 18:16:48 +00:00
}
2014-01-29 00:02:24 +00:00
bool mdJSON::parse()
2014-01-28 19:23:02 +00:00
{
bool value = false;
Json::Features features;
try
{
std::string input = readInputTestFile( path.c_str() );
if ( input.empty() )
{
2014-01-29 23:57:01 +00:00
theseLogs->logN(1, "Failed to read input or empty input: %s", path.c_str() );
2014-01-28 19:23:02 +00:00
return 3;
}
2014-01-29 00:02:24 +00:00
return parseValueTree( input, "input", root, features );
2014-01-28 19:23:02 +00:00
}
catch ( const std::exception &e )
{
2014-01-29 23:57:01 +00:00
theseLogs->logN(1, "Unhandled exception: %s", e.what() );
2014-01-28 19:23:02 +00:00
value = true;
}
return value;
2014-01-28 18:16:48 +00:00
}
2014-01-29 18:10:26 +00:00
void mdJSON::setPath(char *fileName) {
2014-01-28 19:23:02 +00:00
2014-01-29 18:10:26 +00:00
path = string(fileName);
2014-01-28 18:16:48 +00:00
}