Skip to content

Commit

Permalink
#1210 Projects.getByWebHookToken implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
amihaiemil committed Aug 10, 2021
1 parent 4c82a71 commit 61ec62c
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 18 deletions.
7 changes: 7 additions & 0 deletions self-api/src/main/java/com/selfxdsd/api/Projects.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ Project getProjectById(
final String repoProvider
);

/**
* Get the Project with the specified webHookToken.
* @param webHookToken String WebHook Token.
* @return Project or null if no Project found.
*/
Project getByWebHookToken(final String webHookToken);

/**
* Get the Projects at the provided Page.
* @param page Page number.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
* @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 @@ -142,6 +144,17 @@ public Project getProjectById(
.orElse(null);
}

@Override
public Project getByWebHookToken(final String webHookToken) {
final Page page = super.current();
return this.projects.get()
.skip((page.getNumber() - 1) * page.getSize())
.limit(page.getSize())
.filter(p -> p.repoFullName().equals(webHookToken))
.findFirst()
.orElse(null);
}

@Override
public Projects page(final Paged.Page page) {
return new PmProjects(this.pmId, this.projects, this.storage, page);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,20 @@ public Project getProjectById(
return found;
}

@Override
public Project getByWebHookToken(final String webHookToken) {
Project found = this.storage.projects().getByWebHookToken(
webHookToken
);
if(found != null) {
final String ownerUsername = found.owner().username();
if(!ownerUsername.equalsIgnoreCase(this.user.username())) {
found = null;
}
}
return found;
}

@Override
public Projects page(final Paged.Page page) {
return new UserProjects(this.user, this.projects, this.storage, page);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ public Project getProjectById(
.findFirst().orElse(null);
}

@Override
public Project getByWebHookToken(final String webHookToken) {
throw new UnsupportedOperationException("Not yet implemented.");
}

@Override
public Projects page(final Paged.Page page) {
return new InMemoryProjects(this.storage, this.projects, page);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ public void projectByIdFound() {
final Projects projects = new UserProjects(
user,
() -> List.of(
mockProject("mihai/test", "github"),
mockProject("mihai/test2", "github")
mockProject("mihai/test", "github", "wt123"),
mockProject("mihai/test2", "github", "wt124")
).stream(),
storage
);
Expand Down Expand Up @@ -202,8 +202,8 @@ public void projectByIdFoundButHasOtherOwner() {
final Projects projects = new UserProjects(
mihai,
() -> List.of(
mockProject("mihai/test", "github"),
mockProject("mihai/test2", "github")
mockProject("mihai/test", "github", "wt123"),
mockProject("mihai/test2", "github", "wt124")
).stream(),
storage
);
Expand All @@ -214,7 +214,6 @@ public void projectByIdFoundButHasOtherOwner() {
);
}


/**
* Should return null is project is not found by id.
*/
Expand All @@ -233,8 +232,8 @@ public void projectByIdNotFound() {
final Projects projects = new UserProjects(
user,
() -> List.of(
mockProject("mihai/test", "github"),
mockProject("mihai/test2", "github")
mockProject("mihai/test", "github", "wt123"),
mockProject("mihai/test2", "github", "wt124")
).stream(),
storage
);
Expand All @@ -245,6 +244,102 @@ public void projectByIdNotFound() {
);
}

/**
* Should find a project by it's webhook token.
*/
@Test
public void projectByWebHookToken() {
final User user = this.mockUser("mihai", "github");

final Project project = Mockito.mock(Project.class);
Mockito.when(project.owner()).thenReturn(user);
final Projects all = Mockito.mock(Projects.class);
Mockito.when(
all.getByWebHookToken("wt123")
).thenReturn(project);

final Storage storage = Mockito.mock(Storage.class);
Mockito.when(storage.projects()).thenReturn(all);

final Projects projects = new UserProjects(
user,
() -> List.of(
mockProject("mihai/test", "github", "wt123"),
mockProject("mihai/test2", "github", "wt124")
).stream(),
storage
);
final Project found = projects.getByWebHookToken("wt123");
MatcherAssert.assertThat(
found,
Matchers.is(project)
);
}

