Skip to content
Snippets Groups Projects
Commit fff07e34 authored by Danae Savvidi's avatar Danae Savvidi :laughing:
Browse files

add tests

parent bdb49019
No related branches found
No related tags found
2 merge requests!229Version 2.2.1,!221Resolve "Importing groups"
......@@ -36,6 +36,7 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMock
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.security.test.context.support.WithUserDetails;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
......@@ -217,6 +218,24 @@ class StudentGroupControllerTest {
verify(groupService).changeCapacities(MODULE_ID, 2);
}
@Test
@WithUserDetails("username")
void importStudentGroupsTest() throws Exception {
when(authService.canImportStudentGroups(anyLong())).thenReturn(true);
doNothing().when(groupService)
.importStudentGroups(anyLong(), any());
MockMultipartFile file = new MockMultipartFile("file", "importfile".getBytes());
mockMvc.perform(
multipart("/group/{moduleId}/import", MODULE_ID)
.file(file).with(csrf())
.contentType(MediaType.MULTIPART_FORM_DATA))
.andExpect(status().is3xxRedirection())
.andExpect(redirectedUrl("/module/" + MODULE_ID + "/groups"));;
verify(authService).canImportStudentGroups(MODULE_ID);
verify(groupService).importStudentGroups(MODULE_ID, file);
}
@Test
@WithUserDetails("username")
void generateGroupsAllowsIfCanCreateGroup() throws Exception {
......
......@@ -18,8 +18,12 @@
package nl.tudelft.submit.csv;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.nio.file.Path;
import java.nio.file.Paths;
......@@ -45,6 +49,30 @@ class CSVServiceTest {
private final String fileName = "test.csv";
@Test
void parseContentsWithHeaderTest() throws IOException {
String csvData = "Name,Age,Gender\nJohn,25,Male\nJane,30,Female";
InputStream inputStream = new ByteArrayInputStream(csvData.getBytes());
List<List<String>> result = service.parseContents(inputStream, true);
assertEquals(2, result.size());
assertEquals(List.of("John", "25", "Male"), result.get(0));
assertEquals(List.of("Jane", "30", "Female"), result.get(1));
}
@Test
public void parseContentsWithoutHeaderTest() throws IOException {
String csvData = "John,25,Male\nJane,30,Female";
InputStream inputStream = new ByteArrayInputStream(csvData.getBytes());
List<List<String>> result = service.parseContents(inputStream, false);
assertEquals(2, result.size());
assertEquals(List.of("John", "25", "Male"), result.get(0));
assertEquals(List.of("Jane", "30", "Female"), result.get(1));
}
@Test
void writeReadCSV() throws Exception {
// prepare the data
......
......@@ -874,6 +874,14 @@ class AuthorizationServiceTest {
assertThat(service.canCreateGroup(MODULE_ID)).isEqualTo(expected);
}
@ParameterizedTest
@CsvSource({ "TEACHER,true", "HEAD_TA,true", "TA,false", "STUDENT,false", ",false" })
@WithUserDetails("username")
void canImportStudentGroups(RoleDetailsDTO.TypeEnum role, boolean expected) {
mockRole(role);
assertThat(service.canImportStudentGroups(MODULE_ID)).isEqualTo(expected);
}
@ParameterizedTest
@CsvSource({ "TEACHER,true", "HEAD_TA,true", "TA,true", "STUDENT,false", ",false" })
@WithUserDetails("username")
......
......@@ -23,6 +23,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.*;
......@@ -33,6 +34,7 @@ import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.mock.web.MockMultipartFile;
import application.test.TestSubmitApplication;
import nl.tudelft.labracore.api.StudentGroupControllerApi;
......@@ -40,6 +42,7 @@ import nl.tudelft.labracore.api.dto.*;
import nl.tudelft.librador.dto.view.View;
import nl.tudelft.submit.cache.PersonCacheManager;
import nl.tudelft.submit.cache.StudentGroupCacheManager;
import nl.tudelft.submit.csv.CSVService;
import nl.tudelft.submit.dto.id.GroupId;
import nl.tudelft.submit.dto.view.labracore.SubmitGroupDetailsDTO;
import nl.tudelft.submit.dto.view.labracore.SubmitGroupSummaryDTO;
......@@ -47,6 +50,7 @@ import nl.tudelft.submit.enums.SwitchAction;
import nl.tudelft.submit.exception.GroupSwitchingException;
import nl.tudelft.submit.model.*;
import nl.tudelft.submit.model.note.GroupNote;
import nl.tudelft.submit.repository.SubmitGroupRepository;
import nl.tudelft.submit.repository.SwitchEventRepository;
import nl.tudelft.submit.repository.note.GroupNoteRepository;
import reactor.core.publisher.Flux;
......@@ -58,6 +62,7 @@ class StudentGroupServiceTest {
private final Long GROUP_ID = 5L;
private final Long MODULE_ID = 42L;
private final Long EDITION_ID = 33L;
private final Long COURSE_ID = 139L;
private final Long MEMBER_ID = 56123L;
private final Long PERSON_ID = 98451L;
......@@ -85,6 +90,9 @@ class StudentGroupServiceTest {
@Autowired
private GroupNoteRepository groupNoteRepository;
@MockBean
private SubmitGroupRepository groupRepository;
@MockBean
private ModuleDivisionService moduleDivisionService;
......@@ -94,6 +102,12 @@ class StudentGroupServiceTest {
@MockBean
private SubmissionService submissionService;
@MockBean
private CSVService csvService;
@MockBean
private ModuleService moduleService;
@Test
void getGroupDetails() {
StudentGroupDetailsDTO dto = new StudentGroupDetailsDTO().id(GROUP_ID).name("Test group")
......@@ -219,6 +233,39 @@ class StudentGroupServiceTest {
Mockito.verify(groupApi).addGroupMembers(GROUP_ID, usernames);
}
@Test
public void importStudentGroupsTest() throws IOException {
MockMultipartFile file = new MockMultipartFile("file", "test.csv", "text/csv", "content".getBytes());
List<List<String>> lines = Arrays.asList(
List.of("dduck;Group1"),
List.of("mmouse;Group1"),
List.of("batman;Group2"));
PersonSummaryDTO person1 = new PersonSummaryDTO().id(1L).username("dduck").displayName("Donald Duck");
PersonSummaryDTO person2 = new PersonSummaryDTO().id(2L).username("mmouse").displayName("Mickey Mouse");
PersonSummaryDTO person3 = new PersonSummaryDTO().id(3L).username("batman").displayName("Batman");
when(csvService.parseContents(any(), eq(true))).thenReturn(lines);
when(moduleService.getModuleById(eq(MODULE_ID))).thenReturn(
new ModuleDetailsDTO().id(MODULE_ID).edition(new EditionSummaryDTO().id(EDITION_ID)));
when(personCache.get(eq("dduck"))).thenReturn(person1);
when(personCache.get(eq("mmouse"))).thenReturn(person2);
when(personCache.get(eq("batman"))).thenReturn(person3);
when(personCache.get(eq(1L))).thenReturn(Optional.ofNullable(person1));
when(personCache.get(eq(2L))).thenReturn(Optional.ofNullable(person2));
when(personCache.get(eq(3L))).thenReturn(Optional.ofNullable(person3));
when(groupApi.getGroupForPersonAndModule(anyLong(), anyLong())).thenReturn(Mono.empty());
when(groupApi.addGroup(any(StudentGroupCreateDTO.class))).thenReturn(Mono.just(5L));
when(groupRepository.save(any(SubmitGroup.class))).thenReturn(SubmitGroup.builder().id(5L).build());
groupService.importStudentGroups(MODULE_ID, file);
verify(csvService).parseContents(any(), eq(true));
verify(moduleService, times(3)).getModuleById(eq(MODULE_ID));
verify(personCache, times(6)).get(anyString());
verify(personCache, times(3)).get(anyLong());
verify(groupRepository, times(2)).save(any(SubmitGroup.class));
}
@Test
void addPeopleToLockedGroup() {
List<String> usernames = List.of("hpotter", "rweasley", "hgranger");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment