package com.ejie.ad74a.service.test.platea;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ejie.ad74a.jms.service.LibroRegistroJMSService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:test/application-context.xml")
public class TestPapel0Service {

	@Autowired
	private LibroRegistroJMSService libroRegistroJMSService;

	@Test
	@Transactional(propagation = Propagation.SUPPORTS)
	@Rollback(false)
	public void requestAltaMessage() {

		Message inMessage = mock(Message.class);
		TextMessage msg = mock(TextMessage.class);

		String nombre = "CDAR_BUENA.xml";
		String basePath = "D:/taadmin/trunk/datos/ad74a/file/";
		String eventoXML = leerFicheroIS(basePath + nombre);

		try {
			when(msg.getText()).thenReturn(eventoXML);
		} catch (JMSException e1) {

		}

		try {
			inMessage = msg;
			this.libroRegistroJMSService.tratarMessage(inMessage);
		} catch (Throwable e) {
			e.printStackTrace();
			Assert.fail("Error al procesar el message");
		}
		// Assert.assertTrue("Se ha encontrado un registro que no existe", true);
	}

	private String leerFicheroIS(String rutaFichero) {
		BufferedReader b;
		String cadena;
		StringBuilder resultXml = new StringBuilder();
		try {
			b = new BufferedReader(new InputStreamReader(new FileInputStream(rutaFichero), "utf-8"));
			while ((cadena = b.readLine()) != null) {
				resultXml.append(cadena);
			}
			b.close();
		} catch (Throwable e) {
			e.printStackTrace();
		}
		return resultXml.toString();
	}

}
