module TestCodec_IVIM {
    
  // LibCommon
  import from LibCommon_BasicTypesAndValues all;
  import from LibCommon_DataStrings all;
  
  // LibIts
  import from ITS_Container language "ASN.1:1997" all;
  import from IVIM_PDU_Descriptions language "ASN.1:1997" all;
  import from IVI language "ASN.1:1997" all;
  import from EfcDsrcApplication language "ASN.1:1997" all;
  import from AVIAEINumberingAndDataStructures language "ASN.1:1997" all;
  
  // LibItsCommon
  import from LibItsCommon_Functions all;
  import from LibItsCommon_ASN1_NamedNumbers all;
  import from LibItsIvim_ASN1_IVInamedNumbers all;
    
  // LibItsIvim
  import from LibItsIvim_TypesAndValues all;
  import from LibItsIvim_TestSystem all;
  import from LibItsIvim_Templates all;
    
  // TestCodec
  import from TestCodec_TestAndSystem all;
    
  group LibItsIvim_testCases {
        
    group testIvimPrimitiveMessages {
      
      testcase tc_IvimReq() runs on TCType system TCType {
        var template (value) Provider v_provider := {
        countryCode         := '1010101010'B,
        providerIdentifier  := 11
        };
        
        TestIvimReq(
                    m_ivimReq(
                              m_ivimStructurePdu(
                                                 valueof(m_ivimStructure(
                                                                         valueof(m_iviManagementContainer(
                                                                                                          v_provider,
                                                                                                          1,
                                                                                                          LibItsIvim_ASN1_IVInamedNumbers.IviStatus_new_
                                                                                                          ))
                                                                         ))
                                                 )
                              ), 
                    true,
                    oct2bit('01020000273f7c730059f34f8c4e2db0661ffffffc2231129e48927fc1f48082788a800f53080fffc4014004'O)
                    );
      }
      
      testcase tc_IvimInd_1() runs on TCType system TCType {
        var template (value) Provider v_provider := {
        countryCode         := '1010101010'B,
        providerIdentifier  := 11
        };
        
        TestIvimInd(
                    mw_ivimIndWithGnParameters(
                                               m_ivimStructurePdu(
                                                                  valueof(m_ivimStructure(
                                                                                          valueof(m_iviManagementContainer(
                                                                                                                           v_provider,
                                                                                                                           1,
                                                                                                                           LibItsIvim_ASN1_IVInamedNumbers.IviStatus_new_
                                                                                                                           ))
                                                                                          ))
                                                                  ),
                                               1, // 01
                                               2, // 02
                                               3, // 03
                                               67438087, // 04050607
                                               8 // 08
                                               ), 
                    '01020000273f7c730059f34f8c4e2db0661ffffffc2231129e48927fc1f48082788a800f53080fffc40140040102030405060708090a0b0c0d0e0f00000000000000000000000000000000000000000000000000000000000a0a0a0a'O
                    );
      }
      
      testcase tc_IvimInd_2() runs on TCType system TCType {
        var template (value) Provider v_provider := {
        countryCode         := '1010101010'B,
        providerIdentifier  := 11
        };
        
        TestIvimInd(
                    mw_ivimIndWithSecurityParameters(
                                                     m_ivimStructurePdu(
                                                                        valueof(m_ivimStructure(
                                                                                                valueof(m_iviManagementContainer(
                                                                                                                                 v_provider,
                                                                                                                                 1,
                                                                                                                                 LibItsIvim_ASN1_IVInamedNumbers.IviStatus_new_
                                                                                                                                 ))
                                                                                                ))
                                                                        ),
                                                     int2bit(0, 256), // 00{32}
                                                     168430090 // 0a0a0a0a
                                                     ),
                    '01020000273f7c730059f34f8c4e2db0661ffffffc2231129e48927fc1f48082788a800f53080fffc40140040102030405060708090a0b0c0d0e0f00000000000000000000000000000000000000000000000000000000000a0a0a0a'O
                    );
      }
      
    } // End of group testIvimPrimitiveMessages

    group testIvimUpperTester {

      /**
       * @desc Validate template GeoNetworking/UtIvimInitialize
       * @verdict Pass on success, Fail otherwise
       */
      testcase tc_Ivim_UtIvimInitialize() runs on TCType system TCType {
        var bitstring v_encMsg;
        var UtIvimInitialize v_decMsg;
        var integer v_res := 0;
        var template (value) UtIvimInitialize m_utIvimInitialize := { '0809AABBCCDDEEFF'O };
        var bitstring v_expEncMsg := oct2bit('000809AABBCCDDEEFF'O);
        
        // Encode template
        log("Encode template ", valueof(m_utIvimInitialize));
      v_encMsg := encvalue(m_utIvimInitialize);
        log("Encoded message:  ", bit2oct(v_encMsg));
        // Check result
        if (not isbound(v_encMsg)) {
          setverdict(fail, "Encoding failed!");
          stop;
        }
        if (not match(v_encMsg, v_expEncMsg)) {
          log("Expected message: ", bit2oct(valueof(v_expEncMsg)));
          setverdict(fail, "Encoding failed, not the expected result!");
          stop;
        }
        setverdict(pass, "Encoding passed.");
      } // End of tc_Ivim_UtIvimInitialize
      
      /**
       * @desc Validate template Ivim/UtIvimTrigger
       * @verdict Pass on success, Fail otherwise
       */
      testcase tc_Ivim_UtIvimTrigger_1() runs on TCType system TCType {
        var bitstring v_encMsg;
        var integer v_res := 0;
        var bitstring v_expEncMsg := oct2bit('A4A0000000061232000000000000000000000002'O);
    
        // Encode template
        //log("f_getCurrentTime() =", f_getCurrentTime());
        log("int2oct(397874)=", int2oct(397874, 6));
        log("Encode template ", valueof(m_utTriggerEvent(c_duration_2sec, 399874 - 1000 * c_duration_2sec, -/*, -, -*/)));
      v_encMsg := encvalue(m_utTriggerEvent(c_duration_2sec, 399874 - 1000 * c_duration_2sec, -/*, -, -*/));
        log("Encoded message:  ", bit2oct(v_encMsg));
        // Check result
        if (not isbound(v_encMsg)) {
          setverdict(fail, "Encoding failed!");
          stop;
        }
        if (not match(v_encMsg, v_expEncMsg)) {
          log("Expected message: ", bit2oct(valueof(v_expEncMsg)));
          setverdict(fail, "Encoding failed, not the expected result!");
          stop;
        }
        setverdict(pass, "Encoding passed.");
      } // End of tc_Ivim_UtIvimTrigger_1
      
      /**
       * @desc Validate template Ivim/UtIvimTrigger
       * @verdict Pass on success, Fail otherwise
       */
      testcase tc_Ivim_UtIvimTrigger_2() runs on TCType system TCType {
        var bitstring v_encMsg;
        var integer v_res := 0;
        var bitstring v_expEncMsg := oct2bit('A420000000000000000000000000000000000004'O);
    
        // Encode template
        log("Encode template ", valueof(m_utTriggerEvent(float2int(4.0))));
      v_encMsg := encvalue(m_utTriggerEvent(float2int(4.0)));
        log("Encoded message:  ", bit2oct(v_encMsg));
        // Check result
        if (not isbound(v_encMsg)) {
          setverdict(fail, "Encoding failed!");
          stop;
        }
        if (not match(v_encMsg, v_expEncMsg)) {
          log("Expected message: ", bit2oct(valueof(v_expEncMsg)));
          setverdict(fail, "Encoding failed, not the expected result!");
          stop;
        }
        setverdict(pass, "Encoding passed.");
      } // End of tc_Ivim_UtIvimTrigger_2
      
      /**
       * @desc Validate template Ivim/UtIvimUpdate
       * @verdict Pass on success, Fail otherwise
       */
      testcase tc_Ivim_UtIvimUpdate() runs on TCType system TCType {
        var bitstring v_encMsg;
        var integer v_res := 0;
        var bitstring v_expEncMsg := oct2bit('A600000000000002000000000000000000000000'O);
    
        // Encode template
        log("Encode template ", valueof(m_utEventCancellation(4321)));
      v_encMsg := encvalue(m_utIvimUpdateEvent(4321));
        log("Encoded message:  ", bit2oct(v_encMsg));
        // Check result
        if (not isbound(v_encMsg)) {
          setverdict(fail, "Encoding failed!");
          stop;
        }
        if (not match(v_encMsg, v_expEncMsg)) {
          log("Expected message: ", bit2oct(valueof(v_expEncMsg)));
          setverdict(fail, "Encoding failed, not the expected result!");
          stop;
        }
        setverdict(pass, "Encoding passed.");
      } // End of tc_Ivim_UtIvimUpdate
      
      /**
       * @desc Validate template Ivim/UtIvimTermination
       * @verdict Pass on success, Fail otherwise
       */
      testcase tc_Ivim_UtIvimTermination() runs on TCType system TCType {
        var bitstring v_encMsg;
        var integer v_res := 0;
        var bitstring v_expEncMsg := oct2bit('A83039'O);
    
        // Encode template
        log("Encode template ", valueof(m_utEventCancellation(12345)));
      v_encMsg := encvalue(m_utEventCancellation(12345));
        log("Encoded message:  ", bit2oct(v_encMsg));
        // Check result
        if (not isbound(v_encMsg)) {
          setverdict(fail, "Encoding failed!");
          stop;
        }
        if (not match(v_encMsg, v_expEncMsg)) {
          log("Expected message: ", bit2oct(valueof(v_expEncMsg)));
          setverdict(fail, "Encoding failed, not the expected result!");
          stop;
        }
        setverdict(pass, "Encoding passed.");
      } // End of tc_Ivim_UtIvimTermination
      
    } // End of group testIvimUpperTester
    
    group testIvimPort {
            
      /**
       * @desc validate IvimReq
       * @verdict Pass on success, Fail otherwise
       */
      testcase tc_Ivim_Port() runs on ItsIvim system ItsIvimSystem {
        var IvimReq v_ivimReq;
        var template (value) Provider v_provider := {
        countryCode         := '1010101010'B,
        providerIdentifier  := 11
        };
	
        map(self:ivimPort, system:ivimPort);
        
      v_ivimReq := valueof(
                           m_ivimReq(
                                     m_ivimStructurePdu(
                                                        valueof(m_ivimStructure(
                                                                                valueof(m_iviManagementContainer(
                                                                                                                 v_provider,
                                                                                                                 1,
                                                                                                                 LibItsIvim_ASN1_IVInamedNumbers.IviStatus_new_
                                                                                                                 ))
                                                                                ))
                                                        )
                                     )
                           );
        ivimPort.send(v_ivimReq);
        tc_ac.start;
        alt {
          [] ivimPort.receive(IvimInd : ?) {
            setverdict(pass);
          }
          [] tc_ac.timeout {
            setverdict(fail, "Expected message not received1");
          }
        }
                
        unmap(self:ivimPort, system:ivimPort);
                
      }
            
    } // End of group testIvimPort 
        
  } // End of group LibItsIvim_testCases
    
