/******************************************************************************
 * 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:5.1.4, Verify that unqualified default element form is correctly processed
 ** @verdict  pass accept, ttcn3verdict:pass
***************************************************/
// The following requirements are tested:
// All fields of TTCN-3 definitions in the given TTCN-3 module corresponding to local 
// element declarations or element and model group references in schema element information 
// items with the value of its elementFormDefault attribute unqualified (explicitly or 
// implicitly via defaulting) shall be supplied with the "form as unqualified" encoding 
// instruction, unless a form attribute of the given declaration requires differently 
// (see clause 7.1.6).
//
// Note: as only end-to-end solution is tested, the presence of encoding attributes is not
// checked directly, but through the encoded messages.
module Pos_050104_attributes_of_the_xsd_schema_element_003 {

   import from schema_Pos_050104_attributes_of_the_xsd_schema_element_003 language "XSD" all;

   template MyType m_msg := 
   {
      item1 := "abc",
      item2 := "def"
   };

	external function enc_MyType(in MyType pdu) return octetstring
  	 with { extension "prototype (convert) encode(XER:XER_EXTENDED)" }
  
  	external function dec_MyType(in octetstring stream) return MyType
  	 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 MyType, Raw;
    } with {extension "internal"}

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

    testcase TC_Pos_050104_attributes_of_the_xsd_schema_element_003() runs on C system C {
        var Raw v_rcv;
        
		var Raw  v_Raw:="<ns009:MyType xmlns:ns009='schema:Pos_050104_attributes_of_the_xsd_schema_element_003'>\n\t<item1>abc</item1>\n\t<item2>def</item2>\n</ns009:MyType>\n\n";

        connect(self:p, self:p1);

        // encode the message
        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_050104_attributes_of_the_xsd_schema_element_003(), PX_TC_EXECUTION_TIMEOUT);
    }


}
