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