GitMaterialUpdater extends MaterialUpdater<GitMaterial> { @Override public GitMaterial getLatestMaterialVersion(GitMaterial gitMaterial) { boolean repositoryExists = this.gitService.repositoryExists(gitMaterial); if (!repositoryExists) { this.fileManagementService.deleteDirectoryRecursively(gitMaterial.getDestination()); this.gitService.cloneRepository(gitMaterial); if (!gitMaterial.getErrorMessage().isEmpty()) { this.fileManagementService.deleteDirectoryRecursively(gitMaterial.getDestination()); return gitMaterial; } } this.gitService.fetchLatestCommit(gitMaterial); return gitMaterial; } GitMaterialUpdater();  GitMaterialUpdater(IGitService gitService, IFileManagementService fileManagementService); @Override GitMaterial getLatestMaterialVersion(GitMaterial gitMaterial); @Override boolean areMaterialsSameVersion(GitMaterial latestMaterial, GitMaterial dbMaterial);  }
@Test public void getLatestMaterialVersion_successfullyFetchedLatest_allFieldsUpdated() { GitMaterial expectedResult = new GitMaterial(); expectedResult.setCommitId("commitId"); expectedResult.setAuthorName("authorName"); expectedResult.setAuthorEmail("authorEmail"); expectedResult.setComments("comment"); expectedResult.setErrorMessage(""); Mockito.when(this.mockedGitService.repositoryExists(expectedResult)).thenReturn(true); Mockito.when(this.mockedGitService.fetchLatestCommit(expectedResult)).thenReturn(expectedResult); GitMaterial actualResult = this.gitMaterialUpdater.getLatestMaterialVersion(expectedResult); Assert.assertEquals(actualResult.getId(), expectedResult.getId()); Assert.assertNotNull(actualResult.getCommitId()); Assert.assertNotNull(actualResult.getAuthorName()); Assert.assertNotNull(actualResult.getAuthorEmail()); Assert.assertNotNull(actualResult.getComments()); }
@Test public void getLatestMaterialVersion_couldNotCloneRepository_errorMessage() { GitMaterial gitMaterial = new GitMaterial(); gitMaterial.setErrorMessage("errorMessage"); Mockito.when(this.mockedGitService.repositoryExists(gitMaterial)).thenReturn(false); Mockito.when(this.mockedFileManagementService.deleteDirectoryRecursively(Mockito.anyString())).thenReturn(null); Mockito.when(this.mockedGitService.cloneRepository(gitMaterial)).thenReturn(gitMaterial); String expectedResult = "errorMessage"; String actualResult = this.gitMaterialUpdater.getLatestMaterialVersion(gitMaterial).getErrorMessage(); Assert.assertEquals(expectedResult, actualResult); }
@Test public void getLatestMaterialVersion_couldNotFetchLatest_errorMessage() { GitMaterial gitMaterial = new GitMaterial(); gitMaterial.setErrorMessage("errorMessage"); Mockito.when(this.mockedGitService.repositoryExists(gitMaterial)).thenReturn(true); Mockito.when(this.mockedGitService.fetchLatestCommit(gitMaterial)).thenReturn(gitMaterial); String expectedResult = "errorMessage"; String actualResult = this.gitMaterialUpdater.getLatestMaterialVersion(gitMaterial).getErrorMessage(); Assert.assertEquals(expectedResult, actualResult); }