/modules/apps/segments/segments-context-vocabulary-test/src/testIntegration/java/com/liferay/segments/context/vocabulary/internal/portlet/action/test/UpdateSegmentsContextVocabularyConfigurationMVCActionCommandTest.java

https://github.com/mhan810/liferay-portal · Java · 241 lines · 171 code · 54 blank · 16 comment · 0 complexity · 2d777e7b0d92f80f5e5481baa87d0eb9 MD5 · raw file

  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.segments.context.vocabulary.internal.portlet.action.test;
  15. import com.liferay.arquillian.extension.junit.bridge.junit.Arquillian;
  16. import com.liferay.petra.string.StringBundler;
  17. import com.liferay.portal.configuration.persistence.listener.ConfigurationModelListenerException;
  18. import com.liferay.portal.kernel.portlet.bridges.mvc.MVCActionCommand;
  19. import com.liferay.portal.kernel.servlet.MultiSessionErrors;
  20. import com.liferay.portal.kernel.test.portlet.MockLiferayPortletActionRequest;
  21. import com.liferay.portal.kernel.test.portlet.MockLiferayPortletActionResponse;
  22. import com.liferay.portal.kernel.test.util.RandomTestUtil;
  23. import com.liferay.portal.kernel.util.LocaleThreadLocal;
  24. import com.liferay.portal.kernel.util.LocaleUtil;
  25. import com.liferay.portal.test.rule.Inject;
  26. import com.liferay.portal.test.rule.LiferayIntegrationTestRule;
  27. import com.liferay.portletmvc4spring.test.mock.web.portlet.MockPortletSession;
  28. import java.io.IOException;
  29. import java.util.Dictionary;
  30. import java.util.Locale;
  31. import java.util.Objects;
  32. import java.util.Optional;
  33. import java.util.stream.Stream;
  34. import org.junit.AfterClass;
  35. import org.junit.Assert;
  36. import org.junit.BeforeClass;
  37. import org.junit.ClassRule;
  38. import org.junit.Rule;
  39. import org.junit.Test;
  40. import org.junit.rules.TestRule;
  41. import org.junit.runner.RunWith;
  42. import org.osgi.framework.InvalidSyntaxException;
  43. import org.osgi.service.cm.Configuration;
  44. import org.osgi.service.cm.ConfigurationAdmin;
  45. /**
  46. * @author Cristina González
  47. */
  48. @RunWith(Arquillian.class)
  49. public class UpdateSegmentsContextVocabularyConfigurationMVCActionCommandTest {
  50. @ClassRule
  51. @Rule
  52. public static final TestRule testRule = new LiferayIntegrationTestRule();
  53. @BeforeClass
  54. public static void setUpClass() {
  55. _locale = LocaleThreadLocal.getThemeDisplayLocale();
  56. LocaleThreadLocal.setThemeDisplayLocale(LocaleUtil.US);
  57. }
  58. @AfterClass
  59. public static void tearDownClass() {
  60. LocaleThreadLocal.setThemeDisplayLocale(_locale);
  61. }
  62. @Test
  63. public void testProcessAction() throws Exception {
  64. String entityField = RandomTestUtil.randomString();
  65. MockLiferayPortletActionRequest mockLiferayPortletActionRequest =
  66. _getMockLiferayPortletActionRequest(
  67. RandomTestUtil.randomString(), entityField, null);
  68. _mvcActionCommand.processAction(
  69. mockLiferayPortletActionRequest,
  70. new MockLiferayPortletActionResponse());
  71. Assert.assertTrue(
  72. MultiSessionErrors.isEmpty(mockLiferayPortletActionRequest));
  73. Optional<Configuration> configurationOptional =
  74. _getConfigurationOptional(entityField);
  75. Assert.assertTrue(configurationOptional.isPresent());
  76. Configuration configuration = configurationOptional.get();
  77. configuration.delete();
  78. }
  79. @Test
  80. public void testProcessActionWithDuplicatedEntityField() throws Exception {
  81. String entityField = RandomTestUtil.randomString();
  82. MockLiferayPortletActionRequest mockLiferayPortletActionRequest =
  83. _getMockLiferayPortletActionRequest(
  84. RandomTestUtil.randomString(), entityField, null);
  85. _mvcActionCommand.processAction(
  86. mockLiferayPortletActionRequest,
  87. new MockLiferayPortletActionResponse());
  88. mockLiferayPortletActionRequest = _getMockLiferayPortletActionRequest(
  89. RandomTestUtil.randomString(), entityField, null);
  90. _mvcActionCommand.processAction(
  91. mockLiferayPortletActionRequest,
  92. new MockLiferayPortletActionResponse());
  93. Assert.assertTrue(
  94. MultiSessionErrors.contains(
  95. mockLiferayPortletActionRequest,
  96. StringBundler.concat(
  97. "com.liferay.segments.context.vocabulary.internal.",
  98. "configuration.persistence.listener.",
  99. "DuplicatedSegmentsContextVocabularyConfiguration",
  100. "ModelListenerException")));
  101. Optional<Configuration> configurationOptional =
  102. _getConfigurationOptional(entityField);
  103. Configuration configuration = configurationOptional.get();
  104. configuration.delete();
  105. }
  106. @Test
  107. public void testProcessActionWithoutEntityField() throws Exception {
  108. MockLiferayPortletActionRequest mockLiferayPortletActionRequest =
  109. _getMockLiferayPortletActionRequest(null, null, null);
  110. _mvcActionCommand.processAction(
  111. mockLiferayPortletActionRequest,
  112. new MockLiferayPortletActionResponse());
  113. Assert.assertTrue(
  114. MultiSessionErrors.contains(
  115. mockLiferayPortletActionRequest,
  116. ConfigurationModelListenerException.class.getCanonicalName()));
  117. }
  118. @Test
  119. public void testProcessActionWithPid() throws Exception {
  120. String entityField = RandomTestUtil.randomString();
  121. MockLiferayPortletActionRequest mockLiferayPortletActionRequest =
  122. _getMockLiferayPortletActionRequest(
  123. RandomTestUtil.randomString(), entityField, null);
  124. _mvcActionCommand.processAction(
  125. mockLiferayPortletActionRequest,
  126. new MockLiferayPortletActionResponse());
  127. Optional<Configuration> configurationOptional =
  128. _getConfigurationOptional(entityField);
  129. Configuration configuration = configurationOptional.get();
  130. try {
  131. String assetVocabulary = RandomTestUtil.randomString();
  132. mockLiferayPortletActionRequest =
  133. _getMockLiferayPortletActionRequest(
  134. assetVocabulary, entityField, configuration.getPid());
  135. _mvcActionCommand.processAction(
  136. mockLiferayPortletActionRequest,
  137. new MockLiferayPortletActionResponse());
  138. configurationOptional = _getConfigurationOptional(entityField);
  139. configuration = configurationOptional.get();
  140. Dictionary<String, Object> properties =
  141. configuration.getProperties();
  142. Assert.assertEquals(
  143. assetVocabulary, properties.get("assetVocabulary"));
  144. }
  145. finally {
  146. configuration.delete();
  147. }
  148. }
  149. private Optional<Configuration> _getConfigurationOptional(
  150. String entityField)
  151. throws InvalidSyntaxException, IOException {
  152. return Stream.of(
  153. _configurationAdmin.listConfigurations(
  154. StringBundler.concat(
  155. "(", ConfigurationAdmin.SERVICE_FACTORYPID, "=",
  156. "com.liferay.segments.context.vocabulary.internal.",
  157. "configuration.SegmentsContextVocabularyConfiguration",
  158. ")"))
  159. ).filter(
  160. configuration -> {
  161. Dictionary<String, Object> properties =
  162. configuration.getProperties();
  163. return Objects.equals(
  164. properties.get("entityField"), entityField);
  165. }
  166. ).findAny();
  167. }
  168. private MockLiferayPortletActionRequest _getMockLiferayPortletActionRequest(
  169. String assetVocabulary, String entityField, String pid) {
  170. MockLiferayPortletActionRequest mockLiferayPortletActionRequest =
  171. new MockLiferayPortletActionRequest();
  172. mockLiferayPortletActionRequest.setParameter(
  173. "assetVocabulary", assetVocabulary);
  174. mockLiferayPortletActionRequest.setParameter(
  175. "entityField", entityField);
  176. mockLiferayPortletActionRequest.setParameter("pid", pid);
  177. mockLiferayPortletActionRequest.setSession(new MockPortletSession());
  178. return mockLiferayPortletActionRequest;
  179. }
  180. private static Locale _locale;
  181. @Inject
  182. private ConfigurationAdmin _configurationAdmin;
  183. @Inject(
  184. filter = "mvc.command.name=/update_segments_context_vocabulary_configuration"
  185. )
  186. private MVCActionCommand _mvcActionCommand;
  187. }