Skip to content

Commit

Permalink
Add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
AayushSaini101 committed Mar 23, 2024
1 parent ff1af65 commit 820233f
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 30 deletions.
Binary file added .DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Jenkins Infra
* Copyright (c) 2023-2024 Jenkins Infra
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -21,16 +21,15 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package io.jenkins.pluginhealth.scoring.probes;

import java.io.IOException;
import java.util.Optional;

import io.jenkins.pluginhealth.scoring.model.Plugin;
import io.jenkins.pluginhealth.scoring.model.ProbeResult;

import org.kohsuke.github.GHCheckRun;
import org.kohsuke.github.GHCheckRun.Conclusion;
import org.kohsuke.github.GHCommit;
import org.kohsuke.github.GHRepository;
import org.springframework.core.annotation.Order;
Expand All @@ -48,32 +47,31 @@ public class DefaultBranchBuildStatusProbe extends Probe {

@Override
protected ProbeResult doApply(Plugin plugin, ProbeContext context) {
final io.jenkins.pluginhealth.scoring.model.updatecenter.Plugin ucPlugin =
context.getUpdateCenter().plugins().get(plugin.getName());
if (ucPlugin == null) {
return error("Plugin cannot be found in Update-Center.");
}
final String defaultBranch = ucPlugin.defaultBranch();
if (defaultBranch == null || defaultBranch.isBlank()) {
return this.error("No default branch configured for the plugin.");
}
try
{
final Optional<String> repositoryName = context.getRepositoryName();
final GHRepository ghRepository = context.getGitHub().getRepository(repositoryName.get());
GHCommit commit = ghRepository.getCommit(defaultBranch);
GHCheckRun checkRun = commit.getCheckRuns().iterator().next();
GHCheckRun.Conclusion conclusion = checkRun.getConclusion();
try {
final io.jenkins.pluginhealth.scoring.model.updatecenter.Plugin ucPlugin =
context.getUpdateCenter().plugins().get(plugin.getName());
if (ucPlugin == null) {
return error("Plugin cannot be found in Update-Center.");
}
final String defaultBranch = ucPlugin.defaultBranch();
if (defaultBranch == null || defaultBranch.isBlank()) {
return this.error("No default branch configured for the plugin.");
}

if (conclusion == GHCheckRun.Conclusion.FAILURE) {
return this.success("Build Failed in Default Branch");
} else {
return this.success("Build is Success in Default Branch");
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
final Optional<String> repositoryName = context.getRepositoryName();
final GHRepository ghRepository = context.getGitHub().getRepository(repositoryName.get());
GHCommit commit = ghRepository.getCommit(ucPlugin.defaultBranch());
GHCheckRun checkRun = commit.getCheckRuns().iterator().next();
Conclusion conclusion = checkRun.getConclusion();

if (conclusion == Conclusion.FAILURE) {
return this.success("Build Failed in Default Branch");
} else {
return this.success("Build is Success in Default Branch");
}
} catch (Exception ex) {
return this.error("Failed to obtain the status of the default Branch.");
}
}

@Override
Expand All @@ -90,5 +88,4 @@ public String getDescription() {
public long getVersion() {
return 1;
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Jenkins Infra
* Copyright (c) 2023-2024 Jenkins Infra
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -23,13 +23,149 @@
*/
package io.jenkins.pluginhealth.scoring.probes;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

class DefaultBranchBuildStatusProbeTest extends AbstractProbeTest<DefaultBranchBuildStatusProbe> {
import java.io.IOException;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import io.jenkins.pluginhealth.scoring.model.Plugin;
import io.jenkins.pluginhealth.scoring.model.ProbeResult;
import io.jenkins.pluginhealth.scoring.model.updatecenter.UpdateCenter;

import hudson.util.VersionNumber;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.kohsuke.github.GHCheckRun;
import org.kohsuke.github.GHCommit;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import org.kohsuke.github.PagedIterable;
import org.kohsuke.github.PagedIterator;

class DefaultBranchBuildStatusProbeTest extends AbstractProbeTest<DefaultBranchBuildStatusProbe> {

private DefaultBranchBuildStatusProbe probe;

@Override
DefaultBranchBuildStatusProbe getSpy() {
return spy(DefaultBranchBuildStatusProbe.class);
}

@BeforeEach
public void init() {

probe = getSpy();
}

@Test
public void shouldReturnBuildSucessOntheDefaultBranch() throws IOException {

final String pluginName = "mailer";
final String pluginRepo = "jenkinsci/" + pluginName + "-plugin";
final String scmLink = "https://github.com/" + pluginRepo;
final String defaultBranch = "main";
final GitHub gitHub = mock(GitHub.class);
final GHRepository ghRepository = mock(GHRepository.class);
final GHCommit ghCommit = mock(GHCommit.class);
final GHCheckRun checkRun = mock(GHCheckRun.class);
final PagedIterable<GHCheckRun> checkrun = mock(PagedIterable.class);
final PagedIterator<GHCheckRun> checkIterator = mock(PagedIterator.class);
final Plugin plugin = mock(Plugin.class);
final ProbeContext ctx = mock(ProbeContext.class);

final GitHub gh = mock(GitHub.class);

when(plugin.getName()).thenReturn(pluginName);
when(plugin.getScm()).thenReturn(scmLink);
when(ctx.getUpdateCenter())
.thenReturn(new UpdateCenter(
Map.of(
pluginName,
new io.jenkins.pluginhealth.scoring.model.updatecenter.Plugin(
pluginName,
new VersionNumber("1.0"),
scmLink,
ZonedDateTime.now(),
List.of(),
0,
"42",
defaultBranch)),
Map.of(),
List.of()));
when(ctx.getGitHub()).thenReturn(gh);
when(ctx.getRepositoryName()).thenReturn(Optional.of(pluginRepo));
when(ctx.getGitHub()).thenReturn(gitHub);
when(ctx.getRepositoryName()).thenReturn(Optional.of("org/repo"));
when(gitHub.getRepository("org/repo")).thenReturn(ghRepository);
when(ghRepository.getCommit(defaultBranch)).thenReturn(ghCommit);
when(gh.getRepository(pluginRepo)).thenReturn(ghRepository);
when(ghCommit.getCheckRuns()).thenReturn(checkrun);
when(ghCommit.getCheckRuns().iterator()).thenReturn(checkIterator);
when(ghCommit.getCheckRuns().iterator().hasNext()).thenReturn /**/(true);
when(ghCommit.getCheckRuns().iterator().next()).thenReturn(checkRun);
assertThat(probe.apply(plugin, ctx))
.usingRecursiveComparison()
.comparingOnlyFields("id", "message", "status")
.isEqualTo(ProbeResult.success(
DefaultBranchBuildStatusProbe.KEY, "Build is Success in Default Branch", probe.getVersion()));
verify(probe).doApply(plugin, ctx);
}

@Test
public void shouldReturnErrorMessageCommitsNotFound() throws IOException {

final String pluginName = "mailer";
final String pluginRepo = "jenkinsci/" + pluginName + "-plugin";
final String scmLink = "https://github.com/" + pluginRepo;
final String defaultBranch = "main";
final GitHub gitHub = mock(GitHub.class);
final GHRepository ghRepository = mock(GHRepository.class);
final GHCommit ghCommit = mock(GHCommit.class);
final PagedIterable<GHCheckRun> checkrun = mock(PagedIterable.class);
final Plugin plugin = mock(Plugin.class);
final ProbeContext ctx = mock(ProbeContext.class);

final GitHub gh = mock(GitHub.class);

when(plugin.getName()).thenReturn(pluginName);
when(plugin.getScm()).thenReturn(scmLink);
when(ctx.getUpdateCenter())
.thenReturn(new UpdateCenter(
Map.of(
pluginName,
new io.jenkins.pluginhealth.scoring.model.updatecenter.Plugin(
pluginName,
new VersionNumber("1.0"),
scmLink,
ZonedDateTime.now(),
List.of(),
0,
"42",
defaultBranch)),
Map.of(),
List.of()));
when(ctx.getGitHub()).thenReturn(gh);
when(ctx.getRepositoryName()).thenReturn(Optional.of(pluginRepo));
when(ctx.getGitHub()).thenReturn(gitHub);
when(ctx.getRepositoryName()).thenReturn(Optional.of("org/repo"));
when(gitHub.getRepository("org/repo")).thenReturn(ghRepository);
when(ghRepository.getCommit(defaultBranch)).thenReturn(ghCommit);
when(gh.getRepository(pluginRepo)).thenReturn(ghRepository);
when(ghCommit.getCheckRuns()).thenReturn(checkrun);
assertThat(probe.apply(plugin, ctx))
.usingRecursiveComparison()
.comparingOnlyFields("id", "message", "status")
.isEqualTo(ProbeResult.error(
DefaultBranchBuildStatusProbe.KEY,
"Failed to obtain the status of the default Branch.",
probe.getVersion()));
verify(probe).doApply(plugin, ctx);
}
}

0 comments on commit 820233f

Please sign in to comment.