From 3f9f93bdff232b55ba937d835c12c7d011139aa2 Mon Sep 17 00:00:00 2001 From: Mohamed Date: Sun, 10 Nov 2024 20:02:27 +0100 Subject: [PATCH] Add langchain4j to github actions ci --- .../SampleLangChain4jModuleFactory.java | 10 +++--- .../primary/ChatResource.java.mustache | 2 +- .../primary/ChatResourceTest.java.mustache | 31 +++++++++++++++++++ 3 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 src/main/resources/generator/server/springboot/mvc/sample/langchain4j/test/sample/infrastructure/primary/ChatResourceTest.java.mustache diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/mvc/sample/langchain4j/domain/SampleLangChain4jModuleFactory.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/mvc/sample/langchain4j/domain/SampleLangChain4jModuleFactory.java index 608d59defdb..8edc458dbe6 100644 --- a/src/main/java/tech/jhipster/lite/generator/server/springboot/mvc/sample/langchain4j/domain/SampleLangChain4jModuleFactory.java +++ b/src/main/java/tech/jhipster/lite/generator/server/springboot/mvc/sample/langchain4j/domain/SampleLangChain4jModuleFactory.java @@ -1,8 +1,6 @@ package tech.jhipster.lite.generator.server.springboot.mvc.sample.langchain4j.domain; -import static tech.jhipster.lite.module.domain.JHipsterModule.from; -import static tech.jhipster.lite.module.domain.JHipsterModule.moduleBuilder; -import static tech.jhipster.lite.module.domain.JHipsterModule.toSrcMainJava; +import static tech.jhipster.lite.module.domain.JHipsterModule.*; import tech.jhipster.lite.module.domain.JHipsterModule; import tech.jhipster.lite.module.domain.file.JHipsterDestination; @@ -23,12 +21,16 @@ public JHipsterModule buildModule(JHipsterModuleProperties properties) { String packagePath = properties.packagePath(); JHipsterDestination mainDestination = toSrcMainJava().append(packagePath).append(SAMPLE); + JHipsterDestination testDestination = toSrcTestJava().append(packagePath).append(SAMPLE); //@formatter:off return moduleBuilder(properties) .files() .batch(SOURCE.append("main").append(PRIMARY), mainDestination.append(PRIMARY)) - .addTemplate("ChatResource.java.mustache") + .addTemplate("ChatResource.java") + .and() + .batch(SOURCE.append("test").append(SAMPLE).append(PRIMARY), testDestination.append(PRIMARY)) + .addTemplate("ChatResourceTest.java") .and() .and() .build(); diff --git a/src/main/resources/generator/server/springboot/mvc/sample/langchain4j/main/infrastructure/primary/ChatResource.java.mustache b/src/main/resources/generator/server/springboot/mvc/sample/langchain4j/main/infrastructure/primary/ChatResource.java.mustache index c56f31ccf20..dd642e3b365 100644 --- a/src/main/resources/generator/server/springboot/mvc/sample/langchain4j/main/infrastructure/primary/ChatResource.java.mustache +++ b/src/main/resources/generator/server/springboot/mvc/sample/langchain4j/main/infrastructure/primary/ChatResource.java.mustache @@ -17,7 +17,7 @@ class ChatResource { } @GetMapping("/chat") - public String model(@RequestParam(value = "message", defaultValue = "Hello") String message) { + public String send(@RequestParam(value = "message", defaultValue = "Hello") String message) { return chatLanguageModel.generate(message); } } diff --git a/src/main/resources/generator/server/springboot/mvc/sample/langchain4j/test/sample/infrastructure/primary/ChatResourceTest.java.mustache b/src/main/resources/generator/server/springboot/mvc/sample/langchain4j/test/sample/infrastructure/primary/ChatResourceTest.java.mustache new file mode 100644 index 00000000000..45fdfbcf374 --- /dev/null +++ b/src/main/resources/generator/server/springboot/mvc/sample/langchain4j/test/sample/infrastructure/primary/ChatResourceTest.java.mustache @@ -0,0 +1,31 @@ +package {{packageName}}.sample.infrastructure.primary; + +import static org.assertj.core.api.Assertions.*; + +import {{packageName}}.UnitTest; +import dev.langchain4j.model.chat.ChatLanguageModel; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; + +@UnitTest +@ExtendWith(MockitoExtension.class) +class ChatResourceTest { + + private static final String ANSWER = "Hello! How can I assist you today?"; + @Mock + private ChatLanguageModel chatLanguageModel; + + @InjectMocks + private ChatResource chat; + + @Test + void shouldBeUnauthorizedForUnknownAccount() { + Mockito.when(chatLanguageModel.generate("Hello")).thenReturn(ANSWER); + + assertThat(chat.send("Hello")).isEqualTo(ANSWER); + } +}