Test-Scenario-1:
  Id: ts_01
  Path: hydratk/extensions/datagen/asn1codec/01_methods_ut.jedi
  Name: ts_init
  Desc: Test method __init__
  Author: Petr Rasek <bowman@hydratk.org>
  Version: 0.1
  
  Pre-Req: |
    from hydratk.extensions.datagen.asn1codec import ASN1Codec
  
  Test-Case-1:
    Id: tc_01
    Name: tc_init
    Desc: Initialize ASN1Codec
    
    Test-Condition-1: 
      Id: tco_01
      Name: tco_init
      Desc: Initialize
      
      Test: |
        g = ASN1Codec()
        res = g.__class__.__name__
      
      Validate: |
        this.test_result = res
        assert (res == 'ASN1Codec'), '__init__ = ASN1Codec'
        
    Test-Condition-2: 
      Id: tco_02
      Name: tco_config
      Desc: Initialization with configuration
      
      Test: |
        cfg = mh.cfg['Extensions']['DataGen']
        ffasn1dump_path = '/usr/local/bin/ffasn1dump'
        cfg['ffasn1dump_path'] = ffasn1dump_path
        g = ASN1Codec()
        res = g.__class__.__name__
      
      Validate: | 
        this.test_result = res
        assert (res == 'ASN1Codec'), '__init__ = ASN1Codec'
        res = g._path
        exp = ffasn1dump_path   
        this.test_result = str(res)    
        assert (res == exp), '_path = {0}'.format(exp)        
        
Test-Scenario-2:
  Id: ts_02
  Path: hydratk/extensions/datagen/asn1codec/01_methods_ut.jedi
  Name: ts_props
  Desc: Test properties
  Author: Petr Rasek <bowman@hydratk.org>
  Version: 0.1
  
  Pre-Req: |
    from hydratk.extensions.datagen.asn1codec import ASN1Codec   
    
  Test-Case-1:
    Id: tc_01
    Name: tc_getters
    Desc: Getter methods
    
    Test-Condition-1: 
      Id: tco_01
      Name: tco_path
      Desc: Getter path
      
      Test: |
        g = ASN1Codec()
        res = g.path
      
      Validate: |   
        this.test_result = res
        exp = g._path
        assert(res == exp), 'path = {0}'.format(exp)
        
Test-Scenario-3:
  Id: ts_03
  Path: hydratk/extensions/datagen/asn1codec/01_methods_ut.jedi
  Name: ts_compile
  Desc: Test method compile
  Author: Petr Rasek <bowman@hydratk.org>
  Version: 0.1
  
  Pre-Req: |
    from hydratk.core.masterhead import MasterHead
    from hydratk.extensions.datagen.asn1codec import ASN1Codec
    from yodahelpers.hydratk.core.events import events, reg_ev_hook 
    from os import remove
    from sys import prefix
    
  Test-Case-1:
    Id: tc_01
    Name: tc_compile
    Desc: Compile specification
    
    Test-Condition-1: 
      Id: tco_01
      Name: tco_compile
      Desc: Compile ASN.1
      
      Test: |
        mh = MasterHead.get_head()
        evs = ['asn1_before_compile', 'asn1_after_compile']
        reg_ev_hook(mh, evs[0])
        reg_ev_hook(mh, evs[1])
        g = ASN1Codec()
        file = prefix+'/var/local/hydratk/yoda/helpers/yodahelpers/hydratk/extensions/datagen/spec.asn'
        res = g.compile(file)
      
      Validate: |    
        this.test_result = res
        assert (res), 'compile = True'
        res = [events[0]._id, events[1]._id]
        this.test_result = str(res)
        assert (res == evs), 'compile fires {0}'.format(evs)  
        del mh._event_hooks[evs[0]]
        del mh._event_hooks[evs[1]]   
        del events[0]
        del events[0]
        
    Test-Condition-2: 
      Id: tco_02
      Name: tco_invalid_file
      Desc: Invalid file
      
      Test: |
        res = g.compile('xx')
      
      Validate: |                
        this.test_result = res
        assert (not res), 'compile = False'  
        
    Test-Condition-3: 
      Id: tco_03
      Name: tco_invalid_spec
      Desc: Invalid specification
      
      Test: |
        file = './spec.asn'
        with open(file, 'w') as f:
            f.write('xx')
            
        res = g.compile(file)
      
      Validate: |                
        this.test_result = res
        assert (not res), 'compile = False'  
        remove(file)       
        
