Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosapmf committed Sep 4, 2023
1 parent e426a39 commit 090cf5d
Show file tree
Hide file tree
Showing 7 changed files with 274 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* 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;
Expand All @@ -21,6 +21,7 @@
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;
Expand All @@ -40,6 +41,8 @@
import com.liferay.segments.service.SegmentsExperimentLocalService;
import com.liferay.segments.test.util.SegmentsTestUtil;

import java.util.Collections;

import javax.portlet.ActionRequest;

import org.junit.Assert;
Expand Down Expand Up @@ -128,6 +131,9 @@ public void testAddSegmentsExperiment() throws Exception {
TestPropsValues.getCompanyId(),
AnalyticsConfiguration.class.getName(),
HashMapDictionaryBuilder.<String, Object>put(
"liferayAnalyticsFaroBackendURL",
"http://localhost:8086"
).put(
"liferayAnalyticsURL", liferayAnalyticsURL
).build())) {

Expand Down Expand Up @@ -229,9 +235,25 @@ public void testAddSegmentsExperimentWithExistingTerminatedSegmentsExperiment()
TestPropsValues.getCompanyId(),
AnalyticsConfiguration.class.getName(),
HashMapDictionaryBuilder.<String, Object>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())));

ReflectionTestUtil.invoke(
_mvcActionCommand, "_addSegmentsExperiment",
new Class<?>[] {ActionRequest.class},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -101,6 +109,10 @@ public void testEditSegmentsExperimentStatusWithWinnerSegmentsExperienceId()
variantSegmentsExperience.getSegmentsExperienceId(),
ServiceContextTestUtil.getServiceContext(_group.getGroupId()));

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

MockLiferayPortletActionRequest mockLiferayPortletActionRequest =
new MockLiferayPortletActionRequest();

Expand All @@ -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.<String, Object>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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ public String getPageURL() {
return _pageURL;
}

public Boolean getPublishable() {
return _publishable;
}

public String getPublishedDXPVariantId() {
return _publishedDXPVariantId;
}
Expand Down Expand Up @@ -240,6 +244,10 @@ public void setPageURL(String pageURL) {
_pageURL = pageURL;
}

public void setPublishable(Boolean publishable) {
_publishable = publishable;
}

public void setPublishedDXPVariantId(String publishedDXPVariantId) {
_publishedDXPVariantId = publishedDXPVariantId;
}
Expand Down Expand Up @@ -272,6 +280,7 @@ public void setStartedDate(Date startedDate) {
private String _pageRelativePath;
private String _pageTitle;
private String _pageURL;
private Boolean _publishable;
private String _publishedDXPVariantId;
private Date _startedDate;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ protected static Experiment toExperiment(
segmentsExperiment.getWinnerSegmentsExperienceKey());
}

experiment.setPublishable(true);

