Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "recursive membership detected" for plain items #2918

Merged
merged 2 commits into from
Apr 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions bundles/org.openhab.core.io.rest.core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,19 @@ public static EnrichedItemDTO map(Item item, boolean drillDown, @Nullable Predic
return map(item, itemDTO, drillDown, itemFilter, uriBuilder, locale, new ArrayList<>());
}

private static EnrichedItemDTO map(Item item, boolean drillDown, @Nullable Predicate<Item> itemFilter,
private static EnrichedItemDTO mapRecursive(Item item, @Nullable Predicate<Item> itemFilter,
@Nullable UriBuilder uriBuilder, @Nullable Locale locale, List<Item> parents) {
ItemDTO itemDTO = ItemDTOMapper.map(item);
return map(item, itemDTO, drillDown, itemFilter, uriBuilder, locale, parents);
return map(item, itemDTO, true, itemFilter, uriBuilder, locale, parents);
}

private static EnrichedItemDTO map(Item item, ItemDTO itemDTO, boolean drillDown,
@Nullable Predicate<Item> itemFilter, @Nullable UriBuilder uriBuilder, @Nullable Locale locale,
List<Item> parents) {
parents.add(item);
if (item instanceof GroupItem) {
// only add as parent item if it is a group, otherwise duplicate memberships trigger false warnings
parents.add(item);
}
String state = item.getState().toFullString();
String transformedState = considerTransformation(state, item, locale);
if (transformedState != null && transformedState.equals(state)) {
Expand All @@ -88,7 +91,7 @@ private static EnrichedItemDTO map(Item item, ItemDTO itemDTO, boolean drillDown
link = null;
}

EnrichedItemDTO enrichedItemDTO = null;
EnrichedItemDTO enrichedItemDTO;

if (item instanceof GroupItem) {
GroupItem groupItem = (GroupItem) item;
Expand All @@ -101,10 +104,10 @@ private static EnrichedItemDTO map(Item item, ItemDTO itemDTO, boolean drillDown
"Recursive group membership found: {} is both, a direct or indirect parent and a child of {}.",
member.getName(), groupItem.getName());
} else if (itemFilter == null || itemFilter.test(member)) {
members.add(map(member, drillDown, itemFilter, uriBuilder, locale, parents));
members.add(mapRecursive(member, itemFilter, uriBuilder, locale, parents));
}
}
memberDTOs = members.toArray(new EnrichedItemDTO[members.size()]);
memberDTOs = members.toArray(new EnrichedItemDTO[0]);
} else {
memberDTOs = new EnrichedItemDTO[0];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,44 @@
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openhab.core.items.GenericItem;
import org.openhab.core.items.GroupItem;
import org.openhab.core.library.CoreItemFactory;
import org.openhab.core.library.items.NumberItem;
import org.openhab.core.test.java.JavaTest;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.read.ListAppender;

/**
* @author Kai Kreuzer - Initial contribution
*/
@NonNullByDefault
public class EnrichedItemDTOMapperTest extends JavaTest {

private @NonNullByDefault({}) ListAppender<ILoggingEvent> listAppender;

@BeforeEach
public void setup() {
// add logging interceptor
Logger logger = (Logger) LoggerFactory.getLogger(EnrichedItemDTOMapper.class);
logger.setLevel(Level.ERROR);
listAppender = new ListAppender<>();
listAppender.start();
logger.addAppender(listAppender);
}

@AfterEach
public void tearDown() {
listAppender.stop();
}

@Test
public void testFiltering() {
CoreItemFactory itemFactory = new CoreItemFactory();
Expand Down Expand Up @@ -84,6 +110,12 @@ public void testDirectRecursiveMembershipDoesNotThrowStackOverflowException() {
groupItem2.addMember(groupItem1);

assertDoesNotThrow(() -> EnrichedItemDTOMapper.map(groupItem1, true, null, null, null));

assertThat(listAppender.list.size(), is(1));
ILoggingEvent loggingEvent = listAppender.list.get(0);
assertThat(loggingEvent.getLevel(), is(Level.ERROR));
assertThat(loggingEvent.getFormattedMessage(), is(
"Recursive group membership found: group1 is both, a direct or indirect parent and a child of group2."));
}

@Test
Expand All @@ -97,5 +129,26 @@ public void testIndirectRecursiveMembershipDoesNotThrowStackOverflowException()
groupItem3.addMember(groupItem1);

assertDoesNotThrow(() -> EnrichedItemDTOMapper.map(groupItem1, true, null, null, null));

assertThat(listAppender.list.size(), is(1));
ILoggingEvent loggingEvent = listAppender.list.get(0);
assertThat(loggingEvent.getLevel(), is(Level.ERROR));
assertThat(loggingEvent.getFormattedMessage(), is(
"Recursive group membership found: group1 is both, a direct or indirect parent and a child of group3."));
}

@Test
public void testDuplicateMembershipOfPlainItemsDoesNotTriggerWarning() {
GroupItem groupItem1 = new GroupItem("group1");
GroupItem groupItem2 = new GroupItem("group2");
NumberItem numberItem = new NumberItem("number");

groupItem1.addMember(groupItem2);
groupItem1.addMember(numberItem);
groupItem2.addMember(numberItem);

EnrichedItemDTOMapper.map(groupItem1, true, null, null, null);

assertThat(listAppender.list.size(), is(0));
}
}