diff --git a/modules/dxp/apps/segments/segments-experiment-test/src/testIntegration/java/com/liferay/segments/experiment/web/internal/portlet/action/test/EditSegmentsExperimentStatusMVCActionCommandTest.java b/modules/dxp/apps/segments/segments-asah-connector-test/src/testIntegration/java/com/liferay/segments/asah/connector/internal/portlet/action/test/EditSegmentsExperimentStatusMVCActionCommandTest.java similarity index 74% rename from modules/dxp/apps/segments/segments-experiment-test/src/testIntegration/java/com/liferay/segments/experiment/web/internal/portlet/action/test/EditSegmentsExperimentStatusMVCActionCommandTest.java rename to modules/dxp/apps/segments/segments-asah-connector-test/src/testIntegration/java/com/liferay/segments/asah/connector/internal/portlet/action/test/EditSegmentsExperimentStatusMVCActionCommandTest.java index 41c2ea47b43fa8..4daa334e3b0a05 100644 --- a/modules/dxp/apps/segments/segments-experiment-test/src/testIntegration/java/com/liferay/segments/experiment/web/internal/portlet/action/test/EditSegmentsExperimentStatusMVCActionCommandTest.java +++ b/modules/dxp/apps/segments/segments-asah-connector-test/src/testIntegration/java/com/liferay/segments/asah/connector/internal/portlet/action/test/EditSegmentsExperimentStatusMVCActionCommandTest.java @@ -3,25 +3,31 @@ * 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; +package com.liferay.segments.asah.connector.internal.portlet.action.test; +import com.liferay.analytics.settings.configuration.AnalyticsConfiguration; 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.configuration.test.util.CompanyConfigurationTemporarySwapper; +import com.liferay.portal.kernel.json.JSONUtil; 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.ReflectionTestUtil; 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.MockHttp; 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.HashMapDictionaryBuilder; import com.liferay.portal.kernel.util.LocaleUtil; import com.liferay.portal.kernel.util.WebKeys; import com.liferay.portal.test.rule.Inject; @@ -37,6 +43,8 @@ import com.liferay.segments.service.SegmentsExperimentRelLocalService; import com.liferay.segments.test.util.SegmentsTestUtil; +import java.util.Collections; + import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; @@ -101,6 +109,10 @@ public void testEditSegmentsExperimentStatusWithWinnerSegmentsExperienceId() variantSegmentsExperience.getSegmentsExperienceId(), ServiceContextTestUtil.getServiceContext(_group.getGroupId())); + _segmentsExperimentLocalService.updateSegmentsExperimentStatus( + segmentsExperiment.getSegmentsExperimentId(), + SegmentsExperimentConstants.STATUS_RUNNING); + MockLiferayPortletActionRequest mockLiferayPortletActionRequest = new MockLiferayPortletActionRequest(); @@ -112,38 +124,56 @@ public void testEditSegmentsExperimentStatusWithWinnerSegmentsExperienceId() mockLiferayPortletActionRequest.setAttribute( WebKeys.THEME_DISPLAY, themeDisplay); - mockLiferayPortletActionRequest.setParameter( - "winnerSegmentsExperienceId", - String.valueOf( - variantSegmentsExperience.getSegmentsExperienceId())); - - _segmentsExperimentLocalService.updateSegmentsExperimentStatus( - segmentsExperiment.getSegmentsExperimentId(), - SegmentsExperimentConstants.STATUS_RUNNING); - 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()); + try (CompanyConfigurationTemporarySwapper + companyConfigurationTemporarySwapper = + new CompanyConfigurationTemporarySwapper( + TestPropsValues.getCompanyId(), + AnalyticsConfiguration.class.getName(), + HashMapDictionaryBuilder.put( + "liferayAnalyticsFaroBackendURL", + "http://localhost:8086" + ).put( + "liferayAnalyticsURL", "http://localhost:8080/" + ).build())) { + + Object asahFaroBackendClient = ReflectionTestUtil.getFieldValue( + _mvcActionCommand, "_asahFaroBackendClient"); + + ReflectionTestUtil.setFieldValue( + asahFaroBackendClient, "_http", + new MockHttp( + Collections.singletonMap( + "/api/1.0/experiments/" + + segmentsExperiment.getSegmentsExperimentKey(), + () -> JSONUtil.put( + "id", "123456" + ).toString()))); + + _mvcActionCommand.processAction( + mockLiferayPortletActionRequest, + new MockLiferayPortletActionResponse()); + + Assert.assertNull( + _segmentsExperienceRelLocalService.fetchSegmentsExperience( + defaultSegmentsExperience.getSegmentsExperienceId())); + + variantSegmentsExperience = + _segmentsExperienceRelLocalService.getSegmentsExperience( + variantSegmentsExperience.getSegmentsExperienceId()); + + Assert.assertTrue(variantSegmentsExperience.isActive()); + } } private static Company _company; @@ -157,7 +187,7 @@ public void testEditSegmentsExperimentStatusWithWinnerSegmentsExperienceId() @Inject( filter = "mvc.command.name=/segments_experiment/edit_segments_experiment_status" ) - private MVCActionCommand _mvcResourceCommand; + private MVCActionCommand _mvcActionCommand; @Inject private SegmentsExperienceLocalService _segmentsExperienceRelLocalService;