|
| 1 | +package org.ohdsi.webapi.test; |
| 2 | + |
| 3 | +import com.odysseusinc.arachne.commons.types.DBMSType; |
| 4 | + |
| 5 | +import static org.junit.Assert.assertEquals; |
| 6 | +import static org.junit.Assert.assertTrue; |
| 7 | + |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.Arrays; |
| 10 | +import java.util.HashMap; |
| 11 | +import java.util.LinkedHashMap; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Map; |
| 14 | + |
| 15 | +import org.junit.Before; |
| 16 | +import org.junit.Test; |
| 17 | +import org.ohdsi.circe.helper.ResourceHelper; |
| 18 | +import org.ohdsi.sql.SqlRender; |
| 19 | +import org.ohdsi.sql.SqlTranslate; |
| 20 | +import org.ohdsi.webapi.source.SourceRepository; |
| 21 | +import org.springframework.beans.factory.annotation.Autowired; |
| 22 | +import org.springframework.beans.factory.annotation.Value; |
| 23 | +import org.springframework.http.ResponseEntity; |
| 24 | + |
| 25 | +public class CDMResultsServiceIT extends WebApiIT { |
| 26 | + private static final String CDM_RESULTS_FILE_PATH = "/database/cdm_results.sql"; |
| 27 | + |
| 28 | + @Value("${cdmResultsService.endpoint.conceptRecordCount}") |
| 29 | + private String conceptRecordCountEndpoint; |
| 30 | + |
| 31 | + @Autowired |
| 32 | + private SourceRepository sourceRepository; |
| 33 | + |
| 34 | + @Before |
| 35 | + public void init() throws Exception { |
| 36 | + truncateTable(String.format("%s.%s", "public", "source")); |
| 37 | + resetSequence(String.format("%s.%s", "public", "source_sequence")); |
| 38 | + sourceRepository.saveAndFlush(getCdmSource()); |
| 39 | + prepareCdmSchema(); |
| 40 | + prepareResultSchema(); |
| 41 | + addCDMResults(); |
| 42 | + } |
| 43 | + |
| 44 | + private void addCDMResults() { |
| 45 | + String resultSql = SqlRender.renderSql(ResourceHelper.GetResourceAsString( |
| 46 | + CDM_RESULTS_FILE_PATH), |
| 47 | + new String[] { "results_schema" }, new String[] { RESULT_SCHEMA_NAME }); |
| 48 | + String sql = SqlTranslate.translateSql(resultSql, DBMSType.POSTGRESQL.getOhdsiDB()); |
| 49 | + jdbcTemplate.execute(sql); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void requestConceptRecordCounts_firstTime_returnsResults() { |
| 54 | + |
| 55 | + // Arrange |
| 56 | + List<Integer> conceptIds = Arrays.asList(1); |
| 57 | + Map<String, String> queryParameters = new HashMap<String, String>(); |
| 58 | + queryParameters.put("sourceName", SOURCE_KEY); |
| 59 | + |
| 60 | + List<LinkedHashMap<String, List<Integer>>> list = new ArrayList<>(); |
| 61 | + @SuppressWarnings("unchecked") |
| 62 | + Class<List<LinkedHashMap<String, List<Integer>>>> returnClass = (Class<List<LinkedHashMap<String, List<Integer>>>>)list.getClass(); |
| 63 | + |
| 64 | + // Act |
| 65 | + final ResponseEntity<List<LinkedHashMap<String, List<Integer>>>> entity = getRestTemplate().postForEntity(this.conceptRecordCountEndpoint, conceptIds, |
| 66 | + returnClass, queryParameters ); |
| 67 | + |
| 68 | + // Assertion |
| 69 | + assertOK(entity); |
| 70 | + List<LinkedHashMap<String, List<Integer>>> results = entity.getBody(); |
| 71 | + assertEquals(1, results.size()); |
| 72 | + LinkedHashMap<String, List<Integer>> resultHashMap = results.get(0); |
| 73 | + assertEquals(1, resultHashMap.size()); |
| 74 | + assertTrue(resultHashMap.containsKey("1")); |
| 75 | + List<Integer> counts = resultHashMap.get("1"); |
| 76 | + assertEquals(100, counts.get(0).intValue()); |
| 77 | + assertEquals(101, counts.get(1).intValue()); |
| 78 | + assertEquals(102, counts.get(2).intValue()); |
| 79 | + assertEquals(103, counts.get(3).intValue()); |
| 80 | + } |
| 81 | +} |
0 commit comments