Skip to content

Commit

Permalink
introduce PropertiesFilterUtil.removeIgnoredProperties which works wi…
Browse files Browse the repository at this point in the history
…th a map to reduce duplicated code, add dedicated test case for it
  • Loading branch information
stefanseifert committed Dec 5, 2024
1 parent 19bada9 commit 606decb
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
import static io.wcm.caconfig.extensions.persistence.impl.PersistenceUtils.ensureContainingPage;
import static io.wcm.caconfig.extensions.persistence.impl.PersistenceUtils.ensurePageIfNotContainingPage;
import static io.wcm.caconfig.extensions.persistence.impl.PersistenceUtils.getOrCreateResource;
import static io.wcm.caconfig.extensions.persistence.impl.PersistenceUtils.isItemModifiedOrNewlyAdded;
import static io.wcm.caconfig.extensions.persistence.impl.PersistenceUtils.replaceProperties;
import static io.wcm.caconfig.extensions.persistence.impl.PersistenceUtils.updatePageLastMod;
import static io.wcm.caconfig.extensions.persistence.impl.PersistenceUtils.isItemModifiedOrNewlyAdded;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.caconfig.management.ConfigurationManagementSettings;
import org.apache.sling.caconfig.spi.ConfigurationCollectionPersistData;
import org.apache.sling.caconfig.spi.ConfigurationPersistData;
Expand Down Expand Up @@ -296,18 +295,13 @@ public static boolean isItemModifiedOrNewlyAdded(ResourceResolver resolver, Stri
if (resource == null) {
return true; // Resource does not exist, so it is considered modified
}
ValueMap valueMap = resource.getValueMap();
Map<String, Object> currentProperties = new HashMap<>(valueMap);

Map<String, Object> currentProperties = new HashMap<>(resource.getValueMap());
Map<String, Object> newProperties = new HashMap<>(item.getProperties());

// Filter out ignored properties
Set<String> currentPropertyNames = new HashSet<>(currentProperties.keySet());
PropertiesFilterUtil.removeIgnoredProperties(currentPropertyNames, settings);
currentProperties.keySet().retainAll(currentPropertyNames);

Set<String> newPropertyNames = new HashSet<>(newProperties.keySet());
PropertiesFilterUtil.removeIgnoredProperties(newPropertyNames, settings);
newProperties.keySet().retainAll(newPropertyNames);
PropertiesFilterUtil.removeIgnoredProperties(currentProperties, settings);
PropertiesFilterUtil.removeIgnoredProperties(newProperties, settings);

return !currentProperties.equals(newProperties);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
package io.wcm.caconfig.extensions.persistence.impl;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.sling.caconfig.management.ConfigurationManagementSettings;
Expand All @@ -28,13 +30,19 @@
*/
final class PropertiesFilterUtil {

private PropertiesFilterUtil() {
// static methods only
}
private PropertiesFilterUtil() {
// static methods only
}

public static void removeIgnoredProperties(Set<String> propertyNames, ConfigurationManagementSettings settings) {
Set<String> ignoredProperties = settings.getIgnoredPropertyNames(propertyNames);
propertyNames.removeAll(ignoredProperties);
}
public static void removeIgnoredProperties(Set<String> propertyNames, ConfigurationManagementSettings settings) {
Set<String> ignoredProperties = settings.getIgnoredPropertyNames(propertyNames);
propertyNames.removeAll(ignoredProperties);
}

public static void removeIgnoredProperties(Map<String, Object> properties, ConfigurationManagementSettings settings) {
Set<String> propertyNames = new HashSet<>(properties.keySet());
removeIgnoredProperties(propertyNames, settings);
properties.keySet().retainAll(propertyNames);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* #%L
* wcm.io
* %%
* Copyright (C) 2024 wcm.io
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package io.wcm.caconfig.extensions.persistence.impl;

import static io.wcm.caconfig.extensions.persistence.impl.PropertiesFilterUtil.removeIgnoredProperties;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.sling.caconfig.management.ConfigurationManagementSettings;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class PropertiesFilterUtilTest {

@Mock
private ConfigurationManagementSettings settings;

@BeforeEach
void setUp() {
when(settings.getIgnoredPropertyNames(any())).thenReturn(Set.of("prop1", "prop3"));
}

@Test
void testRemoveIgnoredPropertiesSet() {
Set<String> names = new HashSet<>(Set.of("prop1", "prop2", "prop3", "prop4"));
removeIgnoredProperties(names, settings);
assertEquals(Set.of("prop2", "prop4"), names);
}

@Test
void testRemoveIgnoredPropertiesMap() {
Map<String, Object> properties = new HashMap<>(Map.of("prop1", 1, "prop2", 2, "prop3", 3, "prop4", 4));
removeIgnoredProperties(properties, settings);
assertEquals(Map.of("prop2", 2, "prop4", 4), properties);
}

}

0 comments on commit 606decb

Please sign in to comment.