diff --git a/build.gradle b/build.gradle index 0b6c2c7..50dfa77 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ plugins { } group = 'voruti' -version = '1.4' +version = '1.5' description = 'json2config' repositories { diff --git a/src/main/java/voruti/json2config/Starter.java b/src/main/java/voruti/json2config/Starter.java index d2e745f..19e698a 100644 --- a/src/main/java/voruti/json2config/Starter.java +++ b/src/main/java/voruti/json2config/Starter.java @@ -10,7 +10,7 @@ /** * @author voruti */ -@Command(name = "java -jar json2config.jar", mixinStandardHelpOptions = true, version = "1.4", +@Command(name = "java -jar json2config.jar", mixinStandardHelpOptions = true, version = "1.5", description = "Converts openHAB Items from JsonDB Storage files.") public class Starter implements Runnable { diff --git a/src/main/java/voruti/json2config/model/json/JsonChannelLink.java b/src/main/java/voruti/json2config/model/json/JsonChannelLink.java index 96755ce..79c40ce 100644 --- a/src/main/java/voruti/json2config/model/json/JsonChannelLink.java +++ b/src/main/java/voruti/json2config/model/json/JsonChannelLink.java @@ -14,7 +14,15 @@ public class JsonChannelLink implements IConvertible { @Override public String toConfigLine(String lineBefore) { - return String.format("%s {channel=\"%s\"}", lineBefore, String.join(":", value.channelUID.segments)).strip(); + String format = "channel=\"%s\"}"; + if (lineBefore.endsWith("}")) { + lineBefore = lineBefore.substring(0, lineBefore.length() - 1); + format = "%s, " + format; + } else { + format = "%s {" + format; + } + + return String.format(format, lineBefore, String.join(":", value.channelUID.segments)).strip(); } @@ -35,7 +43,9 @@ private static class Configuration { private static class Properties { private String offset; + private String sourceFormat; private String profile; + private String function; } } } diff --git a/src/test/java/voruti/json2config/IntegrationTest.java b/src/test/java/voruti/json2config/IntegrationTest.java index 02385a9..30f2ef4 100644 --- a/src/test/java/voruti/json2config/IntegrationTest.java +++ b/src/test/java/voruti/json2config/IntegrationTest.java @@ -1,6 +1,7 @@ package voruti.json2config; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import java.io.IOException; import java.nio.file.Files; @@ -14,16 +15,22 @@ class IntegrationTest { private static final String RESOURCES = "build/resources/test/"; private static final String TEMPORARY = "build/tmp/test/"; - @Test - void openhab2_example1() throws IOException { + + private String openFile(String file) throws IOException { + Path path = Paths.get(file); + return String.join("\n", Files.readAllLines(path)) + .replaceAll("[\\h\\t ]{2,}", " "); + } + + + @ParameterizedTest + @ValueSource(strings = {"openhab2_example1", "openhab2_multipleChannelsOneItem"}) + void itemsAndChannels(String testName) throws IOException { // arrange: - final String testName = "openhab2_example1"; final String generatedItemsFile = TEMPORARY + testName + "_result.items"; // load template .items file: - Path pathTemplate = Paths.get(RESOURCES + testName + ".items"); - String template = String.join("\n", Files.readAllLines(pathTemplate)) - .replaceAll("[\\h\\t ]{2,}", " "); + String template = openFile(RESOURCES + testName + ".items"); // act: Starter.main(new String[]{"-i", RESOURCES + testName + ".Item.json", @@ -34,23 +41,19 @@ void openhab2_example1() throws IOException { // assert: // load generated .items file: - Path pathGenerated = Paths.get(generatedItemsFile); - String generated = String.join("\n", Files.readAllLines(pathGenerated)) - .replaceAll("[\\h\\t ]{2,}", " "); + String generated = openFile(generatedItemsFile); assertEquals(template, generated); } - @Test - void openhab2_exampleDimensions() throws IOException { + @ParameterizedTest + @ValueSource(strings = {"openhab2_exampleDimensions"}) + void onlyItems(String testName) throws IOException { // arrange: - final String testName = "openhab2_exampleDimensions"; final String generatedItemsFile = TEMPORARY + testName + "_result.items"; // load template .items file: - Path pathTemplate = Paths.get(RESOURCES + testName + ".items"); - String template = String.join("\n", Files.readAllLines(pathTemplate)) - .replaceAll("[\\h\\t ]{2,}", " "); + String template = openFile(RESOURCES + testName + ".items"); // act: Starter.main(new String[]{"-i", RESOURCES + testName + ".Item.json", @@ -58,9 +61,7 @@ void openhab2_exampleDimensions() throws IOException { // assert: // load generated .items file: - Path pathGenerated = Paths.get(generatedItemsFile); - String generated = String.join("\n", Files.readAllLines(pathGenerated)) - .replaceAll("[\\h\\t ]{2,}", " "); + String generated = openFile(generatedItemsFile); assertEquals(template, generated); } diff --git a/src/test/resources/openhab2_multipleChannelsOneItem.Item.json b/src/test/resources/openhab2_multipleChannelsOneItem.Item.json new file mode 100644 index 0000000..c73661e --- /dev/null +++ b/src/test/resources/openhab2_multipleChannelsOneItem.Item.json @@ -0,0 +1,10 @@ +{ + "MultiItem": { + "class": "org.eclipse.smarthome.core.items.ManagedItemProvider$PersistedItem", + "value": { + "groupNames": [], + "itemType": "Switch", + "tags": [] + } + } +} \ No newline at end of file diff --git a/src/test/resources/openhab2_multipleChannelsOneItem.ItemChannelLink.json b/src/test/resources/openhab2_multipleChannelsOneItem.ItemChannelLink.json new file mode 100644 index 0000000..30fd616 --- /dev/null +++ b/src/test/resources/openhab2_multipleChannelsOneItem.ItemChannelLink.json @@ -0,0 +1,80 @@ +{ + "MultiItem -\u003e mqtt:topic:52b61fd6:light": { + "class": "org.eclipse.smarthome.core.thing.link.ItemChannelLink", + "value": { + "channelUID": { + "segments": [ + "mqtt", + "topic", + "52b61fd6", + "light" + ] + }, + "configuration": { + "properties": { + "profile": "system:default" + } + }, + "itemName": "MultiItem" + } + }, + "MultiItem -\u003e mqtt:topic:d589b50d:power": { + "class": "org.eclipse.smarthome.core.thing.link.ItemChannelLink", + "value": { + "channelUID": { + "segments": [ + "mqtt", + "topic", + "d589b50d", + "power" + ] + }, + "configuration": { + "properties": { + "profile": "system:follow" + } + }, + "itemName": "MultiItem" + } + }, + "MultiItem -\u003e mqtt:topic:3621578b:switch": { + "class": "org.eclipse.smarthome.core.thing.link.ItemChannelLink", + "value": { + "channelUID": { + "segments": [ + "mqtt", + "topic", + "3621578b", + "switch" + ] + }, + "configuration": { + "properties": { + "sourceFormat": "%.1f °C", + "profile": "transform:REGEX", + "function": ".*\u003d(\\\\d*.\\\\d*).*" + } + }, + "itemName": "MultiItem" + } + }, + "MultiItem -\u003e mqtt:topic:1c4c5e84:light": { + "class": "org.eclipse.smarthome.core.thing.link.ItemChannelLink", + "value": { + "channelUID": { + "segments": [ + "mqtt", + "topic", + "1c4c5e84", + "light" + ] + }, + "configuration": { + "properties": { + "profile": "system:default" + } + }, + "itemName": "MultiItem" + } + } +} \ No newline at end of file diff --git a/src/test/resources/openhab2_multipleChannelsOneItem.items b/src/test/resources/openhab2_multipleChannelsOneItem.items new file mode 100644 index 0000000..b2418a9 --- /dev/null +++ b/src/test/resources/openhab2_multipleChannelsOneItem.items @@ -0,0 +1 @@ +Switch MultiItem {channel="mqtt:topic:52b61fd6:light", channel="mqtt:topic:d589b50d:power", channel="mqtt:topic:3621578b:switch", channel="mqtt:topic:1c4c5e84:light"}