<commit_before>package at.ac.tuwien.inso.service_tests.course_recommendation;

import at.ac.tuwien.inso.entity.*;
import at.ac.tuwien.inso.repository.*;
import at.ac.tuwien.inso.service.course_recommendation.filters.*;
import at.ac.tuwien.inso.service.course_recommendation.impl.*;
import org.junit.*;
import org.junit.runner.*;
import org.mockito.*;
import org.mockito.runners.*;

import java.util.*;
import java.util.stream.*;

import static java.util.Arrays.*;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class RecommendationServiceTests {

    @Mock
    private Student student;

    @Mock
    private Subject subject;

    @Mock
    private Semester semester;

    @Mock
    private CourseRepository courseRepository;

    @Mock
    private SemesterRepository semesterRepository;

    @Mock
    private TagFrequencyScorer tagFrequencyScorer;

    private List<CourseRelevanceFilter> filters = asList(
            mock(CourseRelevanceFilter.class),
            mock(CourseRelevanceFilter.class)
    );

    @InjectMocks
    private RecommendationServiceImpl recommendationService;

    private HashMap<String, Semester> semesters = new HashMap<String, Semester>() {
        {
            put("WS16", new Semester(2016, SemesterType.WinterSemester));
        }
    };

    private List<Course> courses = asList(
            new Course(subject, semesters.get("WS16")).addTags(new Tag("tag1")),
            new Course(subject, semesters.get("WS16")).addTags(new Tag("tag2")),
            new Course(subject, semesters.get("WS16")).addTags(new Tag("tag3")),
            new Course(subject, semesters.get("WS16")).addTags(new Tag("tag4")),
            new Course(subject, semesters.get("WS16")).addTags(new Tag("tag5"))
    );

    private Map<Course, Double> scoredCoursesByTagFrequency = new HashMap<Course, Double>() {
        {
            put(courses.get(0), 3.1);
            put(courses.get(2), 7.4);
            put(courses.get(1), 4.3);
            put(courses.get(3), 3.8);
            put(courses.get(4), 5.3);
        }
    };

    @Before
    public void setUp() throws Exception {
        when(courseRepository.findAllRecommendableForStudent(student)).thenReturn(courses);
        when(semesterRepository.findFirstByOrderByIdDesc()).thenReturn(semesters.get("WS16"));
        when(tagFrequencyScorer.score(courses, student)).thenReturn(scoredCoursesByTagFrequency);

        filters.forEach(it -> when(it.filter(courses, student)).thenReturn(courses));
        recommendationService.setCourseRelevanceFilters(filters);
    }

    @Test
    public void itRecommendsCoursesByTagScoring() throws Exception {
        List<Course> recommendedCourses = recommendationService.recommendCourses(student);

        List<Course> expected = asList(
                courses.get(2), courses.get(4), courses.get(1), courses.get(3), courses.get(0)
        );

        assertEquals(expected, recommendedCourses);
    }

    @Test
    public void itFiltersCoursesBeforeRecommendingThem() throws Exception {
        List<Course> coursesAfterFilter1 = courses.stream().skip(1).collect(Collectors.toList());
        when(filters.get(0).filter(courses, student)).thenReturn(coursesAfterFilter1);

        List<Course> coursesAfterFilter2 = coursesAfterFilter1.stream().skip(1).collect(Collectors.toList());
        when(filters.get(1).filter(coursesAfterFilter1, student)).thenReturn(coursesAfterFilter2);

        List<Course> recommendedCourses = recommendationService.recommendCourses(student);

        assertThat(recommendedCourses, not(hasItems(courses.get(0), courses.get(1))));
    }
}
<commit_msg>Fixes RecommendationServiceTests<commit_after>package at.ac.tuwien.inso.service_tests.course_recommendation;

