This repository was archived by the owner on Nov 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#27] unknown providers and relation targets are added on the fly
- Loading branch information
Showing
6 changed files
with
185 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,4 @@ docs/build | |
_static | ||
_templates | ||
/.env | ||
/.iml | ||
*.iml |
86 changes: 86 additions & 0 deletions
86
src/main/java/de/bonndan/nivio/input/InstantItemResolver.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,86 @@ | ||
package de.bonndan.nivio.input; | ||
|
||
import de.bonndan.nivio.input.dto.ItemDescription; | ||
import de.bonndan.nivio.input.dto.LandscapeDescription; | ||
import de.bonndan.nivio.model.*; | ||
import org.springframework.util.StringUtils; | ||
|
||
import java.util.*; | ||
|
||
/** | ||
* If the landscape is configured as "greedy", new items are created on the fly by a reference name. | ||
* | ||
* @todo add hosts as soon as relation resolver supports hosts | ||
*/ | ||
public class InstantItemResolver { | ||
|
||
private final ProcessLog log; | ||
|
||
public InstantItemResolver(ProcessLog log) { | ||
this.log = log; | ||
} | ||
|
||
public void processTargets(LandscapeDescription landscape) { | ||
List<ItemDescription> all = landscape.getItemDescriptions(); | ||
|
||
if (!landscape.getConfig().isGreedy()) | ||
return; | ||
|
||
HashSet<ItemDescription> newItems = new HashSet<>(); | ||
all.forEach(itemDescription -> newItems.addAll(resolveTargets(itemDescription, all))); | ||
|
||
landscape.addItems(newItems); | ||
} | ||
|
||
private List<ItemDescription> resolveTargets(ItemDescription description, List<ItemDescription> allItems) { | ||
|
||
List<ItemDescription> newItems = new ArrayList<>(); | ||
//providers | ||
description.getProvidedBy().forEach(term -> { | ||
Optional<? extends LandscapeItem> provider = Items.query(term.toLowerCase(), allItems).stream().findFirst(); | ||
|
||
if (provider.isEmpty()) { | ||
newItems.add(createItem(term)); | ||
} | ||
}); | ||
|
||
//other relations | ||
description.getRelations().forEach(rel -> { | ||
|
||
//skip sources, since only the targets are in the list | ||
|
||
//find targets | ||
if (!StringUtils.isEmpty(rel.getTarget()) && !hasTarget(rel.getTarget().toLowerCase(), allItems)) { | ||
newItems.add(createItem(rel.getTarget())); | ||
} | ||
}); | ||
|
||
return newItems; | ||
} | ||
|
||
/** | ||
* Creates a new item description with source/target as identifier | ||
*/ | ||
private ItemDescription createItem(String term) { | ||
ItemDescription itemDescription = new ItemDescription(); | ||
FullyQualifiedIdentifier fqi = FullyQualifiedIdentifier.from(term); | ||
itemDescription.setGroup(fqi.getGroup()); | ||
itemDescription.setIdentifier(fqi.getIdentifier()); | ||
return itemDescription; | ||
} | ||
|
||
private boolean hasTarget(String term, List<ItemDescription> allItems) { | ||
|
||
if (StringUtils.isEmpty(term)) { | ||
return true; | ||
} | ||
|
||
Collection<? extends LandscapeItem> result = Items.query(term, allItems); | ||
if (result.size() > 1) { | ||
log.warn("Found ambiguous sources matching " + term); | ||
return true; | ||
} | ||
|
||
return result.size() != 0; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,8 +32,8 @@ logging: | |
level: | ||
root: INFO | ||
|
||
#server: | ||
# port: 8081 | ||
server: | ||
port: 8081 | ||
--- | ||
spring: | ||
profiles: test | ||
|
84 changes: 84 additions & 0 deletions
84
src/test/java/de/bonndan/nivio/input/InstantItemResolverTest.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,84 @@ | ||
package de.bonndan.nivio.input; | ||
|
||
import de.bonndan.nivio.input.dto.ItemDescription; | ||
import de.bonndan.nivio.input.dto.LandscapeDescription; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Arrays; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.mock; | ||
|
||
class InstantItemResolverTest { | ||
|
||
private InstantItemResolver instantItemResolver; | ||
private LandscapeDescription landscapeDescription; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
|
||
instantItemResolver = new InstantItemResolver(mock(ProcessLog.class)); | ||
} | ||
|
||
@Test | ||
public void addsNewItems() { | ||
|
||
landscapeDescription = new LandscapeDescription(); | ||
ItemDescription a = new ItemDescription(); | ||
a.setIdentifier("a"); | ||
a.setGroup("a"); | ||
a.getProvidedBy().add("providerA"); | ||
landscapeDescription.getItemDescriptions().add(a); | ||
|
||
ItemDescription b = new ItemDescription(); | ||
b.setIdentifier("b"); | ||
b.setGroup("b"); | ||
a.setRelations(Arrays.asList("adistanttarget")); | ||
|
||
landscapeDescription.getItemDescriptions().add(b); | ||
landscapeDescription.getConfig().setGreedy(true); | ||
|
||
instantItemResolver.processTargets(landscapeDescription); | ||
assertEquals(4, landscapeDescription.getItemDescriptions().size()); | ||
} | ||
|
||
@Test | ||
public void addsNewItemsOnlyOnce() { | ||
|
||
landscapeDescription = new LandscapeDescription(); | ||
ItemDescription a = new ItemDescription(); | ||
a.setIdentifier("a"); | ||
a.setGroup("a"); | ||
a.getProvidedBy().add("providerA"); | ||
landscapeDescription.getItemDescriptions().add(a); | ||
|
||
ItemDescription b = new ItemDescription(); | ||
b.setIdentifier("b"); | ||
b.setGroup("b"); | ||
a.setRelations(Arrays.asList("adistanttarget")); | ||
landscapeDescription.getItemDescriptions().add(b); | ||
|
||
ItemDescription c = new ItemDescription(); | ||
c.setIdentifier("c"); | ||
c.setGroup("c"); | ||
c.setRelations(Arrays.asList("adistanttarget")); | ||
landscapeDescription.getItemDescriptions().add(c); | ||
|
||
landscapeDescription.getConfig().setGreedy(true); | ||
|
||
|
||
instantItemResolver.processTargets(landscapeDescription); | ||
|
||
//3 given plus 2 resolved (like above) | ||
assertEquals(5, landscapeDescription.getItemDescriptions().size()); | ||
|
||
assertTrue(landscapeDescription.getItemDescriptions().stream() | ||
.anyMatch(itemDescription -> itemDescription.getIdentifier().equals("providera")) | ||
); | ||
|
||
assertTrue(landscapeDescription.getItemDescriptions().stream() | ||
.anyMatch(itemDescription -> itemDescription.getIdentifier().equals("adistanttarget")) | ||
); | ||
} | ||
} |