### Question:
RepositoryServiceImpl implements RepositoryService { @Override public List<Repository> findCanAccessRepositoriesByUserId(Integer userId) { List<Repository> projects = repositoryAuthorizationDao.findAllByUserId(userId).stream().flatMap(projectAuthoriation -> { return repositoryDao.findById(projectAuthoriation.getRepositoryId()).stream(); }).sorted(Comparator.comparing(Repository::getLastActiveTime).reversed()).collect(Collectors.toList()); projects.forEach(each -> { String loginName = userService.findById(each.getCreateUserId()).map(UserInfo::getLoginName).orElse(null); each.setCreateUserName(loginName); }); return projects; }  @Override Optional<Repository> find(String userName, String repositoryName); @Override Repository createRepository(UserInfo user, Repository project); @Override List<Repository> findCanAccessRepositoriesByUserId(Integer userId); @Override Optional<GitCommitInfo> findLatestCommitInfo(Repository repository, String relativeFilePath); @Override Optional<Repository> findById(Integer repositoryId);  }### Answer:
@Test public void find_can_access_projects_created_no_data() { List<Repository> projects = repositoryService.findCanAccessRepositoriesByUserId(1); assertThat(projects).isEmpty(); }
@Test public void find_can_access_projects_created_success() { UserInfo userInfo = new UserInfo(); userInfo.setLoginName("user_name"); userInfo.setAvatarUrl("avatar_url"); userInfo.setEmail("email"); userInfo.setMobile("mobile"); userInfo.setCreateTime(LocalDateTime.now()); Integer userId = userDao.save(userInfo).getId(); Repository project = new Repository(); project.setName("project_name"); project.setIsPublic(true); project.setDescription("description"); project.setLastActiveTime(LocalDateTime.now()); project.setCreateUserId(userId); project.setCreateTime(LocalDateTime.now()); Integer savedProjectId = repositoryDao.save(project).getId(); RepositoryAuthorization auth = new RepositoryAuthorization(); auth.setRepositoryId(savedProjectId); auth.setUserId(userId); auth.setAccessLevel(AccessLevel.ADMIN); auth.setCreateTime(LocalDateTime.now()); auth.setCreateUserId(userId); repositoryAuthorizationDao.save(auth); List<Repository> projects = repositoryService.findCanAccessRepositoriesByUserId(userId); assertThat(projects).hasSize(1).allMatch(each -> each.getCreateUserName().equals("user_name")); projects = repositoryService.findCanAccessRepositoriesByUserId(userId + 1); assertThat(projects).isEmpty(); }
@Test public void find_can_access_projects_order_by_last_active_time_desc() { Repository project = new Repository(); project.setName("project_name"); project.setIsPublic(true); project.setDescription("description"); project.setLastActiveTime(LocalDateTime.now()); project.setCreateUserId(1); project.setCreateTime(LocalDateTime.now()); project.setCreateUserName("user_name"); Integer savedProjectId = repositoryDao.save(project).getId(); RepositoryAuthorization auth = new RepositoryAuthorization(); auth.setRepositoryId(savedProjectId); auth.setUserId(1); auth.setAccessLevel(AccessLevel.ADMIN); auth.setCreateTime(LocalDateTime.now()); auth.setCreateUserId(1); repositoryAuthorizationDao.save(auth); project = new Repository(); project.setName("project_name_2"); project.setIsPublic(true); project.setDescription("description"); project.setLastActiveTime(LocalDateTime.now().plusSeconds(1)); project.setCreateUserId(1); project.setCreateTime(LocalDateTime.now()); project.setCreateUserName("user_name"); savedProjectId = repositoryDao.save(project).getId(); auth = new RepositoryAuthorization(); auth.setRepositoryId(savedProjectId); auth.setUserId(1); auth.setAccessLevel(AccessLevel.ADMIN); auth.setCreateTime(LocalDateTime.now()); auth.setCreateUserId(1); repositoryAuthorizationDao.save(auth); List<Repository> projects = repositoryService.findCanAccessRepositoriesByUserId(1); assertThat(projects).hasSize(2).isSortedAccordingTo(Comparator.comparing(Repository::getLastActiveTime).reversed()); }