Skip to content

Commit

Permalink
NMS-15768: Fixed scan-interval handling
Browse files Browse the repository at this point in the history
  • Loading branch information
christianpape authored Jun 28, 2023
1 parent 3aef400 commit d18e4bb
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,16 @@ public String marshal(final Duration v) {
/** {@inheritDoc} */
@Override
public Duration unmarshal(final String v) {
if ("0".equals(v)) {
if (v == null) {
return null;
}
if ("0".equals(v.trim())) {
return Duration.ZERO;
}
return DEFAULT_PERIOD_FORMATTER.parsePeriod(v).toStandardDuration();
if ("-1".equals(v.trim())) {
return Duration.ZERO.minus(1000);
}
return DEFAULT_PERIOD_FORMATTER.parsePeriod(v.trim()).toStandardDuration();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,18 @@ public class StringIntervalPropertyEditor extends PropertyEditorSupport implemen
/** {@inheritDoc} */
@Override
public void setAsText(final String text) throws IllegalArgumentException {
if ("0".equals(text)) {
setValue(Duration.ZERO);
if (text == null) {
setValue(null);
} else {
setValue(StringIntervalAdapter.DEFAULT_PERIOD_FORMATTER.parsePeriod(text).toStandardDuration());
if ("0".equals(text.trim())) {
setValue(Duration.ZERO);
} else {
if ("-1".equals(text.trim())) {
setValue(Duration.ZERO.minus(1000));
} else {
setValue(StringIntervalAdapter.DEFAULT_PERIOD_FORMATTER.parsePeriod(text.trim()).toStandardDuration());
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2023 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2023 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
* OpenNMS(R) is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* OpenNMS(R) is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with OpenNMS(R). If not, see:
* http://www.gnu.org/licenses/
*
* For more information contact:
* OpenNMS(R) Licensing <[email protected]>
* http://www.opennms.org/
* http://www.opennms.com/
*******************************************************************************/

package org.opennms.netmgt.provision.persist;

import static org.junit.Assert.assertEquals;

import org.joda.time.Duration;
import org.junit.Test;

public class StringIntervalAdapterTest {
final StringIntervalAdapter stringIntervalAdapter = new StringIntervalAdapter();
final static long ONE_DAY_MS = 1000 * 3600 * 24;
final static long ONE_SECOND_MS = 1000;

@Test
public void marshalTest() {
assertEquals("0", stringIntervalAdapter.marshal(Duration.ZERO));
assertEquals("-1s", stringIntervalAdapter.marshal(Duration.ZERO.minus(ONE_SECOND_MS)));
assertEquals("1d", stringIntervalAdapter.marshal(Duration.ZERO.plus(ONE_DAY_MS)));
}

@Test
public void unmarshalTest() {
assertEquals(Duration.ZERO, stringIntervalAdapter.unmarshal("0"));
assertEquals(Duration.ZERO, stringIntervalAdapter.unmarshal(" 0 "));
assertEquals(Duration.ZERO.minus(ONE_SECOND_MS), stringIntervalAdapter.unmarshal("-1s"));
assertEquals(Duration.ZERO.minus(ONE_SECOND_MS), stringIntervalAdapter.unmarshal(" -1s "));
assertEquals(Duration.ZERO.plus(ONE_DAY_MS), stringIntervalAdapter.unmarshal("1d"));
assertEquals(Duration.ZERO.plus(ONE_DAY_MS), stringIntervalAdapter.unmarshal(" 1d "));
// see NMS-15768
assertEquals(Duration.ZERO.minus(ONE_SECOND_MS), stringIntervalAdapter.unmarshal("-1"));
assertEquals(Duration.ZERO.minus(ONE_SECOND_MS), stringIntervalAdapter.unmarshal(" -1 "));
}
}

0 comments on commit d18e4bb

Please sign in to comment.