/******************************************************************************
 * 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  1:15.8, Ensure that decoded content match is allowed for present template restriction
 ** @verdict  pass accept, ttcn3verdict:pass
 ***************************************************/

//RAW encoding adds extra bits, up to 8 bit, to a bitstring because it encodes anything into an octetstring

module Sem_1508_TemplateRestrictions_037 { 

	type record MessageType {
	  	hexstring payload
	}
	
	type record Mymessage {
	  	integer field1,
	  	bitstring field2 optional
    } with { variant " "};
	

   	type port loopbackPort message
	{inout MessageType} with {extension "internal"};
	

	type component GeneralComp {	    	    
		  port loopbackPort messagePort
	}	

	testcase TC_Sem_1508_TemplateRestrictions_037() runs on GeneralComp {
		var bitstring v_enc;
		var Mymessage v_testMessage;
		var MessageType Message;

		var MessageType got;
		var Mymessage dec_got;
		
		v_testMessage:=  {  
		    field1 := 10,
		    field2 := '1001'B
		} 

		Message.payload := bit2hex(encvalue(v_testMessage)); //encode message to payload
		      
 		template (present) MessageType mw_matchingTemplate:=
  		{  			
  			payload :=  decmatch Mymessage: {field1:= 10, field2 := '00001001'B}
  		} 

		connect(self:messagePort, self:messagePort);
	  	messagePort.send(Message);	//send message

		var bitstring bitpayload := hex2bit(Message.payload);

		var integer v_res := decvalue(bitpayload, dec_got);

		alt {
		 	[] messagePort.receive(mw_matchingTemplate) {
		    	setverdict(pass);
		 	}
		 	[] messagePort.receive(MessageType:?) -> value got  {
		    	setverdict(fail,"exepted: ", mw_matchingTemplate, " recieved: ", dec_got);
		 	}
		}
    
	}

	control{
		execute(TC_Sem_1508_TemplateRestrictions_037());
	}

} with { encode "RAW"}
 
