Skip to content

Commit

Permalink
feat: generate test first step
Browse files Browse the repository at this point in the history
  • Loading branch information
Angular2Guy committed Jul 10, 2024
1 parent a19c675 commit 9ce8f06
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ public CodeGenerationController(CodeGenerationService codeGenerationService) {

@GetMapping("/test")
public GithubSource getGenerateTests(@RequestParam("url") String url) {
return this.codeGenerationService.generateTests(URLDecoder.decode(url, StandardCharsets.UTF_8), true);
return this.codeGenerationService.createTestSources(URLDecoder.decode(url, StandardCharsets.UTF_8), true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.ChatClient;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.stereotype.Service;

import ch.xxx.aidoclibchat.domain.model.dto.GithubClient;
Expand All @@ -27,12 +30,39 @@
public class CodeGenerationService {
private static final Logger LOGGER = LoggerFactory.getLogger(CodeGenerationService.class);
private final GithubClient githubClient;
private final ChatClient chatClient;
private final String ollamaPrompt = """
You are an assistant to generate spring tests for the class under test.
Generate tests for this class:
public CodeGenerationService(GithubClient githubClient) {
%s
Use these classes as context for the tests:
%s
Use this class as test example class:
%s
""";

public CodeGenerationService(GithubClient githubClient, ChatClient chatClient) {
this.githubClient = githubClient;
this.chatClient = chatClient;
}

public String generateTest(String url) {
var githubSource = this.createTestSources(url, true);
String contextClasses = "";
String testExample = "";
String myPrompt = String.format(this.ollamaPrompt,
githubSource.lines().stream().collect(Collectors.joining(System.getProperty("line.separator"))),
contextClasses, testExample);
var response = chatClient.call(new Prompt(new AssistantMessage(myPrompt)));
return response.getResult().getOutput().getContent();
}

public GithubSource generateTests(String url, final boolean referencedSources) {
public GithubSource createTestSources(String url, final boolean referencedSources) {
final var myUrl = url.replace("https://github.com", GithubClient.GITHUB_BASE_URL).replace("/blob", "");
var result = this.githubClient.readSourceFile(myUrl);
final var isComment = new AtomicBoolean(false);
Expand All @@ -50,7 +80,7 @@ private List<GithubSource> createDependencies(final boolean referencedSources, f
.filter(myLine -> myLine.contains(basePackage))
.map(myLine -> String.format("%s%s%s", myUrl.split(basePackage.replace(".", "/"))[0].trim(),
myLine.split("import")[1].split(";")[0].replaceAll("\\.", "/").trim(), ".java"))
.map(myLine -> this.generateTests(myLine, false)).toList();
.map(myLine -> this.createTestSources(myLine, false)).toList();
}

private boolean filterComments(AtomicBoolean isComment, String myLine) {
Expand Down

0 comments on commit 9ce8f06

Please sign in to comment.