mirror of
https://github.com/danieldemus/openhab-core.git
synced 2026-07-29 12:34:22 +02:00
Fix OOBE in item metadata descriptions providers (#3488)
Signed-off-by: Miguel Álvarez <miguelwork92@gmail.com>
This commit is contained in:
+14
-20
@@ -63,18 +63,8 @@ public class MetadataCommandDescriptionProvider implements CommandDescriptionPro
|
||||
if (metadata.getConfiguration().containsKey("options")) {
|
||||
Stream.of(metadata.getConfiguration().get("options").toString().split(",")).forEach(o -> {
|
||||
if (o.contains("=")) {
|
||||
String command;
|
||||
String label;
|
||||
if (o.startsWith("\"")) {
|
||||
String[] parts = o.trim().split("\"=\"");
|
||||
command = removeSurroundingQuotes(parts[0]);
|
||||
label = removeSurroundingQuotes(parts[1]);
|
||||
} else {
|
||||
String[] parts = o.trim().split("=");
|
||||
command = parts[0];
|
||||
label = parts[1];
|
||||
}
|
||||
commandDescription.addCommandOption(new CommandOption(command.trim(), label.trim()));
|
||||
var pair = parseValueLabelPair(o.trim());
|
||||
commandDescription.addCommandOption(new CommandOption(pair[0], pair[1]));
|
||||
} else {
|
||||
commandDescription.addCommandOption(new CommandOption(o.trim(), null));
|
||||
}
|
||||
@@ -91,14 +81,18 @@ public class MetadataCommandDescriptionProvider implements CommandDescriptionPro
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String removeSurroundingQuotes(String input) {
|
||||
String output = input;
|
||||
if (input.startsWith("\"")) {
|
||||
output = output.substring(1);
|
||||
public static String[] parseValueLabelPair(String text) {
|
||||
String value;
|
||||
String label;
|
||||
if (text.startsWith("\"") && text.contains("\"=\"") && text.endsWith("\"")) {
|
||||
String[] parts = text.split("\"=\"");
|
||||
value = parts[0].substring(1);
|
||||
label = parts[1].substring(0, parts[1].length() - 1);
|
||||
} else {
|
||||
String[] parts = text.split("=");
|
||||
value = parts[0];
|
||||
label = parts[1];
|
||||
}
|
||||
if (input.endsWith("\"")) {
|
||||
output = output.substring(0, output.length() - 1);
|
||||
}
|
||||
return output;
|
||||
return new String[] { value.trim(), label.trim() };
|
||||
}
|
||||
}
|
||||
|
||||
+3
-13
@@ -12,7 +12,7 @@
|
||||
*/
|
||||
package org.openhab.core.internal.items;
|
||||
|
||||
import static org.openhab.core.internal.items.MetadataCommandDescriptionProvider.removeSurroundingQuotes;
|
||||
import static org.openhab.core.internal.items.MetadataCommandDescriptionProvider.parseValueLabelPair;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
@@ -106,18 +106,8 @@ public class MetadataStateDescriptionFragmentProvider implements StateDescriptio
|
||||
List<StateOption> stateOptions = Stream
|
||||
.of(metadata.getConfiguration().get("options").toString().split(",")).map(o -> {
|
||||
if (o.contains("=")) {
|
||||
String value;
|
||||
String label;
|
||||
if (o.startsWith("\"")) {
|
||||
String[] parts = o.trim().split("\"=\"");
|
||||
value = removeSurroundingQuotes(parts[0]);
|
||||
label = removeSurroundingQuotes(parts[1]);
|
||||
} else {
|
||||
String[] parts = o.trim().split("=");
|
||||
value = parts[0];
|
||||
label = parts[1];
|
||||
}
|
||||
return new StateOption(value.trim(), label.trim());
|
||||
var pair = parseValueLabelPair(o.trim());
|
||||
return new StateOption(pair[0], pair[1]);
|
||||
} else {
|
||||
return new StateOption(o.trim(), null);
|
||||
}
|
||||
|
||||
+5
-2
@@ -97,14 +97,14 @@ public class MetadataCommandDescriptionProviderTest {
|
||||
public void testOptions() throws Exception {
|
||||
MetadataKey metadataKey = new MetadataKey("commandDescription", ITEM_NAME);
|
||||
Map<String, Object> metadataConfig = new HashMap<>();
|
||||
metadataConfig.put("options", "OPTION1,OPTION2 , 3 =Option 3 ");
|
||||
metadataConfig.put("options", "OPTION1,OPTION2 , 3 =Option 3 , \" 4=4 \"=\"Option 4 \"");
|
||||
Metadata metadata = new Metadata(metadataKey, "N/A", metadataConfig);
|
||||
|
||||
metadataRegistryMock.added(managedProviderMock, metadata);
|
||||
CommandDescription commandDescription = commandDescriptionProvider.getCommandDescription(ITEM_NAME, null);
|
||||
assertNotNull(commandDescription);
|
||||
assertNotNull(commandDescription.getCommandOptions());
|
||||
assertEquals(3, commandDescription.getCommandOptions().size());
|
||||
assertEquals(4, commandDescription.getCommandOptions().size());
|
||||
|
||||
Iterator<CommandOption> it = commandDescription.getCommandOptions().iterator();
|
||||
CommandOption commandOption = it.next();
|
||||
@@ -116,5 +116,8 @@ public class MetadataCommandDescriptionProviderTest {
|
||||
commandOption = it.next();
|
||||
assertEquals("3", commandOption.getCommand());
|
||||
assertEquals("Option 3", commandOption.getLabel());
|
||||
commandOption = it.next();
|
||||
assertEquals("4=4", commandOption.getCommand());
|
||||
assertEquals("Option 4", commandOption.getLabel());
|
||||
}
|
||||
}
|
||||
|
||||
+4
-1
@@ -96,7 +96,7 @@ public class MetadataStateDescriptionFragmentProviderTest {
|
||||
metadataConfig.put("max", "34");
|
||||
metadataConfig.put("step", 3);
|
||||
metadataConfig.put("readOnly", "true");
|
||||
metadataConfig.put("options", "OPTION1,OPTION2 , 3 =Option 3 ");
|
||||
metadataConfig.put("options", "OPTION1,OPTION2 , 3 =Option 3 ,\"4=4\"=\" Option=4 \" ");
|
||||
Metadata metadata = new Metadata(metadataKey, "N/A", metadataConfig);
|
||||
metadataRegistryMock.added(managedProviderMock, metadata);
|
||||
|
||||
@@ -119,5 +119,8 @@ public class MetadataStateDescriptionFragmentProviderTest {
|
||||
stateOption = it.next();
|
||||
assertEquals("3", stateOption.getValue());
|
||||
assertEquals("Option 3", stateOption.getLabel());
|
||||
stateOption = it.next();
|
||||
assertEquals("4=4", stateOption.getValue());
|
||||
assertEquals("Option=4", stateOption.getLabel());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user