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