Skip to content

Commit

Permalink
[kbss-cvut/record-manager-ui#203] Add new endpoint for available stat…
Browse files Browse the repository at this point in the history
…us list
  • Loading branch information
palagdan committed Aug 2, 2024
1 parent 3111977 commit 1f4e2d8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/main/java/cz/cvut/kbss/study/rest/PatientRecordController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -83,6 +84,16 @@ public List<PatientRecordDto> getRecords(
return result.getContent();
}

@PreAuthorize("hasRole('" + SecurityConstants.ROLE_ADMIN + "') or @securityUtils.isMemberOfInstitution(#institutionKey)")
@GetMapping(value="availablePhases", produces = MediaType.APPLICATION_JSON_VALUE)
public Set<RecordPhase> getAvailableRecordPhases(@RequestParam(value = "institution", required = false) String institutionKey){
List<PatientRecord> 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})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -97,6 +95,24 @@ public void getRecordThrowsNotFoundWhenReportIsNotFound() throws Exception {
verify(patientRecordServiceMock).findByKey(key);
}

@Test
public void testGetAvailableRecordPhases() throws Exception{
Set<RecordPhase> 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<PatientRecord> 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<RecordPhase> body = objectMapper.readValue(result.getResponse().getContentAsString(), new TypeReference<>() {});
assertEquals(phases, body);
}

@Test
public void getRecordReturnsFoundRecord() throws Exception {
final String key = "12345";
Expand Down

0 comments on commit 1f4e2d8

Please sign in to comment.