Skip to content

Commit

Permalink
LPD-37464 Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
jkappler authored and brianchandotcom committed Oct 8, 2024
1 parent 1b953c0 commit 56d7797
Showing 1 changed file with 129 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@

package com.liferay.journal.web.internal.display.context;

import com.liferay.dynamic.data.mapping.model.DDMStructure;
import com.liferay.dynamic.data.mapping.service.DDMStructureLocalServiceUtil;
import com.liferay.dynamic.data.mapping.storage.DDMFormValues;
import com.liferay.journal.model.JournalArticle;
import com.liferay.journal.model.JournalFolder;
import com.liferay.journal.service.JournalArticleServiceUtil;
import com.liferay.journal.service.JournalFolderLocalService;
import com.liferay.journal.service.JournalFolderLocalServiceUtil;
import com.liferay.journal.web.internal.configuration.JournalWebConfiguration;
import com.liferay.portal.kernel.bean.BeanProperties;
import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
import com.liferay.portal.kernel.exception.PortalException;
Expand All @@ -26,14 +30,20 @@
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.test.rule.LiferayUnitTestRule;

import java.util.HashSet;
import java.util.Locale;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;

import org.mockito.MockedStatic;
import org.mockito.Mockito;

/**
Expand Down Expand Up @@ -73,6 +83,12 @@ public void setUp() {
portalUtil.setPortal(_portal);
}

@After
public void tearDown() {
_ddmStructureLocalServiceUtilMockedStatic.close();
_journalArticleServiceUtilMockedStatic.close();
}

@Test
public void testFolderNameValueIsCached() {
String expectedResult = "Home translation";
Expand Down Expand Up @@ -104,6 +120,10 @@ public void testFolderNameValueIsCached() {
@Test
public void testGetDefaultArticleLanguageId() throws PortalException {
_testGetDefaultArticleLanguageIdFromArticle();
_testGetDefaultArticleLanguageIdWithChangeableDefaultLanguageAndAvailableLocale(
LocaleUtil.US, LocaleUtil.UK);
_testGetDefaultArticleLanguageIdWithChangeableDefaultLanguageAndAvailableLocale(
LocaleUtil.CHINA, LocaleUtil.CHINA);
_testGetDefaultArticleLanguageIdWithParameter();
_testGetDefaultArticleLanguageIdWithUnavailableLocale();
}
Expand Down Expand Up @@ -404,6 +424,109 @@ private void _testGetDefaultArticleLanguageIdFromArticle()
_journalEditArticleDisplayContext.getDefaultArticleLanguageId());
}

private void
_testGetDefaultArticleLanguageIdWithChangeableDefaultLanguageAndAvailableLocale(
Locale availableLocale, Locale expectedLocale)
throws PortalException {

JournalArticle journalArticle = Mockito.mock(JournalArticle.class);

Mockito.when(
journalArticle.getArticleId()
).thenReturn(
null
);

Mockito.when(
journalArticle.getDefaultLanguageId()
).thenReturn(
LocaleUtil.toLanguageId(LocaleUtil.CHINA)
);

long ddmStructureId = RandomTestUtil.randomLong();

DDMStructure ddmStructure = Mockito.mock(DDMStructure.class);

Mockito.when(
ddmStructure.getStructureId()
).thenReturn(
ddmStructureId
);

Mockito.when(
DDMStructureLocalServiceUtil.fetchStructure(ddmStructureId)
).thenReturn(
ddmStructure
);

Mockito.when(
JournalArticleServiceUtil.getArticle(
ddmStructure.getGroupId(), DDMStructure.class.getName(),
ddmStructure.getStructureId())
).thenReturn(
journalArticle
);

JournalWebConfiguration journalWebConfiguration = Mockito.mock(
JournalWebConfiguration.class);

Mockito.when(
journalWebConfiguration.changeableDefaultLanguage()
).thenReturn(
true
);

Mockito.when(
_httpServletRequest.getAttribute(
JournalWebConfiguration.class.getName())
).thenReturn(
journalWebConfiguration
);

Mockito.when(
_httpServletRequest.getAttribute(WebKeys.THEME_DISPLAY)
).thenReturn(
_themeDisplay
);

Mockito.when(
_httpServletRequest.getParameter("ddmStructureId")
).thenReturn(
String.valueOf(ddmStructureId)
);

Mockito.when(
_httpServletRequest.getParameter("defaultLanguageId")
).thenReturn(
null
);

Set<Locale> availableLocales = new HashSet<>();

availableLocales.add(availableLocale);

Mockito.when(
_language.getAvailableLocales(Mockito.anyLong())
).thenReturn(
availableLocales
);

Mockito.when(
_language.isAvailableLocale(
0, LocaleUtil.toLanguageId(LocaleUtil.CHINA))
).thenReturn(
true
);

_journalEditArticleDisplayContext =
new JournalEditArticleDisplayContext(
_httpServletRequest, _liferayPortletResponse, journalArticle);

Assert.assertEquals(
LocaleUtil.toLanguageId(expectedLocale),
_journalEditArticleDisplayContext.getDefaultArticleLanguageId());
}

private void _testGetDefaultArticleLanguageIdWithParameter() {
String defaultLanguageId = RandomTestUtil.randomString();

Expand Down Expand Up @@ -465,10 +588,16 @@ private void _testGetDefaultArticleLanguageIdWithUnavailableLocale()

private final BeanProperties _beanProperties = Mockito.mock(
BeanProperties.class);
private final MockedStatic<DDMStructureLocalServiceUtil>
_ddmStructureLocalServiceUtilMockedStatic = Mockito.mockStatic(
DDMStructureLocalServiceUtil.class);
private final HttpServletRequest _httpServletRequest = Mockito.mock(
HttpServletRequest.class);
private final JournalArticle _journalArticle = Mockito.mock(
JournalArticle.class);
private final MockedStatic<JournalArticleServiceUtil>
_journalArticleServiceUtilMockedStatic = Mockito.mockStatic(
JournalArticleServiceUtil.class);
private JournalEditArticleDisplayContext _journalEditArticleDisplayContext;
private final JournalFolderLocalService _journalFolderLocalService =
Mockito.mock(JournalFolderLocalService.class);
Expand Down

0 comments on commit 56d7797

Please sign in to comment.