import at.ac.tuwien.inso.entity.*;
import at.ac.tuwien.inso.repository.CourseRepository;
import at.ac.tuwien.inso.repository.SemesterRepository;
import at.ac.tuwien.inso.service.course_recommendation.filters.CourseRelevanceFilter;
import at.ac.tuwien.inso.service.course_recommendation.impl.MandatoryCourseScorer;
import at.ac.tuwien.inso.service.course_recommendation.impl.RecommendationServiceImpl;
import at.ac.tuwien.inso.service.course_recommendation.impl.TagFrequencyScorer;
import at.ac.tuwien.inso.service.course_recommendation.normalization.CourseNormalizer;
import at.ac.tuwien.inso.service.course_recommendation.user_based.UserBasedCourseScorer;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static java.util.Arrays.asList;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class RecommendationServiceTests {

    @Mock
    private Student student;

    @Mock
    private Subject subject;

    @Mock
    private Semester semester;

    @Mock
    private CourseRepository courseRepository;

    @Mock
    private SemesterRepository semesterRepository;

    @Mock
    private TagFrequencyScorer tagFrequencyScorer;

    @Mock
    private MandatoryCourseScorer mandatoryCourseScorer;

    @Mock
    private UserBasedCourseScorer userBasedCourseScorer;

    @Mock
    private CourseNormalizer courseNormalizer;

    private List<CourseRelevanceFilter> filters = asList(
            mock(CourseRelevanceFilter.class),
            mock(CourseRelevanceFilter.class)
    );

    @InjectMocks
    private RecommendationServiceImpl recommendationService;

    private HashMap<String, Semester> semesters = new HashMap<String, Semester>() {
        {
            put("WS16", new Semester(2016, SemesterType.WinterSemester));
        }
    };

    private List<Course> courses = asList(
            new Course(subject, semesters.get("WS16")).addTags(new Tag("tag1")),
            new Course(subject, semesters.get("WS16")).addTags(new Tag("tag2")),
            new Course(subject, semesters.get("WS16")).addTags(new Tag("tag3")),
            new Course(subject, semesters.get("WS16")).addTags(new Tag("tag4")),
            new Course(subject, semesters.get("WS16")).addTags(new Tag("tag5"))
    );

    private Map<Course, Double> scoredCoursesByTagFrequency = new HashMap<Course, Double>() {
        {
            put(courses.get(0), 3.1);
            put(courses.get(2), 7.4);
            put(courses.get(1), 4.3);
            put(courses.get(3), 3.8);
            put(courses.get(4), 5.3);
        }
    };

    @Before
    public void setUp() throws Exception {
        when(courseRepository.findAllRecommendableForStudent(student)).thenReturn(courses);
        when(semesterRepository.findFirstByOrderByIdDesc()).thenReturn(semesters.get("WS16"));
        when(tagFrequencyScorer.score(courses, student)).thenReturn(scoredCoursesByTagFrequency);

        filters.forEach(it -> when(it.filter(courses, student)).thenReturn(courses));
        recommendationService.setCourseRelevanceFilters(filters);
    }

    @Test
    public void itRecommendsCoursesByTagScoring() throws Exception {
        List<Course> recommendedCourses = recommendationService.recommendCourses(student);

        List<Course> expected = asList(
                courses.get(2), courses.get(4), courses.get(1), courses.get(3), courses.get(0)
        );

        assertEquals(expected, recommendedCourses);
    }

    @Test
    public void itFiltersCoursesBeforeRecommendingThem() throws Exception {
        List<Course> coursesAfterFilter1 = courses.stream().skip(1).collect(Collectors.toList());
        when(filters.get(0).filter(courses, student)).thenReturn(coursesAfterFilter1);

        List<Course> coursesAfterFilter2 = coursesAfterFilter1.stream().skip(1).collect(Collectors.toList());
        when(filters.get(1).filter(coursesAfterFilter1, student)).thenReturn(coursesAfterFilter2);

        List<Course> recommendedCourses = recommendationService.recommendCourses(student);

        assertThat(recommendedCourses, not(hasItems(courses.get(0), courses.get(1))));
    }
}

{'fraction_non_alphanumeric': 0.11792059139143914, 'fraction_numerical': 0.013115535948491713, 'mean_word_length': 3.911007025761124, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3115517', 'n_tokens_mistral': 2627, 'n_tokens_neox': 2465, 'n_words': 398}