Skip to content

Commit

Permalink
Merge pull request #207 from Smarteon/calendar
Browse files Browse the repository at this point in the history
Calendar fix event list deserialization
  • Loading branch information
jimirocks authored Sep 26, 2023
2 parents 2ef3a4a + 5c040e6 commit 4c97275
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 85 deletions.
5 changes: 5 additions & 0 deletions src/main/java/cz/smarteon/loxone/Codec.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ public static <T> T readMessage(final InputStream message, final Class<T> clazz)
return JsonMapperHolder.MAPPER.readValue(message, clazz);
}

public static <T> List<T> readList(final String encodedList, final Class<T> elementClass) throws IOException {
return JsonMapperHolder.MAPPER.readValue(
encodedList, JsonMapperHolder.MAPPER.getTypeFactory().constructCollectionType(List.class, elementClass));
}

public static <T> T readXml(final InputStream xml, final Class<T> clazz) throws IOException {
try {
final JAXBContext ctx = JAXBContext.newInstance(clazz);
Expand Down
17 changes: 15 additions & 2 deletions src/main/java/cz/smarteon/loxone/calendar/CalEntryListValue.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
package cz.smarteon.loxone.calendar;

import com.fasterxml.jackson.annotation.JsonCreator;
import cz.smarteon.loxone.Codec;
import cz.smarteon.loxone.message.LoxoneValue;
import lombok.Getter;

import java.util.ArrayList;
import java.io.IOException;
import java.util.List;

/**
* {@link LoxoneValue} where the JSON string is expected to encode a list of calendar entries.
*/
public class CalEntryListValue extends ArrayList<CalEntryBase> implements LoxoneValue { }
@Getter
public class CalEntryListValue implements LoxoneValue {

private final List<CalEntryBase> events;

@JsonCreator
protected CalEntryListValue(String events) throws IOException {
this.events = Codec.readList(events, CalEntryBase.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import lombok.Getter;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
Expand All @@ -23,6 +22,6 @@ public class UserGroupListValue implements LoxoneValue {

@JsonCreator
public UserGroupListValue(String userGroups) throws IOException {
this.userGroups = Arrays.asList(Codec.readMessage(userGroups, UserGroup[].class));
this.userGroups = Codec.readList(userGroups, UserGroup.class);
}
}
3 changes: 1 addition & 2 deletions src/main/java/cz/smarteon/loxone/user/UserListValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import lombok.Getter;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
Expand All @@ -23,7 +22,7 @@ public class UserListValue implements LoxoneValue {

@JsonCreator
protected UserListValue(String users) throws IOException {
this.users = Arrays.asList(Codec.readMessage(users, UserListElement[].class));
this.users = Codec.readList(users, UserListElement.class);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/test/kotlin/calendar/CalEntryListValueTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ class CalEntryListValueTest {
@Test
fun `should deserialize`() {
expectThat(readResource<CalEntryListValue>("calendar/testEntries.json")) {
get { this[3] }.isA<CalEntryEasterOffset>().and {
get { this.events[3] }.isA<CalEntryEasterOffset>().and {
get { uuid }.isLoxoneUuid("1b6f47dd-0243-9b2c-ffff504f94a03d3e")
get { name }.isEqualTo("Eastern offset test")
get { operatingMode }.isEqualTo(1)
get { calMode }.isEqualTo(1)
get { easterOffset }.isEqualTo(11)
get { createEntryCommand().command }.isEqualTo("jdev/sps/calendarcreateentry/Eastern offset test/1/1/11")
}
get { this[4] }.isA<CalEntrySingleDay>().and {
get { this.events[4] }.isA<CalEntrySingleDay>().and {
get { uuid }.isLoxoneUuid("1b6f47f0-03b0-9b3d-ffff504f94a03d3e")
get { name }.isEqualTo("Single day test")
get { operatingMode }.isEqualTo(1)
Expand Down
78 changes: 1 addition & 77 deletions src/test/resources/calendar/testEntries.json
Original file line number Diff line number Diff line change
@@ -1,77 +1 @@
[
{
"uuid":"16d36904-0135-387f-ffff6b31da1b15a2",
"name":"Perioda topení",
"operatingMode":10,
"calMode":4,
"startMonth":10,
"startDay":1,
"endMonth":4,
"endDay":30
},
{
"uuid":"16d36904-013c-3883-ffff6b31da1b15a2",
"name":"Perioda chlazení",
"operatingMode":11,
"calMode":4,
"startMonth":5,
"startDay":1,
"endMonth":9,
"endDay":30
},
{
"uuid":"1b6f47bd-0390-9b1e-ffff504f94a03d3e",
"name":"Every year test",
"operatingMode":1,
"calMode":0,
"startMonth":4,
"startDay":1
},
{
"uuid":"1b6f47dd-0243-9b2c-ffff504f94a03d3e",
"name":"Eastern offset test",
"operatingMode":1,
"calMode":1,
"easterOffset":11
},
{
"uuid":"1b6f47f0-03b0-9b3d-ffff504f94a03d3e",
"name":"Single day test",
"operatingMode":1,
"calMode":2,
"startYear":2024,
"startMonth":8,
"startDay":3
},
{
"uuid":"1b6f480b-0393-9b59-ffff504f94a03d3e",
"name":"Period test",
"operatingMode":1,
"calMode":3,
"startYear":2024,
"startMonth":8,
"startDay":3,
"endYear":2024,
"endMonth":8,
"endDay":4
},
{
"uuid":"1b6f49be-025c-9ba0-ffff504f94a03d3e",
"name":"Period every year test",
"operatingMode":1,
"calMode":4,
"startMonth":7,
"startDay":1,
"endMonth":7,
"endDay":6
},
{
"uuid":"1b6f49d6-030f-9bc2-ffff504f94a03d3e",
"name":"Day of week test",
"operatingMode":1,
"calMode":5,
"startMonth":7,
"weekDay":2,
"weekDayInMonth":1
}
]
"[\n {\n \"uuid\":\"16d36904-0135-387f-ffff6b31da1b15a2\",\n \"name\":\"Perioda topení\",\n \"operatingMode\":10,\n \"calMode\":4,\n \"startMonth\":10,\n \"startDay\":1,\n \"endMonth\":4,\n \"endDay\":30\n },\n {\n \"uuid\":\"16d36904-013c-3883-ffff6b31da1b15a2\",\n \"name\":\"Perioda chlazení\",\n \"operatingMode\":11,\n \"calMode\":4,\n \"startMonth\":5,\n \"startDay\":1,\n \"endMonth\":9,\n \"endDay\":30\n },\n {\n \"uuid\":\"1b6f47bd-0390-9b1e-ffff504f94a03d3e\",\n \"name\":\"Every year test\",\n \"operatingMode\":1,\n \"calMode\":0,\n \"startMonth\":4,\n \"startDay\":1\n },\n {\n \"uuid\":\"1b6f47dd-0243-9b2c-ffff504f94a03d3e\",\n \"name\":\"Eastern offset test\",\n \"operatingMode\":1,\n \"calMode\":1,\n \"easterOffset\":11\n },\n {\n \"uuid\":\"1b6f47f0-03b0-9b3d-ffff504f94a03d3e\",\n \"name\":\"Single day test\",\n \"operatingMode\":1,\n \"calMode\":2,\n \"startYear\":2024,\n \"startMonth\":8,\n \"startDay\":3\n },\n {\n \"uuid\":\"1b6f480b-0393-9b59-ffff504f94a03d3e\",\n \"name\":\"Period test\",\n \"operatingMode\":1,\n \"calMode\":3,\n \"startYear\":2024,\n \"startMonth\":8,\n \"startDay\":3,\n \"endYear\":2024,\n \"endMonth\":8,\n \"endDay\":4\n },\n {\n \"uuid\":\"1b6f49be-025c-9ba0-ffff504f94a03d3e\",\n \"name\":\"Period every year test\",\n \"operatingMode\":1,\n \"calMode\":4,\n \"startMonth\":7,\n \"startDay\":1,\n \"endMonth\":7,\n \"endDay\":6\n },\n {\n \"uuid\":\"1b6f49d6-030f-9bc2-ffff504f94a03d3e\",\n \"name\":\"Day of week test\",\n \"operatingMode\":1,\n \"calMode\":5,\n \"startMonth\":7,\n \"weekDay\":2,\n \"weekDayInMonth\":1\n }\n]\n"

0 comments on commit 4c97275

Please sign in to comment.