diff --git a/src/main/java/cz/cvut/kbss/study/rest/PatientRecordController.java b/src/main/java/cz/cvut/kbss/study/rest/PatientRecordController.java index 5e8dae63..d675128a 100644 --- a/src/main/java/cz/cvut/kbss/study/rest/PatientRecordController.java +++ b/src/main/java/cz/cvut/kbss/study/rest/PatientRecordController.java @@ -42,6 +42,7 @@ import java.io.InputStream; import java.net.URI; import java.util.*; +import java.util.stream.Collectors; import java.util.stream.Stream; @RestController @@ -83,6 +84,16 @@ public List getRecords( return result.getContent(); } + @PreAuthorize("hasRole('" + SecurityConstants.ROLE_ADMIN + "') or @securityUtils.isMemberOfInstitution(#institutionKey)") + @GetMapping(value="availablePhases", produces = MediaType.APPLICATION_JSON_VALUE) + public Set getAvailableRecordPhases(@RequestParam(value = "institution", required = false) String institutionKey){ + List records = recordService.findAll(); + return records.stream() + .map(PatientRecord::getPhase) + .collect(Collectors.toSet()); + } + + @PreAuthorize( "hasRole('" + SecurityConstants.ROLE_ADMIN + "') or @securityUtils.isMemberOfInstitution(#institutionKey)") @GetMapping(value = "/export", produces = {MediaType.APPLICATION_JSON_VALUE, Constants.MEDIA_TYPE_EXCEL}) diff --git a/src/test/java/cz/cvut/kbss/study/rest/PatientRecordControllerTest.java b/src/test/java/cz/cvut/kbss/study/rest/PatientRecordControllerTest.java index 250d0ce7..cc2ea06b 100644 --- a/src/test/java/cz/cvut/kbss/study/rest/PatientRecordControllerTest.java +++ b/src/test/java/cz/cvut/kbss/study/rest/PatientRecordControllerTest.java @@ -39,9 +39,7 @@ import org.springframework.test.web.servlet.MvcResult; import java.time.LocalDate; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; +import java.util.*; import static cz.cvut.kbss.study.environment.util.ContainsSameEntities.containsSameEntities; import static org.hamcrest.MatcherAssert.assertThat; @@ -97,6 +95,24 @@ public void getRecordThrowsNotFoundWhenReportIsNotFound() throws Exception { verify(patientRecordServiceMock).findByKey(key); } + @Test + public void testGetAvailableRecordPhases() throws Exception{ + Set phases = new HashSet<>(Arrays.asList(RecordPhase.completed, RecordPhase.valid)); + PatientRecord record1 = Generator.generatePatientRecord(user); + record1.setPhase(RecordPhase.completed); + PatientRecord record2 = Generator.generatePatientRecord(user); + record2.setPhase(RecordPhase.valid); + List records = Arrays.asList(record1, record2); + + when(patientRecordServiceMock.findAll()).thenReturn(records); + + final MvcResult result = mockMvc.perform(get("/records/availablePhases")) + .andReturn(); + assertEquals(HttpStatus.OK, HttpStatus.valueOf(result.getResponse().getStatus())); + final Set body = objectMapper.readValue(result.getResponse().getContentAsString(), new TypeReference<>() {}); + assertEquals(phases, body); + } + @Test public void getRecordReturnsFoundRecord() throws Exception { final String key = "12345";