DRDE/AusRegCliever/server/mdJSON.cpp
Ren RenJuan c8ada0ffd4 !
2014-01-29 18:10:26 +00:00

138 lines
3.3 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, debug=true; int i=0, nCases =0;
const Json::Value suite = root["testSuite00"];
if (!suite) {
theseLogs->logN(0,"No 'testSuite00' suite root, can't run.");
return true;
}
for ( i = 0; i < suite.size(); ++i ) { const char *thisItem = suite[i].asString().c_str();
if (debug)
theseLogs->logN(1,"item %.",thisItem);
if (strncmp(thisItem,"case",4)) continue;
if (strlen(thisItem) != 6) continue;
if (debug)
theseLogs->logN(1,"case %.",thisItem);
if (!AC_OTE::theseFuncs[thisItem]) {
theseLogs->logN(0,"No logic to bind to '%s', need it.");
return false;
}
AC_OTE::theseCases[nCases].parms = &suite[i];
AC_OTE::theseCases[nCases].fBody = AC_OTE::theseFuncs[thisItem];
AC_OTE::theseCases[nCases++].caseName = thisItem;
}
while(!done) {
try {
for (i=0;i<AC_OTE::theseCases.size();i++) {
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 );
}
theseLogs->logN(0,"End OTE Session.");
theseLogs->logN(0,"Restart with toolkit scenario >= 5 for production ops.");
}
catch (...)
{
theseLogs->logN(0,"Inner JSON Execution in OTE ");
}
}
}
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: \n%s\n",
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\n", path.c_str() );
return 3;
}
return parseValueTree( input, "input", root, features );
}
catch ( const std::exception &e )
{
theseLogs->logN(1, "Unhandled exception:\n%s\n", e.what() );
value = true;
}
return value;
}
void mdJSON::setPath(char *fileName) {
path = string(fileName);
}