package com.temenos.webservices.ofsconnector.client;

import static org.junit.Assert.assertNotNull;

import java.net.URL;

import javax.xml.namespace.QName;

import ofsconnectorservicews.OFSConnectorServiceWS;
import ofsconnectorservicews.OFSConnectorServiceWSPortType;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.temenos.services.ofsconnector.data.response.xsd.ProcessOFSResponse;
import com.temenos.services.ofsconnector.data.xsd.OFSConnResponse;
import com.temenos.soa.services.data.xsd.Response;
import com.temenos.soa.services.data.xsd.ResponseDetails;
import com.temenos.soa.services.data.xsd.T24UserDetails;

public class OfsConnectorWSClientTest {

	OFSConnectorServiceWSPortType servicePort = null;
	com.temenos.soa.services.data.xsd.ObjectFactory objCommon = null;
	
	@Before
	public void setup() {
		// Initialise WS
		try {
//			Service service = Service.create(new URL(Configurations.WSDL_LOCATION), 
//					new QName(Configurations.SRV_NAMESPACE, Configurations.SRV_NAME));
//			servicePort = service.getPort(OFSConnectorServiceWS.class).getOFSConnectorServiceWSHttpSoap12Endpoint();
	 
			OFSConnectorServiceWS service = new OFSConnectorServiceWS(
					new URL("http://localhost:9090/axis2/services/OFSConnectorServiceWS?wsdl"), 
					new QName("http://OFSConnectorServiceWs", "OFSConnectorServiceWS"));
			servicePort = service.getOFSConnectorServiceWSHttpSoap12Endpoint();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		objCommon = new com.temenos.soa.services.data.xsd.ObjectFactory();
	}
	
	@After
	public void cleanup() {
		servicePort = null;
		objCommon = null;
	}
	
	@Test
	public void testProcessOfsUsingJavaWSForSuccess() {
		
		assertNotNull(servicePort);
		
		// Set User Details for an access
		T24UserDetails userDetails = objCommon.createT24UserDetails();
		userDetails.setUser(objCommon.createT24UserDetailsUser("MYUSER"));
		userDetails.setPassword(objCommon.createT24UserDetailsPassword("123456"));
		
		// Call the WS
		String ofsRequest = "SomeRequet";
		ProcessOFSResponse response = servicePort.processOFS(userDetails, ofsRequest);
		
		// Parse and print the response
		OFSConnResponse ofsResponse = response.getOfsResponse().getValue();
		System.out.println("OFS Response : " + ofsResponse.getOfsResponse().getValue());
		
		ResponseDetails responseDetails =  response.getResponseDetails().getValue();
		System.out.println("Response Details - Response (size)	: " + responseDetails.getResponses().size());
		if (responseDetails.getResponses().size() > 0) {
			System.out.println("	List Of Responses");
			System.out.println("	-----------------");
			int i = 1;
			for (Response res : responseDetails.getResponses()) {
				System.out.println("	" + i + " - Response Code : " + res.getResponseCode().getValue());
				System.out.println("	" + i + " - Response Type : " + res.getResponseType().getValue());
				System.out.println("	" + i + " - Response Text : " + res.getResponseText().getValue());
				System.out.println("	" + i + " - Response Info : " + res.getResponseInfo().getValue());
				i++;
			}
		}
		System.out.println("Response Details - Return Code		: " + responseDetails.getReturnCode().getValue());	
		System.out.println("Response Details - Subroutine		: " + responseDetails.getServiceName().getValue());
	}
}
