This repository has been 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.
- Loading branch information
Daniel Pozzi
committed
Nov 11, 2019
1 parent
09e44c3
commit e9854be
Showing
4 changed files
with
97 additions
and
8 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
src/main/java/de/bonndan/nivio/input/dto/LabelProcessor.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,46 @@ | ||
package de.bonndan.nivio.input.dto; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.Arrays; | ||
import java.util.Optional; | ||
|
||
public class LabelProcessor { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(LabelProcessor.class); | ||
public static final String NIVIO_LABEL_PREFIX = "nivio."; | ||
|
||
/** | ||
* Copies label key and value to item labels, or, if prefixed "nivio.", sets the field to the value. | ||
* | ||
* @param item landscape item | ||
* @param key label name | ||
* @param value label value | ||
*/ | ||
public static void applyLabel(ItemDescription item, String key, Object value) { | ||
|
||
if (!key.toLowerCase().startsWith(NIVIO_LABEL_PREFIX)) { | ||
item.getLabels().put(key, (String) value); | ||
return; | ||
} | ||
|
||
String field = key.substring(NIVIO_LABEL_PREFIX.length()); | ||
setValue(item, field, (String) value); | ||
} | ||
|
||
private static void setValue(ItemDescription item, String name, String value) { | ||
Field[] declaredFields = item.getClass().getDeclaredFields(); | ||
Optional<Field> field = Arrays.stream(declaredFields).filter(field1 -> field1.getName().equals(name)).findFirst(); | ||
field.ifPresent(field1 -> { | ||
try { | ||
field1.setAccessible(true); | ||
field1.set(item, value); | ||
field1.setAccessible(false); | ||
} catch (IllegalAccessException e) { | ||
LOGGER.warn("Failed to set field {} via label: " + e.getMessage(), name); | ||
} | ||
}); | ||
} | ||
} |
4 changes: 1 addition & 3 deletions
4
src/main/java/de/bonndan/nivio/input/kubernetes/ItemDescriptionFactoryKubernetes.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
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
46 changes: 46 additions & 0 deletions
46
src/test/java/de/bonndan/nivio/input/dto/LabelProcessorTest.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,46 @@ | ||
package de.bonndan.nivio.input.dto; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class LabelProcessorTest { | ||
|
||
ItemDescription item; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
item = new ItemDescription(); | ||
} | ||
|
||
@Test | ||
public void regularLabels() { | ||
LabelProcessor.applyLabel(item, "foo", "bar"); | ||
LabelProcessor.applyLabel(item, "niviofoo", "baz"); | ||
|
||
assertEquals(2, item.getLabels().size()); | ||
assertEquals("bar", item.getLabels().get("foo")); | ||
assertEquals("baz", item.getLabels().get("niviofoo")); | ||
} | ||
|
||
@Test | ||
public void noField() { | ||
LabelProcessor.applyLabel(item, "foo", "bar"); | ||
LabelProcessor.applyLabel(item, "nivio.foo", "baz"); | ||
|
||
assertEquals(1, item.getLabels().size()); | ||
assertEquals("bar", item.getLabels().get("foo")); | ||
assertNull(item.getLabels().get("nivio.foo")); | ||
} | ||
|
||
@Test | ||
public void fieldLabel() { | ||
LabelProcessor.applyLabel(item, "foo", "bar"); | ||
LabelProcessor.applyLabel(item, "nivio.description", "baz"); | ||
|
||
assertEquals(1, item.getLabels().size()); | ||
assertEquals("bar", item.getLabels().get("foo")); | ||
assertEquals("baz", item.getDescription()); | ||
} | ||
} |