Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
pionas committed Dec 16, 2023
1 parent 8dfb992 commit 95b5ef9
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@NoArgsConstructor
@Getter
@Setter
class AnswerDto {
class AnswerResponseDto {

private UUID id;
private String content;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
@NoArgsConstructor
@Getter
@Setter
class QuestionDto {
class QuestionResponseDto {

private UUID id;
private String content;
private List<AnswerDto> answers;
private List<AnswerResponseDto> answers;
}
4 changes: 2 additions & 2 deletions src/main/java/info/pionas/quiz/api/quiz/QuizApiMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ interface QuizApiMapper {

QuizResponseDto map(Quiz quiz);

QuestionDto map(Question question);
QuestionResponseDto map(Question question);

AnswerDto map(Answer answer);
AnswerResponseDto map(Answer answer);

UpdateQuestion map(UpdateQuestionDto updateQuestionDto);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ class QuizResponseDto {
private UUID id;
private String title;
private String description;
private List<QuestionDto> questions = new ArrayList<>();
private List<QuestionResponseDto> questions = new ArrayList<>();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package info.pionas.quiz.api.quiz;

import info.pionas.quiz.domain.quiz.QuizService;
import info.pionas.quiz.domain.quiz.api.QuizService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package info.pionas.quiz.domain.quiz;

import info.pionas.quiz.domain.quiz.api.NewQuestion;
import info.pionas.quiz.domain.quiz.api.NewQuiz;
import info.pionas.quiz.domain.quiz.api.Quiz;
import info.pionas.quiz.domain.quiz.api.UpdateQuestion;
import info.pionas.quiz.domain.quiz.api.*;
import info.pionas.quiz.domain.shared.UuidGenerator;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
package info.pionas.quiz.domain.quiz;

import info.pionas.quiz.domain.quiz.api.Quiz;
package info.pionas.quiz.domain.quiz.api;

import java.util.Optional;
import java.util.UUID;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
package info.pionas.quiz.domain.quiz;

import info.pionas.quiz.domain.quiz.api.NewQuestion;
import info.pionas.quiz.domain.quiz.api.NewQuiz;
import info.pionas.quiz.domain.quiz.api.Quiz;
import info.pionas.quiz.domain.quiz.api.UpdateQuestion;
package info.pionas.quiz.domain.quiz.api;

import java.util.UUID;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package info.pionas.quiz.domain.quiz;
package info.pionas.quiz.infrastructure.database.quiz;

import info.pionas.quiz.domain.quiz.api.Answer;
import info.pionas.quiz.domain.quiz.api.Question;
import info.pionas.quiz.domain.quiz.api.Quiz;
import info.pionas.quiz.infrastructure.database.quiz.AnswerEntity;
import info.pionas.quiz.infrastructure.database.quiz.QuestionEntity;
import info.pionas.quiz.infrastructure.database.quiz.QuizEntity;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package info.pionas.quiz.domain.quiz;
package info.pionas.quiz.infrastructure.database.quiz;

import info.pionas.quiz.infrastructure.database.quiz.QuizEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package info.pionas.quiz.domain.quiz;
package info.pionas.quiz.infrastructure.database.quiz;

import info.pionas.quiz.domain.quiz.api.Quiz;
import info.pionas.quiz.domain.quiz.api.QuizRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

Expand Down
59 changes: 29 additions & 30 deletions src/test/java/info/pionas/quiz/api/quiz/QuizRestControllerIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void should_create_quiz() {
assertThat(quizResponseDto).isNotNull();
assertThat(quizResponseDto.getTitle()).isEqualTo(newQuizDto.getTitle());
assertThat(quizResponseDto.getDescription()).isEqualTo(newQuizDto.getDescription());
List<QuestionDto> questions = quizResponseDto.getQuestions();
List<QuestionResponseDto> questions = quizResponseDto.getQuestions();
assertThat(questions).hasSize(2);
final var quizEntity = dbUtil.em().find(QuizEntity.class, quizResponseDto.getId());
assertThat(quizEntity).isNotNull();
Expand All @@ -80,7 +80,7 @@ void should_throw_forbidden_when_try_to_create_quiz() throws IOException {
final var errors = StreamSupport
.stream(errorJson.get("errors").spliterator(), false)
.toList();
assertThat(errors.get(0).asText()).isEqualTo("Access Denied");
assertThat(errors.getFirst().asText()).isEqualTo("Access Denied");
}

@Test
Expand All @@ -102,7 +102,7 @@ void should_throw_bad_request_when_try_to_create_quiz() throws IOException {
final var errors = StreamSupport
.stream(errorJson.get("errors").spliterator(), false)
.toList();
assertThat(errors.get(0).asText()).isEqualTo("title: must not be blank");
assertThat(errors.getFirst().asText()).isEqualTo("title: must not be blank");
}

@Test
Expand Down Expand Up @@ -139,19 +139,19 @@ void should_add_question_to_quiz() {
assertThat(quizResponseDto.getDescription()).isEqualTo("This is first question without answers");
final var questions = quizResponseDto.getQuestions();
assertThat(questions).hasSize(1);
final var questionDto = questions.get(0);
final var questionDto = questions.getFirst();
assertThat(questionDto).isNotNull();
assertThat(questionDto.getContent()).isEqualTo(newQuestionDto.getContent());
final var answers = questionDto.getAnswers();
assertThat(answers).hasSize(2);
AnswerDto answerDto1 = answers.get(0);
assertThat(answerDto1).isNotNull();
assertThat(answerDto1.getContent()).isEqualTo(newQuestionDto.getAnswers().get(0).getContent());
assertThat(answerDto1.getCorrect()).isEqualTo(newQuestionDto.getAnswers().get(0).getCorrect());
AnswerDto answerDto2 = answers.get(1);
assertThat(answerDto2).isNotNull();
assertThat(answerDto2.getContent()).isEqualTo(newQuestionDto.getAnswers().get(1).getContent());
assertThat(answerDto2.getCorrect()).isEqualTo(newQuestionDto.getAnswers().get(1).getCorrect());
AnswerResponseDto answerResponseDto1 = answers.getFirst();
assertThat(answerResponseDto1).isNotNull();
assertThat(answerResponseDto1.getContent()).isEqualTo(newQuestionDto.getAnswers().getFirst().getContent());
assertThat(answerResponseDto1.getCorrect()).isEqualTo(newQuestionDto.getAnswers().getFirst().getCorrect());
AnswerResponseDto answerResponseDto2 = answers.get(1);
assertThat(answerResponseDto2).isNotNull();
assertThat(answerResponseDto2.getContent()).isEqualTo(newQuestionDto.getAnswers().get(1).getContent());
assertThat(answerResponseDto2.getCorrect()).isEqualTo(newQuestionDto.getAnswers().get(1).getCorrect());
final var quizEntity = dbUtil.em().find(QuizEntity.class, quizResponseDto.getId());
assertThat(quizEntity).isNotNull();
assertThat(quizEntity.getTitle()).isEqualTo("First question");
Expand All @@ -178,7 +178,7 @@ void should_throw_forbidden_when_try_to_add_question_to_quiz() throws IOExceptio
final var errors = StreamSupport
.stream(errorJson.get("errors").spliterator(), false)
.toList();
assertThat(errors.get(0).asText()).isEqualTo("Access Denied");
assertThat(errors.getFirst().asText()).isEqualTo("Access Denied");
}

@Test
Expand All @@ -200,7 +200,7 @@ void should_throw_bad_request_when_try_to_add_answer_to_quiz() throws IOExceptio
final var errors = StreamSupport
.stream(errorJson.get("errors").spliterator(), false)
.toList();
assertThat(errors.get(0).asText()).isEqualTo("content: must not be blank");
assertThat(errors.getFirst().asText()).isEqualTo("content: must not be blank");
}

@Test
Expand All @@ -222,7 +222,7 @@ void should_throw_not_found_when_try_add_question_but_quiz_by_id_not_exist() thr
final var errors = StreamSupport
.stream(errorJson.get("errors").spliterator(), false)
.toList();
assertThat(errors.get(0).asText()).isEqualTo("Quiz by ID b4a63897-60f7-4e94-846e-e199d8734144 not exist");
assertThat(errors.getFirst().asText()).isEqualTo("Quiz by ID b4a63897-60f7-4e94-846e-e199d8734144 not exist");
}

@Test
Expand Down Expand Up @@ -277,7 +277,7 @@ void should_throw_forbidden_when_try_to_delete_question_from_quiz() throws IOExc
final var errors = StreamSupport
.stream(errorJson.get("errors").spliterator(), false)
.toList();
assertThat(errors.get(0).asText()).isEqualTo("Access Denied");
assertThat(errors.getFirst().asText()).isEqualTo("Access Denied");
}

@Test
Expand All @@ -286,7 +286,6 @@ void should_throw_not_found_when_try_delete_question_but_quiz_by_id_not_exist()
final var user = new User("admin", "admin", List.of(Role.ROLE_ADMIN));
//when
final var response = webTestClient.delete().uri("/api/v1/quiz/b4a63897-60f7-4e94-846e-e199d8734144/question/ce703f5b-274c-4398-b855-d461887c7ed5")
.accept(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.header(HttpHeaders.AUTHORIZATION, "Bearer " + generateToken(user))
.exchange()
Expand All @@ -298,7 +297,7 @@ void should_throw_not_found_when_try_delete_question_but_quiz_by_id_not_exist()
final var errors = StreamSupport
.stream(errorJson.get("errors").spliterator(), false)
.toList();
assertThat(errors.get(0).asText()).isEqualTo("Quiz by ID b4a63897-60f7-4e94-846e-e199d8734144 not exist");
assertThat(errors.getFirst().asText()).isEqualTo("Quiz by ID b4a63897-60f7-4e94-846e-e199d8734144 not exist");
}

@Test
Expand Down Expand Up @@ -336,19 +335,19 @@ void should_update_question_from_quiz() {
assertThat(quizResponseDto.getDescription()).isEqualTo("This is first question without answers");
final var questions = quizResponseDto.getQuestions();
assertThat(questions).hasSize(1);
final var questionDto = questions.get(0);
final var questionDto = questions.getFirst();
assertThat(questionDto).isNotNull();
assertThat(questionDto.getContent()).isEqualTo(updateQuestionDto.getContent());
final var answers = questionDto.getAnswers();
assertThat(answers).hasSize(2);
AnswerDto answerDto1 = answers.get(0);
assertThat(answerDto1).isNotNull();
assertThat(answerDto1.getContent()).isEqualTo(updateQuestionDto.getAnswers().get(0).getContent());
assertThat(answerDto1.getCorrect()).isEqualTo(updateQuestionDto.getAnswers().get(0).getCorrect());
AnswerDto answerDto2 = answers.get(1);
assertThat(answerDto2).isNotNull();
assertThat(answerDto2.getContent()).isEqualTo(updateQuestionDto.getAnswers().get(1).getContent());
assertThat(answerDto2.getCorrect()).isEqualTo(updateQuestionDto.getAnswers().get(1).getCorrect());
AnswerResponseDto answerResponseDto1 = answers.getFirst();
assertThat(answerResponseDto1).isNotNull();
assertThat(answerResponseDto1.getContent()).isEqualTo(updateQuestionDto.getAnswers().getFirst().getContent());
assertThat(answerResponseDto1.getCorrect()).isEqualTo(updateQuestionDto.getAnswers().getFirst().getCorrect());
AnswerResponseDto answerResponseDto2 = answers.get(1);
assertThat(answerResponseDto2).isNotNull();
assertThat(answerResponseDto2.getContent()).isEqualTo(updateQuestionDto.getAnswers().get(1).getContent());
assertThat(answerResponseDto2.getCorrect()).isEqualTo(updateQuestionDto.getAnswers().get(1).getCorrect());
final var questionEntity = dbUtil.em().find(QuestionEntity.class, questionId);
assertThat(questionEntity).isNotNull();
assertThat(questionEntity.getContent()).isEqualTo(updateQuestionDto.getContent());
Expand All @@ -375,7 +374,7 @@ void should_throw_forbidden_when_try_to_update_question_to_quiz() throws IOExcep
final var errors = StreamSupport
.stream(errorJson.get("errors").spliterator(), false)
.toList();
assertThat(errors.get(0).asText()).isEqualTo("Access Denied");
assertThat(errors.getFirst().asText()).isEqualTo("Access Denied");
}

@Test
Expand All @@ -399,7 +398,7 @@ void should_throw_bad_request_when_try_to_update_question_to_quiz() throws IOExc
final var errors = StreamSupport
.stream(errorJson.get("errors").spliterator(), false)
.toList();
assertThat(errors.get(0).asText()).isEqualTo("content: must not be blank");
assertThat(errors.getFirst().asText()).isEqualTo("content: must not be blank");
}

@Test
Expand All @@ -422,7 +421,7 @@ void should_throw_not_found_when_quiz_by_id_not_exist() throws IOException {
final var errors = StreamSupport
.stream(errorJson.get("errors").spliterator(), false)
.toList();
assertThat(errors.get(0).asText()).isEqualTo("Quiz by ID b4a63897-60f7-4e94-846e-e199d8734144 not exist");
assertThat(errors.getFirst().asText()).isEqualTo("Quiz by ID b4a63897-60f7-4e94-846e-e199d8734144 not exist");
}

private NewQuizDto getNewQuizDto() {
Expand Down

0 comments on commit 95b5ef9

Please sign in to comment.