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.subscriptions.test;
016    
017    import com.dumbster.smtp.MailMessage;
018    
019    import com.liferay.portal.kernel.settings.GroupServiceSettingsLocator;
020    import com.liferay.portal.kernel.settings.ModifiableSettings;
021    import com.liferay.portal.kernel.settings.Settings;
022    import com.liferay.portal.kernel.settings.SettingsFactoryUtil;
023    import com.liferay.portal.kernel.util.LocaleThreadLocal;
024    import com.liferay.portal.kernel.util.LocaleUtil;
025    import com.liferay.portal.kernel.util.LocalizationUtil;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.model.Layout;
028    import com.liferay.portal.util.test.LayoutTestUtil;
029    import com.liferay.portal.util.test.MailServiceTestUtil;
030    
031    import java.util.HashMap;
032    import java.util.List;
033    import java.util.Locale;
034    import java.util.Map;
035    
036    import org.junit.After;
037    import org.junit.Assert;
038    import org.junit.Before;
039    import org.junit.Test;
040    
041    /**
042     * @author Roberto D??az
043     */
044    public abstract class BaseSubscriptionLocalizedContentTestCase
045            extends BaseSubscriptionTestCase {
046    
047            @Before
048            @Override
049            public void setUp() throws Exception {
050                    super.setUp();
051    
052                    defaultLocale = LocaleThreadLocal.getDefaultLocale();
053                    layout = LayoutTestUtil.addLayout(group);
054            }
055    
056            @After
057            public void tearDown() throws Exception {
058                    LocaleThreadLocal.setDefaultLocale(defaultLocale);
059            }
060    
061            @Test
062            public void testSubscriptionLocalizedContentWhenAddingBaseModel()
063                    throws Exception {
064    
065                    Map<Locale, String> previousLocalizedContents = new HashMap<>();
066    
067                    previousLocalizedContents.putAll(localizedContents);
068    
069                    localizedContents.put(LocaleUtil.GERMANY, GERMAN_BODY);
070    
071                    setBaseModelSubscriptionBodyPreferences(
072                            getSubscriptionAddedBodyPreferenceName());
073    
074                    addSubscriptionContainerModel(getDefaultContainerModelId());
075    
076                    LocaleThreadLocal.setDefaultLocale(LocaleUtil.GERMANY);
077    
078                    addBaseModel(creatorUser.getUserId(), getDefaultContainerModelId());
079    
080                    List<MailMessage> messages = MailServiceTestUtil.getMailMessages(
081                            "Body", GERMAN_BODY);
082    
083                    Assert.assertEquals(1, messages.size());
084    
085                    localizedContents = previousLocalizedContents;
086            }
087    
088            @Test
089            public void testSubscriptionLocalizedContentWhenUpdatingBaseModel()
090                    throws Exception {
091    
092                    Map<Locale, String> previousLocalizedContents = new HashMap<>();
093    
094                    previousLocalizedContents.putAll(localizedContents);
095    
096                    localizedContents.put(LocaleUtil.SPAIN, SPANISH_BODY);
097    
098                    setBaseModelSubscriptionBodyPreferences(
099                            getSubscriptionUpdatedBodyPreferenceName());
100    
101                    LocaleThreadLocal.setDefaultLocale(LocaleUtil.SPAIN);
102    
103                    long baseModelId = addBaseModel(
104                            creatorUser.getUserId(), getDefaultContainerModelId());
105    
106                    addSubscriptionContainerModel(getDefaultContainerModelId());
107    
108                    updateBaseModel(creatorUser.getUserId(), baseModelId);
109    
110                    List<MailMessage> messages = MailServiceTestUtil.getMailMessages(
111                            "Body", SPANISH_BODY);
112    
113                    Assert.assertEquals(1, messages.size());
114    
115                    localizedContents = previousLocalizedContents;
116            }
117    
118            protected abstract void addSubscriptionContainerModel(long containerModelId)
119                    throws Exception;
120    
121            protected long getDefaultContainerModelId() {
122                    return PARENT_CONTAINER_MODEL_ID_DEFAULT;
123            }
124    
125            protected abstract String getPortletId();
126    
127            protected String getServiceName() {
128                    return StringPool.BLANK;
129            }
130    
131            protected abstract String getSubscriptionAddedBodyPreferenceName();
132    
133            protected abstract String getSubscriptionUpdatedBodyPreferenceName();
134    
135            protected void setBaseModelSubscriptionBodyPreferences(
136                            String bodyPreferenceName)
137                    throws Exception {
138    
139                    Settings settings = SettingsFactoryUtil.getSettings(
140                            new GroupServiceSettingsLocator(
141                                    group.getGroupId(), getServiceName()));
142    
143                    ModifiableSettings modifiableSettings =
144                            settings.getModifiableSettings();
145    
146                    for (Map.Entry<Locale, String> localizedContent :
147                                    localizedContents.entrySet()) {
148    
149                            Locale locale = localizedContent.getKey();
150    
151                            String subscriptionBodyPreferencesKey =
152                                    LocalizationUtil.getLocalizedName(
153                                            bodyPreferenceName, LocaleUtil.toLanguageId(locale));
154    
155                            String content = localizedContent.getValue();
156    
157                            modifiableSettings.setValue(
158                                    subscriptionBodyPreferencesKey, content);
159                    }
160    
161                    modifiableSettings.store();
162            }
163    
164            protected static final String GERMAN_BODY = "Hallo Welt";
165    
166            protected static final String SPANISH_BODY = "Hola Mundo";
167    
168            protected Locale defaultLocale;
169            protected Layout layout;
170            protected Map<Locale, String> localizedContents = new HashMap<>();
171    
172    }