001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.ratings.util.test;
016    
017    import com.liferay.counter.service.CounterLocalServiceUtil;
018    import com.liferay.portal.kernel.test.util.RandomTestUtil;
019    import com.liferay.portal.kernel.test.util.ServiceContextTestUtil;
020    import com.liferay.portal.kernel.test.util.TestPropsValues;
021    import com.liferay.portlet.ratings.model.RatingsEntry;
022    import com.liferay.portlet.ratings.model.RatingsStats;
023    import com.liferay.portlet.ratings.service.RatingsEntryLocalServiceUtil;
024    import com.liferay.portlet.ratings.service.RatingsStatsLocalServiceUtil;
025    
026    /**
027     * @author Daniel Kocsis
028     */
029    public class RatingsTestUtil {
030    
031            public static RatingsEntry addEntry(String className, long classPK)
032                    throws Exception {
033    
034                    return addEntry(className, classPK, 1.0d, TestPropsValues.getUserId());
035            }
036    
037            public static RatingsEntry addEntry(
038                            String className, long classPK, double score, long userId)
039                    throws Exception {
040    
041                    return RatingsEntryLocalServiceUtil.updateEntry(
042                            userId, className, classPK, score,
043                            ServiceContextTestUtil.getServiceContext());
044            }
045    
046            public static RatingsStats addStats(String className, long classPK)
047                    throws Exception {
048    
049                    return addStats(className, classPK, RandomTestUtil.randomInt());
050            }
051    
052            public static RatingsStats addStats(
053                            String className, long classPK, double averageScore)
054                    throws Exception {
055    
056                    long statsId = CounterLocalServiceUtil.increment();
057    
058                    RatingsStats ratingsStats =
059                            RatingsStatsLocalServiceUtil.createRatingsStats(statsId);
060    
061                    ratingsStats.setClassName(className);
062                    ratingsStats.setClassPK(classPK);
063                    ratingsStats.setTotalEntries(RandomTestUtil.randomInt());
064                    ratingsStats.setTotalScore(RandomTestUtil.randomInt());
065                    ratingsStats.setAverageScore(averageScore);
066    
067                    return RatingsStatsLocalServiceUtil.updateRatingsStats(ratingsStats);
068            }
069    
070    }