|
| 1 | +package de.tum.cit.aet.helios.releaseinfo.release.github; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertSame; |
| 5 | +import static org.mockito.ArgumentMatchers.any; |
| 6 | +import static org.mockito.ArgumentMatchers.anyString; |
| 7 | +import static org.mockito.Mockito.mock; |
| 8 | +import static org.mockito.Mockito.never; |
| 9 | +import static org.mockito.Mockito.verify; |
| 10 | +import static org.mockito.Mockito.when; |
| 11 | + |
| 12 | +import de.tum.cit.aet.helios.commit.Commit; |
| 13 | +import de.tum.cit.aet.helios.commit.CommitRepository; |
| 14 | +import de.tum.cit.aet.helios.commit.github.GitHubCommitSyncService; |
| 15 | +import de.tum.cit.aet.helios.github.GitHubService; |
| 16 | +import de.tum.cit.aet.helios.gitrepo.GitRepoRepository; |
| 17 | +import de.tum.cit.aet.helios.gitrepo.GitRepository; |
| 18 | +import de.tum.cit.aet.helios.releaseinfo.release.Release; |
| 19 | +import de.tum.cit.aet.helios.releaseinfo.release.ReleaseRepository; |
| 20 | +import de.tum.cit.aet.helios.releaseinfo.releasecandidate.ReleaseCandidate; |
| 21 | +import de.tum.cit.aet.helios.releaseinfo.releasecandidate.ReleaseCandidateRepository; |
| 22 | +import java.io.IOException; |
| 23 | +import java.util.Optional; |
| 24 | +import org.junit.jupiter.api.BeforeEach; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 27 | +import org.kohsuke.github.GHCommit; |
| 28 | +import org.kohsuke.github.GHRef; |
| 29 | +import org.kohsuke.github.GHRelease; |
| 30 | +import org.kohsuke.github.GHRepository; |
| 31 | +import org.kohsuke.github.GHTagObject; |
| 32 | +import org.mockito.ArgumentCaptor; |
| 33 | +import org.mockito.InjectMocks; |
| 34 | +import org.mockito.Mock; |
| 35 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 36 | + |
| 37 | +@ExtendWith(MockitoExtension.class) |
| 38 | +class GitHubReleaseSyncServiceTest { |
| 39 | + |
| 40 | + @Mock private ReleaseRepository releaseRepository; |
| 41 | + @Mock private GitHubReleaseConverter releaseConverter; |
| 42 | + @Mock private GitHubCommitSyncService commitSyncService; |
| 43 | + @Mock private GitRepoRepository gitRepoRepository; |
| 44 | + @Mock private ReleaseCandidateRepository releaseCandidateRepository; |
| 45 | + @Mock private CommitRepository commitRepository; |
| 46 | + @Mock private GitHubService gitHubService; |
| 47 | + |
| 48 | + @Mock private GHRelease ghRelease; |
| 49 | + @Mock private GHRepository ghRepository; |
| 50 | + @Mock private GHRepository currentRepository; |
| 51 | + @Mock private GHRef ref; |
| 52 | + @Mock private GHRef.GHObject refObject; |
| 53 | + |
| 54 | + @InjectMocks private GitHubReleaseSyncService service; |
| 55 | + |
| 56 | + private GitRepository repository; |
| 57 | + private Release release; |
| 58 | + |
| 59 | + @BeforeEach |
| 60 | + void setUp() throws IOException { |
| 61 | + repository = new GitRepository(); |
| 62 | + repository.setRepositoryId(42L); |
| 63 | + repository.setNameWithOwner("owner/repo"); |
| 64 | + |
| 65 | + release = new Release(); |
| 66 | + release.setId(1L); |
| 67 | + |
| 68 | + when(ghRelease.getId()).thenReturn(1L); |
| 69 | + when(ghRelease.getTagName()).thenReturn("v1.0.0"); |
| 70 | + when(ghRepository.getFullName()).thenReturn("owner/repo"); |
| 71 | + |
| 72 | + when(gitRepoRepository.findByNameWithOwner("owner/repo")).thenReturn(repository); |
| 73 | + when(releaseRepository.findById(1L)).thenReturn(Optional.of(release)); |
| 74 | + when(releaseRepository.saveAndFlush(release)).thenReturn(release); |
| 75 | + when(releaseCandidateRepository.findByRepositoryRepositoryIdAndName(42L, "v1.0.0")) |
| 76 | + .thenReturn(Optional.empty()); |
| 77 | + |
| 78 | + when(gitHubService.getRepository("owner/repo")).thenReturn(currentRepository); |
| 79 | + when(currentRepository.getRef("tags/v1.0.0")).thenReturn(ref); |
| 80 | + when(ref.getObject()).thenReturn(refObject); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void processRelease_annotatedTag_resolvesCommitAndSavesReleaseCandidate() throws IOException { |
| 85 | + Commit commit = new Commit(); |
| 86 | + commit.setSha("commit-sha"); |
| 87 | + |
| 88 | + when(refObject.getType()).thenReturn("tag"); |
| 89 | + when(refObject.getSha()).thenReturn("tag-sha"); |
| 90 | + GHTagObject tagObject = mock(GHTagObject.class); |
| 91 | + when(currentRepository.getTagObject("tag-sha")).thenReturn(tagObject); |
| 92 | + GHRef.GHObject tagTargetObject = mock(GHRef.GHObject.class); |
| 93 | + when(tagObject.getObject()).thenReturn(tagTargetObject); |
| 94 | + when(tagTargetObject.getType()).thenReturn("commit"); |
| 95 | + when(tagTargetObject.getSha()).thenReturn("commit-sha"); |
| 96 | + when(commitRepository.findByShaAndRepositoryRepositoryId("commit-sha", 42L)) |
| 97 | + .thenReturn(Optional.of(commit)); |
| 98 | + when(releaseCandidateRepository.saveAndFlush(any(ReleaseCandidate.class))) |
| 99 | + .thenAnswer(invocation -> invocation.getArgument(0)); |
| 100 | + |
| 101 | + service.processRelease(ghRelease, ghRepository); |
| 102 | + |
| 103 | + ArgumentCaptor<ReleaseCandidate> captor = ArgumentCaptor.forClass(ReleaseCandidate.class); |
| 104 | + verify(releaseCandidateRepository).saveAndFlush(captor.capture()); |
| 105 | + ReleaseCandidate savedReleaseCandidate = captor.getValue(); |
| 106 | + assertEquals("v1.0.0", savedReleaseCandidate.getName()); |
| 107 | + assertSame(repository, savedReleaseCandidate.getRepository()); |
| 108 | + assertSame(commit, savedReleaseCandidate.getCommit()); |
| 109 | + verify(currentRepository, never()).getCommit(anyString()); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + void processRelease_commitLookupFails_doesNotSaveReleaseCandidate() throws IOException { |
| 114 | + when(refObject.getType()).thenReturn("commit"); |
| 115 | + when(refObject.getSha()).thenReturn("missing-commit-sha"); |
| 116 | + when(commitRepository.findByShaAndRepositoryRepositoryId("missing-commit-sha", 42L)) |
| 117 | + .thenReturn(Optional.empty()); |
| 118 | + when(currentRepository.getCommit("missing-commit-sha")) |
| 119 | + .thenThrow(new IOException("No commit found")); |
| 120 | + |
| 121 | + service.processRelease(ghRelease, ghRepository); |
| 122 | + |
| 123 | + verify(releaseCandidateRepository, never()).saveAndFlush(any(ReleaseCandidate.class)); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + void processRelease_lightweightTag_syncsCommitWhenMissingLocally() throws IOException { |
| 128 | + Commit commit = new Commit(); |
| 129 | + commit.setSha("commit-sha"); |
| 130 | + |
| 131 | + when(refObject.getType()).thenReturn("commit"); |
| 132 | + when(refObject.getSha()).thenReturn("commit-sha"); |
| 133 | + when(commitRepository.findByShaAndRepositoryRepositoryId("commit-sha", 42L)) |
| 134 | + .thenReturn(Optional.empty()); |
| 135 | + GHCommit ghCommit = mock(GHCommit.class); |
| 136 | + when(currentRepository.getCommit("commit-sha")).thenReturn(ghCommit); |
| 137 | + when(commitSyncService.processCommit(ghCommit, ghRepository)).thenReturn(commit); |
| 138 | + when(releaseCandidateRepository.saveAndFlush(any(ReleaseCandidate.class))) |
| 139 | + .thenAnswer(invocation -> invocation.getArgument(0)); |
| 140 | + |
| 141 | + service.processRelease(ghRelease, ghRepository); |
| 142 | + |
| 143 | + verify(commitSyncService).processCommit(ghCommit, ghRepository); |
| 144 | + verify(currentRepository, never()).getTagObject(anyString()); |
| 145 | + verify(releaseCandidateRepository).saveAndFlush(any(ReleaseCandidate.class)); |
| 146 | + } |
| 147 | +} |
0 commit comments