3939import static org .junit .Assert .assertFalse ;
4040import static org .junit .Assert .assertTrue ;
4141import static org .mockito .ArgumentMatchers .any ;
42- import static org .mockito .ArgumentMatchers .contains ;
4342import static org .mockito .ArgumentMatchers .eq ;
4443import static org .mockito .Mockito .mock ;
4544import static org .mockito .Mockito .when ;
4645
4746import com .google .common .collect .ImmutableMap ;
4847import com .google .common .collect .Lists ;
4948import com .google .common .io .Resources ;
50- import com .google .common .net .HttpHeaders ;
5149import com .spotify .github .async .AsyncPage ;
5250import com .spotify .github .jackson .Json ;
5351import com .spotify .github .v3 .comment .Comment ;
6159import com .spotify .github .v3 .repos .FolderContent ;
6260import com .spotify .github .v3 .repos .Repository ;
6361import com .spotify .github .v3 .repos .RepositoryInvitation ;
62+ import com .spotify .github .v3 .repos .RepositoryPermission ;
6463import com .spotify .github .v3 .repos .RepositoryTest ;
6564import com .spotify .github .v3 .repos .Status ;
6665import com .spotify .github .v3 .repos .requests .ImmutableAuthenticatedUserRepositoriesFilter ;
@@ -165,12 +164,15 @@ public void isNotCollaborator() throws Exception {
165164
166165 @ Test
167166 public void addCollaborator () throws Exception {
168- final CompletableFuture < RepositoryInvitation > fixture =
169- completedFuture ( json . fromJson ( getFixture ( "repository_invitation.json" ), RepositoryInvitation . class ));
170- when ( github . put ( "/repos/someowner/somerepo/collaborators/user" , "" , RepositoryInvitation . class )). thenReturn ( fixture );
167+ final Response response = createMockResponse ( "" , getFixture ( "repository_invitation.json" ));
168+ when ( github . put ( "/repos/someowner/somerepo/collaborators/user" , "{ \" permission \" : \" pull \" }" )). thenReturn (
169+ completedFuture ( response ) );
171170
172- final RepositoryInvitation repoInvite = repoClient .addCollaborator ("user" ).get ();
171+ final Optional <RepositoryInvitation > maybeInvite = repoClient .addCollaborator ("user" ,
172+ RepositoryPermission .PULL ).get ();
173173
174+ assertTrue (maybeInvite .isPresent ());
175+ final RepositoryInvitation repoInvite = maybeInvite .get ();
174176 assertThat (repoInvite .id (), is (1 ));
175177 assertThat (repoInvite .nodeId (), is ("MDEwOlJlcG9zaXRvcnkxMjk2MjY5" ));
176178 assertThat (repoInvite .repository ().id (), is (1296269 ));
@@ -180,6 +182,19 @@ public void addCollaborator() throws Exception {
180182 assertThat (repoInvite .permissions (), is ("write" ));
181183 }
182184
185+ @ Test
186+ public void addCollaboratorUserExists () throws Exception {
187+ final Response response = mock (Response .class );
188+ when (response .code ()).thenReturn (204 );
189+ when (github .put ("/repos/someowner/somerepo/collaborators/user" , "{\" permission\" :\" pull\" }" )).thenReturn (
190+ completedFuture (response ));
191+
192+ final Optional <RepositoryInvitation > maybeInvite = repoClient .addCollaborator ("user" ,
193+ RepositoryPermission .PULL ).get ();
194+
195+ assertTrue (maybeInvite .isEmpty ());
196+ }
197+
183198 @ Test
184199 public void removeCollaborator () throws Exception {
185200 CompletableFuture <Response > response = completedFuture (mock (Response .class ));
0 commit comments