-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add AggregateCommits use case
#71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
src/main/java/io/pakland/mdas/githubstats/application/AggregateCommits.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package io.pakland.mdas.githubstats.application; | ||
|
|
||
| import io.pakland.mdas.githubstats.domain.Commit; | ||
| import io.pakland.mdas.githubstats.domain.CommitAggregation; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Service | ||
| public class AggregateCommits { | ||
|
|
||
| public AggregateCommits() {} | ||
|
|
||
| public CommitAggregation execute(List<Commit> commits) { | ||
| return CommitAggregation.aggregate(commits); | ||
| } | ||
| } |
30 changes: 30 additions & 0 deletions
30
src/main/java/io/pakland/mdas/githubstats/domain/CommitAggregation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package io.pakland.mdas.githubstats.domain; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class CommitAggregation { | ||
|
|
||
| private int numCommits; | ||
| private int linesAdded; | ||
| private int linesRemoved; | ||
|
|
||
| public static CommitAggregation aggregate(List<Commit> commits) { | ||
| CommitAggregation commitAggregation = new CommitAggregation(); | ||
| commitAggregation.numCommits = (int) commits.stream().distinct().count(); | ||
| commitAggregation.linesAdded = commits.stream().mapToInt(Commit::getAdditions).sum(); | ||
| commitAggregation.linesRemoved = commits.stream().mapToInt(Commit::getDeletions).sum(); | ||
| return commitAggregation; | ||
| } | ||
|
|
||
| public int getNumCommits() { | ||
| return numCommits; | ||
| } | ||
|
|
||
| public int getLinesAdded() { | ||
| return linesAdded; | ||
| } | ||
|
|
||
| public int getLinesRemoved() { | ||
| return linesRemoved; | ||
| } | ||
| } | ||
67 changes: 67 additions & 0 deletions
67
src/test/java/io/pakland/mdas/githubstats/application/AggregateCommitsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package io.pakland.mdas.githubstats.application; | ||
|
|
||
| import io.pakland.mdas.githubstats.domain.Commit; | ||
| import io.pakland.mdas.githubstats.domain.CommitAggregation; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Random; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| public class AggregateCommitsTest { | ||
|
|
||
| @Test | ||
| public void aggregatingCommits_shouldGiveValidNumCommits() { | ||
| List<Commit> commits = new ArrayList<>(); | ||
|
|
||
| int numCommits = new Random().nextInt(10); | ||
|
|
||
| for (int i = 0; i < numCommits; i++) { | ||
| commits.add(mock(Commit.class)); | ||
| } | ||
|
|
||
| CommitAggregation commitAggregation = CommitAggregation.aggregate(commits); | ||
| assertEquals(commitAggregation.getNumCommits(), numCommits); | ||
| } | ||
|
|
||
| @Test | ||
| public void aggregatingCommits_shouldGiveValidLinesAdded() { | ||
| List<Commit> commits = new ArrayList<>(); | ||
|
|
||
| int totalLines = 0; | ||
|
|
||
| for (int i = 0; i < 10; i++) { | ||
| int numLines = new Random().nextInt(1000); | ||
| Commit commit = mock(Commit.class); | ||
| when(commit.getAdditions()).thenReturn(numLines); | ||
| commits.add(commit); | ||
| totalLines += numLines; | ||
| } | ||
|
|
||
| CommitAggregation commitAggregation = CommitAggregation.aggregate(commits); | ||
| assertEquals(commitAggregation.getLinesAdded(), totalLines); | ||
| } | ||
|
|
||
| @Test | ||
| public void aggregatingCommits_shouldGiveValidLinesRemoved() { | ||
| List<Commit> commits = new ArrayList<>(); | ||
|
|
||
| int totalLines = 0; | ||
|
|
||
| for (int i = 0; i < 10; i++) { | ||
| int numLines = new Random().nextInt(1000); | ||
| Commit commit = mock(Commit.class); | ||
| when(commit.getDeletions()).thenReturn(numLines); | ||
| commits.add(commit); | ||
| totalLines += numLines; | ||
| } | ||
|
|
||
| CommitAggregation commitAggregation = CommitAggregation.aggregate(commits); | ||
| assertEquals(commitAggregation.getLinesRemoved(), totalLines); | ||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we use primitives or classes for numbers and so on?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's keep them as primitives as in the Commit we have primitive types for additions and deletions. Afterwards we can open an issue and homogenize criteria in all places of the code if we find it better.