Skip to content

Commit

Permalink
[astro] Method renamed (relative to TimeZoneProvider) (openhab#7877)
Browse files Browse the repository at this point in the history
* [astro] Method renamed (relative to TimeZoneProvider)
* Fix the integration tests broken by a previous PR

Signed-off-by: Laurent Garnier <[email protected]>
  • Loading branch information
lolodomo authored and markus7017 committed Sep 18, 2020
1 parent 871c04e commit 9c476a9
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ public class AstroHandlerFactory extends BaseThingHandlerFactory {
private final CronScheduler scheduler;

@Activate
public AstroHandlerFactory(final @Reference CronScheduler scheduler, final @Reference TimeZoneProvider timeZone) {
public AstroHandlerFactory(final @Reference CronScheduler scheduler,
final @Reference TimeZoneProvider timeZoneProvider) {
this.scheduler = scheduler;
PropertyUtils.setTimeZone(timeZone);
PropertyUtils.setTimeZoneProvider(timeZoneProvider);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
*/
package org.openhab.binding.astro.internal.config;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
Expand Down Expand Up @@ -68,15 +64,8 @@ public void setThingUid(String thingUid) {

@Override
public String toString() {
TimeZone tz = TimeZone.getDefault();
StringBuilder tzInfo = new StringBuilder();
tzInfo.append(tz.getID());
tzInfo.append(" (").append(tz.getDisplayName(false, TimeZone.SHORT)).append(" ")
.append(new SimpleDateFormat("Z").format(Calendar.getInstance().getTime()));
tzInfo.append(")");
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append("thing", thingUid)
.append("geolocation", geolocation).append("altitude", altitude).append("interval", interval)
.append("systemTimezone", tzInfo.toString())
.append("daylightSavings", Calendar.getInstance().get(Calendar.DST_OFFSET) != 0).toString();
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,8 @@ public static State getState(ChannelUID channelUID, AstroChannelConfig config, O
}
}

public static void setTimeZone(TimeZoneProvider zone) {
PropertyUtils.timeZoneProvider = zone;
}

public static void unsetTimeZone() {
PropertyUtils.timeZoneProvider = null;
public static void setTimeZoneProvider(TimeZoneProvider timeZoneProvider) {
PropertyUtils.timeZoneProvider = timeZoneProvider;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,92 +48,93 @@ public void testIfGeolocationIsProvidedForASunThing_theThingStatusBecomesONLINE(
Configuration thingConfiguration = new Configuration();
thingConfiguration.put(GEOLOCATION_PROPERTY, GEOLOCATION_VALUE);
thingConfiguration.put(INTERVAL_PROPERTY, INTERVAL_DEFAULT_VALUE);
assertThingStatus(thingConfiguration, ThingStatus.ONLINE);
assertThingStatus(thingConfiguration, ThingStatus.ONLINE, ThingStatusDetail.NONE);
}

@Test
public void testIfGeolocationIsProvidedForAMoonThing_theThingStatusBecomesONLINE() {
Configuration thingConfiguration = new Configuration();
thingConfiguration.put(GEOLOCATION_PROPERTY, GEOLOCATION_VALUE);
thingConfiguration.put(INTERVAL_PROPERTY, INTERVAL_DEFAULT_VALUE);
assertThingStatus(thingConfiguration, ThingStatus.ONLINE);
assertThingStatus(thingConfiguration, ThingStatus.ONLINE, ThingStatusDetail.NONE);
}

@Test
public void testIfGeolocationForASunThingIsNull_theThingStatusBecomesOFFLINE() {
Configuration thingConfiguration = new Configuration();
thingConfiguration.put(GEOLOCATION_PROPERTY, null);
assertThingStatus(thingConfiguration, ThingStatus.OFFLINE);
assertThingStatus(thingConfiguration, ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
}

@Test
public void testIfGeolocationForAMoonThingIsNull_theThingStatusBecomesOFFLINE() {
Configuration thingConfiguration = new Configuration();
thingConfiguration.put(GEOLOCATION_PROPERTY, null);
assertThingStatus(thingConfiguration, ThingStatus.OFFLINE);
assertThingStatus(thingConfiguration, ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
}

@Test
public void testIfTheLatitudeForASunThingIsNull_theThingStatusBecomesOFFLINE() {
Configuration thingConfiguration = new Configuration();
thingConfiguration.put(GEOLOCATION_PROPERTY, NULL_LATITUDE);
assertThingStatus(thingConfiguration, ThingStatus.OFFLINE);
assertThingStatus(thingConfiguration, ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
}

@Test
public void testIfTheLatitudeForAMoonThingIsNull_theThingStatusBecomesOFFLINE() {
Configuration thingConfiguration = new Configuration();
thingConfiguration.put(GEOLOCATION_PROPERTY, NULL_LATITUDE);
assertThingStatus(thingConfiguration, ThingStatus.OFFLINE);
assertThingStatus(thingConfiguration, ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
}

@Test
public void testIfTheLongitudeForASunThingIsNull_theThingStatusBecomesOFFLINE() {
Configuration thingConfiguration = new Configuration();
thingConfiguration.put(GEOLOCATION_PROPERTY, NULL_LONGITUDE);
assertThingStatus(thingConfiguration, ThingStatus.OFFLINE);
assertThingStatus(thingConfiguration, ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
}

@Test
public void testIfTheLongitudeForAMoonThingIsNull_theThingStatusBecomesOFFLINE() {
Configuration thingConfiguration = new Configuration();
thingConfiguration.put(GEOLOCATION_PROPERTY, NULL_LONGITUDE);
assertThingStatus(thingConfiguration, ThingStatus.OFFLINE);
assertThingStatus(thingConfiguration, ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
}

@Test
public void testIfTheIntervalForASunThingIsLessThan1_theThingStatusBecomesOFFLINE() {
Configuration thingConfiguration = new Configuration();
thingConfiguration.put(GEOLOCATION_PROPERTY, GEOLOCATION_VALUE);
thingConfiguration.put(INTERVAL_PROPERTY, new Integer(0));
assertThingStatus(thingConfiguration, ThingStatus.OFFLINE);
assertThingStatus(thingConfiguration, ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
}

@Test
public void testIfTheIntervalForAMoonThingIsLessThan1_theThingStatusBecomesOFFLINE() {
Configuration thingConfiguration = new Configuration();
thingConfiguration.put(GEOLOCATION_PROPERTY, GEOLOCATION_VALUE);
thingConfiguration.put(INTERVAL_PROPERTY, new Integer(0));
assertThingStatus(thingConfiguration, ThingStatus.OFFLINE);
assertThingStatus(thingConfiguration, ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
}

@Test
public void testIfTheIntervalForASunThingIsGreaterThan86400_theThingStatusBecomesOFFLINE() {
Configuration thingConfiguration = new Configuration();
thingConfiguration.put(GEOLOCATION_PROPERTY, GEOLOCATION_VALUE);
thingConfiguration.put(INTERVAL_PROPERTY, new Integer(86401));
assertThingStatus(thingConfiguration, ThingStatus.OFFLINE);
assertThingStatus(thingConfiguration, ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
}

@Test
public void testIfTheIntervalForAMoonThingIsGreaterThan86400_theThingStatusBecomesOFFLINE() {
Configuration thingConfiguration = new Configuration();
thingConfiguration.put(GEOLOCATION_PROPERTY, GEOLOCATION_VALUE);
thingConfiguration.put(INTERVAL_PROPERTY, new Integer(86401));
assertThingStatus(thingConfiguration, ThingStatus.OFFLINE);
assertThingStatus(thingConfiguration, ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
}

private void assertThingStatus(Configuration configuration, ThingStatus expectedStatus) {
private void assertThingStatus(Configuration configuration, ThingStatus expectedStatus,
ThingStatusDetail expectedStatusDetail) {
ThingUID thingUID = new ThingUID(THING_TYPE_SUN, TEST_SUN_THING_ID);

Thing thing = mock(Thing.class);
Expand All @@ -147,7 +148,7 @@ private void assertThingStatus(Configuration configuration, ThingStatus expected

sunHandler.initialize();

ThingStatusInfo expectedThingStatus = new ThingStatusInfo(expectedStatus, ThingStatusDetail.NONE, null);
ThingStatusInfo expectedThingStatus = new ThingStatusInfo(expectedStatus, expectedStatusDetail, null);
verify(callback, times(1)).statusUpdated(thing, expectedThingStatus);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,8 @@ public static List<Object[]> data() {

@Test
public void testParametrized() {
PropertyUtils.unsetTimeZone();

// Anonymous implementation of the service to adapt the time zone to the tested longitude and latitude
PropertyUtils.setTimeZone(new TimeZoneProvider() {
PropertyUtils.setTimeZoneProvider(new TimeZoneProvider() {
@Override
public ZoneId getTimeZone() {
return ZONE_ID;
Expand Down

0 comments on commit 9c476a9

Please sign in to comment.