Skip to content

Commit

Permalink
fix(Unable to run IT from the IDE) Refs: #30564 (#30565)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fabrizzio-dotCMS authored Nov 5, 2024
1 parent 011883f commit 0fe6d13
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -323,13 +323,7 @@ public ESContentletAPIImpl(final ElasticReadOnlyCommand readOnlyCommand) {
}

private static UniqueFieldValidationStrategyResolver getUniqueFieldValidationStrategyResolver() {
final Optional<UniqueFieldValidationStrategyResolver> 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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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}
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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));
}
}

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -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;
Expand All @@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -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;
Expand All @@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -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;
Expand All @@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -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;
Expand All @@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,6 +23,7 @@
import org.junit.Test;

import java.util.List;
import org.junit.runner.RunWith;

/**
* Test for the {@link ContentTypeInitializer}
Expand Down
Loading

0 comments on commit 0fe6d13

Please sign in to comment.