From 19ac0988217d6740d2c737a878b40020cd3cc0ca Mon Sep 17 00:00:00 2001 From: t-takahashi Date: Wed, 25 Dec 2024 08:58:45 +0900 Subject: [PATCH] fix groupingby logic --- .../mrls/service/MergeRequestService.java | 24 +++--- .../mrls/service/MergeRequestServiceTest.java | 76 ++++++++++++++----- 2 files changed, 69 insertions(+), 31 deletions(-) diff --git a/src/main/java/io/github/tttol/mrls/service/MergeRequestService.java b/src/main/java/io/github/tttol/mrls/service/MergeRequestService.java index 32fb922..6ebfd4b 100644 --- a/src/main/java/io/github/tttol/mrls/service/MergeRequestService.java +++ b/src/main/java/io/github/tttol/mrls/service/MergeRequestService.java @@ -1,13 +1,5 @@ package io.github.tttol.mrls.service; -import io.github.tttol.mrls.dto.GitLabMergeRequestApiResponseDto; -import io.github.tttol.mrls.external.GitLabApiExecutor; -import io.github.tttol.mrls.form.MrDetailForm; -import io.github.tttol.mrls.form.MrInfoForm; -import io.github.tttol.mrls.form.UserForm; -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Service; - import java.time.ZoneOffset; import java.util.Comparator; import java.util.List; @@ -15,21 +7,31 @@ import java.util.Optional; import java.util.stream.Collectors; +import org.springframework.stereotype.Service; + +import io.github.tttol.mrls.dto.GitLabMergeRequestApiResponseDto; +import io.github.tttol.mrls.external.GitLabApiExecutor; +import io.github.tttol.mrls.form.MrDetailForm; +import io.github.tttol.mrls.form.MrInfoForm; +import io.github.tttol.mrls.form.UserForm; +import lombok.RequiredArgsConstructor; + @Service @RequiredArgsConstructor public class MergeRequestService { private final GitLabApiExecutor gitLabApiExecutor; + private final int NONE_ASSIGNEE = -1; public List get(final String accessToken) { final var mergeRequestInfoDtos = executeGitLabApi(accessToken); return mergeRequestInfoDtos.stream() .collect(Collectors.groupingBy( e -> Objects.isNull(e.getAssignee()) ? - e.getAuthor().getId() : e.getAssignee().getId() + NONE_ASSIGNEE : e.getAssignee().getId() ) ) - .values().stream().map(this::generateForm) + .values().stream().map(this::generateMrInfoForm) .sorted(Comparator.comparing(e -> e.assignee().id())) .toList(); } @@ -38,7 +40,7 @@ private List executeGitLabApi(final String acc return gitLabApiExecutor.getMergeRequests(accessToken); } - private MrInfoForm generateForm(final List responseDtos) { + private MrInfoForm generateMrInfoForm(final List responseDtos) { final var responseDto = responseDtos.stream().findAny().orElseThrow(); final var assigneeForm = Optional.ofNullable(responseDto.getAssignee()) .map(assignee -> new UserForm( diff --git a/src/test/java/io/github/tttol/mrls/service/MergeRequestServiceTest.java b/src/test/java/io/github/tttol/mrls/service/MergeRequestServiceTest.java index 5cc3f33..4cd6a00 100644 --- a/src/test/java/io/github/tttol/mrls/service/MergeRequestServiceTest.java +++ b/src/test/java/io/github/tttol/mrls/service/MergeRequestServiceTest.java @@ -1,23 +1,28 @@ package io.github.tttol.mrls.service; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doReturn; + +import java.time.OffsetDateTime; +import java.time.ZoneOffset; +import java.util.List; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + import io.github.tttol.mrls.dto.GitLabMergeRequestApiResponseDto; import io.github.tttol.mrls.dto.UserDto; import io.github.tttol.mrls.external.GitLabApiExecutor; import io.github.tttol.mrls.form.MrDetailForm; import io.github.tttol.mrls.form.MrInfoForm; import io.github.tttol.mrls.form.UserForm; -import org.junit.jupiter.api.*; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -import java.time.OffsetDateTime; -import java.time.ZoneOffset; -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.doReturn; public class MergeRequestServiceTest { @@ -164,25 +169,40 @@ void formTest() { @Test @DisplayName("MRのAssigneeが未指定の場合") void noAssigneeTest() { + final var assignee = UserDto.builder() + .id(11) + .username("author_username11") + .name("author_name11") + .state("active") + .build(); final var mergeRequestInfoDtos = List.of( + // authors are same + // assignee is null GitLabMergeRequestApiResponseDto.builder() .assignee(null) - .author(UserDto.builder() - .id(11) - .username("author_username11") - .name("author_name11") - .state("active") - .build()) + .author(assignee) .webUrl("url1") .title("title1") .upvotes(0) .labels(List.of()) .createdAt(OffsetDateTime.of(1970, 1, 1, 9, 0, 0, 0, ZoneOffset.of("+09:00"))) .updatedAt(OffsetDateTime.of(2000, 1, 1, 9, 0, 0, 0, ZoneOffset.of("+09:00"))) + .build(), + // assignee is author_username11 + GitLabMergeRequestApiResponseDto.builder() + .assignee(assignee) + .author(assignee) + .webUrl("url2") + .title("title2") + .upvotes(0) + .labels(List.of()) + .createdAt(OffsetDateTime.of(1970, 1, 1, 9, 0, 0, 0, ZoneOffset.of("+09:00"))) + .updatedAt(OffsetDateTime.of(2000, 1, 1, 9, 0, 0, 0, ZoneOffset.of("+09:00"))) .build() ); doReturn(mergeRequestInfoDtos).when(gitLabApiExecutor).getMergeRequests(anyString()); - final var expected = List.of(new MrInfoForm( + final var expected = List.of( + new MrInfoForm( UserForm.empty(), List.of( new MrDetailForm( @@ -196,7 +216,23 @@ void noAssigneeTest() { ) ), 1 - )); + ), + new MrInfoForm( + new UserForm(11, "author_username11", "author_name11", "active", null, null), + List.of( + new MrDetailForm( + "title2", + "url2", + new UserForm(11, "author_username11", "author_name11", "active", null, null), + 0, + List.of(), + OffsetDateTime.of(1970, 1, 1, 9, 0, 0, 0, ZoneOffset.of("+09:00")), + OffsetDateTime.of(2000, 1, 1, 9, 0, 0, 0, ZoneOffset.of("+09:00")) + ) + ), + 1 + ) + ); final var actual = mergeRequestService.get(anyString());