/**
* A Project is found but it belongs to another User so the
* method should return null.
*/
@Test
public void projectByWebHookTokenFoundButHasOtherOwner() {
final User mihai = this.mockUser("mihai", "github");
final User vlad = this.mockUser("vlad", "github");

final Project project = Mockito.mock(Project.class);
Mockito.when(project.owner()).thenReturn(vlad);
final Projects all = Mockito.mock(Projects.class);
Mockito.when(
all.getByWebHookToken("wt200")
).thenReturn(project);

final Storage storage = Mockito.mock(Storage.class);
Mockito.when(storage.projects()).thenReturn(all);

final Projects projects = new UserProjects(
mihai,
() -> List.of(
mockProject("mihai/test", "github", "wt123"),
mockProject("mihai/test2", "github", "wt124")
).stream(),
storage
);
final Project found = projects.getByWebHookToken("wt200");
MatcherAssert.assertThat(
found,
Matchers.nullValue()
);
}

/**
* Should return null is project is not found by webhook token.
*/
@Test
public void projectByWebHookTokenNotFound() {
final User user = this.mockUser("mihai", "github");

final Projects all = Mockito.mock(Projects.class);
Mockito.when(
all.getByWebHookToken("wt404")
).thenReturn(null);

final Storage storage = Mockito.mock(Storage.class);
Mockito.when(storage.projects()).thenReturn(all);

final Projects projects = new UserProjects(
user,
() -> List.of(
mockProject("mihai/test", "github", "wt123"),
mockProject("mihai/test2", "github", "wt124")
).stream(),
storage
);
final Project found = projects.getByWebHookToken("wt404");
MatcherAssert.assertThat(
found,
Matchers.nullValue()
);
}

/**
* We can remove a Project if it's owned by the same User.
*/
Expand All @@ -254,14 +349,14 @@ public void removesProjectIfOwnedBySameUser() {
final Projects projects = new UserProjects(
mihai,
() -> List.of(
mockProject("mihai/test", "github"),
mockProject("mihai/test2", "github"),
mockProject("mihai/test3", "github"),
mockProject("mihai/test4", "github")
mockProject("mihai/test", "github", "wt123"),
mockProject("mihai/test2", "github", "wt124"),
mockProject("mihai/test3", "github", "wt125"),
mockProject("mihai/test4", "github", "wt126")
).stream(),
Mockito.mock(Storage.class)
);
final Project toRemove = mockProject("mihai/test", "github");
final Project toRemove = mockProject("mihai/test", "github", "wt123");
final Repo repo = Mockito.mock(Repo.class);
Mockito.when(toRemove.repo()).thenReturn(repo);
Mockito.when(toRemove.owner()).thenReturn(mihai);
Expand All @@ -279,14 +374,14 @@ public void doesNotRemoveProjectIfNotOwnedBySameUser() {
final Projects projects = new UserProjects(
this.mockUser("mihai", "github"),
() -> List.of(
mockProject("mihai/test", "github"),
mockProject("mihai/test2", "github"),
mockProject("mihai/test3", "github"),
mockProject("mihai/test4", "github")
mockProject("mihai/test", "github", "wt123"),
mockProject("mihai/test2", "github", "wt124"),
mockProject("mihai/test3", "github", "wt125"),
mockProject("mihai/test4", "github", "wt126")
).stream(),
Mockito.mock(Storage.class)
);
final Project toRemove = mockProject("mihai/test", "github");
final Project toRemove = mockProject("mihai/test", "github", "wt123");
final Repo repo = Mockito.mock(Repo.class);
Mockito.when(toRemove.repo()).thenReturn(repo);
final User vlad = this.mockUser("vlad", "github");
Expand Down Expand Up @@ -353,12 +448,18 @@ private Project projectAssignedTo(final int projectManagerId) {
*
* @param fullName Full name.
* @param provider Provider.
* @param webHookToken WebHook Token.
* @return Mocked Project
*/
private Project mockProject(final String fullName, final String provider) {
private Project mockProject(
final String fullName,
final String provider,
final String webHookToken
) {
final Project project = Mockito.mock(Project.class);
Mockito.when(project.repoFullName()).thenReturn(fullName);
Mockito.when(project.provider()).thenReturn(provider);
Mockito.when(project.webHookToken()).thenReturn(webHookToken);
return project;
}
}

1 comment on commit 61ec62c

@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 opened the Issues [#1212] for the newly added to-dos.

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

Please sign in to comment.