Skip to content

Commit

Permalink
#1212 unit tests and bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Aug 10, 2021
1 parent a3d932a commit 4d11b53
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.1
* @todo #1210:60min Continue with the unit tests for method getByWebHookToken
* from this class.
*/
public final class PmProjects extends BasePaged implements Projects {

Expand Down Expand Up @@ -150,7 +148,7 @@ public Project getByWebHookToken(final String webHookToken) {
return this.projects.get()
.skip((page.getNumber() - 1) * page.getSize())
.limit(page.getSize())
.filter(p -> p.repoFullName().equals(webHookToken))
.filter(p -> p.webHookToken().equals(webHookToken))
.findFirst()
.orElse(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ public void projectByIdFound() {
final Projects projects = new PmProjects(
1,
() -> List.of(
mockProject("john/test", "github"),
mockProject("john/test2", "github")
mockProject("john/test", "github", "wt1"),
mockProject("john/test2", "github", "wt2")
).stream(),
Mockito.mock(Storage.class)
);
Expand Down Expand Up @@ -185,7 +185,7 @@ public void iteratePageWorks(){
final Projects projects = new PmProjects(1, () -> IntStream
.rangeClosed(1, 14)
.mapToObj(i -> mockProject("repo-" + i,
Provider.Names.GITHUB)),
Provider.Names.GITHUB, "wt-" + i)),
Mockito.mock(Storage.class)
);
//initial page has all available records
Expand Down Expand Up @@ -219,7 +219,7 @@ public void throwsWhenPageIsUpperOutOfBound(){
new PmProjects(1, () -> IntStream
.rangeClosed(1, 10)
.mapToObj(i -> mockProject("repo-" + i,
Provider.Names.GITHUB)),
Provider.Names.GITHUB, "wt-" + i)),
Mockito.mock(Storage.class)
).page(new Paged.Page(5, 10));
}
Expand All @@ -232,7 +232,7 @@ public void throwsWhenPageIsUnderOutOfBound(){
new PmProjects(1, () -> IntStream
.rangeClosed(1, 10)
.mapToObj(i -> mockProject("repo-" + i,
Provider.Names.GITHUB)),
Provider.Names.GITHUB, "wt-" + i)),
Mockito.mock(Storage.class)
).page(new Paged.Page(0, 10));
}
Expand All @@ -245,7 +245,7 @@ public void projectByIdFoundInPage(){
final Project found = new PmProjects(1, () -> IntStream
.rangeClosed(1, 14)
.mapToObj(i -> mockProject("repo-" + i,
Provider.Names.GITHUB)),
Provider.Names.GITHUB, "wt-" + i)),
Mockito.mock(Storage.class)
).page(new Paged.Page(2, 5)).getProjectById(
"repo-7", Provider.Names.GITHUB
Expand All @@ -270,7 +270,7 @@ public void existingProjectNotFoundByIdInPage(){
1, () -> IntStream
.rangeClosed(1, 14)
.mapToObj(i -> mockProject("repo-" + i,
Provider.Names.GITHUB)),
Provider.Names.GITHUB, "wt-" + i)),
Mockito.mock(Storage.class)
).page(new Paged.Page(2, 5)).getProjectById(
"repo-1", Provider.Names.GITHUB
Expand Down Expand Up @@ -357,6 +357,81 @@ public void doesNotRemoveProjectIfNotOwnedBySamePm() {
Mockito.verify(toRemove, Mockito.times(0)).deactivate(repo);
}

/**
* Should find a project by its Webhook Token.
*/
@Test
public void projectByWebhookTokenFound() {
final Projects projects = new PmProjects(
1,
() -> List.of(
mockProject("john/test", "github", "wt-1"),
mockProject("john/test2", "github", "wt-2")
).stream(),
Mockito.mock(Storage.class)
);
final Project found = projects.getByWebHookToken("wt-1");
MatcherAssert.assertThat(
found.repoFullName(),
Matchers.equalTo("john/test")
);
MatcherAssert.assertThat(
found.provider(),
Matchers.equalTo("github")
);
}

/**
* Should return null if project is not found by webhook token.
*/
@Test
public void projectByWebHookTokenNotFound() {
final Projects projects = new PmProjects(
1, Stream::empty, Mockito.mock(Storage.class)
);
MatcherAssert.assertThat(
projects.getByWebHookToken("wt-1"),
Matchers.nullValue()
);
}

/**
* Should find a project by its webhook token in a random page.
*/
@Test
public void projectByWebHookTokenFoundInPage(){
final Project found = new PmProjects(1, () -> IntStream
.rangeClosed(1, 14)
.mapToObj(i -> mockProject("repo-" + i,
Provider.Names.GITHUB, "wt-" + i)),
Mockito.mock(Storage.class)
).page(new Paged.Page(2, 5)).getByWebHookToken("wt-7");
MatcherAssert.assertThat(
found.repoFullName(),
Matchers.equalTo("repo-7")
);
MatcherAssert.assertThat(
found.provider(),
Matchers.equalTo("github")
);
}

/**
* Should return null if project is not found by webhook token in the
* specified page, even though project exists in overall.
*/
@Test
public void existingProjectNotFoundByWebHookTokenInPage(){
final Project found = new PmProjects(
1, () -> IntStream
.rangeClosed(1, 14)
.mapToObj(i -> mockProject("repo-" + i,
Provider.Names.GITHUB, "wt-" + i)),
Mockito.mock(Storage.class)
).page(new Paged.Page(2, 5)).getByWebHookToken("wt-1");
MatcherAssert.assertThat(found, Matchers.nullValue());
}

/**
* Mock a User.
*
Expand All @@ -380,15 +455,18 @@ private User mockUser(final String username, final String providerName) {
*
* @param repoFullName Repo full name.
* @param repoProvider Provider.
* @param webHookToken WebHook Token.
* @return Mocked Project
*/
private Project mockProject(
final String repoFullName,
final String repoProvider
final String repoProvider,
final String webHookToken
) {
final Project project = Mockito.mock(Project.class);
Mockito.when(project.repoFullName()).thenReturn(repoFullName);
Mockito.when(project.provider()).thenReturn(repoProvider);
Mockito.when(project.webHookToken()).thenReturn(webHookToken);
return project;
}
}

1 comment on commit 4d11b53

@zoeself
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amihaiemil I've closed the Issues [#1212] since their to-dos disappeared from the code.

The to-dos may have been removed in an earlier commit, but I've found it just now.

Please sign in to comment.