-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from ONAV-KEA/tests
added tests for UserService and EventService
- Loading branch information
Showing
11 changed files
with
275 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
src/test/java/dk/kea/onav2ndproject_rest/service/EventServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package dk.kea.onav2ndproject_rest.service; | ||
|
||
import dk.kea.onav2ndproject_rest.dto.EventConverter; | ||
import dk.kea.onav2ndproject_rest.dto.EventDTO; | ||
import dk.kea.onav2ndproject_rest.entity.Department; | ||
import dk.kea.onav2ndproject_rest.entity.Event; | ||
import dk.kea.onav2ndproject_rest.exception.EventNotFoundException; | ||
import dk.kea.onav2ndproject_rest.repository.DepartmentRepository; | ||
import dk.kea.onav2ndproject_rest.repository.EventRepository; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.mockito.ArgumentMatchers; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.MockitoAnnotations; | ||
import org.mockito.invocation.InvocationOnMock; | ||
import org.mockito.stubbing.Answer; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.PageImpl; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
import java.util.*; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.doThrow; | ||
|
||
@SpringBootTest | ||
class EventServiceTest { | ||
|
||
@Mock | ||
private EventRepository mockedEventRepository; | ||
|
||
EventService eventService; | ||
|
||
@Autowired | ||
EventConverter eventConverter; | ||
|
||
@Mock | ||
private DepartmentRepository mockedDepartmentRepository; | ||
|
||
@Mock | ||
private DepartmentService mockedDepartmentService; | ||
|
||
@BeforeEach | ||
void init(){ | ||
MockitoAnnotations.initMocks(this); | ||
Event e1 = new Event(); | ||
e1.setId(1); | ||
e1.setName("Event1"); | ||
|
||
Event e2 = new Event( | ||
2, | ||
"Event2" | ||
); | ||
List<Event> eventList = new ArrayList<>(); | ||
|
||
eventList.add(e1); | ||
eventList.add(e2); | ||
|
||
Page<Event> eventPage = new PageImpl<>(eventList); | ||
|
||
Department department1 = new Department(); | ||
department1.setId(1); | ||
department1.setName("IT"); | ||
|
||
Department department2 = new Department(); | ||
department2.setId(2); | ||
department2.setName("Økonomi"); | ||
|
||
Mockito.when(mockedEventRepository.findAll(ArgumentMatchers.any(Pageable.class))).thenReturn(eventPage); | ||
Mockito.when(mockedEventRepository.findById(1)).thenReturn(Optional.of(e1)); | ||
Mockito.when(mockedEventRepository.findById(42)).thenReturn(Optional.empty()); | ||
Mockito.when(mockedDepartmentRepository.findById(1)).thenReturn(Optional.of(department1)); | ||
Mockito.when(mockedDepartmentRepository.findById(2)).thenReturn(Optional.of(department2)); | ||
Mockito.when(mockedDepartmentService.findById(1)).thenReturn(Optional.of(department1)); | ||
Mockito.when(mockedDepartmentService.findById(2)).thenReturn(Optional.of(department2)); | ||
doThrow(new EventNotFoundException("Event not found with id: 42")).when(mockedEventRepository).deleteById(42); | ||
|
||
Mockito.when(mockedEventRepository.save(ArgumentMatchers.any(Event.class))).thenAnswer(new Answer<Event>() { | ||
@Override | ||
public Event answer(InvocationOnMock invocation) throws Throwable { | ||
Object[] arguments = invocation.getArguments(); | ||
if (arguments.length > 0 && arguments[0] instanceof Event) { | ||
Event eventToSave = (Event) arguments[0]; | ||
if (eventToSave.getId()==0) { | ||
eventToSave.setId(3); | ||
} | ||
return eventToSave; | ||
} else { | ||
throw new IllegalArgumentException("Invalid argument type"); | ||
} | ||
} | ||
}); | ||
|
||
eventService = new EventService(mockedEventRepository, eventConverter, mockedDepartmentService); | ||
} | ||
|
||
@Test | ||
void getAllEvents() { | ||
Page<EventDTO> eventDTOList = eventService.getAllEvents(Pageable.unpaged()); | ||
assertEquals("Event1", eventDTOList.get().findFirst().get().name()); | ||
assertEquals("Event2", eventDTOList.get().skip(1).findFirst().get().name()); | ||
} | ||
|
||
@Test | ||
void getEventById() { | ||
EventDTO eventDTO = eventService.getEventById(1); | ||
assertEquals("Event1", eventDTO.name()); | ||
assertThrows(EventNotFoundException.class, () -> eventService.getEventById(42)); | ||
} | ||
|
||
@Test | ||
void createEvent() { | ||
Event event = new Event(0, "Event3"); | ||
event.setDepartments(new HashSet<>(Arrays.asList(mockedDepartmentService.findById(1).get(), mockedDepartmentService.findById(2).get()))); | ||
EventDTO resultEventDTO = eventService.createEvent(eventConverter.toDTO(event)); | ||
assertEquals(3, resultEventDTO.id()); | ||
} | ||
|
||
@Test | ||
void updateEvent() { | ||
Event event = new Event(1, "UpdatedEvent"); | ||
event.setDepartments(new HashSet<>(Arrays.asList(mockedDepartmentService.findById(1).get(), mockedDepartmentService.findById(2).get()))); | ||
EventDTO resultEventDTO = eventService.updateEvent(1, eventConverter.toDTO(event)); | ||
assertEquals(1, resultEventDTO.id()); | ||
assertEquals("UpdatedEvent", resultEventDTO.name()); | ||
assertThrows(EventNotFoundException.class, () -> eventService.updateEvent(42, resultEventDTO)); | ||
} | ||
|
||
@Test | ||
void deleteEventById() { | ||
assertThrows(EventNotFoundException.class, () -> eventService.deleteEventById(42)); | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
src/test/java/dk/kea/onav2ndproject_rest/service/UserServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package dk.kea.onav2ndproject_rest.service; | ||
|
||
import dk.kea.onav2ndproject_rest.JwtTokenManager; | ||
import dk.kea.onav2ndproject_rest.config.SecurityConfiguration; | ||
import dk.kea.onav2ndproject_rest.dto.UserConverter; | ||
import dk.kea.onav2ndproject_rest.entity.User; | ||
import dk.kea.onav2ndproject_rest.exception.UserNotFoundException; | ||
import dk.kea.onav2ndproject_rest.repository.UserRepository; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.ArgumentMatchers; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.MockitoAnnotations; | ||
import org.mockito.stubbing.Answer; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.doThrow; | ||
|
||
@SpringBootTest | ||
class UserServiceTest { | ||
@Mock | ||
private UserRepository mockedUserRepository; | ||
|
||
UserService userService; | ||
|
||
@Autowired | ||
UserConverter userConverter; | ||
|
||
@BeforeEach | ||
void init(){ | ||
PasswordEncoder pw = SecurityConfiguration.passwordEncoder(); | ||
MockitoAnnotations.initMocks(this); | ||
User u1 = new User(); | ||
u1.setId(1); | ||
u1.setName("User1"); | ||
u1.setPassword(pw.encode("1234")); | ||
|
||
User u2 = new User(); | ||
u2.setId(2); | ||
u2.setName("User2"); | ||
u2.setPassword(pw.encode("1234")); | ||
|
||
List<User> userList = List.of(u1, u2); | ||
|
||
Mockito.when(mockedUserRepository.findAll()).thenReturn(userList); | ||
Mockito.when(mockedUserRepository.findById(1)).thenReturn(java.util.Optional.of(u1)); | ||
Mockito.when(mockedUserRepository.findById(2)).thenReturn(java.util.Optional.of(u2)); | ||
doThrow(new UserNotFoundException("User does not exist with id: " + 3)).when(mockedUserRepository).findById(42); | ||
|
||
Mockito.when(mockedUserRepository.save(ArgumentMatchers.any(User.class))).thenAnswer((Answer) invocation -> { | ||
Object[] args = invocation.getArguments(); | ||
if (args.length > 0 && args[0] instanceof User){ | ||
User u = (User) args[0]; | ||
if(u.getId()==0){ | ||
u.setId(3); | ||
} | ||
return u; | ||
} else{ | ||
throw new IllegalArgumentException("Wrong argument"); | ||
} | ||
}); | ||
|
||
userService = new UserService(mockedUserRepository, userConverter); | ||
|
||
} | ||
|
||
@Test | ||
void getAllUsers() { | ||
assertEquals(2, userService.findAll().size()); | ||
} | ||
|
||
@Test | ||
void getUserById() { | ||
assertEquals("User1", userService.findById(1).get().getName()); | ||
assertEquals("User2", userService.findById(2).get().getName()); | ||
assertThrows(UserNotFoundException.class, () -> userService.findById(42)); | ||
} | ||
|
||
@Test | ||
void createUser() { | ||
PasswordEncoder pw = SecurityConfiguration.passwordEncoder(); | ||
User u3 = new User(); | ||
u3.setId(3); | ||
u3.setName("User3"); | ||
u3.setPassword(pw.encode("1234")); | ||
assertEquals(3, userService.save(u3).getId()); | ||
} | ||
|
||
@Test | ||
void deleteUser() { | ||
User u3 = new User(); | ||
u3.setId(3); | ||
u3.setName("User3"); | ||
userService.delete(u3); | ||
assertEquals(2, userService.findAll().size()); | ||
assertThrows(UserNotFoundException.class, () -> userService.findById(42)); | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
spring.datasource.url=jdbc:h2:mem:test;MODE=MySQL | ||
secret=onavkrergodtogjeghedderanderstellerogdenherkeyersimpelthengodhejhejhejhejhejhejhejhejhejhejhejhejhejhejhejhejhejhejhejhejhejhejhejhejhejhejhejhejhejhejhejhejhejhejhejhejhejhejhej |