void test_profile_view(){
  HTTPClientSession s(HOST, PORT);
  HTTPRequest request(HTTPRequest::HTTP_GET, std::string("/api/v1/profile/").append(created_profile_uuid));
  s.sendRequest(request);
  HTTPResponse response;
  std::istream& rs = s.receiveResponse(response);

  std::string data;
  StreamCopier::copyToString(rs, data);
  LOGDEBUG("response:"<<data);
  JSONNode node = getJson(data);
  assert_response(node);  

  assert(node.contains("data"));
  assert(node["data"].contains("format"));
  assert(node["data"]["format"].contains("id"));
  assert(node["data"]["format"]["id"]=="matroska");

  assert(node["data"].contains("video"));
  assert(node["data"]["video"].contains("id"));
  assert(node["data"]["video"]["id"]=="mpeg4");

  assert(node["data"].contains("audio"));
  
}
void test_profile_create(){
  HTTPClientSession s(HOST, PORT);
  HTTPRequest request(HTTPRequest::HTTP_POST, "/api/v1/profile/");
  //std::ostringstream os;

  
  JSONNode base(JSON_NODE);
  base.push_back(JSONNode("name","test profile"));
  base.push_back(JSONNode("description","test description"));

  JSONNode format(JSON_NODE);
  format.set_name("format");
  format.push_back(JSONNode("id","mp4"));
  base.push_back(format);

  
  JSONNode video(JSON_NODE);
  video.set_name("video");
  //video.push_back(JSONNode("id","mpeg4"));
  video.push_back(JSONNode("id","libx264"));
  video.push_back(JSONNode("qscale","4"));
  video.push_back(JSONNode("aubq","8"));
  video.push_back(JSONNode("me_range","16"));
  video.push_back(JSONNode("qmin","4"));
  video.push_back(JSONNode("qmax","51"));
  video.push_back(JSONNode("qcomp","0.6"));
  video.push_back(JSONNode("qdiff","4"));

  base.push_back(video);
  
  JSONNode audio(JSON_NODE);
  audio.set_name("audio");
  audio.push_back(JSONNode("id","mp2"));
  audio.push_back(JSONNode("ar","44100"));
  audio.push_back(JSONNode("ac","2"));

  base.push_back(audio);

  //request.write(os);
  std::ostream & os=s.sendRequest(request);
  os << base.write_formatted();
  HTTPResponse response;
  std::istream& rs = s.receiveResponse(response);


  std::string data;
  StreamCopier::copyToString(rs, data);
  LOGDEBUG("response:"<<data);
  JSONNode node = getJson(data);
  assert_response(node);
  assert(node.contains("uuid"));
  created_profile_uuid=node["uuid"].as_string();
}
void test_profile_update(){
  HTTPClientSession s(HOST, PORT);
  HTTPRequest request(HTTPRequest::HTTP_PUT, std::string("/api/v1/profile/").append(created_profile_uuid));
  JSONNode base(JSON_NODE);
  base.push_back(JSONNode("name","test profile"));
  base.push_back(JSONNode("description","test description"));

  JSONNode format(JSON_NODE);
  format.set_name("format");
  format.push_back(JSONNode("id","matroska"));
  base.push_back(format);

  
  JSONNode video(JSON_NODE);
  video.set_name("video");
  video.push_back(JSONNode("id","mpeg4"));

  base.push_back(video);
  
  JSONNode audio(JSON_NODE);
  audio.set_name("audio");
  audio.push_back(JSONNode("id","mp2"));
  audio.push_back(JSONNode("ar","44100"));
  audio.push_back(JSONNode("ac","2"));

  base.push_back(audio);

  //request.write(os);
  std::ostream & os=s.sendRequest(request);
  os << base.write_formatted();
  HTTPResponse response;
  std::istream& rs = s.receiveResponse(response);


  std::string data;
  StreamCopier::copyToString(rs, data);
  LOGDEBUG("response:"<<data);
  JSONNode node = getJson(data);
  assert_response(node);
  assert(node.contains("uuid"));
  assert(node["uuid"]==created_profile_uuid);
}
std::string ProfileEditor::checkJsonProfile(JSONNode&root) {
  std::string result;
  /*check the root conatins required data*/
  if (!root.contains("name")) {
    result = "no profile name given!";
  } else if (!root.contains("format")) {
    result = "no format attribute found!";
  } else if (!root.contains("video")) {
    result = "no video attribute found!";
  } else if (!root.contains("audio")) {
    result = "no audio attribute found!";
  } else if (!root["format"].contains("id")) {
    result = "no id attribute found in attribute \"format\"!";
  } else if (!root["video"].contains("id")) {
    result = "no id attribute found in attribute \"video\"!";
  } else if (!root["audio"].contains("id")) {
    result = "no id attribute found in attribute \"audio\"!";
  }else{
    //Wt::WApplication::instance()->attachThread(true);

    analyzerDialog=new Wt::WDialog(this);
    analyzerDialog->setWidth(800);
    analyzerDialog->setHeight(500);
    progressBar=new Wt::WProgressBar();
    progressBar->setMinimum(0);
    progressBar->setMaximum(250);

    fpsText=new Wt::WText("FPS");
    psnrText=new Wt::WText("PSNR");
    Wt::WVBoxLayout *vbox = new Wt::WVBoxLayout();
    analyzerDialog->contents()->setLayout(vbox);

    chart=new Wt::Chart::WCartesianChart();
    chart->resize(700,350);
    //chart->setHidden(true);
    vbox->addWidget(progressBar);
    vbox->addWidget(fpsText);
    vbox->addWidget(psnrText);
    vbox->addWidget(chart);


    analyzerDialog->show();


    //app->enableUpdates();
    LOGDEBUG("thread attached");
    _verifier=new org::esb::plugin::PresetVerifier();
    _verifier->progress=boost::bind(&ProfileEditor::progress, this, _1);
    _verifier->completed=boost::bind(&ProfileEditor::completed, this, _1);

    Wt::WPushButton *ok = new Wt::WPushButton("Close", analyzerDialog->footer());
    ok->setDefault(true);
    ok->setEnabled(false);
    ok->clicked().connect(this, &ProfileEditor::closeAnalyzer);

    _selfApp=Wt::WApplication::instance();
    _selfApp->enableUpdates();
    a=0;
    go(org::esb::plugin::PresetVerifier::verify, _verifier, root, true, true);

    //verifier.verify(root);
    //analyzerDialog->reject();
    //go(ProfileEditor::startAnalyzeThread, this, root, Wt::WApplication::instance());
  }
  return result;
}
      PresetReaderJson::PresetReaderJson(std::string data) {
        LOGDEBUG("Preset Data:" << data);
        JSONNode node = libjson::parse(data);
        if(node.contains("profile-uuid")){
          db::HiveDb db=DatabaseService::getDatabase();//"sqlite3", org::esb::config::Config::get("db.url"));
          litesql::DataSource<db::Preset>s = litesql::select<db::Preset > (db, db::Preset::Uuid == node["profile-uuid"].as_string());
          if(s.count()==1){
            data=s.one().data;
          }else{
            throw org::esb::lang::Exception(__FILE__,__LINE__,"profile defined by profile-uuid not found");
          }
          node = libjson::parse(data);
        }
        if (node.contains("format") && node["format"].contains("id")) {
          _preset["id"] = node["format"]["id"].as_string();
          if(node.contains("name")){
            _preset["name"]=node["name"].as_string();
          }
        }else{
          throw org::esb::lang::Exception(__FILE__,__LINE__,"no format attribute found");
        }
        std::string type;
        type = "video";
        if (node.contains(type)) {
          int c = node[type].size();
          for (int a = 0; a < c; a++) {
            JSONNode n = node[type].at(a);
            std::string name = n.name();
            std::string value = n.as_string();
            /*filter out some unwanted attributes*/
            if (name == "frame_size"){
              int width;
              int height;
              int result=sscanf(value.c_str(),"%dx%d", &width, &height);
              LOGDEBUG("width="<<width);
              LOGDEBUG("width="<<height);
              _codecs[type]["width"]=StringUtil::toString(width);
              _codecs[type]["height"]=StringUtil::toString(height);
              continue;
            }
            //LOGDEBUG("json type : "<<n.type());
            if (name == "id") {
              name = "codec_id";
            }
            if (name == "b" || name == "maxrate" || name == "bt" || name == "bufsize" || name == "minrate")
              value += "000";
            LOGDEBUG("Name=" << name << " val=" << value);

            _codecs[type][name] = value; //.insert(std::pair<std::string, std::string>(name,value));
          }
        }else{
          throw org::esb::lang::Exception(__FILE__,__LINE__,"no video attribute found");
        }
        type = "audio";
        if (node.contains(type)) {
          int c = node[type].size();
          for (int a = 0; a < c; a++) {
            JSONNode n = node[type].at(a);
            LOGDEBUG("Name=" << n.name() << " val=" << n.as_string());
            if (n.name() == "id") {
              n.set_name("codec_id");
              //AVCodec * codec=avcodec_find_encoder_by_name(n.as_string().c_str());
              //n=JSONNode("codec_id",codec->id);
            }
            _codecs[type][n.name()] = n.as_string(); //.insert(std::pair<std::string, std::string>(n.name(),n.as_string()));
          }
        }else{
          throw org::esb::lang::Exception(__FILE__,__LINE__,"no audio attribute found");
        }
      }