From 8bbec66f64fbe79b8b0847da391c1c762b984712 Mon Sep 17 00:00:00 2001 From: Angular2guy Date: Thu, 11 Jul 2024 16:34:48 +0200 Subject: [PATCH] feat: first step tests --- .../controller/CodeGenerationController.java | 23 +++++++++++++++---- .../domain/model/dto/GithubSources.java | 17 ++++++++++++++ .../service/CodeGenerationService.java | 16 +++++++------ 3 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 backend/src/main/java/ch/xxx/aidoclibchat/domain/model/dto/GithubSources.java diff --git a/backend/src/main/java/ch/xxx/aidoclibchat/adapter/controller/CodeGenerationController.java b/backend/src/main/java/ch/xxx/aidoclibchat/adapter/controller/CodeGenerationController.java index 84f2804..c75b320 100644 --- a/backend/src/main/java/ch/xxx/aidoclibchat/adapter/controller/CodeGenerationController.java +++ b/backend/src/main/java/ch/xxx/aidoclibchat/adapter/controller/CodeGenerationController.java @@ -14,6 +14,8 @@ 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; @@ -21,19 +23,32 @@ 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); } } diff --git a/backend/src/main/java/ch/xxx/aidoclibchat/domain/model/dto/GithubSources.java b/backend/src/main/java/ch/xxx/aidoclibchat/domain/model/dto/GithubSources.java new file mode 100644 index 0000000..d8d62ff --- /dev/null +++ b/backend/src/main/java/ch/xxx/aidoclibchat/domain/model/dto/GithubSources.java @@ -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) { + +} diff --git a/backend/src/main/java/ch/xxx/aidoclibchat/usecase/service/CodeGenerationService.java b/backend/src/main/java/ch/xxx/aidoclibchat/usecase/service/CodeGenerationService.java index 6da6ca8..899b8fa 100644 --- a/backend/src/main/java/ch/xxx/aidoclibchat/usecase/service/CodeGenerationService.java +++ b/backend/src/main/java/ch/xxx/aidoclibchat/usecase/service/CodeGenerationService.java @@ -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} """; @@ -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,