package org.fedorahosted.flies.core.rest;

import java.io.ByteArrayInputStream;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.core.MediaType;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.fedorahosted.flies.rest.MediaTypes;
import org.fedorahosted.flies.rest.client.ProjectResource;
import org.fedorahosted.flies.rest.dto.ProjectRef;
import org.fedorahosted.flies.rest.dto.ProjectRefs;
import org.jboss.resteasy.client.ClientRequestFactory;
import org.jboss.resteasy.client.ClientResponse;
import org.jboss.resteasy.plugins.providers.RegisterBuiltin;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.jboss.seam.mock.EnhancedMockHttpServletRequest;
import org.jboss.seam.mock.EnhancedMockHttpServletResponse;
import org.jboss.seam.mock.ResourceRequestEnvironment;
import org.jboss.seam.mock.SeamTest;
import org.jboss.seam.mock.ResourceRequestEnvironment.Method;
import org.jboss.seam.mock.ResourceRequestEnvironment.ResourceRequest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.*;
import static org.testng.Assert.*;

@Test(groups={"seam-tests"})
public class ProjectServiceSeamTest extends SeamTest {

	ResourceRequestEnvironment sharedEnvironment;

	@BeforeClass
	public void prepareSharedEnvironment() throws Exception {
		sharedEnvironment = new ResourceRequestEnvironment(this) {
			@Override
			public Map<String, Object> getDefaultHeaders() {
				return new HashMap<String, Object>() {
					{
						put("X-Auth-Token", "bob");
					}
				};
			}
		};
	}

	public void retrieveListOfProjectsAsJson() throws Exception {
		new ResourceRequest(sharedEnvironment, Method.GET, "/restv1/projects") {
			@Override
			protected void prepareRequest(EnhancedMockHttpServletRequest request) {
				request.addHeader("Accept", MediaType.APPLICATION_JSON);
			}

			@Override
			protected void onResponse(EnhancedMockHttpServletResponse response) {
				assertThat( response.getStatus(), is(200));
			}
		};
	}
	public void retrieveListOfProjectsAsXml() throws Exception {
		new ResourceRequest(sharedEnvironment, Method.GET, "/restv1/projects") {
			@Override
			protected void prepareRequest(EnhancedMockHttpServletRequest request) {
				request.addHeader("Accept", MediaTypes.APPLICATION_FLIES_PROJECTS_XML);
			}

			@Override
			protected void onResponse(EnhancedMockHttpServletResponse response) {
				assertThat( response.getStatus(), is(200));
				
				// check that the content we get back is XML
				byte [] xmlVal = response.getContentAsByteArray();
				JAXBContext context;
				ProjectRefs projectRefs = null;
				try {
					context = JAXBContext.newInstance(ProjectRefs.class);
					Unmarshaller unmarshaller = context.createUnmarshaller();
					projectRefs = (ProjectRefs) unmarshaller.unmarshal(new ByteArrayInputStream(xmlVal));
					
				} catch (JAXBException e) {
					fail("Failed to initialize Jaxb", e);
				}
				
				assertThat( projectRefs, notNullValue() );
				
				assertThat( projectRefs.getProjects().size(), is(1) );
				
				ProjectRef projectRef = projectRefs.getProjects().get(0);
				assertThat( projectRef.getName(), is("Sample Project"));
			}

		}.run();
	}
	

	@Test
	public void doAnotherTry() throws Exception{
		  ResteasyProviderFactory instance = ResteasyProviderFactory.getInstance();
		  RegisterBuiltin.register(instance);

		  ClientRequestFactory clientRequestFactory = new ClientRequestFactory(new SeamMockClientExecutor(this), new URI("/restv1/"));
		  ClientResponse<ProjectRefs> response = clientRequestFactory.createProxy(ProjectResource.class).getProjects();

		  assert response.getStatus() == 200;
		  
		  assert response.getEntity() != null;
		  assert response.getEntity().getProjects().size() == 1;

	}	
}
@@ -1746,16 +1746,133 @@
 %09%7D%0A%09%09%7D;%0A
+%09%09%0A%09%09ResteasyProviderFactory instance = ResteasyProviderFactory.getInstance();%0A%09%09RegisterBuiltin.register(instance);%0A
 %09%7D%0A%0A%09pub
@@ -3497,16 +3497,8 @@
 %09%7D%0A%09
-%0A%0A%09@Test
 %0A%09pu
@@ -3511,164 +3511,71 @@
 oid 
-doAnotherTry() throws Exception%7B%0A%09%09  ResteasyProviderFactory instance = ResteasyProviderFactory.getInstance();%0A%09%09  RegisterBuiltin.register(instance);
+retrieveProjectRefsUsingClientFramework() throws Exception%7B
 %0A%0A%09%09
-  
 Clie
@@ -3697,18 +3697,16 @@
 /%22));%0A%09%09
-  
 ClientRe
@@ -3807,27 +3807,32 @@
 ects();%0A
+%09%09
 %0A%09%09
-  
 assert
+That(
  respons
@@ -3848,32 +3848,34 @@
 us()
- == 200;%0A%09%09  
+, is(200) );
 %0A%09%09
-  
 assert
+That(
  res
@@ -3895,28 +3895,41 @@
 ty()
- != null
+, notNullValue() )
 ;%0A%09%09
-  
 assert
+That(
  res
@@ -3970,13 +3970,17 @@
 ze()
- == 1
+, is(1) )
 ;%0A%0A%09

clean up test