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.rating.ws.RatingWebService;
import eu.dnetlib.clients.functionality.rating.ws.RatingWebServiceException;
import eu.dnetlib.domain.functionality.Rating;

public class TestRatingWebService {
	private static RatingWebService ratingWebService;
	
	@BeforeClass
	public static void setup() {
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		factory.setServiceClass(RatingWebService.class);
		factory.setAddress("http://vatopedi.di.uoa.gr:8280/uoa-rating-latest/services/ratingWebService");
		ratingWebService = (RatingWebService) factory.create();
	}
	
	@Test
	public void testRate() throws RatingWebServiceException {
		Assert.assertNotNull("rating web service is null", ratingWebService);
		ratingWebService.rate("userId", "documentId", 5.0f);
		ratingWebService.rate("userId", "documentId", 4.0f);
		ratingWebService.rate("userId", "foo", 3.0f);
		ratingWebService.rate("foo", "documentId", 2.0f);
	}
	
	@Test
	public void testSearchByUser() throws RatingWebServiceException {
		Assert.assertNotNull("rating web service is null", ratingWebService);
		List<Rating> ratings = ratingWebService.searchRatingsByUser("userId");
		Assert.assertNotNull("rating list is null", ratings);
		Assert.assertTrue("rating list is empty", ratings.size() > 0);
		for (Rating rating : ratings)
			System.out.println("rating (user = " + rating.getUserId() + ", document = " + rating.getDocumentId() + ", score = " + rating.getScore() + ")");
	}

	@Test
	public void testSearchByDocument() throws RatingWebServiceException {
		Assert.assertNotNull("rating web service is null", ratingWebService);
		List<Rating> ratings = ratingWebService.searchRatingsByDocument("documentId");
		Assert.assertNotNull("rating list is null", ratings);
		Assert.assertTrue("rating list is empty", ratings.size() > 0);
		for (Rating rating : ratings)
			System.out.println("rating (user = " + rating.getUserId() + ", document = " + rating.getDocumentId() + ", score = " + rating.getScore() + ")");
	}

	@Test
	public void testGetTopRatings() throws RatingWebServiceException {
		Assert.assertNotNull("rating web service is null", ratingWebService);
		List<Rating> ratings = ratingWebService.getTopRatings(10);
		Assert.assertNotNull("rating list is null", ratings);
		Assert.assertTrue("rating list is empty", ratings.size() > 0);
		for (Rating rating : ratings)
			System.out.println("rating (user = " + rating.getUserId() + ", document = " + rating.getDocumentId() + ", score = " + rating.getScore() + ")");
	}

	@Test
	public void testGetTopDocuments() throws RatingWebServiceException {
		Assert.assertNotNull("rating web service is null", ratingWebService);
		List<Rating> ratings = ratingWebService.getTopDocuments(10);
		Assert.assertNotNull("rating list is null", ratings);
		Assert.assertTrue("rating list is empty", ratings.size() > 0);
		for (Rating rating : ratings)
			System.out.println("rating (user = " + rating.getUserId() + ", document = " + rating.getDocumentId() + ", score = " + rating.getScore() + ")");
	}
}
