/******************************************************************************
 * Copyright (c) 2000-2021 Ericsson Telecom AB
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
 *
 * Contributors:
 *   Bence Janos Szabo – initial implementation
 *
 ******************************************************************************/
module UseNilLong {

import from XSD all;

type component CT {};

type record SoapEnvelope {
	SoapBody body
}
with {
variant "name as 'Envelope'";
variant "namespace as 'http://schemas.xmlsoap.org/soap/envelope/' prefix 'S'";
variant (body) "name as capitalized";
variant (body) "namespace as 'http://schemas.xmlsoap.org/soap/envelope/' prefix 'S'";
};

type union ApplicationBodyContent {
	GetResponse getResponse
}
with {
variant (getResponse) "name as capitalized"
variant (getResponse) "namespace as 'http://schemas.ericsson.com/cai3g1.2/' prefix 'ns2'";
};

type union SoapBody {
	record of ApplicationBodyContent content_list
}
with {
variant "namespace as 'http://schemas.xmlsoap.org/soap/envelope/' prefix 'S'";
variant (content_list) "untagged";
variant (content_list[-]) "untagged";
};

type record GetResponse
{
    GetResponseMOAttributesType mOAttributes optional
}with {
    variant "namespace as 'http://schemas.ericsson.com/cai3g1.2/' prefix 'ns2'";
    variant (mOAttributes) "name as capitalized";
    variant (mOAttributes) "namespace as 'http://schemas.ericsson.com/cai3g1.2/' prefix 'ns2'";
};


type record GetResponseMOAttributesType
{
    GetMODefinition getMODefinition
}
with {
    variant (getMODefinition) "untagged";
};

type AbstractGetAttributeType GetMODefinition;

type union AbstractGetAttributeType
{
    GetResponseMMTel getResponseMMTel
} with {
    variant (getResponseMMTel) "namespace as 'http://schemas.ericsson.com/mtas/mmtel/cai3g' prefix 'mc'"
    variant (getResponseMMTel) "name as uncapitalized";
};

type record GetResponseMMTel
{
    Communication_diversion_type communication_diversion optional
}
with {
variant "name as uncapitalized";
variant "element";
variant (communication_diversion) "name as 'communication-diversion'";
};

type record Communication_diversion_type
{
    record {
        Cdiv_user_configuration_type content optional
    } cdiv_user_configuration optional
}
with {
variant "name as 'communication-diversion-type'";
variant (cdiv_user_configuration) "name as 'cdiv-user-configuration'";
variant (cdiv_user_configuration) "useNil";
};

type record Cdiv_user_configuration_type
{
    record {
        Cdiv_ruleset_type content optional
    } cdiv_ruleset optional
}
with {
variant "name as 'cdiv-user-configuration-type'";
variant (cdiv_ruleset) "name as 'cdiv-ruleset'";
variant (cdiv_ruleset) "useNil";
};


type record Cdiv_ruleset_type
{
    record of record {
        Cdiv_rule_type content optional
    } cdiv_rule_list
}
with {
variant "name as 'cdiv-ruleset-type'";
variant (cdiv_rule_list) "untagged";
variant (cdiv_rule_list[-]) "name as 'cdiv-rule'";
variant (cdiv_rule_list[-]) "useNil";
};

type record Cdiv_rule_type
{
    XSD.NCName id optional,
    XSD.NCName id_1 optional,
    record {
        Cdiv_conditions_type content optional
    } cdiv_conditions optional
}
with {
variant "name as 'cdiv-rule-type'";
variant (id) "attribute";
variant (id_1) "name as 'id2'";
variant (cdiv_conditions) "name as 'cdiv-conditions'";
variant (cdiv_conditions) "useNil";
};


type record Cdiv_conditions_type
{
    Cdiv_call_state_type cdiv_call_state optional
}
with {
variant "name as 'cdiv-conditions-type'";
variant (cdiv_call_state) "name as 'cdiv-call-state'";
};


type enumerated Cdiv_call_state_type
{
    busy,
    no_answer,
    not_reachable,
    not_registered,
    unconditional
}
with {
variant "text 'no_answer' as 'no-answer'";
variant "text 'not_reachable' as 'not-reachable'";
variant "text 'not_registered' as 'not-registered'";
variant "name as 'cdiv-call-state-type'";
};

external function enc(in SoapEnvelope x) return octetstring with { extension "prototype(convert) encode(XER:XER_EXTENDED) errorbehavior(ALL:ERROR)"};  
external function dec(in octetstring x) return SoapEnvelope with { extension "prototype(convert) decode(XER:XER_EXTENDED) errorbehavior(ALL:ERROR)"};  


const SoapEnvelope SoapMessage :=
	{ body := 
		{ content_list := {
			{ getResponse := 
				{ mOAttributes := 
					{ getMODefinition := 
						{ getResponseMMTel := 
							{ communication_diversion := 
								{ cdiv_user_configuration := 
									{ content := 
										{ cdiv_ruleset := 
											{ content := 
												{ cdiv_rule_list := 
													{ 
														{ content := 
															{ id := "23", 
															  id_1 := "ff", 
															  cdiv_conditions := 
																{ content := {
																	cdiv_call_state := no_answer }
																} 
															} 
														}, 
														{ content := 
															{ id := omit,
															  id_1 := "ff",
															  cdiv_conditions := {
																content := {
																	cdiv_call_state := busy }
										 					  } 
															} 
														} 
													} 
												} 
											} 
										} 
									} 
								} 
							} 
						} 
					} 
				} 
			} 
		} 
	} 
};



testcase tc_encode_decode() runs on CT {
  if(dec(enc(SoapMessage)) == SoapMessage) {
    setverdict(pass);
  } else {
    setverdict(fail);
  }
}

testcase tc_decode() runs on CT {
  const charstring result := 
"<S:Envelope xmlns:S='http://schemas.xmlsoap.org/soap/envelope/' "&
"xmlns:ns2='http://schemas.ericsson.com/cai3g1.2/' "&
"xmlns:mc='http://schemas.ericsson.com/mtas/mmtel/cai3g'>"&
"<S:Body>"&
"<ns2:GetResponse>"&
"<ns2:MOAttributes>"&
"<mc:getResponseMMTel>"&
"<communication-diversion>"&
"<cdiv-user-configuration>"&
"<cdiv-ruleset>"&
"<cdiv-rule id='sfsf'>"&
"<id2>ff</id2>"&
"<cdiv-conditions>"&
"<cdiv-call-state>no-answer</cdiv-call-state>"&
"</cdiv-conditions>"&
"</cdiv-rule>"&
"<cdiv-rule id='2fd3'>"&
"<id2>ffa</id2>"&
"<cdiv-conditions>"&
"<cdiv-call-state>busy</cdiv-call-state>"&
"</cdiv-conditions>"&
"</cdiv-rule>"&
"</cdiv-ruleset>"&
"</cdiv-user-configuration>"&
"</communication-diversion>"&
"</mc:getResponseMMTel>"&
"</ns2:MOAttributes>"&
"</ns2:GetResponse>"&
"</S:Body>"&
"</S:Envelope>";
log(result);

const charstring result_omit_attr :=
"<S:Envelope xmlns:S='http://schemas.xmlsoap.org/soap/envelope/' "&
"xmlns:ns2='http://schemas.ericsson.com/cai3g1.2/' "&
"xmlns:mc='http://schemas.ericsson.com/mtas/mmtel/cai3g'>"&
"<S:Body>"&
"<ns2:GetResponse>"&
"<ns2:MOAttributes>"&
"<mc:getResponseMMTel>"&
"<communication-diversion>"&
"<cdiv-user-configuration>"&
"<cdiv-ruleset>"&
"<cdiv-rule>"&
"<id2>ff</id2>"&
"<cdiv-conditions>"&
"<cdiv-call-state>no-answer</cdiv-call-state>"&
"</cdiv-conditions>"&
"</cdiv-rule>"&
"<cdiv-rule id='2fd3'>"&
"<id2>ffa</id2>"&
"<cdiv-conditions>"&
"<cdiv-call-state>busy</cdiv-call-state>"&
"</cdiv-conditions>"&
"</cdiv-rule>"&
"</cdiv-ruleset>"&
"</cdiv-user-configuration>"&
"</communication-diversion>"&
"</mc:getResponseMMTel>"&
"</ns2:MOAttributes>"&
"</ns2:GetResponse>"&
"</S:Body>"&
"</S:Envelope>";

const charstring result_omit_attr2 :=
"<S:Envelope xmlns:S='http://schemas.xmlsoap.org/soap/envelope/' "&
"xmlns:ns2='http://schemas.ericsson.com/cai3g1.2/' "&
"xmlns:mc='http://schemas.ericsson.com/mtas/mmtel/cai3g'>"&
"<S:Body>"&
"<ns2:GetResponse>"&
"<ns2:MOAttributes>"&
"<mc:getResponseMMTel>"&
"<communication-diversion>"&
"<cdiv-user-configuration>"&
"<cdiv-ruleset>"&
"<cdiv-rule>"&
"<id2>ff</id2>"&
"<cdiv-conditions>"&
"<cdiv-call-state>no-answer</cdiv-call-state>"&
"</cdiv-conditions>"&
"</cdiv-rule>"&
"<cdiv-rule>"&
"<cdiv-conditions>"&
"<cdiv-call-state>busy</cdiv-call-state>"&
"</cdiv-conditions>"&
"</cdiv-rule>"&
"</cdiv-ruleset>"&
"</cdiv-user-configuration>"&
"</communication-diversion>"&
"</mc:getResponseMMTel>"&
"</ns2:MOAttributes>"&
"</ns2:GetResponse>"&
"</S:Body>"&
"</S:Envelope>";

const charstring result_single :=
"<S:Envelope xmlns:S='http://schemas.xmlsoap.org/soap/envelope/' "&
"xmlns:ns2='http://schemas.ericsson.com/cai3g1.2/' "&
"xmlns:mc='http://schemas.ericsson.com/mtas/mmtel/cai3g'>"&
"<S:Body>"&
"<ns2:GetResponse>"&
"<ns2:MOAttributes>"&
"<mc:getResponseMMTel>"&
"<communication-diversion>"&
"<cdiv-user-configuration>"&
"<cdiv-ruleset>"&
"<cdiv-rule id='aa'>"&
"<id2>ff</id2>"&
"<cdiv-conditions>"&
"<cdiv-call-state>no-answer</cdiv-call-state>"&
"</cdiv-conditions>"&
"</cdiv-rule>"&
"</cdiv-ruleset>"&
"</cdiv-user-configuration>"&
"</communication-diversion>"&
"</mc:getResponseMMTel>"&
"</ns2:MOAttributes>"&
"</ns2:GetResponse>"&
"</S:Body>"&
"</S:Envelope>";

const charstring result_single_omit_attr :=
"<S:Envelope xmlns:S='http://schemas.xmlsoap.org/soap/envelope/' "&
"xmlns:ns2='http://schemas.ericsson.com/cai3g1.2/' "&
"xmlns:mc='http://schemas.ericsson.com/mtas/mmtel/cai3g'>"&
"<S:Body>"&
"<ns2:GetResponse>"&
"<ns2:MOAttributes>"&
"<mc:getResponseMMTel>"&
"<communication-diversion>"&
"<cdiv-user-configuration>"&
"<cdiv-ruleset>"&
"<cdiv-rule>"&
"<id2>ff</id2>"&
"<cdiv-conditions>"&
"<cdiv-call-state>no-answer</cdiv-call-state>"&
"</cdiv-conditions>"&
"</cdiv-rule>"&
"</cdiv-ruleset>"&
"</cdiv-user-configuration>"&
"</communication-diversion>"&
"</mc:getResponseMMTel>"&
"</ns2:MOAttributes>"&
"</ns2:GetResponse>"&
"</S:Body>"&
"</S:Envelope>";

var SoapEnvelope SoapMsg :=
	{ body := 
		{ content_list := {
			{ getResponse := 
				{ mOAttributes := 
					{ getMODefinition := 
						{ getResponseMMTel := 
							{ communication_diversion := 
								{ cdiv_user_configuration := 
									{ content := 
										{ cdiv_ruleset := 
											{ content := 
												{ cdiv_rule_list := 
													{ 
														{ content := 
															{ id := "sfsf", 
															  id_1 := "ff", 
															  cdiv_conditions := 
																{ content := {
																	cdiv_call_state := no_answer }
																} 
															} 
														}, 
														{ content := 
															{ id := "2fd3",
															  id_1 := "ffa",
															  cdiv_conditions := {
																content := {
																	cdiv_call_state := busy }
										 					  } 
															} 
														} 
													} 
												} 
											} 
										} 
									} 
								} 
							} 
						} 
					} 
				} 
			} 
		} 
	} 
};

var SoapEnvelope SoapMsg_Single :=
	{ body := 
		{ content_list := {
			{ getResponse := 
				{ mOAttributes := 
					{ getMODefinition := 
						{ getResponseMMTel := 
							{ communication_diversion := 
								{ cdiv_user_configuration := 
									{ content := 
										{ cdiv_ruleset := 
											{ content := 
												{ cdiv_rule_list := 
													{ 
														{ content := 
															{ id := "aa", 
															  id_1 := "ff", 
															  cdiv_conditions := 
																{ content := {
																	cdiv_call_state := no_answer }
																} 
															} 
														}
													} 
												} 
											} 
										} 
									} 
								} 
							} 
						} 
					} 
				} 
			} 
		} 
	} 
};

if(dec(char2oct(result)) == SoapMsg) {
  setverdict(pass);
} else {
  setverdict(fail);
}

SoapMsg.body.content_list[0].getResponse.mOAttributes.getMODefinition.getResponseMMTel.communication_diversion.cdiv_user_configuration.content.cdiv_ruleset.content.cdiv_rule_list[0].content.id := omit;

if(dec(char2oct(result_omit_attr)) == SoapMsg) {
  setverdict(pass);
} else {
  setverdict(fail);
}

SoapMsg.body.content_list[0].getResponse.mOAttributes.getMODefinition.getResponseMMTel.communication_diversion.cdiv_user_configuration.content.cdiv_ruleset.content.cdiv_rule_list[1].content.id := omit;
SoapMsg.body.content_list[0].getResponse.mOAttributes.getMODefinition.getResponseMMTel.communication_diversion.cdiv_user_configuration.content.cdiv_ruleset.content.cdiv_rule_list[1].content.id_1 := omit;

if(dec(char2oct(result_omit_attr2)) == SoapMsg) {
  setverdict(pass);
} else {
  setverdict(fail);
}

if(dec(char2oct(result_single)) == SoapMsg_Single) {
  setverdict(pass);
} else {
  setverdict(fail);
}

SoapMsg_Single.body.content_list[0].getResponse.mOAttributes.getMODefinition.getResponseMMTel.communication_diversion.cdiv_user_configuration.content.cdiv_ruleset.content.cdiv_rule_list[0].content.id := omit;

if(dec(char2oct(result_single_omit_attr)) == SoapMsg_Single) {
  setverdict(pass);
} else {
  setverdict(fail);
}

}

control {
  execute(tc_encode_decode());
  execute(tc_decode());
}

}
with {
encode "XML";
variant "controlNamespace 'http://www.w3.org/2001/XMLSchema-instance' prefix 'xsi'";
variant "elementFormQualified";
}
