### Question:
ApiRepoVersionServiceImpl implements ApiRepoVersionService { @Override public Optional<ApiRepoVersion> findLatestStableVersion(Integer apiRepoId) { List<ApiRepoVersion> allVersions = apiRepoVersionDao.findAllByApiRepoId(apiRepoId); if(allVersions.isEmpty()) { return Optional.empty(); } List<ApiRepoVersion> filtered = allVersions .stream() .filter(apiRepoVersion -> !apiRepoVersion.getVersion().equals(Constants.MASTER)) .collect(Collectors.toList()); if(filtered.isEmpty()) { return Optional.empty(); } filtered.sort(new Comparator<ApiRepoVersion>() { @Override public int compare(ApiRepoVersion version1, ApiRepoVersion version2) { return Version.compare(Version.parseVersion(version2.getVersion()), Version.parseVersion(version1.getVersion())); } }); return Optional.of(filtered.get(0)); }  @Override Optional<ApiRepoVersion> findById(Integer apiRepoVersionId); @Override Optional<ApiRepoVersion> findLatestStableVersion(Integer apiRepoId); @Override Optional<ApiRepoVersion> findMasterVersion(Integer apiRepoId);  }### Answer:
@Test public void findLatestVersion_empty_versions() { assertThat(apiRepoVersionService.findLatestStableVersion(Integer.MAX_VALUE)).isEmpty(); }
@Test public void findLatestVersion_only_has_master() { Integer apiRepoId = Integer.MAX_VALUE; ApiRepoVersion master = new ApiRepoVersion(); master.setApiRepoId(apiRepoId); master.setName("a api repo"); master.setVersion("master"); master.setGitTagName("refs/heads/master"); master.setCreateUserId(1); master.setCreateTime(LocalDateTime.now()); master.setLastPublishTime(LocalDateTime.now()); apiRepoVersionDao.save(master); assertThat(apiRepoVersionService.findLatestStableVersion(apiRepoId)).isEmpty(); }
@Test public void findLatestVersion_two_versions() { Integer apiRepoId = Integer.MAX_VALUE; ApiRepoVersion version1 = new ApiRepoVersion(); version1.setApiRepoId(apiRepoId); version1.setName("a api repo"); version1.setVersion("0.1.0"); version1.setGitTagName("v0.1.0"); version1.setCreateUserId(1); version1.setLastPublishTime(LocalDateTime.now()); version1.setCreateTime(LocalDateTime.now()); apiRepoVersionDao.save(version1); ApiRepoVersion version2 = new ApiRepoVersion(); version2.setApiRepoId(apiRepoId); version2.setName("a api repo"); version2.setVersion("0.2.0"); version2.setGitTagName("v0.2.0"); version2.setCreateUserId(1); version2.setCreateTime(LocalDateTime.now()); version2.setLastPublishTime(LocalDateTime.now()); apiRepoVersionDao.save(version2); ApiRepoVersion master = new ApiRepoVersion(); master.setApiRepoId(apiRepoId); master.setName("a api repo"); master.setVersion("master"); master.setGitTagName("refs/heads/master"); master.setCreateUserId(1); master.setCreateTime(LocalDateTime.now()); master.setLastPublishTime(LocalDateTime.now()); apiRepoVersionDao.save(master); assertThat(apiRepoVersionService.findLatestStableVersion(apiRepoId).get().getVersion()).isEqualTo("0.2.0"); }