@Test public void testGetRandomQuizzes(){ long ctgId = createQuizzes("a", "b", "c"); Set<String> questions = new HashSet<>(); for(int i=0; i<50; i++){ List<Quiz> quizzes = quizService.getRandomQuizzes(2, ctgId); assertEquals(2, quizzes.size()); Quiz first = quizzes.get(0); Quiz second = quizzes.get(1); assertNotEquals(first.getQuestion(), second.getQuestion()); questions.add(first.getQuestion()); questions.add(second.getQuestion()); } assertEquals(3, questions.size()); assertTrue(questions.contains("a")); assertTrue(questions.contains("b")); assertTrue(questions.contains("c")); }
public List<Quiz> getRandomQuizzes(int n, long categoryId){ TypedQuery<Long> sizeQuery= em.createQuery( "select count(q) from Quiz q where q.subCategory.parent.id=?1", Long.class); sizeQuery.setParameter(1, categoryId); long size = sizeQuery.getSingleResult(); if(n > size){ throw new IllegalArgumentException("Cannot choose " + n + " unique quizzes out of the " + size + " existing"); } Random random = new Random(); List<Quiz> quizzes = new ArrayList<>(); Set<Integer> chosen = new HashSet<>(); while(chosen.size() < n) { int k = random.nextInt((int)size); if(chosen.contains(k)){ continue; } chosen.add(k); TypedQuery<Quiz> query = em.createQuery( "select q from Quiz q where q.subCategory.parent.id=?1", Quiz.class); query.setParameter(1, categoryId); query.setMaxResults(1); query.setFirstResult(k); quizzes.add(query.getSingleResult()); } return quizzes; }
QuizService { public List<Quiz> getRandomQuizzes(int n, long categoryId){ TypedQuery<Long> sizeQuery= em.createQuery( "select count(q) from Quiz q where q.subCategory.parent.id=?1", Long.class); sizeQuery.setParameter(1, categoryId); long size = sizeQuery.getSingleResult(); if(n > size){ throw new IllegalArgumentException("Cannot choose " + n + " unique quizzes out of the " + size + " existing"); } Random random = new Random(); List<Quiz> quizzes = new ArrayList<>(); Set<Integer> chosen = new HashSet<>(); while(chosen.size() < n) { int k = random.nextInt((int)size); if(chosen.contains(k)){ continue; } chosen.add(k); TypedQuery<Quiz> query = em.createQuery( "select q from Quiz q where q.subCategory.parent.id=?1", Quiz.class); query.setParameter(1, categoryId); query.setMaxResults(1); query.setFirstResult(k); quizzes.add(query.getSingleResult()); } return quizzes; } }
QuizService { public List<Quiz> getRandomQuizzes(int n, long categoryId){ TypedQuery<Long> sizeQuery= em.createQuery( "select count(q) from Quiz q where q.subCategory.parent.id=?1", Long.class); sizeQuery.setParameter(1, categoryId); long size = sizeQuery.getSingleResult(); if(n > size){ throw new IllegalArgumentException("Cannot choose " + n + " unique quizzes out of the " + size + " existing"); } Random random = new Random(); List<Quiz> quizzes = new ArrayList<>(); Set<Integer> chosen = new HashSet<>(); while(chosen.size() < n) { int k = random.nextInt((int)size); if(chosen.contains(k)){ continue; } chosen.add(k); TypedQuery<Quiz> query = em.createQuery( "select q from Quiz q where q.subCategory.parent.id=?1", Quiz.class); query.setParameter(1, categoryId); query.setMaxResults(1); query.setFirstResult(k); quizzes.add(query.getSingleResult()); } return quizzes; }  }
QuizService { public List<Quiz> getRandomQuizzes(int n, long categoryId){ TypedQuery<Long> sizeQuery= em.createQuery( "select count(q) from Quiz q where q.subCategory.parent.id=?1", Long.class); sizeQuery.setParameter(1, categoryId); long size = sizeQuery.getSingleResult(); if(n > size){ throw new IllegalArgumentException("Cannot choose " + n + " unique quizzes out of the " + size + " existing"); } Random random = new Random(); List<Quiz> quizzes = new ArrayList<>(); Set<Integer> chosen = new HashSet<>(); while(chosen.size() < n) { int k = random.nextInt((int)size); if(chosen.contains(k)){ continue; } chosen.add(k); TypedQuery<Quiz> query = em.createQuery( "select q from Quiz q where q.subCategory.parent.id=?1", Quiz.class); query.setParameter(1, categoryId); query.setMaxResults(1); query.setFirstResult(k); quizzes.add(query.getSingleResult()); } return quizzes; }  List<Quiz> getRandomQuizzes(int n, long categoryId); long createQuiz(
            long subCategoryId,
            String question,
            String firstAnswer,
            String secondAnswer,
            String thirdAnswer,
            String fourthAnswer,
            int indexOfCorrectAnswer
    ); List<Quiz> getQuizzes(); Quiz getQuiz(long id); }
QuizService { public List<Quiz> getRandomQuizzes(int n, long categoryId){ TypedQuery<Long> sizeQuery= em.createQuery( "select count(q) from Quiz q where q.subCategory.parent.id=?1", Long.class); sizeQuery.setParameter(1, categoryId); long size = sizeQuery.getSingleResult(); if(n > size){ throw new IllegalArgumentException("Cannot choose " + n + " unique quizzes out of the " + size + " existing"); } Random random = new Random(); List<Quiz> quizzes = new ArrayList<>(); Set<Integer> chosen = new HashSet<>(); while(chosen.size() < n) { int k = random.nextInt((int)size); if(chosen.contains(k)){ continue; } chosen.add(k); TypedQuery<Quiz> query = em.createQuery( "select q from Quiz q where q.subCategory.parent.id=?1", Quiz.class); query.setParameter(1, categoryId); query.setMaxResults(1); query.setFirstResult(k); quizzes.add(query.getSingleResult()); } return quizzes; }  List<Quiz> getRandomQuizzes(int n, long categoryId); long createQuiz(
            long subCategoryId,
            String question,
            String firstAnswer,
            String secondAnswer,
            String thirdAnswer,
            String fourthAnswer,
            int indexOfCorrectAnswer
    ); List<Quiz> getQuizzes(); Quiz getQuiz(long id);  }