Skip to content

Instantly share code, notes, and snippets.

@rmuhamedgaliev
Created February 19, 2022 21:51
Show Gist options
  • Save rmuhamedgaliev/2f7c120d74ffda2efcb91920b2b641fc to your computer and use it in GitHub Desktop.
Save rmuhamedgaliev/2f7c120d74ffda2efcb91920b2b641fc to your computer and use it in GitHub Desktop.
Coursework review
package course2.coursework_2.servise;
import course2.coursework_2.Question;
import course2.coursework_2.exception.QuestionEmptyCollectionException;
import course2.coursework_2.exception.QuestionExistsException;
import course2.coursework_2.exception.QuestionNotFoundException;
import org.springframework.stereotype.Service;
import java.util.*;
@Service
public class JavaQuestionService implements QuestionService {
private final Map<Integer, Question> questions = new HashMap<>();
@Override
public Question add(String question, String answer) {
int nextQuestionId = getNextId();
Question newQuestion = new Question(question, answer);
if (questions.containsValue(newQuestion)) {
throw new QuestionExistsException();
}
questions.put(nextQuestionId, newQuestion);
return newQuestion;
}
private int getNextId() {
return questions.size() + 1;
}
@Override
public Question find(Question question) {
return questions.get(findQuestion(question));
}
@Override
public Question remove(Question question) {
questions.remove(findQuestion(question));
return question;
}
@Override
public Collection<Question> getAll() {
if (questions.values().isEmpty()) {
throw new QuestionEmptyCollectionException();
}
return questions.values();
}
private Integer findQuestion(Question question) {
return questions.entrySet().stream()
.filter(entry -> Objects.equals(entry.getValue(), question))
.map(Map.Entry::getKey)
.findFirst()
.orElseThrow(QuestionNotFoundException::new);
}
@Override
public Question getRandomQuestion() {
Random random = new Random();
int randomNumber = random.nextInt(questions.size());
return questions.get(randomNumber);
}
}
package course2.coursework_2;
import course2.coursework_2.exception.QuestionEmptyCollectionException;
import course2.coursework_2.exception.QuestionExistsException;
import course2.coursework_2.exception.QuestionNotFoundException;
import course2.coursework_2.servise.JavaQuestionService;
import course2.coursework_2.servise.QuestionService;
import org.junit.jupiter.api.Test;
import static course2.coursework_2.JavaExamConstants.ANSWER_1;
import static course2.coursework_2.JavaExamConstants.ANSWER_2;
import static course2.coursework_2.JavaExamConstants.ANSWER_3;
import static course2.coursework_2.JavaExamConstants.ANSWER_4;
import static course2.coursework_2.JavaExamConstants.ANSWER_5;
import static course2.coursework_2.JavaExamConstants.QUESTION_1;
import static course2.coursework_2.JavaExamConstants.QUESTION_2;
import static course2.coursework_2.JavaExamConstants.QUESTION_3;
import static course2.coursework_2.JavaExamConstants.QUESTION_4;
import static course2.coursework_2.JavaExamConstants.QUESTION_5;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
class JavaQuestionServiceTest {
@Test
public void testAdd() {
QuestionService questionService = new JavaQuestionService();
Question question = new Question(QUESTION_1, ANSWER_1);
questionService.add(question.getQuestion(), question.getAnswer());
assertEquals(question, questionService.find(question));
}
@Test
void testFind() {
QuestionService questionService = new JavaQuestionService();
Question question = new Question(QUESTION_1, ANSWER_1);
questionService.add(question.getQuestion(), question.getAnswer());
assertNotNull(questionService.find(question));
}
@Test
void testGetAll() {
QuestionService questionService = new JavaQuestionService();
Question question = new Question(QUESTION_1, ANSWER_1);
questionService.add(question.getQuestion(), question.getAnswer());
assertTrue(questionService.getAll().size() > 0);
}
@Test
void testThrowQuestionExistsException() {
QuestionService questionService = new JavaQuestionService();
Question question = new Question(QUESTION_5, ANSWER_5);
questionService.add(question.getQuestion(), question.getAnswer());
assertThrows(QuestionExistsException.class,
() -> questionService.add(QUESTION_5, ANSWER_5));
}
@Test
void testNotThrowExceptionRemove() {
QuestionService questionService = new JavaQuestionService();
Question question = new Question(QUESTION_5, ANSWER_5);
questionService.add(question.getQuestion(), question.getAnswer());
assertDoesNotThrow(() -> questionService.remove(question));
}
@Test
void testQuestionNotFoundExceptionRemove() {
QuestionService questionService = new JavaQuestionService();
Question question = new Question(QUESTION_5, ANSWER_5);
questionService.add(question.getQuestion(), question.getAnswer());
assertThrows(QuestionNotFoundException.class,
() -> questionService.remove(new Question(QUESTION_1, ANSWER_1)));
}
@Test
void testQuestionNotFoundException() {
QuestionService questionService = new JavaQuestionService();
Question question = new Question(QUESTION_5, ANSWER_5);
questionService.add(question.getQuestion(), question.getAnswer());
assertThrows(QuestionNotFoundException.class,
() -> questionService.find(new Question(QUESTION_1, ANSWER_1)));
}
@Test
void testQuestionEmptyCollectionException() {
QuestionService questionService = new JavaQuestionService();
assertThrows(QuestionEmptyCollectionException.class,
questionService::getAll);
}
@Test
void getRandomQuestion() {
QuestionService questionService = new JavaQuestionService();
questionService.add(QUESTION_1, ANSWER_1);
questionService.add(QUESTION_2, ANSWER_2);
questionService.add(QUESTION_3, ANSWER_3);
questionService.add(QUESTION_4, ANSWER_4);
questionService.add(QUESTION_5, ANSWER_5);
assertNotNull(questionService.getRandomQuestion());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment