From 49947db271ec80c57c1734b96e82de17094bccce Mon Sep 17 00:00:00 2001 From: Abhishek Jain Date: Mon, 4 Sep 2023 14:12:10 +0200 Subject: [PATCH] fix: Fix nil values and int warning for parent team (#146) * fix: Fix nil values and int warning for parent team * fix: Ignore null values --------- Co-authored-by: Abhishek Jain <1445581+Abhi347@users.noreply.github.com> --- .sdkmanrc | 4 ++ .../github/v3/orgs/requests/TeamCreate.java | 51 ++++++++-------- .../github/v3/orgs/requests/TeamUpdate.java | 11 ++-- .../github/v3/clients/TeamClientTest.java | 59 ++++++++++--------- 4 files changed, 65 insertions(+), 60 deletions(-) create mode 100644 .sdkmanrc diff --git a/.sdkmanrc b/.sdkmanrc new file mode 100644 index 00000000..c53a2fca --- /dev/null +++ b/.sdkmanrc @@ -0,0 +1,4 @@ +# Enable auto-env through the sdkman_auto_env config +# Add key=value pairs of SDKs to use below +java=11.0.20-amzn +maven=3.8.6 diff --git a/src/main/java/com/spotify/github/v3/orgs/requests/TeamCreate.java b/src/main/java/com/spotify/github/v3/orgs/requests/TeamCreate.java index ae022c09..e5fe07b7 100644 --- a/src/main/java/com/spotify/github/v3/orgs/requests/TeamCreate.java +++ b/src/main/java/com/spotify/github/v3/orgs/requests/TeamCreate.java @@ -19,12 +19,13 @@ */ package com.spotify.github.v3.orgs.requests; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.spotify.github.GithubStyle; import java.util.List; import java.util.Optional; -import javax.annotation.Nullable; import org.immutables.value.Value; /** Request to create a team within a given organisation */ @@ -32,51 +33,47 @@ @GithubStyle @JsonSerialize(as = ImmutableTeamCreate.class) @JsonDeserialize(as = ImmutableTeamCreate.class) +@JsonInclude(JsonInclude.Include.NON_EMPTY) public interface TeamCreate { /** The name of the team. */ - @Nullable String name(); /** The description of the team. */ Optional description(); /** - * The level of privacy this team should have. - * For a non-nested team: - * secret - only visible to organization owners and members of this team. - * closed - visible to all members of this organization. - * Default: secret - * For a parent or child team: - * closed - visible to all members of this organization. - * Default for child team: closed - * Can be one of: secret, closed + * The level of privacy this team should have. For a non-nested team: secret - only visible to + * organization owners and members of this team. closed - visible to all members of this + * organization. Default: secret For a parent or child team: closed - visible to all members of + * this organization. Default for child team: closed Can be one of: secret, closed */ Optional privacy(); /** * The notification setting the team has chosen. The options are: - * notifications_enabled - team members receive notifications when the team is @mentioned. - * notifications_disabled - no one receives notifications. - * Default: notifications_enabled - * Can be one of: notifications_enabled, notifications_disabled + * + *

notifications_enabled - team members receive notifications when the team is @mentioned. + * + *

notifications_disabled - no one receives notifications. + * + *

Default: notifications_enabled + * + *

Can be one of: notifications_enabled, notifications_disabled */ - @SuppressWarnings("checkstyle:methodname") - Optional notification_setting(); + @JsonProperty("notification_setting") + Optional notificationSetting(); - /** - * List GitHub IDs for organization members who will - * become team maintainers. - */ + /** List GitHub IDs for organization members who will become team maintainers. */ Optional> maintainers(); - /** The full name (e.g., "organization-name/repository-name") - * of repositories to add the team to. + /** + * The full name (e.g., "organization-name/repository-name") of repositories to add the team to. */ - @SuppressWarnings("checkstyle:methodname") - Optional> repo_names(); + @JsonProperty("repo_names") + Optional> repoNames(); /** The ID of a team to set as the parent team. */ - @SuppressWarnings("checkstyle:methodname") - Optional parent_team_id(); + @JsonProperty("parent_team_id") + Optional parentTeamId(); } diff --git a/src/main/java/com/spotify/github/v3/orgs/requests/TeamUpdate.java b/src/main/java/com/spotify/github/v3/orgs/requests/TeamUpdate.java index 00fea962..3652cf85 100644 --- a/src/main/java/com/spotify/github/v3/orgs/requests/TeamUpdate.java +++ b/src/main/java/com/spotify/github/v3/orgs/requests/TeamUpdate.java @@ -19,6 +19,8 @@ */ package com.spotify.github.v3.orgs.requests; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.spotify.github.GithubStyle; @@ -31,6 +33,7 @@ @GithubStyle @JsonSerialize(as = ImmutableTeamUpdate.class) @JsonDeserialize(as = ImmutableTeamUpdate.class) +@JsonInclude(JsonInclude.Include.NON_EMPTY) public interface TeamUpdate { /** The name of the team. */ @@ -60,11 +63,11 @@ public interface TeamUpdate { * Default: notifications_enabled * Can be one of: notifications_enabled, notifications_disabled */ - @SuppressWarnings("checkstyle:methodname") - Optional notification_setting(); + @JsonProperty("notification_setting") + Optional notificationSetting(); /** The ID of a team to set as the parent team. */ - @SuppressWarnings("checkstyle:methodname") - Optional parent_team_id(); + @JsonProperty("parent_team_id") + Optional parentTeamId(); } diff --git a/src/test/java/com/spotify/github/v3/clients/TeamClientTest.java b/src/test/java/com/spotify/github/v3/clients/TeamClientTest.java index e5d7bd84..aa33abf2 100644 --- a/src/test/java/com/spotify/github/v3/clients/TeamClientTest.java +++ b/src/test/java/com/spotify/github/v3/clients/TeamClientTest.java @@ -18,7 +18,6 @@ * -/-/- */ - package com.spotify.github.v3.clients; import static com.google.common.io.Resources.getResource; @@ -31,8 +30,7 @@ import static org.hamcrest.core.Is.is; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; import com.google.common.io.Resources; import com.spotify.github.jackson.Json; @@ -57,7 +55,7 @@ import org.powermock.modules.junit4.PowerMockRunner; @RunWith(PowerMockRunner.class) -@PrepareForTest({ Headers.class, ResponseBody.class, Response.class}) +@PrepareForTest({Headers.class, ResponseBody.class, Response.class}) public class TeamClientTest { private GitHubClient github; @@ -113,42 +111,43 @@ public void deleteTeam() throws Exception { @Test public void createTeam() throws Exception { final TeamCreate teamCreateRequest = - json.fromJson( - getFixture("teams_request.json"), - TeamCreate.class); + json.fromJson(getFixture("teams_request.json"), TeamCreate.class); - final CompletableFuture fixtureResponse = completedFuture(json.fromJson( - getFixture("team_get.json"), - Team.class)); + final CompletableFuture fixtureResponse = + completedFuture(json.fromJson(getFixture("team_get.json"), Team.class)); when(github.post(any(), any(), eq(Team.class))).thenReturn(fixtureResponse); final CompletableFuture actualResponse = teamClient.createTeam(teamCreateRequest); assertThat(actualResponse.get().name(), is("Justice League")); + verify(github, times(1)) + .post(eq("/orgs/github/teams"), eq("{\"name\":\"Justice League\"}"), eq(Team.class)); } @Test public void updateTeam() throws Exception { final TeamUpdate teamUpdateRequest = - json.fromJson( - getFixture("teams_patch.json"), - TeamUpdate.class); + json.fromJson(getFixture("teams_patch.json"), TeamUpdate.class); - final CompletableFuture fixtureResponse = completedFuture(json.fromJson( - getFixture("teams_patch_response.json"), - Team.class)); + final CompletableFuture fixtureResponse = + completedFuture(json.fromJson(getFixture("teams_patch_response.json"), Team.class)); when(github.patch(any(), any(), eq(Team.class))).thenReturn(fixtureResponse); - final CompletableFuture actualResponse = teamClient.updateTeam(teamUpdateRequest, "justice-league"); + final CompletableFuture actualResponse = + teamClient.updateTeam(teamUpdateRequest, "justice-league"); assertThat(actualResponse.get().name(), is("Justice League2")); + verify(github, times(1)) + .patch(eq("/orgs/github/teams/justice-league"), eq("{\"name\":\"Justice League2\"}"), eq(Team.class)); } @Test public void getMembership() throws Exception { final CompletableFuture fixture = completedFuture(json.fromJson(getFixture("membership.json"), Membership.class)); - when(github.request("/orgs/github/teams/1/memberships/octocat", Membership.class)).thenReturn(fixture); + when(github.request("/orgs/github/teams/1/memberships/octocat", Membership.class)) + .thenReturn(fixture); final Membership membership = teamClient.getMembership("1", "octocat").get(); - assertThat(membership.url().toString(), is("https://api.github.com/teams/1/memberships/octocat")); + assertThat( + membership.url().toString(), is("https://api.github.com/teams/1/memberships/octocat")); assertThat(membership.role(), is("maintainer")); assertThat(membership.state(), is("active")); } @@ -167,15 +166,14 @@ public void listTeamMembers() throws Exception { @Test public void updateMembership() throws Exception { final MembershipCreate membershipCreateRequest = - json.fromJson( - getFixture("membership_update.json"), - MembershipCreate.class); + json.fromJson(getFixture("membership_update.json"), MembershipCreate.class); - final CompletableFuture fixtureResponse = completedFuture(json.fromJson( - getFixture("membership_update_response.json"), - Membership.class)); + final CompletableFuture fixtureResponse = + completedFuture( + json.fromJson(getFixture("membership_update_response.json"), Membership.class)); when(github.put(any(), any(), eq(Membership.class))).thenReturn(fixtureResponse); - final CompletableFuture actualResponse = teamClient.updateMembership(membershipCreateRequest, "1", "octocat"); + final CompletableFuture actualResponse = + teamClient.updateMembership(membershipCreateRequest, "1", "octocat"); assertThat(actualResponse.get().role(), is("member")); } @@ -194,9 +192,12 @@ public void deleteMembership() throws Exception { @Test public void listPendingTeamInvitations() throws Exception { final CompletableFuture> fixture = - completedFuture(json.fromJson(getFixture("list_team_invitations.json"), LIST_PENDING_TEAM_INVITATIONS)); - when(github.request("/orgs/github/teams/1/invitations", LIST_PENDING_TEAM_INVITATIONS)).thenReturn(fixture); - final List pendingInvitations = teamClient.listPendingTeamInvitations("1").get(); + completedFuture( + json.fromJson(getFixture("list_team_invitations.json"), LIST_PENDING_TEAM_INVITATIONS)); + when(github.request("/orgs/github/teams/1/invitations", LIST_PENDING_TEAM_INVITATIONS)) + .thenReturn(fixture); + final List pendingInvitations = + teamClient.listPendingTeamInvitations("1").get(); assertThat(pendingInvitations.get(0).login(), is("octocat")); assertThat(pendingInvitations.get(1).id(), is(2)); assertThat(pendingInvitations.size(), is(2));