package org.fedorahosted.flies.rest.service;

import java.net.URI;

import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.dbunit.operation.DatabaseOperation;
import org.fedorahosted.flies.rest.ApiKeyHeaderDecorator;
import org.fedorahosted.flies.rest.client.IProjectsResource;
import org.fedorahosted.flies.rest.dto.Project;
import org.fedorahosted.flies.rest.dto.ProjectList;
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.DBUnitSeamTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static org.testng.Assert.fail;


@Test(groups = { "seam-tests" })
public class ProjectsServiceSeamTest extends DBUnitSeamTest {

	ClientRequestFactory clientRequestFactory;
	IProjectsResource projectService;

	@BeforeClass
	public void prepareRestEasyClientFramework() throws Exception {

		ResteasyProviderFactory instance = ResteasyProviderFactory
				.getInstance();
		RegisterBuiltin.register(instance);

		clientRequestFactory = new ClientRequestFactory(
				new SeamMockClientExecutor(this), new URI("/restv1/"));

		clientRequestFactory.getPrefixInterceptors().registerInterceptor(
				new ApiKeyHeaderDecorator("admin",
						"12345678901234567890123456789012"));

		projectService = clientRequestFactory
				.createProxy(IProjectsResource.class);

	}

	@Override
	protected void prepareDBUnitOperations() {
		beforeTestOperations.add(new DataSetOperation(
				"org/fedorahosted/flies/test/model/ProjectData.dbunit.xml",
				DatabaseOperation.CLEAN_INSERT));
	}

	public void retrieveListofProjects() throws Exception {
		ClientResponse<ProjectList> response = projectService.get();

		assertThat(response.getStatus(), is(200));
		assertThat(response.getEntity(), notNullValue());
		assertThat(response.getEntity().getProjects().size(), is(1));

	}
	
	public void createProject(){
		Project project = new Project("my-new-project", "My New Project", "Another test project");

		projectService = clientRequestFactory.createProxy(IProjectsResource.class);
		
		Response response = projectService.post(project);
		
		assertThat( response.getStatus(), is( Status.CREATED.getStatusCode()));
		
		String location = (String) response.getMetadata().getFirst("Location");
		
		assertThat( location, endsWith("/projects/p/my-new-project"));
		
//		ClientResponse<ProjectInlineList> response = projectService.get();
//		
//		assertThat( response.getStatus(), is( Status.OK.getStatusCode()));
//		
//		project = response.getEntity();
//		
//		assertThat( project.getName(), is("My New Project"));
//		assertThat( project.getDescription(), is("Another test project"));
		
	}
	
	public void createProjectThatAlreadyExists(){
		Project project = new Project("sample-project", "Sample Project", "An example Project");
		Response response = projectService.post(project);
	
        assertThat( response.getStatus(), is( Status.CONFLICT.getStatusCode()));
	}

	public void createProjectWithInvalidData(){
		projectService = clientRequestFactory.createProxy(IProjectsResource.class);

		Project project = new Project("my,new,project", "My New Project", "Another test project");
		Response response = projectService.post(project);
		
        assertThat( response.getStatus(), is( Status.BAD_REQUEST.getStatusCode()));
        
		projectService = clientRequestFactory.createProxy(IProjectsResource.class);
        Project project1 = new Project("my-test-project","My test ProjectMy test ProjectMy test ProjectMy test ProjectMy test ProjectMy test Project", "Length of Project name beyond 80");
        Response response1 = projectService.post(project1);
        
        assertThat(response1.getStatus(), is(Status.BAD_REQUEST.getStatusCode()));
        
	}

}

@@ -2577,18 +2577,16 @@
 %22));%0A%09%09%0A
-//
 %09%09Client
@@ -2605,14 +2605,8 @@
 ject
-Inline
 List
@@ -2607,32 +2607,33 @@
 ctList%3E response
+1
  = projectServic
@@ -2637,31 +2637,24 @@
 vice.get();%0A
-//%09%09%0A//
 %09%09assertThat
@@ -2650,33 +2650,32 @@
 %09assertThat(
- 
 response
 .getStatus()
@@ -2654,32 +2654,33 @@
 ertThat(response
+1
 .getStatus(), is
@@ -2676,25 +2676,24 @@
 tatus(), is(
- 
 Status.OK.ge
@@ -2713,35 +2713,29 @@
 ));%0A
-//%09%09%0A//%09%09project = 
+%09%09assertThat(
 response
 .get
@@ -2722,32 +2722,33 @@
 ertThat(response
+1
 .getEntity();%0A//
@@ -2747,75 +2747,160 @@
 ty()
-;%0A//%09%09%0A//%09%09assertThat( project.getName(), is(%22My New Project%22)
+, notNullValue());%0A%09%09assertThat(response1.getEntity().getProjects().size(), is(2)); %0A%09%09Project projectRef = response1.getEntity().getProjects().get(1
 );%0A
-//
 %09%09as
@@ -2920,45 +2920,35 @@
 ject
+Ref
 .get
-Description(), is(%22Another test p
+Name(), is(%22My New P
 roje
@@ -2954,19 +2954,16 @@
 ect%22));%0A
-%09%09%0A
 %09%7D%0A%09%0A%09pu

Add some clauses to prove CreateProject is success