diff --git a/.github/workflows/build_publish.yml b/.github/workflows/build_publish.yml index bcd50baf..a728b6b3 100644 --- a/.github/workflows/build_publish.yml +++ b/.github/workflows/build_publish.yml @@ -4,9 +4,9 @@ on: branches: - snomed-master - 'snomed-release-*' + - buildAndCloneRepo tags: - 'snomed-[0-9]+.[0-9]+.[0-9]+' - jobs: build-publish-docker: name: Build & Publish Docker Image diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..7dc68d78 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,33 @@ +FROM amazoncorretto:8 + +ENV SERVER_PORT=8051 +ENV BASE_DIR=/var/run/bahmni-reports +ENV CONTEXT_PATH=/bahmnireports +ENV WAR_DIRECTORY=/var/run/bahmni-reports/bahmni-reports +ENV SERVER_OPTS="-Xms512m -Xmx1024m -XX:PermSize=256m -XX:MaxPermSize=512m" +ENV DEBUG_OPTS="-agentlib:jdwp=transport=dt_socket,address=8003,server=y,suspend=n" + +RUN mkdir -p /var/log/bahmni-reports +RUN mkdir -p /home/bahmni/reports +RUN mkdir -p ${WAR_DIRECTORY} +RUN mkdir -p ${HOME}/.bahmni-reports/ +RUN mkdir -p /var/log/bahmni-reports/ +RUN yum install -y gettext unzip + +ADD https://raw.githubusercontent.com/eficode/wait-for/v2.2.3/wait-for /etc/wait-for +RUN yum install -y nc +RUN chmod +x /etc/wait-for + +COPY target/bahmnireports.war /etc/bahmni-reports/bahmnireports.war +RUN cd ${WAR_DIRECTORY} && jar xvf /etc/bahmni-reports/bahmnireports.war + +ADD https://repo.mybahmni.org/packages/build/bahmni-embedded-tomcat-8.0.42.jar /opt/bahmni-reports/lib/bahmni-embedded-tomcat.jar +ADD https://github.com/daviemukungi/bahmni-Icd10extensions/raw/main/jarfile/extensions-1.0-SNAPSHOT.jar /opt/bahmni-reports/lib/extensions-1.0-SNAPSHOT.jar + +COPY package/resources/log4j2.properties ${WAR_DIRECTORY}/WEB-INF/classes/ +COPY package/resources/run-liquibase.sh /etc/bahmni-reports/ +COPY package/docker/bahmni-reports/template/bahmni-reports.properties.template /etc/bahmni-reports/ + +COPY package/docker/bahmni-reports/start.sh start.sh +RUN chmod +x start.sh +CMD [ "./start.sh" ] diff --git a/src/main/java/org/bahmni/reports/extensions/ResultSetExtension.java b/src/main/java/org/bahmni/reports/extensions/ResultSetExtension.java index a815d9ba..a6a94b28 100644 --- a/src/main/java/org/bahmni/reports/extensions/ResultSetExtension.java +++ b/src/main/java/org/bahmni/reports/extensions/ResultSetExtension.java @@ -2,7 +2,6 @@ import net.sf.dynamicreports.jasper.builder.JasperReportBuilder; import org.apache.commons.lang3.StringUtils; - import java.util.Collection; import java.util.Map; diff --git a/src/main/java/org/bahmni/reports/extensions/icd10/Icd10Evaluator.java b/src/main/java/org/bahmni/reports/extensions/icd10/Icd10Evaluator.java deleted file mode 100644 index 0c052219..00000000 --- a/src/main/java/org/bahmni/reports/extensions/icd10/Icd10Evaluator.java +++ /dev/null @@ -1,91 +0,0 @@ -package org.bahmni.reports.extensions.icd10; - -import org.apache.commons.lang3.StringUtils; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.bahmni.reports.extensions.icd10.Impl.Icd10LookupServiceImpl; -import org.bahmni.reports.extensions.icd10.bean.IcdRule; - -import javax.script.ScriptEngine; -import javax.script.ScriptEngineManager; -import java.util.ArrayList; -import java.util.List; -import java.util.stream.Collectors; - -public class Icd10Evaluator { - static final String TRUE_KEYWORD = "TRUE"; - static final String FALSE_KEYWORD = "FALSE"; - static final String OTHERWISE_TRUE_KEYWORD = "OTHERWISE TRUE"; - static final String MALE_KEYWORD = "IFA 248153007 | Male (finding) |"; - static final String FEMALE_KEYWORD = "IFA 248152002 | Female (finding) |"; - static final String AGE_KEYWORD = "IFA 445518008 | Age at onset of clinical finding (observable entity) |"; - static final String AND_KEYWORD = "AND"; - static final String AND_KEYWORD_REPLACE = "&&"; - static final String OR_KEYWORD = "OR"; - static final String OR_KEYWORD_REPLACE = "||"; - static final String YEARS_KEYWORD = " years"; - static final String YEARS_KEYWORD_REPLACE = ""; - private static final Logger logger = LogManager.getLogger(Icd10Evaluator.class); - public Icd10LookupService icd10LookUpService = new Icd10LookupServiceImpl(); - public ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("Nashorn"); - - private static int getMapGroup(IcdRule rule) { - return Integer.parseInt(rule.getMapGroup()); - } - - static String deriveRuleExpression(String rule, Integer age, String gender) { - rule = rule.replace(OTHERWISE_TRUE_KEYWORD, TRUE_KEYWORD); - rule = rule.replace(MALE_KEYWORD, gender.equalsIgnoreCase("M") ? TRUE_KEYWORD.toLowerCase() : FALSE_KEYWORD.toLowerCase()); - rule = rule.replace(FEMALE_KEYWORD, gender.equalsIgnoreCase("F") ? TRUE_KEYWORD.toLowerCase() : FALSE_KEYWORD.toLowerCase()); - rule = rule.replace(AGE_KEYWORD, age.toString()); - rule = rule.replace(AND_KEYWORD, AND_KEYWORD_REPLACE); - rule = rule.replace(OR_KEYWORD, OR_KEYWORD_REPLACE); - rule = rule.replace(YEARS_KEYWORD, YEARS_KEYWORD_REPLACE); - return rule; - } - - private static boolean isRuleWithNextMapGroup(int currentMapGroup, IcdRule rule) { - int ruleMapGroup = getMapGroup(rule); - return ruleMapGroup > currentMapGroup; - } - - public String getMatchingIcdCodes(String snomedCode, int age, String gender) { - List matchingICDCodes = new ArrayList<>(); - try { - List orderedRules = icd10LookUpService.getRules(snomedCode); - int currentMapGroup = 1; - boolean isMapTargetFoundForCurrentMapGroup = false; - for (IcdRule rule : orderedRules) { - if (isRuleWithNextMapGroup(currentMapGroup, rule)) { - currentMapGroup = getMapGroup(rule); - isMapTargetFoundForCurrentMapGroup = false; - } - if (isMapTargetFoundForCurrentMapGroup) { - continue; - } - if (evaluateRule(deriveRuleExpression(rule.getMapRule(), age, gender))) { - matchingICDCodes.add(rule.getMapTarget()); - isMapTargetFoundForCurrentMapGroup = true; - } - } - } catch (Exception exception) { - logger.error("Error occurred when extracting Icd10 codes for snomed code: " + snomedCode, exception); - } - return matchingICDCodes.stream().filter(StringUtils::isNotBlank).collect(Collectors.joining(",")); - } - - boolean evaluateRule(String rule) { - try { - if (rule.equalsIgnoreCase(TRUE_KEYWORD)) { - return true; - } - if (rule.equalsIgnoreCase(FALSE_KEYWORD)) { - return false; - } - return (boolean) scriptEngine.eval(rule); - } catch (Exception exception) { - logger.error("Error occurred during evaluating rules", exception); - throw new RuntimeException(exception); - } - } -} \ No newline at end of file diff --git a/src/main/java/org/bahmni/reports/extensions/icd10/Icd10LookupService.java b/src/main/java/org/bahmni/reports/extensions/icd10/Icd10LookupService.java deleted file mode 100644 index aa45b2fd..00000000 --- a/src/main/java/org/bahmni/reports/extensions/icd10/Icd10LookupService.java +++ /dev/null @@ -1,9 +0,0 @@ -package org.bahmni.reports.extensions.icd10; - -import org.bahmni.reports.extensions.icd10.bean.IcdRule; - -import java.util.List; - -public interface Icd10LookupService { - List getRules(String snomedCode); -} diff --git a/src/main/java/org/bahmni/reports/extensions/icd10/Icd10ResultSetExtension.java b/src/main/java/org/bahmni/reports/extensions/icd10/Icd10ResultSetExtension.java deleted file mode 100644 index f5fa816d..00000000 --- a/src/main/java/org/bahmni/reports/extensions/icd10/Icd10ResultSetExtension.java +++ /dev/null @@ -1,39 +0,0 @@ -package org.bahmni.reports.extensions.icd10; - -import net.sf.dynamicreports.jasper.builder.JasperReportBuilder; -import net.sf.dynamicreports.report.constant.HorizontalAlignment; -import org.bahmni.reports.extensions.ResultSetExtension; -import org.bahmni.reports.template.TSIntegrationDiagnosisLineReportTemplate; - -import java.time.LocalDate; -import java.time.Period; -import java.util.Collection; -import java.util.Map; - -import static net.sf.dynamicreports.report.builder.DynamicReports.col; -import static net.sf.dynamicreports.report.builder.DynamicReports.type; -import static org.bahmni.reports.template.Templates.columnStyle; - - -public class Icd10ResultSetExtension implements ResultSetExtension { - public static final String ICD_10_COLUMN_NAME = "ICD10 Code(s)"; - - public Icd10Evaluator icd10Evaluator = new Icd10Evaluator(); - - public void enrich(Collection> collection, JasperReportBuilder jasperReport) { - collection.forEach(this::enrichRowWithIcdCode); - jasperReport.addColumn(col.column(ICD_10_COLUMN_NAME, ICD_10_COLUMN_NAME, type.stringType()).setStyle(columnStyle).setHorizontalAlignment(HorizontalAlignment.CENTER)); - } - - private void enrichRowWithIcdCode(Map rowMap) { - String terminologyCode = (String) rowMap.get(TSIntegrationDiagnosisLineReportTemplate.TERMINOLOGY_COLUMN_NAME); - int age = getAgeFromDob((String) rowMap.get(TSIntegrationDiagnosisLineReportTemplate.PATIENT_DATE_OF_BIRTH_COLUMN_NAME)); - String gender = (String) rowMap.get(TSIntegrationDiagnosisLineReportTemplate.GENDER_COLUMN_NAME); - String icd10Code = icd10Evaluator.getMatchingIcdCodes(terminologyCode, age, gender); - enrichRow(rowMap, ICD_10_COLUMN_NAME, icd10Code); - } - - private int getAgeFromDob(String dateOfBirth) { - return Period.between(LocalDate.parse(dateOfBirth), LocalDate.now()).getYears(); - } -} diff --git a/src/main/java/org/bahmni/reports/extensions/icd10/Impl/Icd10LookupServiceImpl.java b/src/main/java/org/bahmni/reports/extensions/icd10/Impl/Icd10LookupServiceImpl.java deleted file mode 100644 index 0a7ad264..00000000 --- a/src/main/java/org/bahmni/reports/extensions/icd10/Impl/Icd10LookupServiceImpl.java +++ /dev/null @@ -1,91 +0,0 @@ -package org.bahmni.reports.extensions.icd10.Impl; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.bahmni.reports.extensions.icd10.Icd10LookupService; -import org.bahmni.reports.extensions.icd10.bean.IcdResponse; -import org.bahmni.reports.extensions.icd10.bean.IcdRule; -import org.bahmni.reports.util.PropertyUtil; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpMethod; -import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; -import org.springframework.web.client.RestTemplate; -import org.springframework.web.util.UriComponentsBuilder; - -import java.io.UnsupportedEncodingException; -import java.net.URI; -import java.net.URLEncoder; -import java.nio.charset.StandardCharsets; -import java.util.*; -import java.util.stream.Collectors; - -public class Icd10LookupServiceImpl implements Icd10LookupService { - public static final Comparator customComparator = Comparator.comparingInt((IcdRule rule) -> Integer.parseInt(rule.mapGroup)).thenComparingInt(rule -> Integer.parseInt(rule.mapPriority)); - private static final Logger logger = LogManager.getLogger(Icd10LookupServiceImpl.class); - private static final String ICD_PROPERTIES_FILENAME = "icd-service-config.properties"; - private static final Properties icd10Properties = loadIcdProperties(); - private RestTemplate restTemplate = new RestTemplate(); - - static Properties loadIcdProperties() { - return PropertyUtil.loadProperties(ICD_PROPERTIES_FILENAME); - } - - @Override - public List getRules(String snomedCode) { - try { - IcdResponse icdResponse; - List icdRules = new ArrayList<>(); - int offset = 0, limit = 100; - do { - URI encodedURI = getEndPoint(snomedCode, offset, limit); - icdResponse = getResponse(encodedURI); - icdRules.addAll(icdResponse.getItems()); - offset += limit; - } while (offset < icdResponse.getTotal()); - return icdRules.stream().sorted(customComparator).collect(Collectors.toList()); - } catch (Exception exception) { - logger.error(String.format("Error caused during ICD lookup rest call: %s", exception.getMessage())); - return new ArrayList<>(); - } - - } - - private String getEclUrl(String referencedComponentId) { - return String.format(icd10Properties.getProperty("icd.eclUrl"), referencedComponentId); - } - - private String encode(String rawStr) throws UnsupportedEncodingException { - return URLEncoder.encode(rawStr, StandardCharsets.UTF_8.name()); - } - - private IcdResponse getResponse(URI encodedURI) { - ResponseEntity responseEntity = restTemplate.exchange(encodedURI, HttpMethod.GET, new org.springframework.http.HttpEntity<>(null, getHeaders()), IcdResponse.class); - if (responseEntity.getStatusCode().is2xxSuccessful()) { - return responseEntity.getBody(); - } - return new IcdResponse(); - } - - private URI getEndPoint(String snomedCode, Integer offset, Integer limit) { - try { - UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString(icd10Properties.getProperty("icd.baseUrl")) - .queryParam("offset", offset) - .queryParam("termActive", true) - .queryParam("ecl", encode(getEclUrl(snomedCode))) - .queryParam("limit", limit); - return uriBuilder.build(true).toUri(); - } catch (Exception exception) { - logger.error("Error while encoding ecl url ", exception); - throw new RuntimeException(exception); - } - } - - private HttpHeaders getHeaders() { - HttpHeaders headers = new HttpHeaders(); - headers.setContentType(MediaType.APPLICATION_JSON); - headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); - return headers; - } - -} diff --git a/src/main/java/org/bahmni/reports/extensions/icd10/bean/IcdMap.java b/src/main/java/org/bahmni/reports/extensions/icd10/bean/IcdMap.java deleted file mode 100644 index f4aab050..00000000 --- a/src/main/java/org/bahmni/reports/extensions/icd10/bean/IcdMap.java +++ /dev/null @@ -1,40 +0,0 @@ -package org.bahmni.reports.extensions.icd10.bean; - -public class IcdMap { - String snomedCode; - int age; - String gender; - String icdCodes; - - public String getSnomedCode() { - return snomedCode; - } - - public void setSnomedCode(String snomedCode) { - this.snomedCode = snomedCode; - } - - public int getAge() { - return age; - } - - public void setAge(int age) { - this.age = age; - } - - public String getGender() { - return gender; - } - - public void setGender(String gender) { - this.gender = gender; - } - - public String getIcdCodes() { - return icdCodes; - } - - public void setIcdCodes(String icdCodes) { - this.icdCodes = icdCodes; - } -} diff --git a/src/main/java/org/bahmni/reports/extensions/icd10/bean/IcdResponse.java b/src/main/java/org/bahmni/reports/extensions/icd10/bean/IcdResponse.java deleted file mode 100644 index 2b41e221..00000000 --- a/src/main/java/org/bahmni/reports/extensions/icd10/bean/IcdResponse.java +++ /dev/null @@ -1,32 +0,0 @@ -package org.bahmni.reports.extensions.icd10.bean; - -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; - -import java.util.List; - -@JsonIgnoreProperties(ignoreUnknown = true) -public class IcdResponse { - List items; - - int total; - - public IcdResponse() { - - } - - public List getItems() { - return items; - } - - public void setItems(List items) { - this.items = items; - } - - public int getTotal() { - return total; - } - - public void setTotal(int total) { - this.total = total; - } -} diff --git a/src/main/java/org/bahmni/reports/extensions/icd10/bean/IcdRule.java b/src/main/java/org/bahmni/reports/extensions/icd10/bean/IcdRule.java deleted file mode 100644 index f0a9bc11..00000000 --- a/src/main/java/org/bahmni/reports/extensions/icd10/bean/IcdRule.java +++ /dev/null @@ -1,48 +0,0 @@ -package org.bahmni.reports.extensions.icd10.bean; - -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; - -@JsonIgnoreProperties(ignoreUnknown = true) -public class IcdRule { - public String mapPriority; - public String mapGroup; - public String mapRule; - public String mapTarget; - - @Override - public String toString() { - return this.mapGroup + " " + this.mapPriority + " " + this.mapRule; - } - - public String getMapPriority() { - return mapPriority; - } - - public void setMapPriority(String mapPriority) { - this.mapPriority = mapPriority; - } - - public String getMapGroup() { - return mapGroup; - } - - public void setMapGroup(String mapGroup) { - this.mapGroup = mapGroup; - } - - public String getMapRule() { - return mapRule; - } - - public void setMapRule(String mapRule) { - this.mapRule = mapRule; - } - - public String getMapTarget() { - return mapTarget; - } - - public void setMapTarget(String mapTarget) { - this.mapTarget = mapTarget; - } -} \ No newline at end of file diff --git a/src/main/java/org/bahmni/reports/template/TSIntegrationDiagnosisLineReportTemplate.java b/src/main/java/org/bahmni/reports/template/TSIntegrationDiagnosisLineReportTemplate.java index 23a9e052..c83883a6 100644 --- a/src/main/java/org/bahmni/reports/template/TSIntegrationDiagnosisLineReportTemplate.java +++ b/src/main/java/org/bahmni/reports/template/TSIntegrationDiagnosisLineReportTemplate.java @@ -99,7 +99,7 @@ private void enrichUsingReflection(String extensionClassStr, Collection mockSortedRules = getMockMapRules("terminologyServices/icdRules_SingleMapGroup.json"); - when(icd10Service.getRules(any())).thenReturn(mockSortedRules); - String codes = icd10Evaluator.getMatchingIcdCodes("dummycode", 34, "M"); - assertNotNull(codes); - assertEquals("J45.9", codes); - } - - @Test - public void shouldSelectFirstMatchingICDCodeInMapPriorityOrder_PerMapGroupForMultipleMapGroups() { - List mockSortedRules = getMockMapRules("terminologyServices/icdRules_MultipleMapGroups.json"); - when(icd10Service.getRules(any())).thenReturn(mockSortedRules); - String codes = icd10Evaluator.getMatchingIcdCodes("dummycode", 34, "M"); - assertNotNull(codes); - assertEquals("J45.9,N45.9", codes); - } - - @Test - public void shouldSelectFirstMatchingICDCodeInMapPriorityOrder_PerMapGroupForMultipleMapGroups_BasedOnAgeGreaterThanPredicate() { - List mockSortedRules = getMockMapRules("terminologyServices/icdRules_Age.json"); - when(icd10Service.getRules(any())).thenReturn(mockSortedRules); - String codes = icd10Evaluator.getMatchingIcdCodes("dummycode", 90, "M"); - assertNotNull(codes); - assertEquals("M83.19", codes); - } - - @Test - public void shouldSelectFirstMatchingICDCodeInMapPriorityOrder_PerMapGroupForMultipleMapGroups_BasedOnAgeLesserThanPredicate() { - List mockSortedRules = getMockMapRules("terminologyServices/icdRules_Age.json"); - when(icd10Service.getRules(any())).thenReturn(mockSortedRules); - String codes = icd10Evaluator.getMatchingIcdCodes("dummycode", 10, "M"); - assertNotNull(codes); - assertEquals("E55.0", codes); - } - - @Test - public void shouldSelectFirstMatchingICDCodeInMapPriorityOrder_PerMapGroupForMultipleMapGroups_BasedOnAgeRangePredicate() { - List mockSortedRules = getMockMapRules("terminologyServices/icdRules_Age.json"); - when(icd10Service.getRules(any())).thenReturn(mockSortedRules); - String codes = icd10Evaluator.getMatchingIcdCodes("dummycode", 14, "M"); - assertNotNull(codes); - assertEquals("E55.0,M87.19", codes); - } - - @Test - public void shouldSelectFirstMatchingICDCodeInMapPriorityOrder_ForSingleMapGroup_BasedOnMaleGenderPredicate() { - List mockSortedRules = getMockMapRules("terminologyServices/icdRules_Gender.json"); - when(icd10Service.getRules(any())).thenReturn(mockSortedRules); - String codes = icd10Evaluator.getMatchingIcdCodes("dummycode", 34, "M"); - assertNotNull(codes); - assertEquals("N46", codes); - } - - @Test - public void shouldSelectFirstMatchingICDCodeInMapPriorityOrder_ForSingleMapGroup_BasedOnFemaleGenderPredicate() { - List mockSortedRules = getMockMapRules("terminologyServices/icdRules_Gender.json"); - when(icd10Service.getRules(any())).thenReturn(mockSortedRules); - String codes = icd10Evaluator.getMatchingIcdCodes("dummycode", 34, "F"); - assertNotNull(codes); - assertEquals("N97.9", codes); - } - - @Test - public void shouldSelectFallbackICDCodeInMapPriorityOrder_ForSingleMapGroup() { - List mockSortedRules = getMockMapRules("terminologyServices/icdRules_Gender.json"); - when(icd10Service.getRules(any())).thenReturn(mockSortedRules); - String codes = icd10Evaluator.getMatchingIcdCodes("dummycode", 34, "OTHER"); - assertNotNull(codes); - assertEquals("", codes); - } - - - @Test - public void shouldReturnEmptyICDCode_PerMapGroupForMultipleMapGroups_WhenMatchingMapTargetIsEmpty() { - List mockSortedRules = getMockMapRules("terminologyServices/icdRules_WithEmptyMapTargets.json"); - when(icd10Service.getRules(any())).thenReturn(mockSortedRules); - String codes = icd10Evaluator.getMatchingIcdCodes("dummycode", 34, "OTHER"); - assertNotNull(codes); - assertEquals("", codes); - } - - - List getMockMapRules(String filePath) { - IcdResponse icdResponse = Icd10LookupServiceImplTest.getMockIcdResponse(filePath); - return icdResponse.getItems().stream().sorted(Icd10LookupServiceImpl.customComparator).collect(Collectors.toList()); - } - -} \ No newline at end of file diff --git a/src/test/java/org/bahmni/reports/extensions/icd10/Icd10ResultSetExtensionTest.java b/src/test/java/org/bahmni/reports/extensions/icd10/Icd10ResultSetExtensionTest.java deleted file mode 100644 index 5bf42ced..00000000 --- a/src/test/java/org/bahmni/reports/extensions/icd10/Icd10ResultSetExtensionTest.java +++ /dev/null @@ -1,110 +0,0 @@ -package org.bahmni.reports.extensions.icd10; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; -import net.sf.dynamicreports.jasper.builder.JasperReportBuilder; -import org.apache.commons.lang3.StringUtils; -import org.bahmni.reports.util.FileReaderUtil; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.powermock.core.classloader.annotations.PowerMockIgnore; -import org.powermock.modules.junit4.PowerMockRunner; - -import java.util.List; -import java.util.Map; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.*; - -@PowerMockIgnore({"javax.management.*", "javax.net.ssl.*", "javax.script.*"}) -@RunWith(PowerMockRunner.class) -public class Icd10ResultSetExtensionTest { - @InjectMocks - Icd10ResultSetExtension icd10ResultSetExtension; - @Mock - Icd10Evaluator mockIcd10Evaluator; - private ObjectMapper mapper = new ObjectMapper(); - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - } - - @Test - public void shouldAddNonBlankValueInEnrichedColumnInResultSet_WhenIcdEvaluatorReturnsNonBlankValue() { - when(mockIcd10Evaluator.getMatchingIcdCodes(anyString(), anyInt(), anyString())).thenReturn("dummyIcdCode"); - List> collection = getMockDiagnosisResultSetCollection(); - for (Map rowMap : collection) { - assertEquals(7, rowMap.size()); - } - icd10ResultSetExtension.enrich(collection, new JasperReportBuilder()); - for (Map rowMap : collection) { - assertEquals(8, rowMap.size()); - String icd10Value = (String) rowMap.get(Icd10ResultSetExtension.ICD_10_COLUMN_NAME); - assertTrue(StringUtils.isNotBlank(icd10Value)); - assertEquals("dummyIcdCode", icd10Value); - } - } - - @Test - public void shouldAddEmptyValueInEnrichedColumnInResultSet_WhenIcdEvaluatorReturnsBlankValue() { - when(mockIcd10Evaluator.getMatchingIcdCodes(anyString(), anyInt(), anyString())).thenReturn(" "); - List> collection = getMockDiagnosisResultSetCollection(); - for (Map rowMap : collection) { - assertEquals(7, rowMap.size()); - } - icd10ResultSetExtension.enrich(collection, new JasperReportBuilder()); - for (Map rowMap : collection) { - assertEquals(8, rowMap.size()); - String icd10Value = (String) rowMap.get(Icd10ResultSetExtension.ICD_10_COLUMN_NAME); - assertTrue(StringUtils.isEmpty(icd10Value)); - } - } - - @Test - public void shouldAddEmptyValueInEnrichedColumnInResultSet_WhenIcdEvaluatorReturnsEmptyValue() { - when(mockIcd10Evaluator.getMatchingIcdCodes(anyString(), anyInt(), anyString())).thenReturn(""); - List> collection = getMockDiagnosisResultSetCollection(); - for (Map rowMap : collection) { - assertEquals(7, rowMap.size()); - } - icd10ResultSetExtension.enrich(collection, new JasperReportBuilder()); - for (Map rowMap : collection) { - assertEquals(8, rowMap.size()); - String icd10Value = (String) rowMap.get(Icd10ResultSetExtension.ICD_10_COLUMN_NAME); - assertTrue(StringUtils.isEmpty(icd10Value)); - } - } - - @Test - public void shouldAddEmptyValueInEnrichedColumnInResultSet_WhenIcdEvaluatorReturnsNullValue() { - when(mockIcd10Evaluator.getMatchingIcdCodes(anyString(), anyInt(), anyString())).thenReturn(null); - List> collection = getMockDiagnosisResultSetCollection(); - for (Map rowMap : collection) { - assertEquals(7, rowMap.size()); - } - icd10ResultSetExtension.enrich(collection, new JasperReportBuilder()); - for (Map rowMap : collection) { - assertEquals(8, rowMap.size()); - String icd10Value = (String) rowMap.get(Icd10ResultSetExtension.ICD_10_COLUMN_NAME); - assertTrue(StringUtils.isEmpty(icd10Value)); - } - } - - private List> getMockDiagnosisResultSetCollection() { - String resultSetCollectionStr = FileReaderUtil.getFileContent("terminologyServices/diagnosisResultSetCollection.json"); - try { - return mapper.readValue(resultSetCollectionStr, new TypeReference>>() { - }); - } catch (JsonProcessingException e) { - throw new RuntimeException(e); - } - - } -} \ No newline at end of file diff --git a/src/test/java/org/bahmni/reports/extensions/icd10/Impl/Icd10LookupServiceImplTest.java b/src/test/java/org/bahmni/reports/extensions/icd10/Impl/Icd10LookupServiceImplTest.java deleted file mode 100644 index 4b82cfa1..00000000 --- a/src/test/java/org/bahmni/reports/extensions/icd10/Impl/Icd10LookupServiceImplTest.java +++ /dev/null @@ -1,92 +0,0 @@ -package org.bahmni.reports.extensions.icd10.Impl; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.bahmni.reports.extensions.icd10.bean.IcdResponse; -import org.bahmni.reports.extensions.icd10.bean.IcdRule; -import org.bahmni.reports.util.FileReaderUtil; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.powermock.core.classloader.annotations.PowerMockIgnore; -import org.powermock.modules.junit4.PowerMockRunner; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.client.RestTemplate; - -import java.util.List; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.*; - -@PowerMockIgnore({"javax.management.*", "javax.net.ssl.*"}) -@RunWith(PowerMockRunner.class) -public class Icd10LookupServiceImplTest { - private static final String SNOMED_CODE = "dummy"; - private static ObjectMapper mapper = new ObjectMapper(); - @InjectMocks - Icd10LookupServiceImpl icd10LookupService; - @Mock - ResponseEntity mockIcdResponse; - @Mock - RestTemplate restTemplate; - - public static IcdResponse getMockIcdResponse(String relativePath) { - try { - return mapper.readValue(FileReaderUtil.getFileContent(relativePath), IcdResponse.class); - } catch (JsonProcessingException e) { - throw new RuntimeException(e); - } - } - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - } - - @Test - public void shouldReturnIcdRulesOrderedByMapGroupAndThenMapPriority_WhenValidSnomedCodeIsPassed() { - IcdResponse mockResponse = getMockIcdResponse("terminologyServices/icdRules_MultipleMapGroups.json"); - when(restTemplate.exchange(any(), any(), any(), eq(IcdResponse.class))).thenReturn(mockIcdResponse); - when(mockIcdResponse.getStatusCode()).thenReturn(HttpStatus.ACCEPTED); - when(mockIcdResponse.getBody()).thenReturn(mockResponse); - List sortedRules = icd10LookupService.getRules(SNOMED_CODE); - assertNotNull(sortedRules); - assertEquals(4, sortedRules.size()); - assertEquals("1", sortedRules.get(0).getMapGroup()); - assertEquals("1", sortedRules.get(0).getMapPriority()); - assertEquals("1", sortedRules.get(1).getMapGroup()); - assertEquals("2", sortedRules.get(1).getMapPriority()); - assertEquals("2", sortedRules.get(2).getMapGroup()); - assertEquals("1", sortedRules.get(2).getMapPriority()); - assertEquals("2", sortedRules.get(3).getMapGroup()); - assertEquals("2", sortedRules.get(3).getMapPriority()); - } - - @Test - public void shouldReturnEmptyList_WhenInvalidSnomedCodeIsPassed() { - when(restTemplate.exchange(any(), any(), any(), eq(IcdResponse.class))).thenReturn(mockIcdResponse); - when(mockIcdResponse.getStatusCode()).thenReturn(HttpStatus.BAD_REQUEST); - List sortedRules = icd10LookupService.getRules(SNOMED_CODE); - assertNotNull(sortedRules); - assertEquals(0, sortedRules.size()); - } - - @Test - public void shouldInvokePaginatedCalls_WhenIcdRulesHasLargeResultSet() { - IcdResponse mockResponse = getMockIcdResponse("terminologyServices/icdRules_WithLargeResultSet.json"); - when(restTemplate.exchange(any(), any(), any(), eq(IcdResponse.class))).thenReturn(mockIcdResponse); - when(mockIcdResponse.getStatusCode()).thenReturn(HttpStatus.ACCEPTED); - when(mockIcdResponse.getBody()).thenReturn(mockResponse); - List sortedRules = icd10LookupService.getRules(SNOMED_CODE); - assertNotNull(sortedRules); - verify(restTemplate, times(2)).exchange(any(), any(), any(), eq(IcdResponse.class)); - } - -} \ No newline at end of file diff --git a/src/test/java/org/bahmni/reports/extensions/sample/SampleResultSetExtension.java b/src/test/java/org/bahmni/reports/extensions/sample/SampleResultSetExtension.java deleted file mode 100644 index e1e60016..00000000 --- a/src/test/java/org/bahmni/reports/extensions/sample/SampleResultSetExtension.java +++ /dev/null @@ -1,25 +0,0 @@ -package org.bahmni.reports.extensions.sample; - -import net.sf.dynamicreports.jasper.builder.JasperReportBuilder; -import net.sf.dynamicreports.report.constant.HorizontalAlignment; -import org.bahmni.reports.extensions.ResultSetExtension; - -import java.sql.SQLException; -import java.util.Collection; -import java.util.Map; -import java.util.Random; - -import static net.sf.dynamicreports.report.builder.DynamicReports.col; -import static net.sf.dynamicreports.report.builder.DynamicReports.type; -import static org.bahmni.reports.template.Templates.columnStyle; - -public class SampleResultSetExtension implements ResultSetExtension { - public static final String SAMPLE_COLUMN_NAME = "Sample Column"; - - public void enrich(Collection> collection, JasperReportBuilder jasperReport) throws SQLException { - for (Map rowMap : collection) { - enrichRow(rowMap, SAMPLE_COLUMN_NAME, String.valueOf(new Random().nextInt())); - } - jasperReport.addColumn(col.column(SAMPLE_COLUMN_NAME, SAMPLE_COLUMN_NAME, type.stringType()).setStyle(columnStyle).setHorizontalAlignment(HorizontalAlignment.CENTER)); - } -}