Skip to content
This repository was archived by the owner on May 14, 2025. It is now read-only.

Commit ea1b5f6

Browse files
committed
Polish
- Have only one constructor in AiCommands and use openAiHandler as Optional to support testing
1 parent 3815f8b commit ea1b5f6

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

src/main/java/org/springframework/cli/command/AiCommands.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021-2023 the original author or authors.
2+
* Copyright 2021-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,11 +16,12 @@
1616

1717
package org.springframework.cli.command;
1818

19+
import java.util.Optional;
20+
1921
import org.jline.utils.AttributedStringBuilder;
2022
import org.jline.utils.AttributedStyle;
2123

2224
import org.springframework.beans.BeansException;
23-
import org.springframework.beans.factory.annotation.Autowired;
2425
import org.springframework.cli.merger.ai.OpenAiHandler;
2526
import org.springframework.cli.merger.ai.service.GenerateCodeAiService;
2627
import org.springframework.cli.util.TerminalMessage;
@@ -39,15 +40,11 @@ public class AiCommands implements ApplicationContextAware {
3940

4041
private ApplicationContext applicationContext;
4142

42-
@Autowired
43-
public AiCommands(TerminalMessage terminalMessage) {
44-
this.terminalMessage = terminalMessage;
45-
this.openAiHandler = new OpenAiHandler(new GenerateCodeAiService(this.terminalMessage));
46-
}
47-
48-
public AiCommands(OpenAiHandler openAiHandler, TerminalMessage terminalMessage) {
43+
public AiCommands(Optional<OpenAiHandler> openAiHandler, TerminalMessage terminalMessage) {
4944
this.terminalMessage = terminalMessage;
50-
this.openAiHandler = openAiHandler;
45+
this.openAiHandler = openAiHandler.orElseGet(() -> {
46+
return new OpenAiHandler(new GenerateCodeAiService(this.terminalMessage));
47+
});
5148
}
5249

5350
@Command(command = "add", description = "Add code to the project from AI for a Spring project.")

src/test/java/org/springframework/cli/command/AiCommandsTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 the original author or authors.
2+
* Copyright 2021-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.cli.command;
1818

1919
import java.nio.file.Path;
20+
import java.util.Optional;
2021

2122
import org.junit.jupiter.api.Test;
2223
import org.junit.jupiter.api.condition.DisabledOnOs;
@@ -42,8 +43,8 @@ void addJpa(@TempDir(cleanup = CleanupMode.ON_SUCCESS) Path workingDir) {
4243
this.contextRunner.withUserConfiguration(MockUserConfig.class).run((context) -> {
4344

4445
StubGenerateCodeAiService stubGenerateCodeAiService = new StubGenerateCodeAiService(TerminalMessage.noop());
45-
AiCommands aiCommands = new AiCommands(new OpenAiHandler(stubGenerateCodeAiService),
46-
TerminalMessage.noop());
46+
OpenAiHandler openAiHandler = new OpenAiHandler(stubGenerateCodeAiService);
47+
AiCommands aiCommands = new AiCommands(Optional.of(openAiHandler), TerminalMessage.noop());
4748

4849
CommandRunner commandRunner = new CommandRunner.Builder(context).prepareProject("rest-service", workingDir)
4950
.build();

0 commit comments

Comments
 (0)