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.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.apache.commons.httpclient.HttpException;
import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.jboss.resteasy.core.Dispatcher;
import org.jboss.resteasy.mock.MockDispatcherFactory;
import org.jboss.resteasy.mock.MockHttpRequest;
import org.jboss.resteasy.mock.MockHttpResponse;
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 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 Dispatcher dispatcher;

    private ConfigurableApplicationContext factory;

    @Before
    public void setUp() throws URISyntaxException {
        dispatcher = MockDispatcherFactory.createDispatcher();

        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);
    }

    @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"));

        MockHttpRequest request = MockHttpRequest
                .get("/filesystem/getFile/content/A");
        MockHttpResponse response = new MockHttpResponse();
        dispatcher.invoke(request, response);

        Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatus());
        String responseBodyAsString = response.getContentAsString();

        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);
    }
}

