Skip to content

Commit

Permalink
feat: first step tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Angular2Guy committed Jul 11, 2024
1 parent f67450c commit 8bbec66
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,41 @@

import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Optional;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import ch.xxx.aidoclibchat.domain.model.dto.GithubSource;
import ch.xxx.aidoclibchat.domain.model.dto.GithubSources;
import ch.xxx.aidoclibchat.usecase.service.CodeGenerationService;

@RestController
@RequestMapping("rest/code-generation")
public class CodeGenerationController {
private final CodeGenerationService codeGenerationService;

public CodeGenerationController(CodeGenerationService codeGenerationService) {
this.codeGenerationService = codeGenerationService;
}

@GetMapping("/test")
public GithubSource getGenerateTests(@RequestParam("url") String url) {
return this.codeGenerationService.createTestSources(URLDecoder.decode(url, StandardCharsets.UTF_8), true);
public String getGenerateTests(@RequestParam("url") String url,
@RequestParam(required = false) String testUrl) {
return this.codeGenerationService.generateTest(URLDecoder.decode(url, StandardCharsets.UTF_8),URLDecoder.decode(testUrl, StandardCharsets.UTF_8));
}

@GetMapping("/sources")
public GithubSources getSources(@RequestParam("url") String url, @RequestParam(required = false) String testUrl) {
var sources = this.codeGenerationService.createTestSources(URLDecoder.decode(url, StandardCharsets.UTF_8),
true);
var test = Optional.ofNullable(testUrl)
.map(myTestUrl -> this.codeGenerationService
.createTestSources(URLDecoder.decode(myTestUrl, StandardCharsets.UTF_8), false))
.orElse(new GithubSource("none", "none", List.of(), List.of()));
return new GithubSources(sources, test);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright 2023 Sven Loesekann
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ch.xxx.aidoclibchat.domain.model.dto;

public record GithubSources(GithubSource sources, GithubSource testExample) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,11 @@ public class CodeGenerationService {
private final String ollamaPrompt = """
You are an assistant to generate spring tests for the class under test.
Generate tests for this class:
{classToTest}
Use these classes as context for the tests:
{contextClasses}
Use this class as test example class:
{testExample}
""";

Expand All @@ -52,10 +48,16 @@ public CodeGenerationService(GithubClient githubClient, ChatClient chatClient) {
this.chatClient = chatClient;
}

public String generateTest(String url) {
public String generateTest(String url, String testUrl) {
var githubSource = this.createTestSources(url, true);
String contextClasses = "";
String testExample = "";
var githubTestSource = this.createTestSources(testUrl, false);
String contextClasses = githubSource.dependencies().stream()
.map(myGithubSource -> myGithubSource.sourceName() + ":" + System.getProperty("line.separator")
+ myGithubSource.lines().stream()
.collect(Collectors.joining(System.getProperty("line.separator"))))
.collect(Collectors.joining(System.getProperty("line.separator")));
String testExample = "Use this class as test example:" + System.getProperty("line.separator")
+ githubTestSource.lines().stream().collect(Collectors.joining(System.getProperty("line.separator")));
String classToTest = githubSource.lines().stream()
.collect(Collectors.joining(System.getProperty("line.separator")));
var response = chatClient.call(new Prompt(new AssistantMessage(this.ollamaPrompt,
Expand Down

0 comments on commit 8bbec66

Please sign in to comment.