Skip to content

Commit

Permalink
[kbss-cvut/record-manager-ui#184] Updated tests for importRecords
Browse files Browse the repository at this point in the history
  • Loading branch information
palagdan committed Jul 20, 2024
1 parent fe50696 commit b1a5100
Showing 1 changed file with 45 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cz.cvut.kbss.study.rest;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import cz.cvut.kbss.study.dto.PatientRecordDto;
import cz.cvut.kbss.study.dto.RecordImportResult;
import cz.cvut.kbss.study.environment.generator.Generator;
Expand Down Expand Up @@ -32,6 +33,7 @@
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.web.servlet.MvcResult;

import java.time.LocalDate;
Expand All @@ -51,10 +53,7 @@
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@ExtendWith(MockitoExtension.class)
Expand Down Expand Up @@ -302,47 +301,82 @@ void exportRecordsExportsRecordsForProvidedInstitutionForSpecifiedPeriod() throw
Pageable.unpaged());
}

// @Test
// void importRecordsJsonImportsSpecifiedRecordsAndReturnsImportResult() throws Exception {
// final List<PatientRecord> records =
// List.of(Generator.generatePatientRecord(user), Generator.generatePatientRecord(user));
// final RecordImportResult importResult = new RecordImportResult(records.size());
// importResult.setImportedCount(records.size());
// when(patientRecordServiceMock.importRecords(anyList())).thenReturn(importResult);
//
// final MvcResult mvcResult = mockMvc.perform(
// post("/records/import/json").content(toJson(records)).contentType(MediaType.MULTIPART_FORM_DATA_VALUE)).andReturn();
// final RecordImportResult result = readValue(mvcResult, RecordImportResult.class);
// assertEquals(importResult.getTotalCount(), result.getTotalCount());
// assertEquals(importResult.getImportedCount(), result.getImportedCount());
// assertThat(importResult.getErrors(), anyOf(nullValue(), empty()));
// @SuppressWarnings("unchecked")
// final ArgumentCaptor<List<PatientRecord>> captor = ArgumentCaptor.forClass(List.class);
// verify(patientRecordServiceMock).importRecords(captor.capture());
// assertEquals(records.size(), captor.getValue().size());
// }

@Test
void importRecordsImportsSpecifiedRecordsAndReturnsImportResult() throws Exception {
void importRecordsJsonImportsSpecifiedRecordsAndReturnsImportResult() throws Exception {
final List<PatientRecord> records =
List.of(Generator.generatePatientRecord(user), Generator.generatePatientRecord(user));
final RecordImportResult importResult = new RecordImportResult(records.size());
importResult.setImportedCount(records.size());

when(patientRecordServiceMock.importRecords(anyList())).thenReturn(importResult);

MockMultipartFile file = new MockMultipartFile("file", "records.json",
MediaType.MULTIPART_FORM_DATA_VALUE, toJson(records).getBytes());

String jsonContent = new String(file.getBytes());
System.out.println("Received JSON content: " + jsonContent);

final MvcResult mvcResult = mockMvc.perform(
post("/records/import").content(toJson(records)).contentType(MediaType.APPLICATION_JSON)).andReturn();
multipart("/records/import/json")
.file(file)
.contentType(MediaType.MULTIPART_FORM_DATA_VALUE)
).andReturn();

final RecordImportResult result = readValue(mvcResult, RecordImportResult.class);

// Assertions
assertEquals(importResult.getTotalCount(), result.getTotalCount());
assertEquals(importResult.getImportedCount(), result.getImportedCount());
assertThat(importResult.getErrors(), anyOf(nullValue(), empty()));


@SuppressWarnings("unchecked")
final ArgumentCaptor<List<PatientRecord>> captor = ArgumentCaptor.forClass(List.class);
verify(patientRecordServiceMock).importRecords(captor.capture());
assertEquals(records.size(), captor.getValue().size());
}


@Test
void importRecordsImportsSpecifiedRecordsWithSpecifiedPhaseAndReturnsImportResult() throws Exception {
void importRecordsJsonImportsSpecifiedRecordsWithSpecifiedPhaseAndReturnsImportResult() throws Exception {
final List<PatientRecord> records =
List.of(Generator.generatePatientRecord(user), Generator.generatePatientRecord(user));
final RecordImportResult importResult = new RecordImportResult(records.size());
importResult.setImportedCount(records.size());
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").content(toJson(records)).contentType(MediaType.APPLICATION_JSON)
mockMvc.perform(post("/records/import/json").content(toJson(records)).contentType(MediaType.MULTIPART_FORM_DATA_VALUE)
.param("phase", targetPhase.getIri())).andExpect(status().isOk());
verify(patientRecordServiceMock).importRecords(anyList(), eq(targetPhase));
}

@Test
void importRecordsReturnsConflictWhenServiceThrowsRecordAuthorNotFound() throws Exception {
void importRecordsJsonReturnsConflictWhenServiceThrowsRecordAuthorNotFound() throws Exception {
final List<PatientRecord> records =
List.of(Generator.generatePatientRecord(user), Generator.generatePatientRecord(user));
when(patientRecordServiceMock.importRecords(anyList())).thenThrow(RecordAuthorNotFoundException.class);

mockMvc.perform(post("/records/import").content(toJson(records)).contentType(MediaType.APPLICATION_JSON))
mockMvc.perform(post("/records/import/json").content(toJson(records)).contentType(MediaType.MULTIPART_FORM_DATA_VALUE))
.andExpect(status().isConflict());
}

Expand Down

0 comments on commit b1a5100

Please sign in to comment.