Skip to content

Commit

Permalink
Merge pull request #1202 from amihaiemil/1200
Browse files Browse the repository at this point in the history
#1200 unit tests for StoredProject.rename(...)
  • Loading branch information
amihaiemil committed Aug 6, 2021
2 parents 432c8f0 + f183a08 commit 0285430
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
* the Issue Assigned case and Comment Created case.
* @todo #1996:30min Add the Project_Renamed case to the resolve(...) method
* and forward the call to the ProjectManager to take care of it.
* @todo #1196:30min Add unit tests for method rename(...) in this class.
*/
public final class StoredProject implements Project {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,88 @@ public void canBeDeactivated() {
Mockito.verify(collaborators, Mockito.times(1)).remove("charlesmike");
}

/**
* StoredProject can be renamed. Webhooks should be removed, project renamed
* in the Storage and new Webhook should be added.
*/
@Test
public void canBeRenamed() {
final Project renamed = Mockito.mock(Project.class);

final Webhooks hooks = Mockito.mock(Webhooks.class);
Mockito.when(hooks.remove()).thenReturn(true);

final Repo repo = Mockito.mock(Repo.class);
Mockito.when(repo.webhooks()).thenReturn(hooks);

final Storage storage = Mockito.mock(Storage.class);
final Provider prov = Mockito.mock(Provider.class);
Mockito.when(prov.name()).thenReturn(Provider.Names.GITHUB);
Mockito.when(prov.repo("john", "test")).thenReturn(repo);

final User owner = Mockito.mock(User.class);
Mockito.when(owner.provider()).thenReturn(prov);
final ProjectManager manager = Mockito.mock(ProjectManager.class);
Mockito.when(manager.provider()).thenReturn(prov);

final Project project = new StoredProject(
owner,
"john/test",
"wh123token",
manager,
storage
);

final Projects all = Mockito.mock(Projects.class);
Mockito.when(all.rename(project, "newName"))
.thenReturn(renamed);
Mockito.when(storage.projects()).thenReturn(all);
Mockito.when(hooks.add(renamed)).thenReturn(true);

project.rename("newName");

Mockito.verify(hooks, Mockito.times(1)).remove();
Mockito.verify(all, Mockito.times(1)).rename(project, "newName");
Mockito.verify(hooks, Mockito.times(1)).add(renamed);
}

/**
* StoredProject should not be renamed if we fail to remove the original
* Webhooks.
*/
@Test
public void doesNotRenameWhenWebhookRemovalFails() {
final Webhooks hooks = Mockito.mock(Webhooks.class);
Mockito.when(hooks.remove()).thenReturn(false);

final Repo repo = Mockito.mock(Repo.class);
Mockito.when(repo.webhooks()).thenReturn(hooks);

final Storage storage = Mockito.mock(Storage.class);
final Provider prov = Mockito.mock(Provider.class);
Mockito.when(prov.name()).thenReturn(Provider.Names.GITHUB);
Mockito.when(prov.repo("john", "test")).thenReturn(repo);

final User owner = Mockito.mock(User.class);
Mockito.when(owner.provider()).thenReturn(prov);
final ProjectManager manager = Mockito.mock(ProjectManager.class);
Mockito.when(manager.provider()).thenReturn(prov);

final Project project = new StoredProject(
owner,
"john/test",
"wh123token",
manager,
storage
);

project.rename("newName");

Mockito.verify(hooks, Mockito.times(1)).remove();
Mockito.verify(storage, Mockito.times(0)).projects();
Mockito.verify(hooks, Mockito.times(0)).add(Mockito.any());
}

/**
* Mock a Repo for test.
*
Expand Down

0 comments on commit 0285430

Please sign in to comment.