-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[enturno] Remove org.apache.commons #14406
Conversation
Signed-off-by: lsiepel <leosiepel@gmail.com>
Signed-off-by: lsiepel <leosiepel@gmail.com>
@klocsson are you able to review? |
...enturno/src/main/java/org/openhab/binding/enturno/internal/connection/EnturNoConnection.java
Outdated
Show resolved
Hide resolved
Signed-off-by: lsiepel <leosiepel@gmail.com>
Signed-off-by: lsiepel <leosiepel@gmail.com>
@lsiepel - I'm wondering if you could be persuaded into writing a few unit tests (positive and negative)? 🙂 Since this is not manually tested it would be a quite convincing proof that it works correctly. It's not a lot of code, but for me to assess if it actually works as intended for some different scenarios, I think writing tests would almost be faster than to manually go through the code case by case with different inputs, read the implementations of the methods called before/after the change and calculate the outputs from that. |
Just added some tests. I must say that the implementation of The method:
|
Seeing the tests and your comment made me realize the actual goal here (I think), so I have given it some attention due to a strange obsession of mine with date and time logic. 😄 First thing I did was running your tests on the original method:
So it seems that we don't actually need to handle the case of missing offset because it was never supported and the parameter name I tried to create a PR towards your branch, but realized I would either need to have access to your repository, fork it or be a Git expert. Anyway, I have some proposed code here. There's nothing wrong with yours, I just wanted to give it a shot to further improve. New class: /**
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.enturno.internal.util;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import org.eclipse.jdt.annotation.NonNullByDefault;
/**
* EnturNo date utility methods.
*
* @author Jacob Laursen - Initial contribution
*/
@NonNullByDefault
public class DateUtil {
/**
* Converts a zoned date time string that lacks a colon in the zone to an ISO-8601 formatted string.
*
* @param dateTimeWithoutColonInZone
* @return ISO-8601 formatted string
*/
public static String getIsoDateTime(String dateTimeWithoutColonInZone) {
ZonedDateTime zonedDateTime = null;
try {
zonedDateTime = ZonedDateTime.parse(dateTimeWithoutColonInZone);
} catch (DateTimeParseException e) {
// Skip
}
try {
zonedDateTime = ZonedDateTime.parse(dateTimeWithoutColonInZone.replaceAll("(\\d{2})(\\d{2})$", "$1:$2"));
} catch (DateTimeParseException e) {
// Skip
}
if (zonedDateTime != null) {
return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(zonedDateTime);
}
return dateTimeWithoutColonInZone;
}
} and corresponding test class: /**
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.enturno.internal.util;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import java.util.stream.Stream;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
/**
* Tests for {@link DateUtil}.
*
* @author Jacob Laursen - Initial contribution
*/
@NonNullByDefault
public class DateUtilTest {
@ParameterizedTest
@MethodSource("provideTestCasesForGetIsoDateTime")
void getIsoDateTime(String value, String expected) {
assertThat(DateUtil.getIsoDateTime(value), is(expected));
}
private static Stream<Arguments> provideTestCasesForGetIsoDateTime() {
return Stream.of( //
Arguments.of("2023-10-25T09:01:00+0200", "2023-10-25T09:01:00+02:00"),
Arguments.of("2023-10-25T09:01:00+02:00", "2023-10-25T09:01:00+02:00"),
Arguments.of("2023-10-25T09:01:00-0300", "2023-10-25T09:01:00-03:00"),
Arguments.of("2023-10-25T09:01:00+02:30", "2023-10-25T09:01:00+02:30"),
Arguments.of("2023-10-25T09:01:00", "2023-10-25T09:01:00"));
}
} Some notes:
Let me know what you think. With the added test coverage we should have minimized the risk and have also proven that the previous cases are still working (with the addition of further cases). |
Signed-off-by: Leo Siepel <leosiepel@gmail.com>
Signed-off-by: Leo Siepel <leosiepel@gmail.com>
Allways a bit balancing between the goal of the PR and the issues that i discover in the proces. I also thought about extracting the method and improve it. But now i actually did with your code snippet. The method can be further improved, but as the current implementation allready handles much more cases (all time zones etc) then before i think it is good as is. Thanks, the regex is something i didn't think about. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
* Remove org.apache.commons * Fix warnings Also-by: Jacob Laursen <jacob-github@vindvejr.dk> Signed-off-by: Leo Siepel <leosiepel@gmail.com>
* Remove org.apache.commons * Fix warnings Also-by: Jacob Laursen <jacob-github@vindvejr.dk> Signed-off-by: Leo Siepel <leosiepel@gmail.com> Signed-off-by: Jørgen Austvik <jaustvik@acm.org>
Untested, minor refactoring from mainly from code perspective
Updated test jar 4.1.0: https://1drv.ms/u/s!AnMcxmvEeupwjq5aI8LUb81H1d5OdQ?e=FhltuE