Advertisement
rplantiko

TWiki: Testing the SAP Connect Plugin

Feb 10th, 2013
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 5.64 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. # Tests the SapConnectPlugin
  3.  
  4. BEGIN {
  5. # Add the path to TWiki lib, and further paths if required by your installation
  6.  push @INC, qw|
  7.    /srv/www/vhosts/sapdev.mits.ch/twiki/lib
  8.    |;
  9.  }
  10.  
  11. # --- Testing the SapConnectPlugin
  12.  
  13. use TWiki::Plugins::SapConnectPlugin;
  14.  
  15. use Test::More tests => 26;
  16.  
  17. init_tests();
  18.  
  19. # Test API function
  20. is(    get_data_from_sap( type => 'transports', dest => 'D12' ),
  21.        'http://d12.migros.ch:1091/sap/bc/twiki/transports',
  22.        'get_data_from_sap() URL with no id is built properly' );
  23. is(    get_data_from_sap( type => 'transport', id => 'D12K123456' ),
  24.        'http://d11.migros.ch:1090/sap/bc/twiki/transport?id=D12K123456',
  25.        'get_data_from_sap() URL with id is built properly' );
  26. is(    get_data_from_sap( type => 'transports', args => { list_scope => 'light' } ),
  27.        'http://d11.migros.ch:1090/sap/bc/twiki/transports?list_scope=light',
  28.        'get_data_from_sap() URL with extra parameters is built properly' );
  29.  
  30. # Test request URL properly produced
  31. is(    incl_from_sap( 'docu', {_DEFAULT => 'FU.Z_TEST', dest => 'D12'}),
  32.        'http://d12.migros.ch:1091/sap/bc/twiki/docu?id=FU.Z_TEST',
  33.        'incl_from_sap([docu]) generating the correct URL' );
  34.  
  35. is(    (incl_from_sap( 'code', {_DEFAULT => 'FU.Z_TEST'})=~ /<pre>\s*(.*?)\s*<\/pre>/ )[0],
  36.        'http://d11.migros.ch:1090/sap/bc/twiki/code?id=FU.Z_TEST',
  37.        'incl_from_sap([code]) generating the correct URL' );
  38. is(    incl_from_sap( 'addi', {_DEFAULT => 'TRANSPORT.D12K165202'}),
  39.        'http://d11.migros.ch:1090/sap/bc/twiki/transport?id=D12K165202',
  40.        'incl_from_sap([addi]) generating the correct URL' );
  41.  
  42. is(    url_for_sap( 'docu', { _DEFAULT => 'CL.ZCL_TEST',
  43.                               expand   => 'true' } ),
  44.        'http://sapdev.mits.ch/twiki/bin/view/TWiki/SapDocu?id=CL.ZCL_TEST&expand=X',
  45.        'url_for_sap() generates URL to special TWiki topic' );
  46.        
  47.  
  48. # enrich URL from TWiki tag args
  49. is(    test_enrich_url({ expand     => 'true',
  50.                          _DEFAULT => 'RE.ZZ_TEST' })->query_param('expand'),
  51.        'X',
  52.        'enrich_url_from_tag_args() maps true values to X' );
  53. is(    test_enrich_url({ expand   => 'false',
  54.                          _DEFAULT => 'RE.ZZ_TEST' })->query_param('expand'),
  55.        ' ',
  56.        'enrich_url_from_tag_args() maps false values to space' );
  57. is(    test_enrich_url({ level     => 3,
  58.                          _DEFAULT  => 'RE.ZZ_TEST' })->query_param('level'),
  59.        '3',
  60.        'enrich_url_from_tag_args() maps level value' );
  61.  
  62.  
  63. is(    test_enrich_url({ _DEFAULT => 'RE.ZZ_TEST' })->path,
  64.        '/docu',
  65.        'enrich_url_from_tag_args([docu]) sets path extension "docu"' );
  66. is(    test_enrich_url({ _DEFAULT => 'RE.ZZ_TEST' })->query_param('id'),
  67.        'RE.ZZ_TEST',
  68.        'enrich_url_from_tag_args([docu]) sets ID parameter from default argument' );
  69.  
  70. is(    test_enrich_url({ _DEFAULT => 'TRANSPORT.D12K104256'},'addi')->path,
  71.        '/transport',
  72.        'enrich_url_from_tag_args([addi]) fills path extension from first part of default argument' );
  73. is(    test_enrich_url({ _DEFAULT => 'TRANSPORT.D12K104256' },'addi')->query_param('id'),
  74.        'D12K104256',
  75.        'enrich_url_from_tag_args([addi]) sets ID parameter from 2nd part of default argument' );
  76.  
  77. sub test_enrich_url {
  78.   my $url = URI->new( 'http://test.com' );
  79.   my $args = shift;
  80.   my $type = shift || 'docu';
  81.   enrich_url_from_tag_args( $url, $type, $args);
  82.   return $url;
  83.   }
  84.  
  85. # get URL function
  86. isa_ok( get_url_from_dest(),      'URI',    
  87.         'get_url_from_dest() passes URI object');
  88. like(   get_url_from_dest(),      qr/d11/,  
  89.         'get_url_from_dest() uses default dest if none passed');
  90. is(     get_url_from_dest('D12'), 'http://d12.migros.ch:1091/sap/bc/twiki/',
  91.         'get_url_from_dest() passes complete URL');
  92. like(   get_url_from_dest('d11'), qr/d11/,'get_url_from_dest() accepts lowercase destinations' );
  93. fails(  get_url_from_dest,      'D99',
  94.         'get_url_from_dest() raises exception for unknown destinations');
  95.  
  96. for (qw/
  97.   RE.ZZ_TEST
  98.   CL.ZCL_XML_HELPER
  99.   TX.ZZ_HELP
  100.   fu.z_convert_itf
  101.   /) {
  102.   passes( \&check_docu_key, $_, "is_docu_key() accepts syntactically correct arguments");
  103.   }
  104.  
  105. for (qw/
  106.   REP.ZZ_TEST
  107.   RE
  108.   Something
  109.   /) {
  110.   fails( \&check_docu_key, $_, "is_docu_key() refuses syntactically incorrect arguments");
  111.   }
  112.  
  113. sub fails {
  114.   my $testname = pop @_;
  115.   ok( died( @_ ), $testname );
  116.   }  
  117.  
  118. sub passes {
  119.   my $testname = pop @_;
  120.   ok( !died( @_ ), $testname );
  121.   }  
  122.  
  123. sub died {
  124.   my ($f,@args) = @_;
  125.   eval {
  126.     &$f(@args);
  127.     };
  128.   return !!$@;
  129.   }
  130.  
  131. sub init_tests {
  132.   no warnings 'once';
  133.   # initialize some variables
  134.   %TWiki::Plugins::SapConnectPlugin::sapDest = ( D11 => "d11.migros.ch:1090",
  135.                                                  D12 => "d12.migros.ch:1091" );
  136.   $TWiki::Plugins::SapConnectPlugin::defaultDest = "D11";
  137.   $TWiki::Plugins::SapConnectPlugin::sapPath = "/sap/bc/twiki/";
  138.   $TWiki::Plugins::SapConnectPlugin::scheme = "http";
  139.   no warnings;
  140.   *TWiki::Plugins::SapConnectPlugin::get_request = sub { return shift };            
  141.   *TWiki::Plugins::SapConnectPlugin::get_topic_url_for_link = sub { return URI->new( 'http://sapdev.mits.ch/twiki/bin/view/TWiki/Sap' . ucfirst lc shift ); };
  142.  
  143. # For better readability of the tests: Import these methods into own namespace  
  144.   import_methods_under_test(qw|
  145.     incl_from_sap
  146.     url_for_sap
  147.     check_docu_key
  148.     enrich_url_from_tag_args
  149.     get_url_from_dest
  150.     get_data_from_sap
  151.     |);
  152.   }
  153.  
  154. sub import_methods_under_test {
  155.   for (@_) {
  156.     *{$_} = \*{"TWiki::Plugins::SapConnectPlugin::$_"};
  157.     }
  158.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement