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.api.functionality.ForumService;
import eu.dnetlib.api.functionality.ForumServiceException;
import eu.dnetlib.clients.functionality.forum.ws.ForumWebService;
import eu.dnetlib.clients.functionality.forum.ws.ForumWebServiceClient;
import eu.dnetlib.domain.functionality.Post;
import eu.dnetlib.domain.functionality.Thread;

public class TestForumService {
	private static ForumService forumService;
	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");
		forumService = new ForumWebServiceClient();
		((ForumWebServiceClient) forumService).setWebService((ForumWebService) factory.create());
	}
	
	@Test
	public void testOpen() throws ForumServiceException {
		Assert.assertNotNull("forum service is null", forumService);
		threadId = forumService.openThread("communityId", "userId", "topic");
		System.out.println("thread id = " + threadId);
	}
	
	@Test
	public void testEdit() throws ForumServiceException {
		Post post = new Post("userId", "content");
		List<Post> posts = new ArrayList<Post>();
		posts.add(post);
		forumService.editThread(threadId, "topic", posts);
	}
	
	@Test
	public void testSearchById() throws ForumServiceException {
		Thread thread = forumService.searchThread(threadId);
		Assert.assertNotNull("thread is null", thread);
		System.out.println("thread: id = " + thread.getThreadId() + ", community = " + thread.getCommunityId() + ", user = " + thread.getUserId() + ", creation date  = " + thread.getCreationDate() + ", topic = " + thread.getTopic() + ", posts = " + thread.getPosts());
	}
	
	@Test
	public void testSearchByCommunity() throws ForumServiceException {
		List<Thread> threads = forumService.searchThread("communityId");
		Assert.assertNotNull("thread list is null", threads);
		Assert.assertTrue("thread list is empty", threads.size() > 0);
		for (Thread thread : threads)
			System.out.println("thread: id = " + thread.getThreadId() + ", community = " + thread.getCommunityId() + ", user = " + thread.getUserId() + ", creation date  = " + thread.getCreationDate() + ", topic = " + thread.getTopic() + ", posts = " + thread.getPosts());
	}
	
	@Test
	public void testDelete() throws ForumServiceException {
		forumService.deleteThread(threadId);
	}
}
