Skip to content

Commit

Permalink
#30279 Fixing test
Browse files Browse the repository at this point in the history
  • Loading branch information
freddyDOTCMS committed Oct 25, 2024
1 parent df2e213 commit 4069133
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public abstract class UniqueFieldValidationStrategy {
public final void validate(final Contentlet contentlet, final Field uniqueField)
throws UniqueFieldValueDuplicatedException, DotDataException, DotSecurityException {

if (!uniqueField.unique()) {
throw new IllegalArgumentException("The Field " + uniqueField.variable() + " is not unique");
}

Object value = contentlet.get(uniqueField.variable());

Objects.requireNonNull(contentlet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import com.dotcms.contenttype.business.uniquefields.UniqueFieldValidationStrategy;
import com.dotcms.contenttype.model.type.ContentType;
import com.dotcms.util.CollectionsUtils;
import com.dotcms.util.JsonUtil;
import com.dotmarketing.beans.Host;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.common.db.DotConnect;
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.exception.DotRuntimeException;
import com.dotmarketing.exception.DotSecurityException;
import com.dotmarketing.portlets.contentlet.model.Contentlet;
import com.dotmarketing.portlets.languagesmanager.model.Language;
Expand All @@ -19,7 +21,11 @@
import com.dotmarketing.util.UtilMethods;
import com.google.common.annotations.VisibleForTesting;
import com.liferay.portal.model.User;
import org.postgresql.util.PGobject;

import static com.dotcms.util.CollectionsUtils.list;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -52,8 +58,8 @@ public void innerValidate(final Contentlet contentlet, final Field field, final
final ContentType contentType) throws UniqueFieldValueDuplicatedException, DotDataException, DotSecurityException {

if (UtilMethods.isSet(contentlet.getIdentifier())) {
cleanUniqueFieldsUp(contentlet);
}
cleanUniqueFieldsUp(contentlet, field);
}

final User systemUser = APILocator.systemUser();
final Host host = APILocator.getHostAPI().find(contentlet.getHost(), systemUser, false);
Expand All @@ -71,22 +77,27 @@ public void innerValidate(final Contentlet contentlet, final Field field, final
checkUnique(uniqueFieldCriteria, contentlet.getIdentifier());
}

private static void cleanUniqueFieldsUp(Contentlet contentlet) throws DotDataException {
private static void cleanUniqueFieldsUp(final Contentlet contentlet, final Field field) throws DotDataException {
Optional<Map<String, Object>> uniqueFieldOptional = UniqueFieldDataBaseUtil.INSTANCE.get(contentlet);

if (uniqueFieldOptional.isPresent()) {
final Map<String, Object> uniqueFields = uniqueFieldOptional.get();

final String hash = uniqueFields.get("unique_key_val").toString();
final Map<String, Object> supportingValues = (Map<String, Object>) uniqueFields.get("supporting_values");
final List<String> contentletsId = (List<String>) supportingValues.get("contentletsId");

if (contentletsId.size() == 1) {
UniqueFieldDataBaseUtil.INSTANCE.delete(hash);
} else {
contentletsId.remove(contentlet.getIdentifier());
UniqueFieldDataBaseUtil.INSTANCE.updateContentLists(hash, contentletsId);
try {
if (uniqueFieldOptional.isPresent()) {
final Map<String, Object> uniqueFields = uniqueFieldOptional.get();

final String hash = uniqueFields.get("unique_key_val").toString();
final PGobject supportingValues = (PGobject) uniqueFields.get("supporting_values");
final Map<String, Object> supportingValuesMap = JsonUtil.getJsonFromString(supportingValues.getValue());
final List<String> contentletsId = (List<String>) supportingValuesMap.get("contentletsId");

if (contentletsId.size() == 1) {
UniqueFieldDataBaseUtil.INSTANCE.delete(hash, field.variable());
} else {
contentletsId.remove(contentlet.getIdentifier());
UniqueFieldDataBaseUtil.INSTANCE.updateContentLists(hash, contentletsId);
}
}
} catch (IOException e){
throw new DotDataException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.dotcms.contenttype.business.uniquefields.extratable;

import com.dotcms.business.CloseDBIfOpened;
import com.dotmarketing.common.db.DotConnect;
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.portlets.contentlet.model.Contentlet;
Expand All @@ -25,7 +24,8 @@ enum UniqueFieldDataBaseUtil {
private final static String GET_UNIQUE_FIELDS_BY_CONTENTLET = "SELECT * FROM unique_fields " +
"WHERE supporting_values->'contentletsId' @> ?::jsonb AND supporting_values->>'variant' = ?";

private final String DELETE_UNIQUE_FIELDS = "DELETE FROM unique_fields WHERE unique_key_val = ?";
private final String DELETE_UNIQUE_FIELDS = "DELETE FROM unique_fields WHERE unique_key_val = ? " +
"AND supporting_values->>'fieldVariableName' = ?";

/**
* Insert a new register into the unique_fields table, if already exists another register with the same
Expand Down Expand Up @@ -64,9 +64,10 @@ public Optional<Map<String, Object>> get(final Contentlet contentlet) throws Dot
return results.isEmpty() ? Optional.empty() : Optional.of(results.get(0));
}

public void delete(final String hash) throws DotDataException {
public void delete(final String hash, String fiedVariable) throws DotDataException {
new DotConnect().setSQL(DELETE_UNIQUE_FIELDS)
.addParam(hash)
.addParam(fiedVariable)
.loadObjectResults();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.postgresql.util.PGobject;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
Expand Down Expand Up @@ -2292,7 +2293,7 @@ public void uniqueFieldWithArchiveContentlet(final Boolean enabledDataBaseValida
*/
@Test
public void updateContentletWithDuplicateValuesInUniqueFields()
throws DotDataException, DotSecurityException, InterruptedException {
throws DotDataException, DotSecurityException, InterruptedException, IOException {
ESContentletAPIImpl.setEnabledUniqueFieldsDataBaseValidation(true);

final Language language = new LanguageDataGen().nextPersisted();
Expand Down Expand Up @@ -2328,6 +2329,7 @@ public void updateContentletWithDuplicateValuesInUniqueFields()
final ImmutableTextField uniqueFieldUpdated = ImmutableTextField.builder()
.from(uniqueTextField)
.contentTypeId(contentType.id())
.unique(true)
.build();

APILocator.getContentTypeFieldAPI().save(uniqueFieldUpdated, APILocator.systemUser());
Expand All @@ -2338,7 +2340,8 @@ public void updateContentletWithDuplicateValuesInUniqueFields()
"fieldValue", uniqueVersionValue.toString(),
"languageId", language.getId(),
"hostId", host.getIdentifier(),
"uniquePerSite", true
"uniquePerSite", true,
"variant", VariantAPI.DEFAULT_VARIANT.name()
);

final Map<String, Object> supportingValues = new HashMap<>(uniqueFieldCriteriaMap);
Expand All @@ -2359,7 +2362,24 @@ public void updateContentletWithDuplicateValuesInUniqueFields()

APILocator.getContentletAPI().checkin(checkout, APILocator.systemUser(), false);

checkUniqueFieldsTable(false, contentType, uniqueTextField, contentlet_1, contentlet_2);
checkContentletInUniqueFieldsTable(contentlet_1);
checkContentletInUniqueFieldsTable(contentlet_2);
}

private static void checkContentletInUniqueFieldsTable(final Contentlet contentlet) throws DotDataException, IOException {
final List<Map<String, Object>> result_1 = new DotConnect()
.setSQL("SELECT * FROM unique_fields WHERE supporting_values->'contentletsId' @> ?::jsonb")
.addParam("\"" + contentlet.getIdentifier() + "\"")
.loadObjectResults();

assertEquals(1, result_1.size());

final PGobject supportingValues = (PGobject) result_1.get(0).get("supporting_values");
final Map<String, Object> supportingValuesMap = JsonUtil.getJsonFromString(supportingValues.getValue());
final List<String> contentletsId = (List<String>) supportingValuesMap.get("contentletsId");

assertEquals(1, contentletsId.size());
assertEquals(contentlet.getIdentifier(), contentletsId.get(0));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public void insertNotUniqueField() throws DotDataException, UniqueFieldValueDupl

final ExtraTableUniqueFieldValidationStrategy extraTableUniqueFieldValidationStrategy = new ExtraTableUniqueFieldValidationStrategy();
extraTableUniqueFieldValidationStrategy.validate(contentlet, notUniqueField);
throw new AssertionError("IllegalArgumentExceptionΩ Expected");
throw new AssertionError("IllegalArgumentException Expected");
} catch (IllegalArgumentException e) {
//expected
assertEquals("The Field " + notUniqueField.variable() + " is not unique", e.getMessage());
Expand Down Expand Up @@ -433,10 +433,24 @@ public void insertWithDifferentValue() throws DotDataException, UniqueFieldValue

extraTableUniqueFieldValidationStrategy.validate(contentlet_2, field);

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

private static void validateDoesNotExists(final UniqueFieldCriteria uniqueFieldCriteria_1) throws DotDataException {
final List<Map<String, Object>> results = new DotConnect()
.setSQL("SELECT * FROM unique_fields WHERE supporting_values->>'contentTypeID' = ? AND " +
"supporting_values->>'fieldVariableName' = ? AND supporting_values->>'fieldValue' = ? AND " +
"(supporting_values->>'languageId')::numeric = ?")
.addParam(uniqueFieldCriteria_1.contentType().id())
.addParam(uniqueFieldCriteria_1.field().variable())
.addParam(uniqueFieldCriteria_1.value())
.addParam(uniqueFieldCriteria_1.language().getId())
.loadObjectResults();

assertTrue(results.isEmpty());
}

/**
* Method to test: {@link ExtraTableUniqueFieldValidationStrategy#validate(Contentlet, Field)}
* When: Called the method twice with different Language
Expand Down Expand Up @@ -495,7 +509,7 @@ public void insertWithDifferentLanguage() throws DotDataException, UniqueFieldVa

extraTableUniqueFieldValidationStrategy.validate(contentlet_2, field);

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

Expand Down

0 comments on commit 4069133

Please sign in to comment.