/*!
  @header FTCreateGraphTEST
  @abstract Module of FT
 
  @availability OS X, GNUstep
  @copyright 2004, 2005 Free Software Foundation, Inc.
  
  Author: Oliver Langer

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

  <pre>
  -------------------------------------------------------------------------
  Modification history

  23.02.05 ola     initial version
  -------------------------------------------------------------------------
  </pre>
*/
#include <Encore/Encore.h>
#include "FTCreateGraphTEST.h"

@implementation FTCreateGraphTEST

- testGraphCreation {
  id <FTGraphManager> graphManager;
  id <FTGraph> graph;
  id <FTId> graphId;
  id <FTId> nid1,nid2,nid3,e1, e2;
  id <FTObjectToIdMapper> objectToIdMapper;
  id <FTObjectToIdMapper> graphIdMapper;
  id <FTNode> n1,n2,n3;
  id <FTEdge> edge;
  id <FTTransaction> transaction;
  id <ECIterator> nodeIterator;
  
  NSLog( @"FTCreateGraphTEST::testGraphCreation: BEGINNING...." );
  
  EC_AUTORELEASEPOOL_BEGIN
  objectToIdMapper = [session defaultObjectToIdMapper];
  graphManager = [session graphManager];

  NSLog( @"Now creating graph..." );
  transaction = [session beginTransactionWithParent: nil
    withSettings: nil];
  
  graphId = [objectToIdMapper mapObject: @"firstGraph"];
  graph = [graphManager createGraphWithId: graphId];
  ECAssertTrue( nil != graph, @"Graph creation failed!" );
  
  NSLog( @"Now creating some nodes..." );
  graphIdMapper = [[graph objectToIdMapper] retain];
  ECAssertTrue( nil != graphIdMapper,
     @"getting objectToIdMapper of graph FAILED" );
  
  NSLog( @"Creating nodes and relationships..." );
  nid1 = [graphIdMapper mapObject: @"fatherNode"];
  nid2 = [graphIdMapper mapObject: @"child_1_Node"];
  nid3 = [graphIdMapper mapObject: @"child_2_Node"];
  e1 = [graphIdMapper mapObject: @"edge1"];
  e2 = [graphIdMapper mapObject: @"edge2"];
  
  n1 = [graph createNodeWithId: nid1];
  n2 = [graph createNodeWithId: nid2];
  n3 = [graph createNodeWithId: nid3];
  
  NSLog( @"Created nodes n1, n2, n3" );
  
  edge = [n1 createAndAppendEdgeWithId: e1 withTargetNode: n2];
  edge = [n1 createAndAppendEdgeWithId: e2 withTargetNode: n3];
  
  NSLog( @"Performing a commit..." );
  [transaction commit];
  NSLog( @"Graph created! Now trying to read the graph" );

  graphId = [objectToIdMapper mapObject: @"firstGraph"];
  [graph close];

  graphId = [objectToIdMapper mapObject: @"firstGraph"];
  graph = [graphManager graphWithId: graphId];

  NSLog( @"Got graph! Now reading nodes..." );
  
  n1 = [graph nodeWithId: nid1];
  ECAssertTrue( n1 != nil, @"Unable to fetch node with id=\"fatherNode\"" );

  NSLog( @"All nodes loaded!" );
  
  /**
   * Now checking links...
   */
  NSLog( @"Loading node n2..." );

  nodeIterator = [n1 outgoingNodes];
  ECAssertTrue( [nodeIterator hasNext], @"Unable to read first node of n1!" );
  n2 = [nodeIterator next];

  ECAssertTrue( YES == [[n2 nodeId] isEqual: nid2], @"Expected node n2!" );
  NSLog( @"Node n2 LOADED. Now loading n3..." );
  
  ECAssertTrue( [nodeIterator hasNext], @"Unable to read second node of n1!" );
  n3 = [nodeIterator next];

  ECAssertTrue( YES == [[n3 nodeId] isEqual: nid3], @"Expected node n3!" );
  NSLog( @"Node n3 LOADED." );
  
  NSLog( @"Closing Graph..." );

  [graph close];
  
  EC_AUTORELEASEPOOL_END
  NSLog( @"FTCreateGraphTEST::testGraphCreation: FINISHED" );  
  return self;
}
@end