Test-Scenario-4:
  Id: ts_04
  Path: hydratk/extensions/datagen/asn1codec/01_methods_ut.jedi
  Name: ts_encode
  Desc: Test method encode
  Author: Petr Rasek <bowman@hydratk.org>
  Version: 0.1
  
  Pre-Req: |
    from hydratk.core.masterhead import MasterHead
    from hydratk.extensions.datagen.asn1codec import ASN1Codec
    from yodahelpers.hydratk.core.events import events, reg_ev_hook 
    from os import path, remove
    from sys import prefix
    
  Test-Case-1:
    Id: tc_01
    Name: tc_encode
    Desc: Encode ASN.1 file
    
    Test-Condition-1: 
      Id: tco_01
      Name: tco_encode
      Desc: Encode
      
      Test: |
        mh = MasterHead.get_head()
        evs = ['asn1_before_encode', 'asn1_after_encode']
        reg_ev_hook(mh, evs[0])
        reg_ev_hook(mh, evs[1])  
        g = ASN1Codec()      
        spec = prefix+'/var/local/hydratk/yoda/helpers/yodahelpers/hydratk/extensions/datagen/spec.asn'
        
        infile = './in.gser'
        input = '{a 20, b TRUE, c {d FALSE, e \'780000\'H}, f {g 128, h TRUE}}'
        with open(infile, 'w') as f:
            f.write(input)
            
        res = g.encode(spec, 'TestSeq2', infile, 'ber')
      
      Validate: |    
        this.test_result = res
        assert (res), 'encode = True'
        res = [events[0]._id, events[1]._id]
        this.test_result = str(res)
        assert (res == evs), 'encode fires {0}'.format(evs)
        outfile = infile[:-4]+'ber'
        this.test_result = res = (path.exists(outfile))
        assert (res), 'file {0} created'.format(outfile)        
        del mh._event_hooks[evs[0]]
        del mh._event_hooks[evs[1]]   
        del events[0]
        del events[0]  
        remove(infile)
        remove(outfile)  
        
    Test-Condition-2: 
      Id: tco_02
      Name: tco_outfile
      Desc: Set output file
      
      Test: |
        infile = 'in.gser'
        input = '{a 20, b TRUE, c {d FALSE, e \'780000\'H}, f {g 128, h TRUE}}'
        with open(infile, 'w') as f:
            f.write(input)
            
        outfile = './test.ber'    
        res = g.encode(spec, 'TestSeq2', infile, 'ber', outfile)
      
      Validate: |    
        this.test_result = res
        assert (res), 'encode = True'
        this.test_result = res = (path.exists(outfile))
        assert (res), 'file {0} created'.format(outfile)         
        remove(infile)
        remove(outfile)          
    
    Test-Condition-3: 
      Id: tco_03
      Name: tco_invalid_file
      Desc: Invalid file
      
      Test: |
        res = g.encode(spec, 'TestSeq2', 'xx')
      
      Validate: |                     
        this.test_result = res
        assert (not res), 'encode = False'
        
    Test-Condition-4: 
      Id: tco_04
      Name: tco_invalid_gser
      Desc: Invalid GSER file
      
      Test: |
        file = './in.gser'
        with open(file, 'w') as f:
            f.write('xx')      
      
        res = g.encode(spec, 'TestSeq2', file)
      
      Validate: |                     
        this.test_result = res
        assert (not res), 'encode = False'        
        remove(file)
        
    Test-Condition-5: 
      Id: tco_05
      Name: tco_invalid_elem
      Desc: Invalid element        
        
      Test: |         
        infile = './in.gser'
        input = 'TestSeq2: {a 20, b true, c {d false, e \'780000\'H}, f {g 128, h true}}'
        with open(file, 'w') as f:
            f.write(input)
            
        res = g.encode(spec, 'xx', infile)
        
      Validate: |
        this.test_result = res
        assert (not res), 'encode = False'
        remove(file)       
        