SegmentsExperience segmentsExperience =
segmentsExperienceLocalService.getSegmentsExperience(
segmentsExperiment.getSegmentsExperienceId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
* 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;
package com.liferay.segments.asah.connector.internal.portlet.action;

import com.liferay.analytics.settings.configuration.AnalyticsConfiguration;
import com.liferay.analytics.settings.rest.manager.AnalyticsSettingsManager;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.json.JSONUtil;
Expand All @@ -14,32 +15,46 @@
import com.liferay.portal.kernel.portlet.JSONPortletResponseUtil;
import com.liferay.portal.kernel.portlet.bridges.mvc.BaseMVCActionCommand;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCActionCommand;
import com.liferay.portal.kernel.service.CompanyLocalService;
import com.liferay.portal.kernel.service.GroupLocalService;
import com.liferay.portal.kernel.service.LayoutLocalService;
import com.liferay.portal.kernel.service.ServiceContext;
import com.liferay.portal.kernel.service.ServiceContextFactory;
import com.liferay.portal.kernel.theme.ThemeDisplay;
import com.liferay.portal.kernel.transaction.Propagation;
import com.liferay.portal.kernel.transaction.TransactionConfig;
import com.liferay.portal.kernel.transaction.TransactionInvokerUtil;
import com.liferay.portal.kernel.util.Http;
import com.liferay.portal.kernel.util.LocaleUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Portal;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.segments.asah.connector.internal.client.AsahFaroBackendClient;
import com.liferay.segments.asah.connector.internal.client.AsahFaroBackendClientImpl;
import com.liferay.segments.asah.connector.internal.client.model.Experiment;
import com.liferay.segments.asah.connector.internal.client.model.util.ExperimentUtil;
import com.liferay.segments.asah.connector.internal.util.SegmentsExperimentUtil;
import com.liferay.segments.constants.SegmentsExperimentConstants;
import com.liferay.segments.constants.SegmentsPortletKeys;
import com.liferay.segments.exception.DuplicateSegmentsExperimentException;
import com.liferay.segments.experiment.web.internal.util.SegmentsExperimentUtil;
import com.liferay.segments.model.SegmentsExperiment;
import com.liferay.segments.model.SegmentsExperimentRel;
import com.liferay.segments.service.SegmentsEntryLocalService;
import com.liferay.segments.service.SegmentsExperienceLocalService;
import com.liferay.segments.service.SegmentsExperimentRelService;
import com.liferay.segments.service.SegmentsExperimentService;

import java.util.Map;
import java.util.concurrent.Callable;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;

import javax.servlet.http.HttpServletResponse;

import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;

/**
Expand All @@ -55,6 +70,17 @@
public class AddSegmentsExperimentMVCActionCommand
extends BaseMVCActionCommand {

@Activate
protected void activate(Map<String, Object> properties) {
_asahFaroBackendClient = new AsahFaroBackendClientImpl(
_analyticsSettingsManager, _http);
}

@Deactivate
protected void deactivate() {
_asahFaroBackendClient = null;
}

@Override
protected void doProcessAction(
ActionRequest actionRequest, ActionResponse actionResponse)
Expand Down Expand Up @@ -98,6 +124,10 @@ private JSONObject _addSegmentsExperiment(ActionRequest actionRequest)
ServiceContext serviceContext = ServiceContextFactory.getInstance(
actionRequest);

AnalyticsConfiguration analyticsConfiguration =
_analyticsSettingsManager.getAnalyticsConfiguration(
serviceContext.getCompanyId());

long segmentsExperienceId = ParamUtil.getLong(
actionRequest, "segmentsExperienceId");
long plid = ParamUtil.getLong(actionRequest, "plid");
Expand All @@ -110,6 +140,19 @@ private JSONObject _addSegmentsExperiment(ActionRequest actionRequest)
if (segmentsExperiment.getStatus() ==
SegmentsExperimentConstants.STATUS_TERMINATED) {

Experiment experiment = ExperimentUtil.toExperiment(
_companyLocalService,
analyticsConfiguration.liferayAnalyticsDataSourceId(),
_groupLocalService, _layoutLocalService,
LocaleUtil.getSiteDefault(), _portal,
_segmentsEntryLocalService, _segmentsExperienceLocalService,
segmentsExperiment);

experiment.setPublishable(false);

_asahFaroBackendClient.updateExperiment(
segmentsExperiment.getCompanyId(), experiment);

segmentsExperiment.setStatus(
SegmentsExperimentConstants.STATUS_DELETING_ON_DXP_ONLY);

Expand Down Expand Up @@ -139,9 +182,8 @@ private JSONObject _addSegmentsExperiment(ActionRequest actionRequest)
return JSONUtil.put(
"segmentsExperiment",
SegmentsExperimentUtil.toSegmentsExperimentJSONObject(
_analyticsSettingsManager.getAnalyticsConfiguration(
segmentsExperiment.getCompanyId()),
themeDisplay.getLocale(), segmentsExperiment)
analyticsConfiguration, themeDisplay.getLocale(),
segmentsExperiment)
).put(
"segmentsExperimentRel",
SegmentsExperimentUtil.toSegmentsExperimentRelJSONObject(
Expand All @@ -159,12 +201,32 @@ private JSONObject _addSegmentsExperiment(ActionRequest actionRequest)
@Reference
private AnalyticsSettingsManager _analyticsSettingsManager;

private AsahFaroBackendClient _asahFaroBackendClient;

@Reference
private CompanyLocalService _companyLocalService;

@Reference
private GroupLocalService _groupLocalService;

@Reference
private Http _http;

@Reference
private Language _language;

@Reference
private LayoutLocalService _layoutLocalService;

@Reference
private Portal _portal;

@Reference
private SegmentsEntryLocalService _segmentsEntryLocalService;

@Reference
private SegmentsExperienceLocalService _segmentsExperienceLocalService;

@Reference
private SegmentsExperimentRelService _segmentsExperimentRelService;

Expand Down
Loading

0 comments on commit 090cf5d

Please sign in to comment.