Skip to content

Commit

Permalink
SMA-45: added test still work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
markpernia committed Feb 15, 2024
1 parent b3a127a commit 63f6c7b
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.sportsmatch.service;

import com.sportsmatch.dtos.RatingDTO;
import com.sportsmatch.mappers.RatingMapper;
import com.sportsmatch.models.*;
import com.sportsmatch.repositories.EventPlayerRepository;
import com.sportsmatch.repositories.RatingRepository;
import com.sportsmatch.repositories.UserEventRatingRepository;
import com.sportsmatch.services.RatingService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.security.core.Authentication;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class RatingServiceTest {

@Mock private RatingRepository ratingRepository;
@Mock private UserEventRatingRepository userEventRatingRepository;
@Mock private EventPlayerRepository eventPlayerRepository;
@Mock private RatingMapper ratingMapper;
@InjectMocks private RatingService ratingService;

@Test
void addRating() {
MockitoAnnotations.openMocks(this);
RatingDTO ratingDTO =
RatingDTO.builder()
.userTextRating("it was a long game")
.userStarRating(2)
.eventStarRating(2)
.myScore(10)
.opponentScore(9)
.build();

// Authentication and Player
Authentication authentication = mock(Authentication.class);
User player = mock(User.class);
when(authentication.getPrincipal()).thenReturn(player);

// EventPlayer
EventPlayer eventPlayer = mock(EventPlayer.class);
when(eventPlayerRepository.findEventPlayerByPlayer(any()))
.thenReturn(Optional.of(eventPlayer));

// Opponent
Event event = mock(Event.class);
when(eventPlayer.getPlayer()).thenReturn(player);
when(eventPlayer.getEvent()).thenReturn(event);
when(player.getId()).thenReturn(1L);

User opponent = mock(User.class);
EventPlayer opponentEvent = mock(EventPlayer.class);
when(opponentEvent.getPlayer()).thenReturn(opponent);
when(opponent.getId()).thenReturn(2L);

List<EventPlayer> eventPlayers = new ArrayList<>();
eventPlayers.add(opponentEvent);
eventPlayers.add(eventPlayer);

when(eventPlayerRepository.findEventPlayersByEvent(event)).thenReturn(eventPlayers);

// Mapping
Rating userRating = mock(Rating.class);
Rating eventRating = mock(Rating.class);
when(ratingMapper.toUserRatingEntity(ratingDTO)).thenReturn(userRating);
when(ratingMapper.toEventRatingEntity(ratingDTO)).thenReturn(eventRating);


ratingService.addRating(ratingDTO, authentication);
}
// TODO:
// Event Player Behaviour - error: status exception 404 Optional is empty
// User Opponent Behaviour
// Mapping Behaviour
// set Score
// Rating Behaviour

@Test
void saveUserEventRating() {
MockitoAnnotations.openMocks(this);

Authentication authentication = mock(Authentication.class);
User user = mock(User.class);
when(authentication.getPrincipal()).thenReturn(user);

UserEventRating userEventRating = mock(UserEventRating.class);

userEventRatingRepository.save(userEventRating);
ratingService.saveUserEventRating(authentication);

verify(userEventRatingRepository).save(userEventRating);
}
}

Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.sportsmatch.services;
package com.sportsmatch.service;

import com.sportsmatch.dtos.SportDTO;
import com.sportsmatch.models.Sport;
import com.sportsmatch.repositories.SportRepository;
import com.sportsmatch.services.SportServiceImp;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
Expand Down

0 comments on commit 63f6c7b

Please sign in to comment.