-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #217 from michielspiritus7/develop
added filterByPropertyIsMultiple
- Loading branch information
Showing
20 changed files
with
195 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
.../java/de/valtech/aecu/api/groovy/console/bindings/filters/FilterByPropertyIsMultiple.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 2018 - 2019 Valtech GmbH | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and | ||
* associated documentation files (the "Software"), to deal in the Software without restriction, | ||
* including without limitation the rights to use, copy, modify, merge, publish, distribute, | ||
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all copies or | ||
* substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT | ||
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
package de.valtech.aecu.api.groovy.console.bindings.filters; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
import org.apache.sling.api.resource.Resource; | ||
import org.apache.sling.api.resource.ValueMap; | ||
|
||
import de.valtech.aecu.api.groovy.console.bindings.GStringConverter; | ||
|
||
|
||
/** | ||
* Filters resources by a given property. The filter only matches if the attribute exists and has | ||
* the exact given value and the property is an array or a list. | ||
* | ||
* @author Michiel Spiritus | ||
*/ | ||
public class FilterByPropertyIsMultiple implements FilterBy { | ||
|
||
private String name; | ||
private Object value; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param name attribute name | ||
* @param value attribute value | ||
*/ | ||
public FilterByPropertyIsMultiple(@Nonnull String name, Object value) { | ||
this.name = name; | ||
this.value = GStringConverter.convert(value); | ||
} | ||
|
||
@Override | ||
public boolean filter(@Nonnull Resource resource, StringBuilder output) { | ||
ValueMap properties = resource.getValueMap(); | ||
Object attrValue = properties.get(name); | ||
|
||
// Check if the property is an array or a list | ||
if (attrValue instanceof Object[] || attrValue instanceof java.util.List) { | ||
return ((value == null) && (attrValue == null)) || ((value != null) && value.equals(attrValue)); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
.../de/valtech/aecu/core/groovy/console/bindings/filters/FilterByPropertyIsMultipleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright 2023 Valtech GmbH | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and | ||
* associated documentation files (the "Software"), to deal in the Software without restriction, | ||
* including without limitation the rights to use, copy, modify, merge, publish, distribute, | ||
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all copies or | ||
* substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT | ||
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
package de.valtech.aecu.core.groovy.console.bindings.filters; | ||
|
||
import org.apache.sling.api.resource.ValueMap; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.Mockito.when; | ||
|
||
import org.apache.sling.api.resource.Resource; | ||
import org.apache.sling.api.resource.ValueMap; | ||
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; | ||
import org.mockito.junit.jupiter.MockitoSettings; | ||
import org.mockito.quality.Strictness; | ||
|
||
import de.valtech.aecu.api.groovy.console.bindings.filters.FilterByPropertyIsMultiple; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* Tests FilterByPropertyIsMultiple | ||
* | ||
* @author Roland Gruber | ||
*/ | ||
@ExtendWith(MockitoExtension.class) | ||
@MockitoSettings(strictness = Strictness.LENIENT) | ||
public class FilterByPropertyIsMultipleTest { | ||
|
||
private static final String NAME = "name"; | ||
private static final List<String> MULTIPLE_VALUE = Arrays.asList("value1", "value2"); | ||
|
||
@Mock | ||
private Resource resource; | ||
|
||
@Mock | ||
ValueMap values; | ||
|
||
@Mock | ||
ValueMap valueMap; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
when(resource.getValueMap()).thenReturn(values); | ||
} | ||
|
||
@Test | ||
void filterAttributeArray() { | ||
Object[] attrArray = new Object[]{"value1", "value2", "value3"}; | ||
when(resource.getValueMap()).thenReturn(valueMap); | ||
when(valueMap.get("name")).thenReturn(attrArray); | ||
|
||
FilterByPropertyIsMultiple filter = new FilterByPropertyIsMultiple("name", attrArray); | ||
|
||
assertTrue(filter.filter(resource, new StringBuilder())); | ||
} | ||
|
||
|
||
|
||
@Test | ||
public void filterAttributeList() { | ||
FilterByPropertyIsMultiple filter = new FilterByPropertyIsMultiple(NAME, MULTIPLE_VALUE); | ||
when(values.get(NAME)).thenReturn(MULTIPLE_VALUE); | ||
|
||
assertTrue(filter.filter(resource, new StringBuilder())); | ||
} | ||
|
||
@Test | ||
public void filterAttributeNonMultiple() { | ||
FilterByPropertyIsMultiple filter = new FilterByPropertyIsMultiple(NAME, "value"); | ||
when(values.get(NAME)).thenReturn("value"); | ||
|
||
assertFalse(filter.filter(resource, new StringBuilder())); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters