Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(#1660): Add RepositoryStatistics class and corresponding test
- Loading branch information
1 parent
023ee04
commit de7350d
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,53 @@ | ||
package com.jcabi.github; | ||
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import javax.json.JsonObject; | ||
import javax.json.JsonValue; | ||
import org.cactoos.map.MapEntry; | ||
import org.cactoos.map.MapOf; | ||
|
||
public class RepositoryStatistics { | ||
private final Repo repo; | ||
|
||
public RepositoryStatistics(final Repo repo) { | ||
this.repo = repo; | ||
} | ||
|
||
Map<String, Object> toMap() throws IOException { | ||
final JsonObject json = repo.json(); | ||
return new MapOf<>( | ||
Arrays.stream(KEY.values()).map(key -> key.toEntry(json)).collect(Collectors.toList()) | ||
); | ||
} | ||
|
||
private enum KEY { | ||
LANGUAGE("language"), | ||
FORKS_COUNT("forks_count"), | ||
STARGAZERS_COUNT("stargazers_count"), | ||
WATCHERS_COUNT("watchers_count"), | ||
SIZE("size"), | ||
OPEN_ISSUES_COUNT("open_issues_count"), | ||
CREATED_AT("created_at"), | ||
UPDATED_AT("updated_at"); | ||
|
||
private final String key; | ||
|
||
KEY(final String key) { | ||
this.key = key; | ||
} | ||
|
||
private MapEntry<String, Object> toEntry(final JsonObject object) { | ||
final String k = this.getKey(); | ||
final JsonValue value = object.get(k); | ||
return new MapEntry<>(k, value.toString()); | ||
} | ||
|
||
public String getKey() { | ||
return key; | ||
} | ||
} | ||
} | ||
|
32 changes: 32 additions & 0 deletions
32
src/test/java/com/jcabi/github/RepositoryStatisticsTest.java
This file contains 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,32 @@ | ||
package com.jcabi.github; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
import org.hamcrest.MatcherAssert; | ||
import org.hamcrest.Matchers; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Test case for {@link RepositoryStatistics}. | ||
* | ||
*/ | ||
public class RepositoryStatisticsTest { | ||
|
||
@Test | ||
public void gathersBasicStatisticsFromRepo() throws IOException { | ||
MatcherAssert.assertThat( | ||
"We expect to have basic statistics from repo", | ||
new RepositoryStatistics(null).toMap(), | ||
Matchers.allOf( | ||
Matchers.hasKey("language"), | ||
Matchers.hasKey("forks_count"), | ||
Matchers.hasKey("stargazers_count"), | ||
Matchers.hasKey("watchers_count"), | ||
Matchers.hasKey("size"), | ||
Matchers.hasKey("open_issues_count"), | ||
Matchers.hasKey("created_at"), | ||
Matchers.hasKey("updated_at") | ||
) | ||
); | ||
} | ||
} |