END_TEST


START_TEST (test_CVTerm_addResource)
{
  CVTerm_t *term = CVTerm_createWithQualifierType(MODEL_QUALIFIER);
  const char * resource = "GO6666";
  XMLAttributes_t *xa;

  fail_unless(term != NULL);
  fail_unless(CVTerm_getQualifierType(term) == MODEL_QUALIFIER);
  
  int i = CVTerm_addResource(term, "");

  fail_unless ( i == LIBSBML_OPERATION_FAILED);

  xa = CVTerm_getResources(term);

  fail_unless(XMLAttributes_getLength(xa) == 0);
  
  i = CVTerm_addResource(term, resource);

  fail_unless ( i == LIBSBML_OPERATION_SUCCESS);

  xa = CVTerm_getResources(term);

  fail_unless(XMLAttributes_getLength(xa) == 1);
  fail_unless(!strcmp(XMLAttributes_getName(xa, 0), "rdf:resource"));
  fail_unless(!strcmp(XMLAttributes_getValue(xa, 0), "GO6666"));

  CVTerm_free(term);
}
END_TEST


START_TEST (test_CVTerm_removeResource)
{
  CVTerm_t *term = CVTerm_createWithQualifierType(MODEL_QUALIFIER);
  const char * resource = "GO6666";
  XMLAttributes_t *xa;

  fail_unless(term != NULL);
  fail_unless(CVTerm_getQualifierType(term) == MODEL_QUALIFIER);
  
  CVTerm_addResource(term, resource);
  xa = CVTerm_getResources(term);

  fail_unless(XMLAttributes_getLength(xa) == 1);
  
  int i = CVTerm_removeResource(term, "CCC");

  fail_unless ( i == LIBSBML_INVALID_ATTRIBUTE_VALUE);

  xa = CVTerm_getResources(term);

  fail_unless(XMLAttributes_getLength(xa) == 1);
  
  i = CVTerm_removeResource(term, resource);

  fail_unless ( i == LIBSBML_OPERATION_SUCCESS);

  xa = CVTerm_getResources(term);

  fail_unless(XMLAttributes_getLength(xa) == 0);

  CVTerm_free(term);
}
int
main (int argc, char *argv[])
{

  SBMLDocument_t* d;
  Model_t* m;
  unsigned int  errors, n;
  Reaction_t *r;

  if (argc != 3)
  {
      printf("\n"
         "  usage: addingEvidenceCodes_1 <input-filename> <output-filename>\n"
         "  Adds controlled vocabulary term to a reaction\n"        
         "\n");
    return 2;
  }


  d      = readSBML(argv[1]);
  errors = SBMLDocument_getNumErrors(d);

  if (errors > 0)
  {
    printf("Read Error(s):\n");
    SBMLDocument_printErrors(d, stdout);	 
    printf("Correct the above and re-run.\n");
  }
  else
  {
  
    m = SBMLDocument_getModel(d);
    n =  Model_getNumReactions(m);
    
    if (n <= 0)
    {
      printf( "Model has no reactions.\n Cannot add CV terms\n");
    }
    else
    {      
      CVTerm_t *cv1, *cv2;
      r = Model_getReaction(m, 0);

      /* check that the reaction has a metaid
       * no CVTerms will be added if there is no metaid to reference
       */
      if (SBase_isSetMetaId((SBase_t*)r))
        SBase_setMetaId((SBase_t*)r, "metaid_0000052");

      cv1 = CVTerm_createWithQualifierType(BIOLOGICAL_QUALIFIER);
      CVTerm_setBiologicalQualifierType(cv1, BQB_IS_DESCRIBED_BY);
      CVTerm_addResource(cv1, "urn:miriam:obo.eco:ECO%3A0000183");

      SBase_addCVTerm((SBase_t*)r, cv1);

      cv2 = CVTerm_createWithQualifierType(BIOLOGICAL_QUALIFIER);
      CVTerm_setBiologicalQualifierType(cv2, BQB_IS);
      CVTerm_addResource(cv2, "urn:miriam:kegg.reaction:R00756");
      CVTerm_addResource(cv2, "urn:miriam:reactome:REACT_736");
      
      SBase_addCVTerm((SBase_t*)r, cv2);

      writeSBML(d, argv[2]);
    }
  }

  SBMLDocument_free(d);
  return errors;
}