package com.engroup.module.ecm.service.test;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.net.URISyntaxException;
import java.util.Calendar;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.jboss.resteasy.core.Dispatcher;
import org.jboss.resteasy.mock.MockDispatcherFactory;
import org.jboss.resteasy.plugins.server.tjws.TJWSEmbeddedJaxrsServer;
import org.jboss.resteasy.plugins.spring.SpringBeanProcessor;
import org.jboss.resteasy.plugins.spring.SpringResourceFactory;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.engroup.module.ecm.ContentConstants;
import com.engroup.module.ecm.domain.File;
import com.engroup.module.ecm.domain.Resource;
import com.engroup.module.ecm.domain.SimpleFile;
import com.engroup.module.ecm.service.FileSystemService;

/** 
 *
 */
public class FileSystemRestTest {
    protected FileSystemService fileSystemService;

    private ConfigurableApplicationContext factory;

    @Before
    public void setUp() throws URISyntaxException {
        Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
        TJWSEmbeddedJaxrsServer tjws = new TJWSEmbeddedJaxrsServer();
        tjws.setPort(7070);
        tjws.setDispatcher(dispatcher);

        SpringBeanProcessor processor = new SpringBeanProcessor(dispatcher,
                null, null);
        factory = new ClassPathXmlApplicationContext("ecm-context-test.xml");
        factory.addBeanFactoryPostProcessor(processor);

        SpringResourceFactory noDefaults = new SpringResourceFactory(
                "fileSystemService", factory, FileSystemService.class);
        dispatcher.getRegistry().addResourceFactory(noDefaults);
        tjws.start();
    }

    @Test
    public void testGetCollectionOfContents() throws URISyntaxException,
            HttpException, IOException, JAXBException {
        fileSystemService = (FileSystemService) factory
                .getBean("fileSystemService");
        Resource resource = new Resource();
        resource.setData(new ByteArrayInputStream("this is the content"
                .getBytes()));
        resource.setLastModified(Calendar.getInstance());
        resource.setMimeType("plain/text");
        File file = new File();
        file.setPath("/A");
        file.setResource(resource);
        fileSystemService.save(file);

        Assert.assertNotNull(fileSystemService
                .findByPath(ContentConstants.FILE_ROOT_PATH + "/A"));

        HttpClient client = new HttpClient();
        GetMethod method = new GetMethod(
                "http://localhost:7070/filesystem/getFile/content/A");
        int status = client.executeMethod(method);

        Assert.assertEquals(200, status);
        
        String responseBodyAsString = method.getResponseBodyAsString();

        JAXBContext jaxbContext = JAXBContext.newInstance(SimpleFile.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        SimpleFile resultFile = (SimpleFile) unmarshaller
                .unmarshal(new StringReader(responseBodyAsString));

        Assert.assertEquals(ContentConstants.FILE_ROOT_PATH + "/A", resultFile
                .getPath());

        fileSystemService.remove(file);
    }
}
