/******************************************************************************
 * Copyright (c) ETSI 2017.
 *
 * This file is subject to copyrights owned by ETSI. Non-exclusive permission
 * is hereby granted, free of charge, to copy, reproduce and amend this file
 * under the following conditions: It is provided "as is", without warranty of any
 * kind, expressed or implied.
 * 
 * ETSI shall never be liable for any claim, damages, or other liability arising
 * from its use or inability of use.This permission does not apply to any documentation
 * associated with this file for which ETSI keeps all rights reserved. The present
 * copyright notice shall be included in all copies of whole or part of this
 * file and shall not imply any sub-license right.
 *
 * Modified by: Adrien Kirjak
 *
 ** @version  0.0.1
 ** @purpose  9:7.6.2.1, Verify mapping of complex type where both the base and the 
 **           extending types have the compositor sequence and multiple occurrences 
 **           are allowed.
 ** @verdict  pass accept, ttcn3verdict:pass
 ***************************************************/
module Pos_07060201_derived_by_extension_004 {

    import from schema_Pos_07060201_derived_by_extension_004 language "XSD" all;
//  template for EXAMPLE 2: type element e26seqReccurrence and two records of sequence_list
    // and following type system shall be generated from xsd file:
    /*
    type record E1 {
        // fields corresponding to attributes of the base and the extending type
        // (in alphabetical order)
        XSD.Integer genderAttrBase optional,
        XSD.String jobPositionAttrGroup optional,
        XSD.String unitOfAge optional,
        // followed by a "simple" field list corresponding to elements of the base type
        XSD.String titleElemBase,
        XSD.String forenameElemBase,
        XSD.String surnameElemBase,
        // the extending sequence is recurring (see clause 7.6.6.6 for the mapping)
        record of record {
            G25seq g25seq
            XSD.Integer ageElemExt,
        } sequence_list
    }
    with {
        variant "name as uncapitalized";
        variant(sequence_list) "untagged";
        variant (genderAttrBase, jobPositionAttrGroup, unitOfAge) "attribute"
    
      */

    template E1 m_msg := {
      genderAttrBase := omit,
      jobPositionAttrGroup  := omit,
      unitOfAge := omit,
      titleElemBase := "titleElemBase",
      forenameElemBase := "forenameElemBase",
      surnameElemBase := "surnameElemBase",
      sequence_list := {
      	{
			ageElemExt := 1,
      		familyStatusElemInGroup :="familyStatusElemInGroup",
       		spouseElemInGroup := omit
      	},
      	{
            ageElemExt := 2,
			familyStatusElemInGroup :="familyStatusElemInGroup2",
			spouseElemInGroup := omit
      	}
      }
    }

	external function enc_MyType(in E1 pdu) return octetstring
  	 with { extension "prototype (convert) encode(XER:XER_EXTENDED)" }
  
  	external function dec_MyType(in octetstring stream) return E1
  	 with { extension "prototype (convert) decode(XER:XER_EXTENDED)" }

    /**
     * @desc The timeout given in seconds after which the test case will be stopped.
     */
    modulepar float PX_TC_EXECUTION_TIMEOUT := 5.0;

    type universal charstring Raw;

    type port P message {
        inout Raw;
    } with {extension "internal"}

    type component C {
        port P p;
		port P p1;
    }

    testcase TC_Pos_07060201_derived_by_extension_004() runs on C system C {
        var Raw v_rcv;

        var Raw  v_Raw:="<ns162:e1 xmlns:ns162='schema:Pos_07060201_derived_by_extension_004'>\n\t<titleElemBase>titleElemBase</titleElemBase>\n\t<forenameElemBase>forenameElemBase</forenameElemBase>\n\t<surnameElemBase>surnameElemBase</surnameElemBase>\n\t<ageElemExt>1</ageElemExt>\n\t<familyStatusElemInGroup>familyStatusElemInGroup</familyStatusElemInGroup>\n\t<ageElemExt>2</ageElemExt>\n\t<familyStatusElemInGroup>familyStatusElemInGroup2</familyStatusElemInGroup>\n</ns162:e1>\n\n";

        connect(self:p, self:p1);

		p.send(oct2unichar(enc_MyType(valueof(m_msg))));

        alt {
            // compare the encoded message with the reference XML file
            [] p1.check(receive(Raw:?) -> value v_rcv) {
                log("XML message ", v_rcv);
                if (match(v_rcv, v_Raw) ) {
					alt {
                     	[] p1.receive(Raw:?) {
							if(match(dec_MyType(unichar2oct(v_rcv)),m_msg))	{
								setverdict(pass, "Decoded value matches encoded template and reference XML");
							}
							else 
								{setverdict(fail, "XML decoding failure, reason:", dec_MyType(unichar2oct(v_rcv)));}	
						}
					}
                } else {
                    setverdict(fail, "Unexpected message: ", v_rcv, " expected: ", v_Raw);
                }
            }
            [] p1.receive {
                setverdict(fail, "Raw decoding failure");
            }
        }
    }

    control {
        execute(TC_Pos_07060201_derived_by_extension_004(), PX_TC_EXECUTION_TIMEOUT);
    }

}