  group encdec_functions {
        
    function TestIvimReq(
                         in template (value) IvimReq p_ivimReq,
                         in boolean p_decode := true, 
                         in template (omit) bitstring p_expEncMsg := omit 
                         ) runs on TCType {
      var bitstring v_encMsg;
      var template (omit) TestRecord v_tr := { bs := p_expEncMsg };
      var IvimReq v_decMsg;
      var integer v_res := 0;
    
      // Encode template
      log("Encode template ", valueof(p_ivimReq));
    v_encMsg := encvalue(p_ivimReq);
      log("Encoded message:  ", bit2oct(v_encMsg));
      // Check result
      if (not isbound(v_encMsg)) {
        setverdict(fail, "Encoding failed!");
        stop;
      }
      if (ispresent(v_tr.bs)) {
        if (not match(v_encMsg, p_expEncMsg)) {
          log("Expected message: ", bit2oct(valueof(p_expEncMsg)));
          setverdict(fail, "Encoding failed, not the expected result!");
          stop;
        }
      }
      setverdict(pass, "Encoding passed.");
    
      // Check decoding
      if (p_decode == true) {
      v_res := decvalue(v_encMsg, v_decMsg);
        log("Decoded message: ", v_decMsg);
        select (v_res) {
        case (0) {
          if(match(v_decMsg, p_ivimReq)) {
            setverdict(pass);
          } else {
            setverdict(fail);
          }
        }
        case (1) {
          setverdict(fail, "Decoding failed.");
        }
        case (2) {
          setverdict(fail, "Not enough bits.");
        }
        }
      }
            
    } // End of function TestIvimReq
        
    function TestIvimInd(
                         in template IvimInd p_ivimInd,
                         in octetstring p_expEncMsg 
                         ) runs on TCType {
      var bitstring v_encMsg := oct2bit(p_expEncMsg);
      var IvimInd v_decMsg;
      var integer v_res := 0;
      
      // Check decoding
    v_res := decvalue(v_encMsg, v_decMsg);
      select (v_res) {
      case (0) {
        log("v_decMsg: ", v_decMsg);
        if(match(valueof(v_decMsg), p_ivimInd)) {
          setverdict(pass);
        } else {
          setverdict(fail);
        }
      }
      case (1) {
        setverdict(fail, "Decoding failed.");
      }
      case (2) {
        setverdict(fail, "Not enough bits.");
      }
      } // End of 'select' statement
      
    } // End of function TestIvimInd
    
  } // End of group encdec_functions
  
  control {
    /*execute(tc_IvimReq());
    execute(tc_IvimInd_1());
    execute(tc_IvimInd_2());
    execute(tc_Ivim_Port());*/
    execute(tc_Ivim_UtIvimInitialize());
    execute(tc_Ivim_UtIvimTrigger_1());
    execute(tc_Ivim_UtIvimTrigger_2());
    execute(tc_Ivim_UtIvimUpdate());
    execute(tc_Ivim_UtIvimTermination());
  }
    
} // End of module TestCodec_IVIM 
