From 0fe6d139c8b502e9fc1797f9604f495a4e86f319 Mon Sep 17 00:00:00 2001 From: Fabrizzio Araya <37148755+fabrizzio-dotCMS@users.noreply.github.com> Date: Mon, 4 Nov 2024 20:23:06 -0600 Subject: [PATCH] fix(Unable to run IT from the IDE) Refs: #30564 (#30565) Normally, the rule for running an integration test is to annotate it with the JUnit4WeldRunner. Additionally, annotate the test as @ApplicationScoped. However, there was an issue with a bean that was instantiated deep within the core of the CMS, causing a failure that impacted integration tests. Particularly on `UniqueFieldValidationStrategyResolver`which has an injection problem I resolved the problem by moving the injection to the constructor Injecting private fields as follows is problematic : ``` @Inject private DBUniqueFieldValidationStrategy dbUniqueFieldValidationStrategy; ``` This problem did not surface in other scenarios, but it became visible when running directly from the integration test. Additionally, there was a public field with an @Rule in JUnit 4 that was also causing issues, which has been corrected as well. --- .../business/ESContentletAPIImpl.java | 8 +- ...UniqueFieldValidationStrategyResolver.java | 18 ++--- .../java/com/dotcms/IntegrationTestBase.java | 18 +++-- .../track/collectors/FilesCollectorTest.java | 5 ++ .../collectors/PageDetailCollectorTest.java | 6 ++ .../track/collectors/PagesCollectorTest.java | 5 ++ .../collectors/SyncVanitiesCollectorTest.java | 5 ++ .../WebEventsCollectorServiceImplTest.java | 5 +- .../ContentTypeDestroyAPIImplTest.java | 8 +- .../business/ContentTypeInitializerTest.java | 3 + .../contenttype/test/ContentResourceTest.java | 79 ++++++++++--------- 11 files changed, 90 insertions(+), 70 deletions(-) diff --git a/dotCMS/src/main/java/com/dotcms/content/elasticsearch/business/ESContentletAPIImpl.java b/dotCMS/src/main/java/com/dotcms/content/elasticsearch/business/ESContentletAPIImpl.java index 8f27e6eb9454..6d9a01da8fb9 100644 --- a/dotCMS/src/main/java/com/dotcms/content/elasticsearch/business/ESContentletAPIImpl.java +++ b/dotCMS/src/main/java/com/dotcms/content/elasticsearch/business/ESContentletAPIImpl.java @@ -323,13 +323,7 @@ public ESContentletAPIImpl(final ElasticReadOnlyCommand readOnlyCommand) { } private static UniqueFieldValidationStrategyResolver getUniqueFieldValidationStrategyResolver() { - final Optional uniqueFieldValidationStrategyResolver = - CDIUtils.getBean(UniqueFieldValidationStrategyResolver.class); - - if (!uniqueFieldValidationStrategyResolver.isPresent()) { - throw new DotRuntimeException("Could not instance UniqueFieldValidationStrategyResolver"); - } - return uniqueFieldValidationStrategyResolver.get(); + return CDIUtils.getBeanThrows(UniqueFieldValidationStrategyResolver.class); } public ESContentletAPIImpl() { diff --git a/dotCMS/src/main/java/com/dotcms/contenttype/business/uniquefields/UniqueFieldValidationStrategyResolver.java b/dotCMS/src/main/java/com/dotcms/contenttype/business/uniquefields/UniqueFieldValidationStrategyResolver.java index 2db8a7bc7e55..f59fc8564d52 100644 --- a/dotCMS/src/main/java/com/dotcms/contenttype/business/uniquefields/UniqueFieldValidationStrategyResolver.java +++ b/dotCMS/src/main/java/com/dotcms/contenttype/business/uniquefields/UniqueFieldValidationStrategyResolver.java @@ -1,14 +1,9 @@ package com.dotcms.contenttype.business.uniquefields; -import com.dotcms.cdi.CDIUtils; import com.dotcms.content.elasticsearch.business.ESContentletAPIImpl; import com.dotcms.contenttype.business.uniquefields.extratable.DBUniqueFieldValidationStrategy; -import com.dotmarketing.exception.DotRuntimeException; -import com.google.common.annotations.VisibleForTesting; - -import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.context.Dependent; import javax.inject.Inject; -import java.util.Optional; /** * Utility class responsible for returning the appropriate {@link UniqueFieldValidationStrategy} @@ -17,17 +12,14 @@ * an {@link ESUniqueFieldValidationStrategy} is used. * */ -@ApplicationScoped +@Dependent public class UniqueFieldValidationStrategyResolver { - @Inject - private ESUniqueFieldValidationStrategy esUniqueFieldValidationStrategy; - @Inject - private DBUniqueFieldValidationStrategy dbUniqueFieldValidationStrategy; + private final ESUniqueFieldValidationStrategy esUniqueFieldValidationStrategy; - public UniqueFieldValidationStrategyResolver(){} + private final DBUniqueFieldValidationStrategy dbUniqueFieldValidationStrategy; - @VisibleForTesting + @Inject public UniqueFieldValidationStrategyResolver(final ESUniqueFieldValidationStrategy esUniqueFieldValidationStrategy, final DBUniqueFieldValidationStrategy dbUniqueFieldValidationStrategy){ this.esUniqueFieldValidationStrategy = esUniqueFieldValidationStrategy; diff --git a/dotcms-integration/src/test/java/com/dotcms/IntegrationTestBase.java b/dotcms-integration/src/test/java/com/dotcms/IntegrationTestBase.java index 427dee4fd2ea..270ec1540e84 100644 --- a/dotcms-integration/src/test/java/com/dotcms/IntegrationTestBase.java +++ b/dotcms-integration/src/test/java/com/dotcms/IntegrationTestBase.java @@ -29,10 +29,10 @@ import java.io.File; import java.io.PrintStream; import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import java.util.List; import org.apache.commons.io.FileUtils; import org.junit.After; -import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; @@ -51,11 +51,13 @@ public abstract class IntegrationTestBase extends BaseMessageResources { private static Boolean debugMode = Boolean.FALSE; - private final static PrintStream stdout = System.out; - private final static ByteArrayOutputStream output = new ByteArrayOutputStream(); + private static final PrintStream stdout = System.out; + private static final ByteArrayOutputStream output = new ByteArrayOutputStream(); @Rule - public TestName name = new TestName(); + public TestName getTestName() { + return new TestName(); + } @BeforeClass public static void beforeInit() throws Exception { @@ -64,12 +66,12 @@ public static void beforeInit() throws Exception { Config.setProperty("SYSTEM_EXIT_ON_STARTUP_FAILURE", false); } - protected static void setDebugMode(final boolean mode) throws UnsupportedEncodingException { + protected static void setDebugMode(final boolean mode) { debugMode = mode; if (debugMode) { - System.setOut(new PrintStream(output, true, "UTF-8")); + System.setOut(new PrintStream(output, true, StandardCharsets.UTF_8)); } } @@ -126,12 +128,12 @@ public void after() { if (DbConnectionFactory.inTransaction()) { Logger.error(IntegrationTestBase.class, - "Test " + name.getMethodName() + " has open transaction after"); + "Test " + getTestName().getMethodName() + " has open transaction after"); } if (DbConnectionFactory.connectionExists()) { Logger.error(IntegrationTestBase.class, - "Test " + name.getMethodName() + " has open connection after"); + "Test " + getTestName().getMethodName() + " has open connection after"); } //Closing the session diff --git a/dotcms-integration/src/test/java/com/dotcms/analytics/track/collectors/FilesCollectorTest.java b/dotcms-integration/src/test/java/com/dotcms/analytics/track/collectors/FilesCollectorTest.java index 99db1867f220..827b923619ea 100644 --- a/dotcms-integration/src/test/java/com/dotcms/analytics/track/collectors/FilesCollectorTest.java +++ b/dotcms-integration/src/test/java/com/dotcms/analytics/track/collectors/FilesCollectorTest.java @@ -1,6 +1,7 @@ package com.dotcms.analytics.track.collectors; import com.dotcms.IntegrationTestBase; +import com.dotcms.JUnit4WeldRunner; import com.dotcms.LicenseTestUtil; import com.dotcms.analytics.track.matchers.FilesRequestMatcher; import com.dotcms.datagen.ContentletDataGen; @@ -18,6 +19,7 @@ import com.dotmarketing.util.PageMode; import com.dotmarketing.util.UUIDUtil; import com.dotmarketing.util.UtilMethods; +import javax.enterprise.context.ApplicationScoped; import org.junit.BeforeClass; import org.junit.Test; @@ -26,6 +28,7 @@ import java.io.IOException; import java.util.HashMap; import java.util.Map; +import org.junit.runner.RunWith; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; @@ -36,6 +39,8 @@ * @author Jose Castro * @since Oct 16th, 2024 */ +@ApplicationScoped +@RunWith(JUnit4WeldRunner.class) public class FilesCollectorTest extends IntegrationTestBase { private static final String PARENT_FOLDER_1_NAME = "parent-folder"; diff --git a/dotcms-integration/src/test/java/com/dotcms/analytics/track/collectors/PageDetailCollectorTest.java b/dotcms-integration/src/test/java/com/dotcms/analytics/track/collectors/PageDetailCollectorTest.java index 17b67a72dbf2..4586f863dde6 100644 --- a/dotcms-integration/src/test/java/com/dotcms/analytics/track/collectors/PageDetailCollectorTest.java +++ b/dotcms-integration/src/test/java/com/dotcms/analytics/track/collectors/PageDetailCollectorTest.java @@ -1,6 +1,7 @@ package com.dotcms.analytics.track.collectors; import com.dotcms.IntegrationTestBase; +import com.dotcms.JUnit4WeldRunner; import com.dotcms.LicenseTestUtil; import com.dotcms.analytics.track.matchers.PagesAndUrlMapsRequestMatcher; import com.dotcms.contenttype.model.type.ContentType; @@ -23,6 +24,7 @@ import com.dotmarketing.util.PageMode; import com.dotmarketing.util.UUIDUtil; import com.dotmarketing.util.UtilMethods; +import javax.enterprise.context.ApplicationScoped; import org.junit.BeforeClass; import org.junit.Test; @@ -31,6 +33,7 @@ import java.net.UnknownHostException; import java.util.HashMap; import java.util.Map; +import org.junit.runner.RunWith; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -42,6 +45,9 @@ * @author Jose Castro * @since Oct 14th, 2024 */ + +@ApplicationScoped +@RunWith(JUnit4WeldRunner.class) public class PageDetailCollectorTest extends IntegrationTestBase { private static final String PARENT_FOLDER_1_NAME = "news"; diff --git a/dotcms-integration/src/test/java/com/dotcms/analytics/track/collectors/PagesCollectorTest.java b/dotcms-integration/src/test/java/com/dotcms/analytics/track/collectors/PagesCollectorTest.java index 245b4a54d28d..ff1de6696bf0 100644 --- a/dotcms-integration/src/test/java/com/dotcms/analytics/track/collectors/PagesCollectorTest.java +++ b/dotcms-integration/src/test/java/com/dotcms/analytics/track/collectors/PagesCollectorTest.java @@ -1,6 +1,7 @@ package com.dotcms.analytics.track.collectors; import com.dotcms.IntegrationTestBase; +import com.dotcms.JUnit4WeldRunner; import com.dotcms.LicenseTestUtil; import com.dotcms.analytics.track.matchers.PagesAndUrlMapsRequestMatcher; import com.dotcms.contenttype.model.type.ContentType; @@ -23,6 +24,7 @@ import com.dotmarketing.util.UUIDUtil; import com.dotmarketing.util.UtilMethods; import io.vavr.API; +import javax.enterprise.context.ApplicationScoped; import org.junit.BeforeClass; import org.junit.Test; @@ -31,6 +33,7 @@ import java.net.UnknownHostException; import java.util.HashMap; import java.util.Map; +import org.junit.runner.RunWith; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -43,6 +46,8 @@ * @author Jose Castro * @since Oct 9th, 2024 */ +@ApplicationScoped +@RunWith(JUnit4WeldRunner.class) public class PagesCollectorTest extends IntegrationTestBase { private static final String TEST_PAGE_NAME = "index"; diff --git a/dotcms-integration/src/test/java/com/dotcms/analytics/track/collectors/SyncVanitiesCollectorTest.java b/dotcms-integration/src/test/java/com/dotcms/analytics/track/collectors/SyncVanitiesCollectorTest.java index a5626f6398dd..0c6779786881 100644 --- a/dotcms-integration/src/test/java/com/dotcms/analytics/track/collectors/SyncVanitiesCollectorTest.java +++ b/dotcms-integration/src/test/java/com/dotcms/analytics/track/collectors/SyncVanitiesCollectorTest.java @@ -1,6 +1,7 @@ package com.dotcms.analytics.track.collectors; import com.dotcms.IntegrationTestBase; +import com.dotcms.JUnit4WeldRunner; import com.dotcms.LicenseTestUtil; import com.dotcms.datagen.ContentletDataGen; import com.dotcms.datagen.FileAssetDataGen; @@ -24,6 +25,7 @@ import com.dotmarketing.util.PageMode; import com.dotmarketing.util.UUIDUtil; import com.dotmarketing.util.UtilMethods; +import javax.enterprise.context.ApplicationScoped; import org.junit.BeforeClass; import org.junit.Test; @@ -33,6 +35,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Optional; +import org.junit.runner.RunWith; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; @@ -44,6 +47,8 @@ * @author Jose Castro * @since Oct 21st, 2024 */ +@ApplicationScoped +@RunWith(JUnit4WeldRunner.class) public class SyncVanitiesCollectorTest extends IntegrationTestBase { private static final String TEST_PAGE_NAME = "index"; diff --git a/dotcms-integration/src/test/java/com/dotcms/analytics/track/collectors/WebEventsCollectorServiceImplTest.java b/dotcms-integration/src/test/java/com/dotcms/analytics/track/collectors/WebEventsCollectorServiceImplTest.java index e0953235c0a1..9798517ddb81 100644 --- a/dotcms-integration/src/test/java/com/dotcms/analytics/track/collectors/WebEventsCollectorServiceImplTest.java +++ b/dotcms-integration/src/test/java/com/dotcms/analytics/track/collectors/WebEventsCollectorServiceImplTest.java @@ -1,5 +1,6 @@ package com.dotcms.analytics.track.collectors; +import com.dotcms.DataProviderWeldRunner; import com.dotcms.IntegrationTestBase; import com.dotcms.LicenseTestUtil; import com.dotcms.analytics.app.AnalyticsApp; @@ -45,6 +46,7 @@ import com.tngtech.java.junit.dataprovider.DataProvider; import com.tngtech.java.junit.dataprovider.DataProviderRunner; import com.tngtech.java.junit.dataprovider.UseDataProvider; +import javax.enterprise.context.ApplicationScoped; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; @@ -76,7 +78,8 @@ * @author Jose Castro * @since Oct 3rd, 2024 */ -@RunWith(DataProviderRunner.class) +@ApplicationScoped +@RunWith(DataProviderWeldRunner.class) public class WebEventsCollectorServiceImplTest extends IntegrationTestBase { private static final String PARENT_FOLDER_1_NAME = "parent-folder"; diff --git a/dotcms-integration/src/test/java/com/dotcms/contenttype/business/ContentTypeDestroyAPIImplTest.java b/dotcms-integration/src/test/java/com/dotcms/contenttype/business/ContentTypeDestroyAPIImplTest.java index d36f32207874..8de5ac5a8e9c 100644 --- a/dotcms-integration/src/test/java/com/dotcms/contenttype/business/ContentTypeDestroyAPIImplTest.java +++ b/dotcms-integration/src/test/java/com/dotcms/contenttype/business/ContentTypeDestroyAPIImplTest.java @@ -1,6 +1,7 @@ package com.dotcms.contenttype.business; import com.dotcms.IntegrationTestBase; +import com.dotcms.JUnit4WeldRunner; import com.dotcms.business.CloseDBIfOpened; import com.dotcms.content.elasticsearch.business.ESSearchResults; import com.dotcms.contenttype.business.ContentTypeDestroyAPIImpl.ContentletVersionInfo; @@ -14,7 +15,6 @@ import com.dotcms.datagen.SiteDataGen; import com.dotcms.datagen.TagDataGen; import com.dotcms.datagen.TestDataUtils; -import com.dotcms.datagen.TestWorkflowUtils; import com.dotcms.util.IntegrationTestInitService; import com.dotmarketing.beans.Host; import com.dotmarketing.business.APILocator; @@ -37,14 +37,18 @@ import java.util.Optional; import java.util.Random; import java.util.Set; +import javax.enterprise.context.ApplicationScoped; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; +import org.junit.runner.RunWith; /** * Here we test the {@link ContentTypeDestroyAPIImpl} * @author Fabrizzio */ +@ApplicationScoped +@RunWith(JUnit4WeldRunner.class) public class ContentTypeDestroyAPIImplTest extends IntegrationTestBase { @BeforeClass @@ -140,7 +144,7 @@ public void Destroy_General_Test() throws DotDataException, DotSecurityException Assert.assertNull(deletedContentType); //Test no copy structure is left hang around - final String likeName = String.format("%s_disposed_*", name); + final String likeName = String.format("%s_disposed_*", getTestName()); int count = new DotConnect().setSQL("select count(*) as x from structure where velocity_var_name like ? ").addParam(likeName).getInt("x"); Assert.assertEquals(0, count); diff --git a/dotcms-integration/src/test/java/com/dotcms/contenttype/business/ContentTypeInitializerTest.java b/dotcms-integration/src/test/java/com/dotcms/contenttype/business/ContentTypeInitializerTest.java index 6fe0b8dd39ca..778e0e7c75f6 100644 --- a/dotcms-integration/src/test/java/com/dotcms/contenttype/business/ContentTypeInitializerTest.java +++ b/dotcms-integration/src/test/java/com/dotcms/contenttype/business/ContentTypeInitializerTest.java @@ -1,10 +1,12 @@ package com.dotcms.contenttype.business; import com.dotcms.IntegrationTestBase; +import com.dotcms.JUnit4WeldRunner; import com.dotcms.content.elasticsearch.constants.ESMappingConstants; import com.dotcms.contenttype.model.type.ContentType; import com.dotcms.datagen.ContentletDataGen; import com.dotcms.datagen.UserDataGen; +import com.dotcms.repackage.org.directwebremoting.guice.ApplicationScoped; import com.dotcms.util.IntegrationTestInitService; import com.dotmarketing.beans.Permission; import com.dotmarketing.business.APILocator; @@ -21,6 +23,7 @@ import org.junit.Test; import java.util.List; +import org.junit.runner.RunWith; /** * Test for the {@link ContentTypeInitializer} diff --git a/dotcms-integration/src/test/java/com/dotcms/contenttype/test/ContentResourceTest.java b/dotcms-integration/src/test/java/com/dotcms/contenttype/test/ContentResourceTest.java index a4a454c4d4b2..6f37b9257b24 100644 --- a/dotcms-integration/src/test/java/com/dotcms/contenttype/test/ContentResourceTest.java +++ b/dotcms-integration/src/test/java/com/dotcms/contenttype/test/ContentResourceTest.java @@ -1,5 +1,17 @@ package com.dotcms.contenttype.test; +import static com.dotcms.rest.api.v1.workflow.WorkflowTestUtil.DM_WORKFLOW; +import static com.dotmarketing.business.Role.ADMINISTRATOR; +import static com.dotmarketing.portlets.workflows.business.BaseWorkflowIntegrationTest.createContentTypeAndAssignPermissions; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import com.dotcms.DataProviderWeldRunner; import com.dotcms.IntegrationTestBase; import com.dotcms.contenttype.business.ContentTypeAPI; import com.dotcms.contenttype.business.FieldAPI; @@ -62,35 +74,8 @@ import com.liferay.portal.util.WebKeys; import com.liferay.util.StringPool; import com.tngtech.java.junit.dataprovider.DataProvider; -import com.tngtech.java.junit.dataprovider.DataProviderRunner; import com.tngtech.java.junit.dataprovider.UseDataProvider; import io.vavr.Tuple2; -import org.glassfish.jersey.internal.util.Base64; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mockito; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.NodeList; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; - -import javax.servlet.ReadListener; -import javax.servlet.ServletInputStream; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.Response.Status; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.xpath.XPath; -import javax.xml.xpath.XPathConstants; -import javax.xml.xpath.XPathExpressionException; -import javax.xml.xpath.XPathFactory; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; @@ -107,19 +92,35 @@ import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; +import javax.enterprise.context.ApplicationScoped; +import javax.servlet.ReadListener; +import javax.servlet.ServletInputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; +import org.glassfish.jersey.internal.util.Base64; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; -import static com.dotcms.rest.api.v1.workflow.WorkflowTestUtil.DM_WORKFLOW; -import static com.dotmarketing.business.Role.ADMINISTRATOR; -import static com.dotmarketing.portlets.workflows.business.BaseWorkflowIntegrationTest.createContentTypeAndAssignPermissions; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -@RunWith(DataProviderRunner.class) +@ApplicationScoped +@RunWith(DataProviderWeldRunner.class) public class ContentResourceTest extends IntegrationTestBase { final static String REQUIRED_NUMERIC_FIELD_NAME = "numeric";