Skip to content

Commit de7350d

Browse files
feat(#1660): Add RepositoryStatistics class and corresponding test
1 parent 023ee04 commit de7350d

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.jcabi.github;
2+
3+
import java.io.IOException;
4+
import java.util.Arrays;
5+
import java.util.Map;
6+
import java.util.stream.Collectors;
7+
import javax.json.JsonObject;
8+
import javax.json.JsonValue;
9+
import org.cactoos.map.MapEntry;
10+
import org.cactoos.map.MapOf;
11+
12+
public class RepositoryStatistics {
13+
private final Repo repo;
14+
15+
public RepositoryStatistics(final Repo repo) {
16+
this.repo = repo;
17+
}
18+
19+
Map<String, Object> toMap() throws IOException {
20+
final JsonObject json = repo.json();
21+
return new MapOf<>(
22+
Arrays.stream(KEY.values()).map(key -> key.toEntry(json)).collect(Collectors.toList())
23+
);
24+
}
25+
26+
private enum KEY {
27+
LANGUAGE("language"),
28+
FORKS_COUNT("forks_count"),
29+
STARGAZERS_COUNT("stargazers_count"),
30+
WATCHERS_COUNT("watchers_count"),
31+
SIZE("size"),
32+
OPEN_ISSUES_COUNT("open_issues_count"),
33+
CREATED_AT("created_at"),
34+
UPDATED_AT("updated_at");
35+
36+
private final String key;
37+
38+
KEY(final String key) {
39+
this.key = key;
40+
}
41+
42+
private MapEntry<String, Object> toEntry(final JsonObject object) {
43+
final String k = this.getKey();
44+
final JsonValue value = object.get(k);
45+
return new MapEntry<>(k, value.toString());
46+
}
47+
48+
public String getKey() {
49+
return key;
50+
}
51+
}
52+
}
53+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.jcabi.github;
2+
3+
import java.io.IOException;
4+
import java.util.Map;
5+
import org.hamcrest.MatcherAssert;
6+
import org.hamcrest.Matchers;
7+
import org.junit.Test;
8+
9+
/**
10+
* Test case for {@link RepositoryStatistics}.
11+
*
12+
*/
13+
public class RepositoryStatisticsTest {
14+
15+
@Test
16+
public void gathersBasicStatisticsFromRepo() throws IOException {
17+
MatcherAssert.assertThat(
18+
"We expect to have basic statistics from repo",
19+
new RepositoryStatistics(null).toMap(),
20+
Matchers.allOf(
21+
Matchers.hasKey("language"),
22+
Matchers.hasKey("forks_count"),
23+
Matchers.hasKey("stargazers_count"),
24+
Matchers.hasKey("watchers_count"),
25+
Matchers.hasKey("size"),
26+
Matchers.hasKey("open_issues_count"),
27+
Matchers.hasKey("created_at"),
28+
Matchers.hasKey("updated_at")
29+
)
30+
);
31+
}
32+
}

0 commit comments

Comments
 (0)