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.Validator;
023    import com.liferay.portal.model.Group;
024    import com.liferay.portal.service.ServiceContext;
025    import com.liferay.portal.service.ServiceContextFactory;
026    import com.liferay.portal.theme.ThemeDisplay;
027    import com.liferay.portal.util.WebKeys;
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<>();
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                                    Assert.assertFalse(
138                                            "Title contains parameters: " + title,
139                                            title.matches("\\{\\d\\}"));
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 = new ArrayList<SocialActivity>(
177                            SocialActivityLocalServiceUtil.getGroupActivities(
178                                    group.getGroupId(), QueryUtil.ALL_POS, QueryUtil.ALL_POS));
179    
180                    Collections.reverse(activities);
181    
182                    return activities;
183            }
184    
185            protected abstract SocialActivityInterpreter getActivityInterpreter();
186    
187            protected abstract int[] getActivityTypes();
188    
189            protected boolean hasActivityType(int activityType) {
190                    for (int curActivityType : getActivityTypes()) {
191                            if (curActivityType == activityType) {
192                                    return true;
193                            }
194                    }
195    
196                    return false;
197            }
198    
199            protected boolean hasClassName(
200                    SocialActivityInterpreter activityInterpreter, String className) {
201    
202                    for (String curClassName : activityInterpreter.getClassNames()) {
203                            if (curClassName.equals(className)) {
204                                    return true;
205                            }
206                    }
207    
208                    return false;
209            }
210    
211            protected boolean isSupportsRename(String className) {
212                    return true;
213            }
214    
215            protected boolean isSupportsTrash() {
216                    return true;
217            }
218    
219            protected abstract void moveModelsToTrash() throws Exception;
220    
221            protected abstract void renameModels() throws Exception;
222    
223            protected abstract void restoreModelsFromTrash() throws Exception;
224    
225            @DeleteAfterTestRun
226            protected Group group;
227    
228            protected ServiceContext serviceContext;
229    
230    }