Skip to content

Commit

Permalink
LPD-35285 Integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
icgsbr authored and brianchandotcom committed Sep 4, 2024
1 parent 616a2e3 commit 6709fb5
Showing 1 changed file with 57 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@
import com.liferay.portal.kernel.portlet.LiferayPortletConfig;
import com.liferay.portal.kernel.portlet.PortletConfigFactoryUtil;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCResourceCommand;
import com.liferay.portal.kernel.security.auth.PrincipalThreadLocal;
import com.liferay.portal.kernel.security.permission.PermissionChecker;
import com.liferay.portal.kernel.security.permission.PermissionCheckerFactoryUtil;
import com.liferay.portal.kernel.security.permission.PermissionThreadLocal;
import com.liferay.portal.kernel.service.PortletLocalService;
import com.liferay.portal.kernel.test.ReflectionTestUtil;
import com.liferay.portal.kernel.test.portlet.MockLiferayResourceRequest;
import com.liferay.portal.kernel.test.portlet.MockLiferayResourceResponse;
import com.liferay.portal.kernel.test.rule.AggregateTestRule;
import com.liferay.portal.kernel.test.rule.DeleteAfterTestRun;
import com.liferay.portal.kernel.test.util.GroupTestUtil;
import com.liferay.portal.kernel.test.util.RandomTestUtil;
Expand All @@ -38,6 +43,7 @@
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.test.rule.Inject;
import com.liferay.portal.test.rule.LiferayIntegrationTestRule;
import com.liferay.portal.test.rule.PermissionCheckerMethodTestRule;

import java.io.ByteArrayOutputStream;

Expand All @@ -48,7 +54,6 @@
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.junit.runner.RunWith;

/**
Expand All @@ -59,7 +64,10 @@ public class GetFormReportDataMVCResourceCommandTest {

@ClassRule
@Rule
public static final TestRule testRule = new LiferayIntegrationTestRule();
public static final AggregateTestRule aggregateTestRule =
new AggregateTestRule(
new LiferayIntegrationTestRule(),
PermissionCheckerMethodTestRule.INSTANCE);

@Before
public void setUp() throws Exception {
Expand Down Expand Up @@ -90,7 +98,7 @@ public void testServeResource() throws Exception {
Assert.assertNotNull(ddmFormInstanceReport);

JSONObject jsonObject = _getJSONObject(
ddmFormInstance.getFormInstanceId());
ddmFormInstance.getFormInstanceId(), true);

Assert.assertEquals(
ddmFormInstanceReport.getData(), jsonObject.get("data"));
Expand All @@ -115,17 +123,56 @@ public void testServeResource() throws Exception {

@Test
public void testServeResourceWithError() throws Exception {
JSONObject jsonObject = _getJSONObject(RandomTestUtil.randomLong());

// Nonexistent DDMFormInstance

JSONObject jsonObject = _getJSONObject(
RandomTestUtil.randomLong(), true);

Assert.assertTrue(jsonObject.has("errorMessage"));

// Signed out user

DDMFormInstance ddmFormInstance =
DDMFormInstanceTestUtil.addDDMFormInstance(
_group, _user.getUserId());

jsonObject = _getJSONObject(ddmFormInstance.getFormInstanceId(), false);

Assert.assertTrue(jsonObject.has("errorMessage"));

// User with no permission

PermissionChecker originalPermissionChecker =
PermissionThreadLocal.getPermissionChecker();
String originalName = PrincipalThreadLocal.getName();
User user = UserTestUtil.addUser();

try {
PermissionThreadLocal.setPermissionChecker(
PermissionCheckerFactoryUtil.create(user));
PrincipalThreadLocal.setName(user.getUserId());

jsonObject = _getJSONObject(
ddmFormInstance.getFormInstanceId(), true);

Assert.assertTrue(jsonObject.has("errorMessage"));
}
finally {
PermissionThreadLocal.setPermissionChecker(
originalPermissionChecker);
PrincipalThreadLocal.setName(originalName);
}
}

private JSONObject _getJSONObject(long formInstanceId) throws Exception {
private JSONObject _getJSONObject(long formInstanceId, boolean signedIn)
throws Exception {

MockLiferayResourceResponse mockLiferayResourceResponse =
new MockLiferayResourceResponse();

_mvcResourceCommand.serveResource(
_mockLiferayResourceRequest(formInstanceId),
_mockLiferayResourceRequest(formInstanceId, signedIn),
mockLiferayResourceResponse);

ByteArrayOutputStream byteArrayOutputStream =
Expand All @@ -143,17 +190,18 @@ private LiferayPortletConfig _getLiferayPortletConfig() {
null);
}

private ThemeDisplay _getThemeDisplay() throws Exception {
private ThemeDisplay _getThemeDisplay(boolean signedIn) throws Exception {
ThemeDisplay themeDisplay = new ThemeDisplay();

themeDisplay.setLocale(LocaleUtil.getSiteDefault());
themeDisplay.setSignedIn(signedIn);
themeDisplay.setTimeZone(TimeZoneUtil.getDefault());

return themeDisplay;
}

private MockLiferayResourceRequest _mockLiferayResourceRequest(
long formInstanceId)
long formInstanceId, boolean signedIn)
throws Exception {

MockLiferayResourceRequest mockLiferayResourceRequest =
Expand All @@ -162,7 +210,7 @@ private MockLiferayResourceRequest _mockLiferayResourceRequest(
mockLiferayResourceRequest.setAttribute(
JavaConstants.JAVAX_PORTLET_CONFIG, _getLiferayPortletConfig());
mockLiferayResourceRequest.setAttribute(
WebKeys.THEME_DISPLAY, _getThemeDisplay());
WebKeys.THEME_DISPLAY, _getThemeDisplay(signedIn));
mockLiferayResourceRequest.addParameter(
"formInstanceId", String.valueOf(formInstanceId));

Expand Down

0 comments on commit 6709fb5

Please sign in to comment.