Skip to content

Commit

Permalink
#30277 Feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
freddyDOTCMS committed Oct 15, 2024
1 parent 5d0574a commit 504efaa
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 71 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,30 @@
import java.util.Map;

/**
* Default implementation of {@link UniqueFieldAPI}
* This Helper allow you to interact with the unique_fields table
*/
public class UniqueFieldAPIImpl implements UniqueFieldAPI {
public class UniqueFieldAPIHelper {

private final UniqueFieldFactory uniqueFieldFactory;

public UniqueFieldAPIImpl(){
public UniqueFieldAPIHelper(){
this(FactoryLocator.getUniqueFieldFactory());
}

public UniqueFieldAPIImpl(final UniqueFieldFactory uniqueFieldFactory) {
public UniqueFieldAPIHelper(final UniqueFieldFactory uniqueFieldFactory) {
this.uniqueFieldFactory = uniqueFieldFactory;
}


/**
* Default implementation of {@link UniqueFieldAPI#insert(UniqueFieldCriteria, String)}
* Insert a new unique field value, if the value is duplicated then a {@link java.sql.SQLException} is thrown.
*
* @param uniqueFieldCriteria
* @param contentletId
*
* @throws UniqueFieldValueDupliacatedException
* @throws DotDataException
* @throws UniqueFieldValueDupliacatedException when the Value is duplicated
* @throws DotDataException when a DotDataException is throws
*/
@Override
@WrapInTransaction
public void insert(final UniqueFieldCriteria uniqueFieldCriteria, final String contentletId)
throws UniqueFieldValueDupliacatedException, DotDataException {

Expand Down
13 changes: 1 addition & 12 deletions dotCMS/src/main/java/com/dotmarketing/business/APILocator.java
Original file line number Diff line number Diff line change
Expand Up @@ -1182,15 +1182,6 @@ public static ContentAnalyticsAPI getContentAnalyticsAPI() {
return (ContentAnalyticsAPI) getInstance(APIIndex.CONTENT_ANALYTICS_API);
}

/**
* Returns a singleton instance of the {@link UniqueFieldAPIImpl} class.
*
* @return The {@link UniqueFieldAPI} instance.
*/
public static UniqueFieldAPI getUniqueFieldAPI() {
return (UniqueFieldAPI) getInstance(APIIndex.UNIQUE_FIELD_API);
}

/**
* Generates a unique instance of the specified dotCMS API.
*
Expand Down Expand Up @@ -1348,8 +1339,7 @@ enum APIIndex
SYSTEM_API,
ACHECKER_API,
CONTENT_ANALYTICS_API,
JOB_QUEUE_MANAGER_API,
UNIQUE_FIELD_API;
JOB_QUEUE_MANAGER_API;

Object create() {
switch(this) {
Expand Down Expand Up @@ -1444,7 +1434,6 @@ Object create() {
case ACHECKER_API: return new ACheckerAPIImpl();
case CONTENT_ANALYTICS_API: CDIUtils.getBean(ContentAnalyticsAPI.class).orElseThrow(() -> new DotRuntimeException("Content Analytics API not found"));
case JOB_QUEUE_MANAGER_API: return CDIUtils.getBean(JobQueueManagerAPI.class).orElseThrow(() -> new DotRuntimeException("JobQueueManagerAPI not found"));
case UNIQUE_FIELD_API: return new UniqueFieldAPIImpl();
}
throw new AssertionError("Unknown API index: " + this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@
Task241013RemoveFullPathLcColumnFromIdentifierTest.class,
Task241009CreatePostgresJobQueueTablesTest.class,
UniqueFieldFactoryImplTest.class,
UniqueFieldAPIImplTest.class
UniqueFieldAPIHelperTest.class
})

public class MainSuite2b {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.dotcms.contenttype.business;

import com.dotcms.contenttype.business.UniqueFieldFactoryImpl;
import com.dotcms.contenttype.model.field.Field;
import com.dotcms.contenttype.model.field.TextField;
import com.dotcms.contenttype.model.type.ContentType;
Expand All @@ -14,12 +13,10 @@
import com.dotmarketing.portlets.contentlet.model.Contentlet;
import com.dotmarketing.portlets.languagesmanager.model.Language;
import com.dotmarketing.util.StringUtils;
import com.liferay.portal.language.LanguageException;
import net.bytebuddy.utility.RandomString;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -28,7 +25,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

public class UniqueFieldAPIImplTest {
public class UniqueFieldAPIHelperTest {

@BeforeClass
public static void prepare() throws Exception {
Expand All @@ -46,7 +43,7 @@ public static void prepare() throws Exception {
}

/**
* Method to test: {@link UniqueFieldAPIImpl#insert(UniqueFieldCriteria, String)}
* Method to test: {@link UniqueFieldAPIHelper#insert(UniqueFieldCriteria, String)}
* When: Called the method with the right parameters
* Should: Insert a register in the unique_fields table
*/
Expand All @@ -71,13 +68,14 @@ public void insert() throws DotDataException, UniqueFieldValueDupliacatedExcepti
.host(site)
.next();

APILocator.getUniqueFieldAPI().insert(uniqueFieldCriteria, contentlet.getIdentifier());
final UniqueFieldAPIHelper uniqueFieldAPIHelper = new UniqueFieldAPIHelper();
uniqueFieldAPIHelper.insert(uniqueFieldCriteria, contentlet.getIdentifier());

validateAfterInsert(uniqueFieldCriteria, contentlet);
}

/**
* Method to test: {@link UniqueFieldAPIImpl#insert(UniqueFieldCriteria, String)}
* Method to test: {@link UniqueFieldAPIHelper#insert(UniqueFieldCriteria, String)}
* When: Called the method with a 'unique_key_val' duplicated
* Should: Throw a {@link UniqueFieldValueDupliacatedException}
*/
Expand All @@ -102,15 +100,16 @@ public void tryToInsertDuplicated() throws DotDataException, UniqueFieldValueDup
.host(site)
.next();

APILocator.getUniqueFieldAPI().insert(uniqueFieldCriteria, contentlet.getIdentifier());
final UniqueFieldAPIHelper uniqueFieldAPIHelper = new UniqueFieldAPIHelper();
uniqueFieldAPIHelper.insert(uniqueFieldCriteria, contentlet.getIdentifier());

final String hash = StringUtils.hashText(contentType.id() + uniqueField.variable() + language.getId() + value);

final int countBefore = Integer.parseInt(new DotConnect()
.setSQL("SELECT COUNT(*) as count FROM unique_fields WHERE unique_key_val = ?")
.addParam(hash).loadObjectResults().get(0).get("count").toString());
try {
APILocator.getUniqueFieldAPI().insert(uniqueFieldCriteria, contentlet.getIdentifier());
uniqueFieldAPIHelper.insert(uniqueFieldCriteria, contentlet.getIdentifier());
throw new AssertionError("UniqueFieldValueDupliacatedException expected");
} catch (UniqueFieldValueDupliacatedException e) {

Expand All @@ -123,7 +122,7 @@ public void tryToInsertDuplicated() throws DotDataException, UniqueFieldValueDup
}

/**
* Method to test: {@link UniqueFieldAPIImpl#insert(UniqueFieldCriteria, String)}
* Method to test: {@link UniqueFieldAPIHelper#insert(UniqueFieldCriteria, String)}
* When: Called the method with a field with uniquePerSite set to true
* Should: Allow insert the same values in different Host
*/
Expand Down Expand Up @@ -160,7 +159,8 @@ public void insertWithUniquePerSiteSetToTrue() throws DotDataException, UniqueFi
.host(site)
.next();

APILocator.getUniqueFieldAPI().insert(uniqueFieldCriteria_1, contentlet_1.getIdentifier());
final UniqueFieldAPIHelper uniqueFieldAPIHelper = new UniqueFieldAPIHelper();
uniqueFieldAPIHelper.insert(uniqueFieldCriteria_1, contentlet_1.getIdentifier());

final UniqueFieldCriteria uniqueFieldCriteria_2 = new UniqueFieldCriteria.Builder()
.setContentType(contentType)
Expand All @@ -175,7 +175,7 @@ public void insertWithUniquePerSiteSetToTrue() throws DotDataException, UniqueFi
.host(site_2)
.next();

APILocator.getUniqueFieldAPI().insert(uniqueFieldCriteria_2, contentlet_2.getIdentifier());
uniqueFieldAPIHelper.insert(uniqueFieldCriteria_2, contentlet_2.getIdentifier());

final List<Map<String, Object>> results = new DotConnect()
.setSQL("SELECT * FROM unique_fields WHERE supporting_values->>'contentTypeID' = ?")
Expand All @@ -201,7 +201,7 @@ public void insertWithUniquePerSiteSetToTrue() throws DotDataException, UniqueFi
}

/**
* Method to test: {@link UniqueFieldAPIImpl#insert(UniqueFieldCriteria, String)}
* Method to test: {@link UniqueFieldAPIHelper#insert(UniqueFieldCriteria, String)}
* When: Called the method with a Not Unique Field
* Should: thrown an {@link IllegalArgumentException}
*/
Expand All @@ -227,7 +227,8 @@ public void insertNotUniqueField() throws DotDataException, UniqueFieldValueDupl
.next();

try {
APILocator.getUniqueFieldAPI().insert(uniqueFieldCriteria, contentlet.getIdentifier());
final UniqueFieldAPIHelper uniqueFieldAPIHelper = new UniqueFieldAPIHelper();
uniqueFieldAPIHelper.insert(uniqueFieldCriteria, contentlet.getIdentifier());
throw new AssertionError("IllegalArgumentExceptionΩ Expected");
} catch (IllegalArgumentException e) {
//expected
Expand Down Expand Up @@ -264,7 +265,7 @@ private static void validateAfterInsert(UniqueFieldCriteria uniqueFieldCriteria,
}

/**
* Method to test: {@link UniqueFieldAPIImpl#insert(UniqueFieldCriteria, String)}
* Method to test: {@link UniqueFieldAPIHelper#insert(UniqueFieldCriteria, String)}
* When: Called the method twice with different Content Type
* Should: Insert a register in the unique_fields table
*/
Expand All @@ -289,7 +290,8 @@ public void insertWithDifferentContentType() throws DotDataException, UniqueFiel
.host(site)
.next();

APILocator.getUniqueFieldAPI().insert(uniqueFieldCriteria_1, contentlet_1.getIdentifier());
final UniqueFieldAPIHelper uniqueFieldAPIHelper = new UniqueFieldAPIHelper();
uniqueFieldAPIHelper.insert(uniqueFieldCriteria_1, contentlet_1.getIdentifier());
validateAfterInsert(uniqueFieldCriteria_1, contentlet_1);

final Field field_2 = new FieldDataGen().type(TextField.class).velocityVarName("unique").unique(true).next();
Expand All @@ -308,12 +310,12 @@ public void insertWithDifferentContentType() throws DotDataException, UniqueFiel
.setSite(site)
.build();

APILocator.getUniqueFieldAPI().insert(uniqueFieldCriteria_2, contentlet_2.getIdentifier());
uniqueFieldAPIHelper.insert(uniqueFieldCriteria_2, contentlet_2.getIdentifier());
validateAfterInsert(uniqueFieldCriteria_2, contentlet_2);
}

/**
* Method to test: {@link UniqueFieldAPIImpl#insert(UniqueFieldCriteria, String)}
* Method to test: {@link UniqueFieldAPIHelper#insert(UniqueFieldCriteria, String)}
* When: Called the method twice with different Field
* Should: Insert a register in the unique_fields table
*/
Expand Down Expand Up @@ -343,7 +345,8 @@ public void insertWithDifferentField() throws DotDataException, UniqueFieldValue
.setSite(site)
.build();

APILocator.getUniqueFieldAPI().insert(uniqueFieldCriteria_1, contentlet.getIdentifier());
final UniqueFieldAPIHelper uniqueFieldAPIHelper = new UniqueFieldAPIHelper();
uniqueFieldAPIHelper.insert(uniqueFieldCriteria_1, contentlet.getIdentifier());

final UniqueFieldCriteria uniqueFieldCriteria_2 = new UniqueFieldCriteria.Builder()
.setContentType(contentType)
Expand All @@ -353,14 +356,14 @@ public void insertWithDifferentField() throws DotDataException, UniqueFieldValue
.setSite(site)
.build();

APILocator.getUniqueFieldAPI().insert(uniqueFieldCriteria_2, contentlet.getIdentifier());
uniqueFieldAPIHelper.insert(uniqueFieldCriteria_2, contentlet.getIdentifier());

validateAfterInsert(uniqueFieldCriteria_1, contentlet);
validateAfterInsert(uniqueFieldCriteria_2, contentlet);
}

/**
* Method to test: {@link UniqueFieldAPIImpl#insert(UniqueFieldCriteria, String)}
* Method to test: {@link UniqueFieldAPIHelper#insert(UniqueFieldCriteria, String)}
* When: Called the method twice with different Value
* Should: Insert a register in the unique_fields table
*/
Expand Down Expand Up @@ -388,7 +391,8 @@ public void insertWithDifferentValue() throws DotDataException, UniqueFieldValue
.setSite(site)
.build();

APILocator.getUniqueFieldAPI().insert(uniqueFieldCriteria_1, contentlet.getIdentifier());
final UniqueFieldAPIHelper uniqueFieldAPIHelper = new UniqueFieldAPIHelper();
uniqueFieldAPIHelper.insert(uniqueFieldCriteria_1, contentlet.getIdentifier());

final UniqueFieldCriteria uniqueFieldCriteria_2 = new UniqueFieldCriteria.Builder()
.setContentType(contentType)
Expand All @@ -398,14 +402,14 @@ public void insertWithDifferentValue() throws DotDataException, UniqueFieldValue
.setSite(site)
.build();

APILocator.getUniqueFieldAPI().insert(uniqueFieldCriteria_2, contentlet.getIdentifier());
uniqueFieldAPIHelper.insert(uniqueFieldCriteria_2, contentlet.getIdentifier());

validateAfterInsert(uniqueFieldCriteria_1, contentlet);
validateAfterInsert(uniqueFieldCriteria_2, contentlet);
}

/**
* Method to test: {@link UniqueFieldAPIImpl#insert(UniqueFieldCriteria, String)}
* Method to test: {@link UniqueFieldAPIHelper#insert(UniqueFieldCriteria, String)}
* When: Called the method twice with different Language
* Should: Insert a register in the unique_fields table
*/
Expand Down Expand Up @@ -434,9 +438,9 @@ public void insertWithDifferentLanguage() throws DotDataException, UniqueFieldVa
.setSite(site)
.build();

APILocator.getUniqueFieldAPI().insert(uniqueFieldCriteria_1, contentlet.getIdentifier());


final UniqueFieldAPIHelper uniqueFieldAPIHelper = new UniqueFieldAPIHelper();
uniqueFieldAPIHelper.insert(uniqueFieldCriteria_1, contentlet.getIdentifier());
final UniqueFieldCriteria uniqueFieldCriteria_2 = new UniqueFieldCriteria.Builder()
.setContentType(contentType)
.setField(field)
Expand All @@ -445,7 +449,7 @@ public void insertWithDifferentLanguage() throws DotDataException, UniqueFieldVa
.setSite(site)
.build();

APILocator.getUniqueFieldAPI().insert(uniqueFieldCriteria_2, contentlet.getIdentifier());
uniqueFieldAPIHelper.insert(uniqueFieldCriteria_2, contentlet.getIdentifier());

validateAfterInsert(uniqueFieldCriteria_1, contentlet);
validateAfterInsert(uniqueFieldCriteria_2, contentlet);
Expand Down

0 comments on commit 504efaa

Please sign in to comment.