Skip to content
Snippets Groups Projects
Commit 7a8750f2 authored by Dominika Zemanovičová's avatar Dominika Zemanovičová
Browse files

Method getQuestionAnswers use only questionService

parent 7790c0e8
No related branches found
No related tags found
1 merge request!32M2 fix exercise
Pipeline #
......@@ -7,7 +7,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
* Represent a service for managing Answer entities
......@@ -31,21 +30,6 @@ public class AnswerService extends DomainService<Answer> {
this.repository = repository;
}
/**
* Retrieve a list of Answer entities with the specified question ID
*
* @param questionId the ID of the question to retrieve answers for
* @return a list of Answer entities with the specified question ID
* @throws EntityNotFoundException if question with questionId does not exist
*/
@Transactional(readOnly = true)
public List<Answer> findAllByQuestionId(long questionId) {
if (!getRepository().existsById(questionId)) {
throw new EntityNotFoundException("Question with id " + questionId + " not found.");
}
return repository.findByQuestionId(questionId);
}
/**
* Retrieve the Answer entity with the specified id
*
......
......@@ -7,10 +7,10 @@ import org.fuseri.model.dto.exercise.QuestionCreateDto;
import org.fuseri.model.dto.exercise.QuestionDto;
import org.fuseri.model.dto.exercise.QuestionUpdateDto;
import org.fuseri.moduleexercise.answer.AnswerMapper;
import org.fuseri.moduleexercise.answer.AnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
......@@ -21,17 +21,14 @@ import java.util.List;
@Service
public class QuestionFacade {
private final QuestionService questionService;
private final AnswerService answerService;
private final QuestionMapper questionMapper;
private final AnswerMapper answerMapper;
@Autowired
public QuestionFacade(
QuestionService questionService,
AnswerService answerService, QuestionMapper questionMapper,
QuestionService questionService, QuestionMapper questionMapper,
AnswerMapper answerMapper) {
this.questionService = questionService;
this.answerService = answerService;
this.questionMapper = questionMapper;
this.answerMapper = answerMapper;
}
......@@ -53,7 +50,8 @@ public class QuestionFacade {
* @return a List of AnswerDto objects
*/
public List<AnswerDto> getQuestionAnswers(long questionId) {
return answerMapper.toDtoList(answerService.findAllByQuestionId(questionId));
var question = questionService.find(questionId);
return answerMapper.toDtoList(new ArrayList<>(question.getAnswers()));
}
/**
......
......@@ -9,9 +9,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import static org.mockito.ArgumentMatchers.anyLong;
......@@ -62,17 +60,4 @@ class AnswerServiceTest {
Assertions.assertEquals(answer, result);
verify(answerRepository).findById(anyLong());
}
@Test
void findAllByQuestionId() {
long id = 1;
List<Answer> list = Collections.emptyList();
when(answerRepository.existsById(id)).thenReturn(true);
when(answerRepository.findByQuestionId(id)).thenReturn(list);
List<Answer> result = service.findAllByQuestionId(1L);
Assertions.assertEquals(list, result);
verify(answerRepository).findByQuestionId(id);
}
}
......@@ -102,9 +102,10 @@ class QuestionFacadeTest {
void getQuestionAnswers() {
long id = 1L;
List<Answer> answers = List.of(new Answer("BA", true, question));
question.setAnswers(new HashSet<>(answers));
List<AnswerDto> answerDtos = List.of(new AnswerDto("BA", true));
when(questionService.find(id)).thenReturn(question);
when(answerMapper.toDtoList(answers)).thenReturn(answerDtos);
when(answerService.findAllByQuestionId(id)).thenReturn(answers);
var actualAnswers = questionFacade.getQuestionAnswers(id);
assertEquals(answerDtos, actualAnswers);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment