/***************************************************
 ** @author   STF 475
 ** @version  0.0.1
 ** @purpose  9:7.6.5, Verify that choice content with maxOccurs larger than 1 is correctly transformed
 ** @verdict  pass accept, ttcn3verdict:pass
***************************************************/
// The following requirements are tested:
// If the value of the minOccurs or the maxOccurs attributes or both differ
//  from "1", the following rules shall apply:
// a) The union field shall be generated as above (including attaching the
// "untagged" encoding instruction).
// b) The procedures in clause 7.1.4 shall be called for the union field.
// NOTE: As the result of applying clause 7.1.4, the type of the field may be
// changed to record of union and in parallel the name of the field may be
// changed to "choice_list".
// c) Finally, clause 5.2.2 shall be applied to the name of the resulted field
// and subsequently the field shall be added to the enframing TTCN-3 record
// type (see clause 7.6) or record or union field corresponding to the parent
// of the mapped choice compositor.

module Pos_070605_top_level_002 {

   import from schema_Pos_070605_top_level_002 language "XSD" all;

   template MyType m_msg := {
      choice_list := {
         {
	        foo := 1
         }, {
            bar := "bar"
         }
      }
   };



    /**
     * @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 universal charstring File;
    type record of File FileList;

    type port P message {
        inout all;
    }
    type component C {
        port P p;
    }

    /**
     * @desc lexical compare the charstring p_textToMatch with the contents of the reference XML file and returns true if they represent the same XML structure
     * @param p_textToMatch text to be compared with the UTF-8 contents of the XML file
     * @param p_referenceXmlFile the XML file
     * @param p_xsdFileList the list of XSD files
     * @param p_matchError the error result in case it did not match
     * @param p_referenceTTCN3File the file of the TTCN-3 test module. This path is used to find the reference XML file relative to this path, by keeping the TTCN-3 code file system independent.
     * @return true if p_textToMatch and the contents of p_referenceXmlFile represent the same XML structure
     */
    external function matchFile(Raw p_textToMatch, File p_referenceXmlFile, FileList p_xsdFileList, out universal charstring p_matchError, File p_referenceTTCN3File := __FILE__) return boolean;

    testcase TC_Pos_070605_top_level_002() runs on C system C {
        var Raw v_rcv;
        var universal charstring v_matchError;

        map(self:p, system:p);

        // encode the message
        p.send(m_msg);

        alt {
            // compare the encoded message with the reference XML file
            []    p.check(receive(Raw:?) -> value v_rcv) {
                log("XML message ", v_rcv);
                if (matchFile(v_rcv, "Pos_070605_top_level_002.xml", { "Pos_070605_top_level_002.xsd" }, v_matchError)) {
                    alt {
                        // match decoded value to pass test
                        [] p.receive(m_msg) {
                            setverdict(pass, "Decoded value matches encoded template and reference XML");
                        }
                        [] p.receive {
                            setverdict(fail, "XML decoding failure");
                        }
                    }
                } else {
                    setverdict(fail, v_matchError);
                }
            }
            [] p.receive {
                setverdict(fail, "Raw decoding failure");
            }
        }
    }

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


}
