Skip to content

Commit

Permalink
(#58) Merge pull request #156 from iakunin/58
Browse files Browse the repository at this point in the history
  • Loading branch information
iakunin committed May 4, 2020
2 parents 9f9d000 + 3946e03 commit 9aa5350
Show file tree
Hide file tree
Showing 17 changed files with 248 additions and 16 deletions.
14 changes: 5 additions & 9 deletions src/main/java/dev/iakunin/codexiabot/bot/toosmall/ExactItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.cactoos.text.Lowered;
import org.cactoos.text.NoNulls;
import org.cactoos.text.TextEnvelope;
import org.cactoos.text.TextOf;

@Slf4j
public final class ExactItem implements Scalar<Optional<Item>> {
Expand Down Expand Up @@ -155,21 +154,18 @@ private MappedText(Text source) {
this(
source,
new MapOf<>(
new MapEntry<>(
new TextOf("Vue"),
new TextOf("JavaScript")
)
new MapEntry<>("Vue", "JavaScript")
)
);
}

private MappedText(Text source, Map<Text, Text> map) {
private MappedText(Text source, Map<String, String> map) {
super(
new Unchecked<>(
() -> map.getOrDefault(
new NoNulls(source),
new NoNulls(source)
).asString()
new NoNulls(source).toString(),
new NoNulls(source).toString()
)
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@
import dev.iakunin.codexiabot.github.repository.GithubRepoStatRepository;
import dev.iakunin.codexiabot.github.sdk.CodetabsClient;
import java.util.Objects;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@AllArgsConstructor(onConstructor_={@Autowired})
public final class LinesOfCode implements Runnable {

private final GithubRepoRepository githubRepoRepository;
Expand All @@ -23,6 +21,20 @@ public final class LinesOfCode implements Runnable {

private final CodetabsClient codetabsClient;

private final Integer delay;

public LinesOfCode(
GithubRepoRepository githubRepoRepository,
GithubRepoStatRepository githubRepoStatRepository,
CodetabsClient codetabsClient,
@Value("${app.cron.github.stat.lines-of-code.delay}") Integer delay
) {
this.githubRepoRepository = githubRepoRepository;
this.githubRepoStatRepository = githubRepoStatRepository;
this.codetabsClient = codetabsClient;
this.delay = delay;
}

public void run() {
this.githubRepoRepository.findAllWithoutLinesOfCode().forEach(
githubRepo -> {
Expand All @@ -31,7 +43,8 @@ public void run() {
this.githubRepoStatRepository.save(
GithubRepoStat.Factory.from(
Objects.requireNonNull(
this.codetabsClient.getLinesOfCode(githubRepo.getFullName())
this.codetabsClient
.getLinesOfCode(githubRepo.getFullName())
.getBody()
)
).setGithubRepo(githubRepo)
Expand All @@ -49,7 +62,7 @@ public void run() {
}
} finally {
log.debug("Sleeping...");
sleep(5000);
sleep(this.delay);
}
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

@FeignClient(
name = "codetabsClient",
url = "https://api.codetabs.com/v1/",
url = "${app.codetabs.base-url}",
configuration = GeneralClientConfiguration.class
)
public interface CodetabsClient {
Expand Down
10 changes: 10 additions & 0 deletions src/main/resources/META-INF/spring-configuration-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,16 @@
"name": "app.reddit.client-secret",
"type": "java.lang.String",
"description": "Reddit client secret."
},
{
"name": "app.codetabs.base-url",
"type": "java.lang.String",
"description": "Base url of Codetabs."
},
{
"name": "app.cron.github.stat.lines-of-code.delay",
"type": "java.lang.Integer",
"description": "Delay for github.stat.lines-of-code cron."
}
]
}
4 changes: 4 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@ app.reddit.username=<insert-your-reddit-username-here>
app.reddit.password=<insert-your-reddit-password-here>
app.reddit.client-id=<insert-your-reddit-client-id-here>
app.reddit.client-secret=<insert-your-reddit-client-secret-here>

app.codetabs.base-url=https://api.codetabs.com/v1/

app.cron.github.stat.lines-of-code.delay=5000
Original file line number Diff line number Diff line change
@@ -1,13 +1,119 @@
package dev.iakunin.codexiabot.github.cron.stat;

import com.github.database.rider.core.api.dataset.DataSet;
import com.github.database.rider.core.api.dataset.ExpectedDataSet;
import com.github.tomakehurst.wiremock.client.WireMock;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.ok;
import dev.iakunin.codexiabot.AbstractIntegrationTest;
import dev.iakunin.codexiabot.util.WireMockServer;
import org.cactoos.io.ResourceOf;
import org.cactoos.text.TextOf;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.ContextConfiguration;

@ContextConfiguration(initializers = LinesOfCodeIntegrationTest.Initializer.class)
public class LinesOfCodeIntegrationTest extends AbstractIntegrationTest {

@Autowired
private LinesOfCode linesOfCode;

// @todo #10 Implement LinesOfCodeIntegrationTest
@Test
@DataSet(
value = "db-rider/github/cron/stat/lines-of-code/initial/emptyDatabase.yml",
cleanBefore = true, cleanAfter = true
)
@ExpectedDataSet("db-rider/github/cron/stat/lines-of-code/expected/emptyDatabase.yml")
public void emptyDatabase() {
linesOfCode.run();
}

@Test
@DataSet(
value = "db-rider/github/cron/stat/lines-of-code/initial/processedRepo.yml",
cleanBefore = true, cleanAfter = true
)
@ExpectedDataSet("db-rider/github/cron/stat/lines-of-code/expected/processedRepo.yml")
public void processedRepo() {
linesOfCode.run();
}

@Test
@DataSet(
value = "db-rider/github/cron/stat/lines-of-code/initial/happyPath.yml",
cleanBefore = true, cleanAfter = true
)
@ExpectedDataSet("db-rider/github/cron/stat/lines-of-code/expected/happyPath.yml")
public void happyPath() {
WireMockServer.getInstance().stubFor(
get(WireMock.urlPathEqualTo("/loc"))
.willReturn(ok()
.withHeader("Content-Type", "application/json")
.withBody(
new TextOf(
new ResourceOf(
"wiremock/github/cron/stat/lines-of-code/happyPath.json"
)
).toString()
)
)
);

linesOfCode.run();
}

@Test
@DataSet(
value = "db-rider/github/cron/stat/lines-of-code/initial/tooManyRequests.yml",
cleanBefore = true, cleanAfter = true
)
@ExpectedDataSet("db-rider/github/cron/stat/lines-of-code/expected/tooManyRequests.yml")
public void tooManyRequests() {
WireMockServer.getInstance().stubFor(
get(WireMock.urlPathEqualTo("/loc"))
.willReturn(aResponse()
.withStatus(429)
)
);

linesOfCode.run();
}

@Test
@DataSet(
value = "db-rider/github/cron/stat/lines-of-code/initial/codetabsException.yml",
cleanBefore = true, cleanAfter = true
)
@ExpectedDataSet("db-rider/github/cron/stat/lines-of-code/expected/codetabsException.yml")
public void codetabsException() {
WireMockServer.getInstance().stubFor(
get(WireMock.urlPathEqualTo("/loc"))
.willReturn(aResponse()
.withStatus(500)
)
);

linesOfCode.run();
}

@AfterEach
void after() {
WireMockServer.getInstance().resetAll();
}

static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
TestPropertyValues.of(
"app.codetabs.base-url=" + WireMockServer.getInstance().baseUrl(),
"app.cron.github.stat.lines-of-code.delay=0"
).applyTo(applicationContext.getEnvironment());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
github_repo:
-
id: 1
external_id: "150713223"
full_name: "test-project1/test-repo1"

github_repo_stat:
-
github_repo_id: 1
type: "LINES_OF_CODE"
stat: '{"itemList":null,"type":"LINES_OF_CODE"}'
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
github_repo: []

github_repo_stat: []
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
github_repo:
-
id: 1
external_id: "150713223"
full_name: "test-project1/test-repo1"

github_repo_stat:
-
github_repo_id: 1
type: "LINES_OF_CODE"
stat: '{"itemList":[{"language":"TypeScript","files":84,"lines":9415,"blanks":1200,"comments":682,"linesOfCode":3533},{"language":"Total","files":173,"lines":31482,"blanks":1446,"comments":706,"linesOfCode":29330}],"type":"LINES_OF_CODE"}'
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
github_repo:
-
id: 1
external_id: "150713223"
full_name: "test-project1/test-repo1"

github_repo_stat:
-
id: 1
github_repo_id: 1
type: "LINES_OF_CODE"
stat: '{"itemList":null,"type":"LINES_OF_CODE"}'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
github_repo:
-
id: 1
external_id: "150713223"
full_name: "test-project1/test-repo1"

github_repo_stat: []
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
github_repo:
-
id: 1
uuid: "groovy: UUID.randomUUID().toString()"
external_id: "150713223"
full_name: "test-project1/test-repo1"

github_repo_stat: []
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
github_repo: []

github_repo_stat: []
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
github_repo:
-
id: 1
uuid: "groovy: UUID.randomUUID().toString()"
external_id: "150713223"
full_name: "test-project1/test-repo1"

github_repo_stat: []
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
github_repo:
-
id: 1
uuid: "groovy: UUID.randomUUID().toString()"
external_id: "150713223"
full_name: "test-project1/test-repo1"

github_repo_stat:
-
id: 1
uuid: "groovy: UUID.randomUUID().toString()"
github_repo_id: 1
type: "LINES_OF_CODE"
stat: '{"itemList":null,"type":"LINES_OF_CODE"}'
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
github_repo:
-
id: 1
uuid: "groovy: UUID.randomUUID().toString()"
external_id: "150713223"
full_name: "test-project1/test-repo1"

github_repo_stat: []
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"language": "TypeScript",
"files": 84,
"lines": 9415,
"blanks": 1200,
"comments": 682,
"linesOfCode": 3533
},
{
"language": "Total",
"files": 173,
"lines": 31482,
"blanks": 1446,
"comments": 706,
"linesOfCode": 29330
}
]

1 comment on commit 9aa5350

@0pdd
Copy link

@0pdd 0pdd commented on 9aa5350 May 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 10-6d9090eb disappeared from src/test/java/dev/iakunin/codexiabot/github/cron/stat/LinesOfCodeIntegrationTest.java, that's why I closed #58. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.