Skip to content

Commit

Permalink
#121 | Ensure integers when creating an integer
Browse files Browse the repository at this point in the history
  • Loading branch information
vinayvenu committed Apr 23, 2024
1 parent ae4da20 commit e0183ad
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.stream.Collectors;

import static org.avni_integration_service.goonj.config.GoonjMappingDbConstants.*;
import static org.avni_integration_service.goonj.util.NumberUtil.getInteger;

@Component("ActivityRepository")
public class ActivityRepository extends GoonjBaseRepository implements ActivityConstants {
Expand Down Expand Up @@ -129,7 +130,7 @@ private ActivityDTO createActivityRequest(Subject subject) {
activityDTO.setOtherObjective((String) subject.getObservation(SPECIFY_OTHER_FOR_OBJECTIVE_OF_WORK));
/* Measurement fields */
activityDTO.setMeasurementType((String) subject.getObservation(MEASUREMENTS_TYPE));
activityDTO.setNos((Integer) subject.getObservation(NOS));
activityDTO.setNos(getInteger((subject.getObservation(NOS))));
activityDTO.setBreadth((subject.getObservation(BREADTH) == null) ? 0D : Double.valueOf(subject.getObservation(BREADTH).toString()));
activityDTO.setDiameter((subject.getObservation(DIAMETER) == null) ? 0D : Double.valueOf(subject.getObservation(DIAMETER).toString()));
activityDTO.setLength((subject.getObservation(LENGTH) == null) ? 0D : Double.valueOf(subject.getObservation(LENGTH).toString()));
Expand Down
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();
}
}
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);
}

}

0 comments on commit e0183ad

Please sign in to comment.