Skip to content

Commit eb7d5ad

Browse files
committed
LPD-52709 Add tests
1 parent 6f8509a commit eb7d5ad

File tree

1 file changed

+220
-0
lines changed

1 file changed

+220
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
/**
2+
* SPDX-FileCopyrightText: (c) 2025 Liferay, Inc. https://liferay.com
3+
* SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
4+
*/
5+
6+
package com.liferay.frontend.js.web.internal.resource.handler;
7+
8+
import com.liferay.frontend.js.web.internal.configuration.FrontendCachingConfiguration;
9+
import com.liferay.frontend.js.web.internal.resource.FrontendResource;
10+
import com.liferay.petra.io.StreamUtil;
11+
import com.liferay.portal.configuration.module.configuration.ConfigurationProvider;
12+
import com.liferay.portal.json.JSONFactoryImpl;
13+
import com.liferay.portal.kernel.frontend.hashed.files.HashedFilesRegistry;
14+
import com.liferay.portal.kernel.language.Language;
15+
import com.liferay.portal.kernel.language.LanguageUtil;
16+
import com.liferay.portal.kernel.util.ContentTypes;
17+
import com.liferay.portal.kernel.util.LocaleUtil;
18+
import com.liferay.portal.kernel.util.Portal;
19+
import com.liferay.portal.test.rule.LiferayUnitTestRule;
20+
21+
import jakarta.servlet.http.HttpServletRequest;
22+
23+
import java.io.ByteArrayInputStream;
24+
25+
import java.net.URL;
26+
27+
import java.nio.charset.StandardCharsets;
28+
29+
import java.util.Locale;
30+
31+
import org.junit.Assert;
32+
import org.junit.Test;
33+
34+
import org.mockito.Mockito;
35+
import org.mockito.stubbing.Answer;
36+
37+
import org.springframework.mock.web.MockHttpServletRequest;
38+
39+
/**
40+
* @author Iván Zaera Avellón
41+
*/
42+
public class LanguageFrontendResourceRequestHandlerTest {
43+
44+
public static final LiferayUnitTestRule liferayUnitTestRule =
45+
LiferayUnitTestRule.INSTANCE;
46+
47+
@Test
48+
public void testCanHandleRequest() throws Exception {
49+
LanguageFrontendResourceRequestHandler
50+
languageFrontendResourceRequestHandler =
51+
new LanguageFrontendResourceRequestHandler(
52+
_mockConfigurationProvider(1234L, false),
53+
_mockHashedFilesRegistry(), new JSONFactoryImpl(),
54+
_mockLanguage(), _mockPortal());
55+
56+
Assert.assertTrue(
57+
languageFrontendResourceRequestHandler.canHandleRequest(
58+
_mockHttpServletRequest(
59+
"/o/js/language/en_US/frontend-js-web/all.js")));
60+
61+
Assert.assertFalse(
62+
languageFrontendResourceRequestHandler.canHandleRequest(
63+
_mockHttpServletRequest("/nonsense/request/index.js")));
64+
}
65+
66+
@Test
67+
public void testConfiguration() throws Exception {
68+
LanguageFrontendResourceRequestHandler
69+
languageFrontendResourceRequestHandler =
70+
new LanguageFrontendResourceRequestHandler(
71+
_mockConfigurationProvider(4321L, true),
72+
_mockHashedFilesRegistry(), new JSONFactoryImpl(),
73+
_mockLanguage(), _mockPortal());
74+
75+
FrontendResource frontendResource =
76+
languageFrontendResourceRequestHandler.handleRequest(
77+
_mockHttpServletRequest(
78+
"/o/js/language/en_US/frontend-js-web/all.js"));
79+
80+
Assert.assertNotNull(frontendResource);
81+
82+
Assert.assertEquals(4321L, frontendResource.getMaxAge());
83+
84+
Assert.assertTrue(frontendResource.isSendNoCache());
85+
}
86+
87+
@Test
88+
public void testHandleRequest() throws Exception {
89+
LanguageFrontendResourceRequestHandler
90+
languageFrontendResourceRequestHandler =
91+
new LanguageFrontendResourceRequestHandler(
92+
_mockConfigurationProvider(1234L, false),
93+
_mockHashedFilesRegistry(), new JSONFactoryImpl(),
94+
_mockLanguage(), _mockPortal());
95+
96+
FrontendResource frontendResource =
97+
languageFrontendResourceRequestHandler.handleRequest(
98+
_mockHttpServletRequest(
99+
"/o/js/language/en_US/frontend-js-web/all.js"));
100+
101+
Assert.assertNotNull(frontendResource);
102+
103+
Assert.assertEquals(
104+
ContentTypes.TEXT_JAVASCRIPT, frontendResource.getContentType());
105+
106+
Assert.assertNull(frontendResource.getETag());
107+
108+
Assert.assertEquals(1234L, frontendResource.getMaxAge());
109+
110+
Assert.assertFalse(frontendResource.isImmutable());
111+
112+
Assert.assertFalse(frontendResource.isSendNoCache());
113+
114+
String content = StreamUtil.toString(frontendResource.getInputStream());
115+
116+
Assert.assertTrue(content.contains("'a-key':'a-key',"));
117+
}
118+
119+
private ConfigurationProvider _mockConfigurationProvider(
120+
long maxAge, boolean sendNoCache)
121+
throws Exception {
122+
123+
ConfigurationProvider configurationProvider = Mockito.mock(
124+
ConfigurationProvider.class);
125+
126+
FrontendCachingConfiguration frontendCachingConfiguration =
127+
Mockito.mock(FrontendCachingConfiguration.class);
128+
129+
Mockito.when(
130+
frontendCachingConfiguration.labelsModulesMaxAge()
131+
).thenReturn(
132+
maxAge
133+
);
134+
135+
Mockito.when(
136+
frontendCachingConfiguration.sendNoCacheForLabelsModules()
137+
).thenReturn(
138+
sendNoCache
139+
);
140+
141+
Mockito.when(
142+
configurationProvider.getCompanyConfiguration(
143+
FrontendCachingConfiguration.class, _COMPANY_ID)
144+
).thenReturn(
145+
frontendCachingConfiguration
146+
);
147+
148+
return configurationProvider;
149+
}
150+
151+
private HashedFilesRegistry _mockHashedFilesRegistry() throws Exception {
152+
HashedFilesRegistry hashedFilesRegistry = Mockito.mock(
153+
HashedFilesRegistry.class);
154+
155+
URL url = Mockito.mock(URL.class);
156+
157+
Mockito.when(
158+
url.openStream()
159+
).thenReturn(
160+
new ByteArrayInputStream(
161+
"{keys:['a-key']}".getBytes(StandardCharsets.UTF_8))
162+
);
163+
164+
Mockito.when(
165+
hashedFilesRegistry.getResource(Mockito.anyString())
166+
).thenReturn(
167+
url
168+
);
169+
170+
return hashedFilesRegistry;
171+
}
172+
173+
private HttpServletRequest _mockHttpServletRequest(String requestURI) {
174+
MockHttpServletRequest mockHttpServletRequest =
175+
new MockHttpServletRequest();
176+
177+
mockHttpServletRequest.setRequestURI(requestURI);
178+
179+
return mockHttpServletRequest;
180+
}
181+
182+
private Language _mockLanguage() {
183+
Language language = Mockito.mock(Language.class);
184+
185+
Mockito.when(
186+
language.isAvailableLocale(LocaleUtil.ENGLISH)
187+
).thenReturn(
188+
true
189+
);
190+
191+
Mockito.when(
192+
language.get(Mockito.any(Locale.class), Mockito.anyString())
193+
).thenAnswer(
194+
(Answer<String>)invocationOnMock -> invocationOnMock.getArgument(
195+
1, String.class)
196+
);
197+
198+
new LanguageUtil(
199+
).setLanguage(
200+
language
201+
);
202+
203+
return language;
204+
}
205+
206+
private Portal _mockPortal() {
207+
Portal portal = Mockito.mock(Portal.class);
208+
209+
Mockito.when(
210+
portal.getCompanyId(Mockito.any(HttpServletRequest.class))
211+
).thenReturn(
212+
_COMPANY_ID
213+
);
214+
215+
return portal;
216+
}
217+
218+
private static final long _COMPANY_ID = 1L;
219+
220+
}

0 commit comments

Comments
 (0)