Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LRAC-14484 | master #330

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,8 @@ private SegmentsExperiment _updateWinnerSegmentsExperienceId(
_segmentsExperienceLocalService.
fetchDefaultSegmentsExperienceId(
segmentsExperiment.getPlid())) &&
(statusObject == SegmentsExperimentConstants.Status.COMPLETED) &&
((statusObject == SegmentsExperimentConstants.Status.COMPLETED) ||
(statusObject == SegmentsExperimentConstants.Status.TERMINATED)) &&
(winnerSegmentsExperienceId !=
segmentsExperiment.getSegmentsExperienceId())) {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/**
* SPDX-FileCopyrightText: (c) 2023 Liferay, Inc. https://liferay.com
* SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
*/

package com.liferay.segments.experiment.web.internal.portlet.action.test;

import com.liferay.arquillian.extension.junit.bridge.junit.Arquillian;
import com.liferay.layout.test.util.LayoutTestUtil;
import com.liferay.petra.string.StringPool;
import com.liferay.portal.kernel.model.Company;
import com.liferay.portal.kernel.model.Group;
import com.liferay.portal.kernel.model.Layout;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCActionCommand;
import com.liferay.portal.kernel.service.CompanyLocalService;
import com.liferay.portal.kernel.test.portlet.MockLiferayPortletActionRequest;
import com.liferay.portal.kernel.test.portlet.MockLiferayPortletActionResponse;
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;
import com.liferay.portal.kernel.test.util.ServiceContextTestUtil;
import com.liferay.portal.kernel.test.util.TestPropsValues;
import com.liferay.portal.kernel.theme.ThemeDisplay;
import com.liferay.portal.kernel.util.LocaleUtil;
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 com.liferay.segments.constants.SegmentsEntryConstants;
import com.liferay.segments.constants.SegmentsExperimentConstants;
import com.liferay.segments.model.SegmentsEntry;
import com.liferay.segments.model.SegmentsExperience;
import com.liferay.segments.model.SegmentsExperiment;
import com.liferay.segments.service.SegmentsExperienceLocalService;
import com.liferay.segments.service.SegmentsExperimentLocalService;
import com.liferay.segments.service.SegmentsExperimentRelLocalService;
import com.liferay.segments.test.util.SegmentsTestUtil;

import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
* @author Marcos Martins
*/
@RunWith(Arquillian.class)
public class EditSegmentsExperimentStatusMVCActionCommandTest {

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

@BeforeClass
public static void setUpClass() throws Exception {
_company = _companyLocalService.getCompany(
TestPropsValues.getCompanyId());
}

@Before
public void setUp() throws Exception {
_group = GroupTestUtil.addGroup();
}

@Test
public void testEditSegmentsExperimentStatusWithWinnerSegmentsExperienceId()
throws Exception {

SegmentsEntry segmentsEntry = SegmentsTestUtil.addSegmentsEntry(
_group.getGroupId(), RandomTestUtil.randomString(),
RandomTestUtil.randomString(), RandomTestUtil.randomString(),
StringPool.BLANK, SegmentsEntryConstants.SOURCE_DEFAULT);

Layout layout = LayoutTestUtil.addTypeContentLayout(_group);

SegmentsExperience defaultSegmentsExperience =
SegmentsTestUtil.addSegmentsExperience(
segmentsEntry.getSegmentsEntryId(), layout.getPlid(),
ServiceContextTestUtil.getServiceContext(_group.getGroupId()));

SegmentsExperiment segmentsExperiment =
SegmentsTestUtil.addSegmentsExperiment(
_group.getGroupId(),
defaultSegmentsExperience.getSegmentsExperienceId(),
layout.getPlid());

SegmentsExperience variantSegmentsExperience =
SegmentsTestUtil.addSegmentsExperience(
segmentsEntry.getSegmentsEntryId(), layout.getPlid(),
ServiceContextTestUtil.getServiceContext(_group.getGroupId()));

_segmentsExperimentRelLocalService.addSegmentsExperimentRel(
segmentsExperiment.getSegmentsExperimentId(),
variantSegmentsExperience.getSegmentsExperienceId(),
ServiceContextTestUtil.getServiceContext(_group.getGroupId()));

_segmentsExperimentLocalService.updateSegmentsExperimentStatus(
segmentsExperiment.getSegmentsExperimentId(),
SegmentsExperimentConstants.STATUS_RUNNING);

MockLiferayPortletActionRequest mockLiferayPortletActionRequest =
new MockLiferayPortletActionRequest();

ThemeDisplay themeDisplay = new ThemeDisplay();

themeDisplay.setCompany(_company);
themeDisplay.setLocale(LocaleUtil.US);

mockLiferayPortletActionRequest.setAttribute(
WebKeys.THEME_DISPLAY, themeDisplay);

mockLiferayPortletActionRequest.setParameter(
"segmentsExperimentId",
String.valueOf(segmentsExperiment.getSegmentsExperimentId()));
mockLiferayPortletActionRequest.setParameter(
"status",
String.valueOf(SegmentsExperimentConstants.STATUS_TERMINATED));
mockLiferayPortletActionRequest.setParameter(
"winnerSegmentsExperienceId",
String.valueOf(
variantSegmentsExperience.getSegmentsExperienceId()));

_mvcResourceCommand.processAction(
mockLiferayPortletActionRequest,
new MockLiferayPortletActionResponse());

defaultSegmentsExperience =
_segmentsExperienceRelLocalService.getSegmentsExperience(
defaultSegmentsExperience.getSegmentsExperienceId());

Assert.assertFalse(defaultSegmentsExperience.isActive());

variantSegmentsExperience =
_segmentsExperienceRelLocalService.getSegmentsExperience(
variantSegmentsExperience.getSegmentsExperienceId());

Assert.assertTrue(variantSegmentsExperience.isActive());
}

private static Company _company;

@Inject
private static CompanyLocalService _companyLocalService;

@DeleteAfterTestRun
private Group _group;

@Inject(
filter = "mvc.command.name=/segments_experiment/edit_segments_experiment_status"
)
private MVCActionCommand _mvcResourceCommand;

@Inject
private SegmentsExperienceLocalService _segmentsExperienceRelLocalService;

@Inject
private SegmentsExperimentLocalService _segmentsExperimentLocalService;

@Inject
private SegmentsExperimentRelLocalService
_segmentsExperimentRelLocalService;

}