DRDE/AusRegCliever/server/mdJSON.cpp

154 lines
3.9 KiB
C++

/*
* mdJSON.cpp
*
* Created on: Jan 28, 2014
* Author: jdaugherty
*/
#define MD_JSON
#include "cliever-md.h"
#include <algorithm> // sort
#include <json/json.h>
#include <stdio.h>
#include "mdJSON.hpp"
namespace AC_OTE {
extern testCases theseCases;
extern testFuncs theseFuncs;
}
using namespace std;
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;
}
bool mdJSON::run()
{
bool done=false;
int i=0, nCases =0, nThings = 0, debug=1000;
const Json::Value suite = root["testSuite00"];
if (!suite) {
theseLogs->logN(0,"No 'testSuite00' suite root, can't run.");
return true;
}
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);
if (strncmp(thisItem,"case",4)) continue;
if (strlen(thisItem) != 6) continue;
if (debug > 100)
theseLogs->logN(1,"case %s.",thisItem);
if (!AC_OTE::theseFuncs[thisItem]) {
theseLogs->logN(1,"No logic to bind to '%s', need it.",thisItem);
return false;
}
if (!suite[i].isObject()) {
theseLogs->logN(1,"'%s' isn't an object, can't use.",thisItem);
continue;
}
AC_OTE::theseCases[nCases].parms = &suite[i];
AC_OTE::theseCases[nCases].fBody = AC_OTE::theseFuncs[thisItem];
AC_OTE::theseCases[nCases++].caseName = thisItem;
}
theseLogs->logN(1,"%d cases bound.",nCases);
for (i=0;i<AC_OTE::theseCases.size();i++) {
try{
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 );
}
catch (...)
{
theseLogs->logN(1,"Inner JSON Exception in %s ",AC_OTE::theseCases[i].caseName );
}
}
theseLogs->logN(0,"Suite 'testSuite00' end execution.");
}
static bool
parseValueTree( const std::string &input, const std::string &kind, Json::Value &root, const Json::Features &features)
{
Json::Reader reader( features );
bool parsingSuccessful = reader.parse( input, root );
if ( !parsingSuccessful )
{
theseLogs->logN(2, "Failed to parse %s file: %s",
kind.c_str(),
reader.getFormattedErrorMessages().c_str() );
return true;
}
return false;
}
bool mdJSON::parse()
{
bool value = false;
Json::Features features;
try
{
std::string input = readInputTestFile( path.c_str() );
if ( input.empty() )
{
theseLogs->logN(1, "Failed to read input or empty input: %s", path.c_str() );
return 3;
}
return parseValueTree( input, "input", root, features );
}
catch ( const std::exception &e )
{
theseLogs->logN(1, "Unhandled exception: %s", e.what() );
value = true;
}
return value;
}
void mdJSON::setPath(char *fileName) {
path = string(fileName);
}