Skip to content

Commit

Permalink
[kbss-cvut/record-manager-ui#184] Fix test importRecordsJsonImportsSp…
Browse files Browse the repository at this point in the history
…ecifiedRecordsWithSpecifiedPhaseAndReturnsImportResult
  • Loading branch information
blcham committed Jul 20, 2024
1 parent b1a5100 commit f1255af
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,17 @@ public class PatientRecordController extends BaseController {
private final ExcelRecordConverter excelRecordConverter;
private final RestTemplate restTemplate;
private final ConfigReader configReader;
private ObjectMapper objectMapper;

public PatientRecordController(PatientRecordService recordService, ApplicationEventPublisher eventPublisher, ExcelRecordConverter excelRecordConverter, RestTemplate restTemplate, ConfigReader configReader) {
public PatientRecordController(PatientRecordService recordService, ApplicationEventPublisher eventPublisher,
ExcelRecordConverter excelRecordConverter, RestTemplate restTemplate,
ConfigReader configReader, ObjectMapper objectMapper) {
this.recordService = recordService;
this.eventPublisher = eventPublisher;
this.excelRecordConverter = excelRecordConverter;
this.restTemplate = restTemplate;
this.configReader = configReader;
this.objectMapper = objectMapper;
}

@PreAuthorize("hasRole('" + SecurityConstants.ROLE_ADMIN + "') or @securityUtils.isMemberOfInstitution(#institutionKey)")
Expand Down Expand Up @@ -167,7 +171,7 @@ public RecordImportResult importRecordsJson(@RequestPart("file") MultipartFile f
if(file.isEmpty())
throw new IllegalArgumentException("Cannot import records, missing input file");
try {
records = new ObjectMapper().readValue(file.getBytes(), new TypeReference<List<PatientRecord>>(){});
records = objectMapper.readValue(file.getBytes(), new TypeReference<List<PatientRecord>>(){});
} catch (IOException e) {
throw new RuntimeException("Failed to parse JSON content", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public interface PatientRecordService extends BaseService<PatientRecord> {
* current user is set as the record's author.
*
* @param records Records to import
* @param targetPhase Phase to be set to all imported records.
* @return Instance representing the import result
* @throws cz.cvut.kbss.study.exception.RecordAuthorNotFoundException Thrown when importing a record whose author
* does not exist in this application instance's
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Page;
Expand Down Expand Up @@ -65,6 +66,9 @@ public class PatientRecordControllerTest extends BaseControllerTestRunner {
@Mock
private ApplicationEventPublisher eventPublisherMock;

@Spy
private ObjectMapper objectMapper = Environment.getObjectMapper();

@InjectMocks
private PatientRecordController controller;

Expand Down Expand Up @@ -365,8 +369,16 @@ void importRecordsJsonImportsSpecifiedRecordsWithSpecifiedPhaseAndReturnsImportR
final RecordPhase targetPhase = RecordPhase.values()[Generator.randomInt(0, RecordPhase.values().length)];
when(patientRecordServiceMock.importRecords(anyList(), any(RecordPhase.class))).thenReturn(importResult);

mockMvc.perform(post("/records/import/json").content(toJson(records)).contentType(MediaType.MULTIPART_FORM_DATA_VALUE)
.param("phase", targetPhase.getIri())).andExpect(status().isOk());
MockMultipartFile file = new MockMultipartFile("file", "records.json",
MediaType.MULTIPART_FORM_DATA_VALUE, toJson(records).getBytes());

mockMvc.perform(
multipart("/records/import/json")
.file(file)
.contentType(MediaType.MULTIPART_FORM_DATA_VALUE)
.param("phase", targetPhase.getIri())
).andExpect(status().isOk());

verify(patientRecordServiceMock).importRecords(anyList(), eq(targetPhase));
}

Expand Down

0 comments on commit f1255af

Please sign in to comment.