Test-Scenario-5:
  Id: ts_05
  Path: hydratk/extensions/datagen/asn1codec/01_methods_ut.jedi
  Name: ts_decode
  Desc: Test method decode
  Author: Petr Rasek <bowman@hydratk.org>
  Version: 0.1
  
  Pre-Req: |
    from hydratk.core.masterhead import MasterHead
    from hydratk.extensions.datagen.asn1codec import ASN1Codec
    from yodahelpers.hydratk.core.events import events, reg_ev_hook 
    from os import path, remove
    from sys import prefix
    
  Test-Case-1:
    Id: tc_01
    Name: tc_decode
    Desc: Decode ASN.1 file    
    
    Test-Condition-1: 
      Id: tco_01
      Name: tco_decode
      Desc: Decode
      
      Test: |
        mh = MasterHead.get_head()
        evs = ['asn1_before_decode', 'asn1_after_decode']
        reg_ev_hook(mh, evs[0])
        reg_ev_hook(mh, evs[1])    
        g = ASN1Codec()    
        spec = prefix+'/var/local/hydratk/yoda/helpers/yodahelpers/hydratk/extensions/datagen/spec.asn'
        
        infile = './in.gser'
        input = '{a 20, b TRUE, c {d FALSE, e \'780000\'H}, f {g 128, h TRUE}}'
        with open(infile, 'w') as f:
            f.write(input)            
        g.encode(spec, 'TestSeq2', infile)
        remove(infile)
        infile = './in.ber'
        
        res = g.decode(spec, 'TestSeq2', infile, 'ber')
      
      Validate: |    
        this.test_result = res
        assert (res), 'decode = True'
        res = [events[0]._id, events[1]._id]
        this.test_result = str(res)
        assert (res == evs), 'decode fires {0}'.format(evs)
        outfile = infile[:-3]+'gser'
        this.test_result = res = (path.exists(outfile))
        assert (res), 'file {0} created'.format(outfile)  
        
        with open(outfile, 'r') as f:
            cont = f.read()
        cont = cont.replace('\n', '')
        cont = cont.replace(' ', '')
              
        this.test_result = cont
        exp = input.replace(' ', '')
        assert (cont == exp), 'output = {0}'.format(exp)
        del mh._event_hooks[evs[0]]
        del mh._event_hooks[evs[1]]   
        del events[0]
        del events[0]  
        remove(outfile) 
        
    Test-Condition-2: 
      Id: tco_02
      Name: tco_outfile
      Desc: Set output file
      
      Test: |
        outfile = './test.gser'    
        res = g.decode(spec, 'TestSeq2', infile, 'ber', outfile)
      
      Validate: |    
        this.test_result = res
        assert (res), 'decode = True'
        this.test_result = res = (path.exists(outfile))
        assert (res), 'file {0} created'.format(outfile)         
        remove(outfile)          
    
    Test-Condition-3: 
      Id: tco_03
      Name: tco_invalid_file
      Desc: Invalid file
      
      Test: |
        res = g.decode(spec, 'TestSeq2', 'xx')
      
      Validate: |                     
        this.test_result = res
        assert (not res), 'decode = False'
        
    Test-Condition-4: 
      Id: tco_04
      Name: tco_invalid_bin
      Desc: Invalid BIN file
      
      Test: |
        file = './test.ber'
        with open(file, 'w') as f:
            f.write('xx')      
      
        res = g.encode(spec, 'TestSeq2', file)
      
      Validate: |                     
        this.test_result = res
        assert (not res), 'decode = False'        
        remove(file)
        
    Test-Condition-5: 
      Id: tco_05
      Name: tco_invalid_elem
      Desc: Invalid element        
        
      Test: |         
        res = g.decode(spec, 'xx', file)
        
      Validate: |
        this.test_result = res
        assert (not res), 'decode = False'
        remove(infile)                 
        
Test-Scenario-6:
  Id: ts_06
  Path: hydratk/extensions/datagen/asn1codec/01_methods_ut.jedi
  Name: ts_transcode
  Desc: Test method transcode
  Author: Petr Rasek <bowman@hydratk.org>
  Version: 0.1
  
  Pre-Req: |
    from hydratk.core.masterhead import MasterHead
    from hydratk.extensions.datagen.asn1codec import ASN1Codec
    from yodahelpers.hydratk.core.events import events, reg_ev_hook 
    from os import path, remove
    from sys import prefix
    
  Test-Case-1:
    Id: tc_01
    Name: tc_transcode
    Desc: Transcode ASN.1 file    
    
    Test-Condition-1: 
      Id: tco_01
      Name: tco_transcode
      Desc: Transcode
      
      Test: |
        mh = MasterHead.get_head()
        evs = ['asn1_before_transcode', 'asn1_after_transcode']
        reg_ev_hook(mh, evs[0])
        reg_ev_hook(mh, evs[1])    
        g = ASN1Codec()    
        spec = prefix+'/var/local/hydratk/yoda/helpers/yodahelpers/hydratk/extensions/datagen/spec.asn'
        
        infile = './in.gser'
        input = '{a 20, b TRUE, c {d FALSE, e \'780000\'H}, f {g 128, h TRUE}}'
        with open(infile, 'w') as f:
            f.write(input)            
        g.encode(spec, 'TestSeq2', infile)
        remove(infile)
        infile = './in.ber'
        
        res = g.transcode(spec, 'TestSeq2', infile, 'ber', 'oer')
      
      Validate: |    
        this.test_result = res
        assert (res), 'decode = True'
        res = [events[0]._id, events[1]._id]
        this.test_result = str(res)
        assert (res == evs), 'decode fires {0}'.format(evs)
        outfile = infile[:-3]+'oer'
        this.test_result = res = (path.exists(outfile))
        assert (res), 'file {0} created'.format(outfile)  
        del mh._event_hooks[evs[0]]
        del mh._event_hooks[evs[1]]   
        del events[0]
        del events[0]  
        remove(outfile) 
        
    Test-Condition-2: 
      Id: tco_02
      Name: tco_outfile
      Desc: Set output file
      
      Test: |
        outfile = './test.oer'    
        res = g.transcode(spec, 'TestSeq2', infile, 'ber', 'oer', outfile)
      
      Validate: |    
        this.test_result = res
        assert (res), 'decode = True'
        this.test_result = res = (path.exists(outfile))
        assert (res), 'file {0} created'.format(outfile)  
        remove(infile)       
        remove(outfile)               