package eu.dnetlib.clients;

import static org.junit.Assert.assertNotNull;
import deltix.qsrv.comm.xml.ThrowableTransientAnnotationReaderBuilder;
import eu.dnetlib.api.enabling.A2ServiceException;
import eu.dnetlib.clients.enabling.aas.ws.AASClient;
import eu.dnetlib.enabling.aas.rmi.A2Service;
import eu.dnetlib.utils.md5.MD5;
import gr.uoa.di.driver.enabling.a2.A2Wrapper;
import gr.uoa.di.driver.enabling.a2.A2WrapperImpl;
import gr.uoa.di.driver.util.StaticServiceLocator;

import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

import org.apache.cxf.jaxb.JAXBDataBinding;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.junit.BeforeClass;
import org.junit.Test;

public class TestA2Wrapper {

	private static A2Wrapper a2Wrapper = new A2WrapperImpl();

	@BeforeClass
	public static void setup() throws Throwable {
		try {
			JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
			factory.setServiceClass(A2Service.class);
			factory.setAddress("http://88.197.20.214/a2service-ws/A2Service");

			 JAXBDataBinding dataBinding = new JAXBDataBinding();
             Map<String, Object> contextProperties = new HashMap<String, Object>();
             try {
                     contextProperties.put(com.sun.xml.bind.api.JAXBRIContext.ANNOTATION_READER, 
                             new ThrowableTransientAnnotationReaderBuilder().buildInstance());
             } catch(Exception e) {
                     throw new RuntimeException(e);
             }
             dataBinding.setContextProperties(contextProperties);
             factory.setDataBinding(dataBinding);
			
			A2Service webService = (A2Service) factory.create();
			
			AASClient aasClient = new AASClient();
			aasClient.setA2Service(webService);
			StaticServiceLocator<eu.dnetlib.api.enabling.A2Service> locator = new StaticServiceLocator<eu.dnetlib.api.enabling.A2Service>();
			
			locator.setService(aasClient);

			((A2WrapperImpl) a2Wrapper).setLocator(locator);
		} catch (Throwable t) {
			t.printStackTrace();

			throw t;
		}
	}
	
	@Test
	public void test() throws A2ServiceException, NoSuchAlgorithmException {
		String secContextId = a2Wrapper.authenticateUser("kiatrop@di.uoa.gr", 
				MD5.encrypt2Hex("1234"));
		
		assertNotNull(secContextId);
	}
}
