Add Conversions.variantToBoolean to fix API bug

Also, add tests of Conversions.variantToBoolean

Change-Id: I07bc8a84d511d36122ee8ffd153ae0961b77f65d
This commit is contained in:
Craig Bryant 2016-06-23 12:20:45 -06:00
parent 3e27d3fea4
commit e84ab435cd
2 changed files with 31 additions and 2 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
* (C) Copyright 2015-2016 Hewlett Packard Enterprise Development Company LP.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -129,4 +129,17 @@ public final class Conversions {
}
/**
* Converts a Java Object to Boolean
*
* @param variant object of type Boolean or Number
*
* @return Boolean TRUE if Boolean and TRUE or Number and value is 1
*/
public static Boolean variantToBoolean(final Object input) {
if (input instanceof Boolean) {
return (Boolean) input;
}
return "1".equals(input.toString());
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
* (C) Copyright 2015-2016 Hewlett Packard Enterprise Development Company LP.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
@ -14,7 +14,9 @@
package monasca.common.util;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotEquals;
import static org.testng.Assert.assertTrue;
import java.math.BigDecimal;
import java.util.TimeZone;
@ -117,4 +119,18 @@ public class ConversionsTest {
private enum MockEnum {
THIS,IS,TEST
}
public void testVariantToBoolean() {
assertTrue(Conversions.variantToBoolean(new Long(1)));
assertFalse(Conversions.variantToBoolean(new Long(0)));
assertTrue(Conversions.variantToBoolean(new Integer(1)));
assertFalse(Conversions.variantToBoolean(new Integer(0)));
assertTrue(Conversions.variantToBoolean(new Short((short)1)));
assertFalse(Conversions.variantToBoolean(new Short((short)0)));
assertTrue(Conversions.variantToBoolean(Boolean.TRUE));
assertFalse(Conversions.variantToBoolean(Boolean.FALSE));
}
}