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.test;
016    
017    import com.liferay.portal.kernel.test.rule.DeleteAfterTestRun;
018    import com.liferay.portal.kernel.test.util.GroupTestUtil;
019    import com.liferay.portal.kernel.test.util.ServiceContextTestUtil;
020    import com.liferay.portal.model.BaseModel;
021    import com.liferay.portal.model.ClassedModel;
022    import com.liferay.portal.model.Group;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portlet.ratings.exception.NoSuchStatsException;
025    import com.liferay.portlet.ratings.model.RatingsStats;
026    import com.liferay.portlet.ratings.service.RatingsStatsLocalServiceUtil;
027    import com.liferay.portlet.ratings.util.test.RatingsTestUtil;
028    
029    import org.junit.Before;
030    import org.junit.Test;
031    
032    /**
033     * @author Cristina Gonz??lez
034     */
035    public abstract class BaseRatingsTestCase {
036    
037            @Before
038            public void setUp() throws Exception {
039                    group = GroupTestUtil.addGroup();
040            }
041    
042            @Test(expected = NoSuchStatsException.class)
043            public void testDeleteRatings() throws Exception {
044                    ServiceContext serviceContext =
045                            ServiceContextTestUtil.getServiceContext(group.getGroupId());
046    
047                    BaseModel<?> parentBaseModel = getParentBaseModel(
048                            group, serviceContext);
049    
050                    BaseModel<?> baseModel = addBaseModel(parentBaseModel, serviceContext);
051    
052                    RatingsStats ratingsStats = RatingsTestUtil.addStats(
053                            _getBaseModelClassName(), getRatingsClassPK(baseModel));
054    
055                    deleteBaseModel(baseModel, serviceContext);
056    
057                    RatingsStatsLocalServiceUtil.getRatingsStats(ratingsStats.getStatsId());
058            }
059    
060            protected abstract BaseModel<?> addBaseModel(
061                            BaseModel<?> parentBaseModel, ServiceContext serviceContext)
062                    throws Exception;
063    
064            protected abstract BaseModel<?> deleteBaseModel(
065                            BaseModel<?> baseModel, ServiceContext serviceContext)
066                    throws Exception;
067    
068            protected abstract Class<?> getBaseModelClass();
069    
070            protected BaseModel<?> getParentBaseModel(
071                            Group group, ServiceContext serviceContext)
072                    throws Exception {
073    
074                    return group;
075            }
076    
077            protected long getRatingsClassPK(ClassedModel classedModel) {
078                    return (Long)classedModel.getPrimaryKeyObj();
079            }
080    
081            @DeleteAfterTestRun
082            protected Group group;
083    
084            private String _getBaseModelClassName() {
085                    Class<?> clazz = getBaseModelClass();
086    
087                    return clazz.getName();
088            }
089    
090    }