import java.util.ArrayList;
import java.util.List;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import eu.dnetlib.clients.functionality.forum.ws.ForumWebService;
import eu.dnetlib.clients.functionality.forum.ws.ForumWebServiceException;
import eu.dnetlib.domain.functionality.Post;
import eu.dnetlib.domain.functionality.Thread;

public class TestForumWebService {
	private static ForumWebService forumWebService;
	private static long threadId;
	
	@BeforeClass
	public static void setup() {
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.setServiceClass(ForumWebService.class);
		factory.setAddress("http://vatopedi.di.uoa.gr:8280/uoa-forum-latest/services/forumWebService");
		forumWebService = (ForumWebService) factory.create();	
	}
	
	@Test
	public void testOpen() throws ForumWebServiceException {
		Assert.assertNotNull("forumWebService is null", forumWebService);
		threadId = forumWebService.openThread("communityId", "userId", "topic");
	}
	
	@Test
	public void testEdit() throws ForumWebServiceException {
		Assert.assertNotNull("forumWebService is null", forumWebService);
		Post post = new Post("userId", "content");
		List<Post> posts = new ArrayList<Post>();
		posts.add(post);
		forumWebService.editThread(threadId, "foo", posts);
	}
	
	@Test
	public void testSearchById() throws ForumWebServiceException {
		Assert.assertNotNull("forumWebService is null", forumWebService);
		Thread thread = forumWebService.searchThread(threadId);
		Assert.assertNotNull("thread is null", thread);
	}
	
	@Test
	public void testSearchByCommunity() throws ForumWebServiceException {
		Assert.assertNotNull("forum web service is null", forumWebService);
		List<Thread> threads = forumWebService.searchThread("communityId");
		Assert.assertNotNull("thread list is null", threads);
		Assert.assertTrue("thread list is empty", threads.size() > 0);
	}
	
	@Test
	public void testDelete() throws ForumWebServiceException {
		Assert.assertNotNull("forum web service is null", forumWebService);
		forumWebService.deleteThread(threadId);
	}
}
