Skip to content

Commit

Permalink
add option to overwrite metadata (#206)
Browse files Browse the repository at this point in the history
add option to overwrite metadata
  • Loading branch information
querdenker2k authored Aug 18, 2024
1 parent 93f04d7 commit 6bb7288
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public Map<String, JRuleItemMetadata> getMetadata() {
}

@Override
public void addMetadata(String namespace, JRuleItemMetadata metadata) {
this.metadataRegistry.addMetadata(namespace, name, metadata);
public void addMetadata(String namespace, JRuleItemMetadata metadata, boolean override) {
this.metadataRegistry.addMetadata(namespace, name, metadata, override);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static Optional<JRuleItem> forNameOptional(String itemName) {
@NonNullByDefault
Map<String, JRuleItemMetadata> getMetadata();

void addMetadata(String namespace, JRuleItemMetadata metadata);
void addMetadata(String namespace, JRuleItemMetadata metadata, boolean override);

@NonNullByDefault
List<String> getTags();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,13 @@ public static Map<String, JRuleItemMetadata> getAllMetadata(String itemName, Met
metadata -> new JRuleItemMetadata(metadata.getValue(), metadata.getConfiguration())));
}

public void addMetadata(String namespace, String itemName, JRuleItemMetadata metadata) {
metadataRegistry.add(
new Metadata(new MetadataKey(namespace, itemName), metadata.getValue(), metadata.getConfiguration()));
public void addMetadata(String namespace, String itemName, JRuleItemMetadata metadata, boolean override) {
MetadataKey key = new MetadataKey(namespace, itemName);
Metadata foundMetadata = metadataRegistry.get(key);
if (foundMetadata != null && override) {
metadataRegistry.remove(key);
}

metadataRegistry.add(new Metadata(key, metadata.getValue(), metadata.getConfiguration()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public abstract class JRuleItemTestBase {
public static final String ITEM_NAME_2 = "Name2";
public static final String GROUP_NAME_2 = "Group2";
public static final String SUB_ITEM_NAME = "NameSub";
public static final String METADATA_WHAT_S_THE_TIME = "what's the time";
public static final String METADATA_VOICE_SYSTEM = "voiceSystem";

protected EventPublisher eventPublisher;

Expand Down Expand Up @@ -104,7 +106,8 @@ public void init() throws ItemNotFoundException {
JRuleItemRegistry.setMetadataRegistry(metadataRegistry);

Mockito.when(mock.getAllMetadata(Mockito.anyString()))
.thenReturn(Map.of("Speech", new JRuleItemMetadata("SetLightState", Map.of("location", "Livingroom"))));
.thenReturn(Map.of("Speech", new JRuleItemMetadata("SetLightState", Map.of("location", "Livingroom")),
METADATA_VOICE_SYSTEM, new JRuleItemMetadata(METADATA_WHAT_S_THE_TIME)));
}

protected abstract JRuleItem getJRuleItem();
Expand All @@ -129,7 +132,7 @@ public void testGetGroupItems() {
@Test
public void testGetMetadata() {
JRuleItem item = getJRuleItem();
Assertions.assertEquals(1, item.getMetadata().size());
Assertions.assertEquals(2, item.getMetadata().size());
Assertions.assertEquals("SetLightState", item.getMetadata().get("Speech").getValue());
Assertions.assertEquals(1, item.getMetadata().get("Speech").getConfiguration().size());
Assertions.assertEquals("Livingroom", item.getMetadata().get("Speech").getConfiguration().get("location"));
Expand All @@ -138,7 +141,15 @@ public void testGetMetadata() {
@Test
public void testAddMetadata() {
JRuleItem item = getJRuleItem();
item.addMetadata("voiceSystem", new JRuleItemMetadata("what's the time"));
item.addMetadata(METADATA_VOICE_SYSTEM, new JRuleItemMetadata(METADATA_WHAT_S_THE_TIME), false);
Assertions.assertEquals(METADATA_WHAT_S_THE_TIME, item.getMetadata().get(METADATA_VOICE_SYSTEM).getValue());

item.addMetadata(METADATA_VOICE_SYSTEM, new JRuleItemMetadata("something else"), false);
Assertions.assertEquals(METADATA_WHAT_S_THE_TIME, item.getMetadata().get(METADATA_VOICE_SYSTEM).getValue());

// not handled by the mock, therefor it's ok here
item.addMetadata(METADATA_VOICE_SYSTEM, new JRuleItemMetadata("something else"), true);
Assertions.assertEquals(METADATA_WHAT_S_THE_TIME, item.getMetadata().get(METADATA_VOICE_SYSTEM).getValue());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ public void getTagsAndMetadata() {
logInfo("Metadata Configuration: '{}'", item.getMetadata().get("Speech").getConfiguration());

// add something new and check it
item.addMetadata("VoiceSystem", new JRuleItemMetadata("myNewValue", Map.of("mykey", "myvalue")));
item.addMetadata("VoiceSystem", new JRuleItemMetadata("myNewValue", Map.of("mykey", "myvalue")), false);
logInfo("Metadata: '{}'", item.getMetadata());
logInfo("Metadata Value: '{}'", item.getMetadata().get("VoiceSystem").getValue());
logInfo("Metadata Configuration: '{}'", item.getMetadata().get("VoiceSystem").getConfiguration());
Expand Down

0 comments on commit 6bb7288

Please sign in to comment.