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.social;
016    
017    import com.liferay.portal.events.ServicePreAction;
018    import com.liferay.portal.kernel.dao.orm.QueryUtil;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portal.model.Group;
021    import com.liferay.portal.service.ServiceContext;
022    import com.liferay.portal.service.ServiceContextFactory;
023    import com.liferay.portal.test.DeleteAfterTestRun;
024    import com.liferay.portal.theme.ThemeDisplay;
025    import com.liferay.portal.util.WebKeys;
026    import com.liferay.portal.util.test.GroupTestUtil;
027    import com.liferay.portal.util.test.TestPropsValues;
028    import com.liferay.portlet.social.model.SocialActivity;
029    import com.liferay.portlet.social.model.SocialActivityFeedEntry;
030    import com.liferay.portlet.social.model.SocialActivityInterpreter;
031    import com.liferay.portlet.social.service.SocialActivityLocalServiceUtil;
032    import com.liferay.portlet.trash.util.TrashUtil;
033    
034    import java.util.ArrayList;
035    import java.util.Collections;
036    import java.util.HashMap;
037    import java.util.List;
038    import java.util.Map;
039    
040    import javax.portlet.PortletURL;
041    
042    import javax.servlet.http.HttpServletRequest;
043    
044    import org.junit.Assert;
045    import org.junit.Before;
046    import org.junit.Test;
047    
048    import org.springframework.mock.web.MockHttpServletRequest;
049    import org.springframework.mock.web.MockHttpServletResponse;
050    
051    /**
052     * @author Zsolt Berentey
053     */
054    public abstract class BaseSocialActivityInterpreterTestCase {
055    
056            @Before
057            public void setUp() throws Exception {
058                    group = GroupTestUtil.addGroup();
059    
060                    HttpServletRequest request = new MockHttpServletRequest();
061    
062                    request.setAttribute(
063                            WebKeys.COMPANY_ID, TestPropsValues.getCompanyId());
064                    request.setAttribute(
065                            WebKeys.CURRENT_URL, "http://localhost:80/web/guest/home");
066                    request.setAttribute(WebKeys.USER, TestPropsValues.getUser());
067    
068                    ServicePreAction servicePreAction = new ServicePreAction();
069    
070                    ThemeDisplay themeDisplay = servicePreAction.initThemeDisplay(
071                            request, new MockHttpServletResponse());
072    
073                    request.setAttribute(WebKeys.THEME_DISPLAY, themeDisplay);
074    
075                    serviceContext = ServiceContextFactory.getInstance(request);
076            }
077    
078            @Test
079            public void testActivityInterpreter() throws Exception {
080                    addActivities();
081    
082                    long time = System.currentTimeMillis();
083    
084                    renameModels();
085    
086                    if (isSupportsTrash()) {
087                            moveModelsToTrash();
088    
089                            checkLinks();
090    
091                            restoreModelsFromTrash();
092                    }
093    
094                    checkInterpret(time);
095            }
096    
097            protected abstract void addActivities() throws Exception;
098    
099            protected void checkInterpret(long time) throws Exception {
100                    List<SocialActivity> activities = getActivities();
101    
102                    Assert.assertFalse(activities.isEmpty());
103    
104                    Map<String, String> entryTitles = new HashMap<String, String>();
105    
106                    SocialActivityInterpreter activityInterpreter =
107                            getActivityInterpreter();
108    
109                    for (SocialActivity activity : activities) {
110                            String title = activity.getExtraDataValue(
111                                    "title", serviceContext.getLocale());
112    
113                            if (isSupportsRename(activity.getClassName()) &&
114                                    Validator.isNotNull(title)) {
115    
116                                    if (activity.getCreateDate() < time) {
117                                            entryTitles.put(activity.getClassName(), title);
118                                    }
119                                    else {
120                                            Assert.assertNotNull(
121                                                    entryTitles.get(activity.getClassName()));
122                                            Assert.assertNotEquals(
123                                                    entryTitles.get(activity.getClassName()), title);
124                                    }
125                            }
126    
127                            if (hasClassName(activityInterpreter, activity.getClassName()) &&
128                                    hasActivityType(activity.getType())) {
129    
130                                    SocialActivityFeedEntry activityFeedEntry =
131                                            activityInterpreter.interpret(activity, serviceContext);
132    
133                                    Assert.assertNotNull(activityFeedEntry);
134    
135                                    title = activityFeedEntry.getTitle();
136    
137                                    if (title.matches("\\{\\d\\}")) {
138                                            Assert.fail("Title contains parameters: " + title);
139                                    }
140                            }
141                    }
142            }
143    
144            protected void checkLinks() throws Exception {
145                    List<SocialActivity> activities = getActivities();
146    
147                    Assert.assertFalse(activities.isEmpty());
148    
149                    SocialActivityInterpreter activityInterpreter =
150                            getActivityInterpreter();
151    
152                    for (SocialActivity activity : activities) {
153                            if (hasClassName(activityInterpreter, activity.getClassName()) &&
154                                    hasActivityType(activity.getType())) {
155    
156                                    SocialActivityFeedEntry activityFeedEntry =
157                                            activityInterpreter.interpret(activity, serviceContext);
158    
159                                    PortletURL portletURL = TrashUtil.getViewContentURL(
160                                            serviceContext.getRequest(), activity.getClassName(),
161                                            activity.getClassPK());
162    
163                                    if (Validator.isNull(activityFeedEntry.getLink()) &&
164                                            (portletURL == null)) {
165    
166                                            continue;
167                                    }
168    
169                                    Assert.assertEquals(
170                                            portletURL.toString(), activityFeedEntry.getLink());
171                            }
172                    }
173            }
174    
175            protected List<SocialActivity> getActivities() throws Exception {
176                    List<SocialActivity> activities =
177                            new ArrayList<SocialActivity>(
178                                    SocialActivityLocalServiceUtil.getGroupActivities(
179                                            group.getGroupId(), QueryUtil.ALL_POS, QueryUtil.ALL_POS));
180    
181                    Collections.reverse(activities);
182    
183                    return activities;
184            }
185    
186            protected abstract SocialActivityInterpreter getActivityInterpreter();
187    
188            protected abstract int[] getActivityTypes();
189    
190            protected boolean hasActivityType(int activityType) {
191                    for (int curActivityType : getActivityTypes()) {
192                            if (curActivityType == activityType) {
193                                    return true;
194                            }
195                    }
196    
197                    return false;
198            }
199    
200            protected boolean hasClassName(
201                    SocialActivityInterpreter activityInterpreter, String className) {
202    
203                    for (String curClassName : activityInterpreter.getClassNames()) {
204                            if (curClassName.equals(className)) {
205                                    return true;
206                            }
207                    }
208    
209                    return false;
210            }
211    
212            protected boolean isSupportsRename(String className) {
213                    return true;
214            }
215    
216            protected boolean isSupportsTrash() {
217                    return true;
218            }
219    
220            protected abstract void moveModelsToTrash() throws Exception;
221    
222            protected abstract void renameModels() throws Exception;
223    
224            protected abstract void restoreModelsFromTrash() throws Exception;
225    
226            @DeleteAfterTestRun
227            protected Group group;
228    
229            protected ServiceContext serviceContext;
230    
231    }