forked from avni-bahmni-integration/integration-service
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#121 | Ensure integers when creating an integer
- Loading branch information
Showing
3 changed files
with
39 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
goonj/src/main/java/org/avni_integration_service/goonj/util/NumberUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.avni_integration_service.goonj.util; | ||
|
||
public class NumberUtil { | ||
public static Integer getInteger(Object value) { | ||
if (value == null) { | ||
return null; | ||
} | ||
return Double.valueOf(value.toString()).intValue(); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
goonj/src/test/java/org/avni_integration_service/goonj/util/NumberUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.avni_integration_service.goonj.util; | ||
|
||
import org.avni_integration_service.util.ObjectJsonMapper; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Map; | ||
|
||
import static org.avni_integration_service.goonj.util.NumberUtil.getInteger; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class NumberUtilTest { | ||
|
||
@Test | ||
public void shouldConvertToInteger() { | ||
Map<String, Object> map = ObjectJsonMapper.readValue( | ||
"{ \n" + | ||
" \"a\": \"1\", \n" + | ||
" \"b\": 1, \n" + | ||
" \"c\": 1.5 \n" + | ||
"}", java.util.Map.class); | ||
assertEquals(getInteger(map.get("a")), 1); | ||
assertEquals(getInteger(map.get("b")), 1); | ||
assertEquals(getInteger(map.get("c")), 1); | ||
assertEquals(getInteger(map.get("d")), null); | ||
} | ||
|
||
} |