From a870b6d45f9a3fcb29e1b28ef2613634c06c6531 Mon Sep 17 00:00:00 2001 From: Aleksandrs Semjonovs Date: Fri, 10 Oct 2025 15:15:49 +0300 Subject: [PATCH 1/3] Task 1 --- gradlew | 0 .../greeting/GreetingController.java | 23 +++++++++++++++++ .../greeting/GreetingService.java | 25 +++++++++++++++++++ .../GreetingServiceIntegrationTest.java | 21 +++++++++++----- 4 files changed, 63 insertions(+), 6 deletions(-) mode change 100644 => 100755 gradlew create mode 100644 src/main/java/lv/ctco/springboottemplate/greeting/GreetingController.java create mode 100644 src/main/java/lv/ctco/springboottemplate/greeting/GreetingService.java diff --git a/gradlew b/gradlew old mode 100644 new mode 100755 diff --git a/src/main/java/lv/ctco/springboottemplate/greeting/GreetingController.java b/src/main/java/lv/ctco/springboottemplate/greeting/GreetingController.java new file mode 100644 index 0000000..15c371d --- /dev/null +++ b/src/main/java/lv/ctco/springboottemplate/greeting/GreetingController.java @@ -0,0 +1,23 @@ +package lv.ctco.springboottemplate.greeting; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/api/greeting") +@Tag(name = "Greeting Controller", description = "Greeting management endpoints") +public class GreetingController { + + private final GreetingService greetingService; + + public GreetingController(GreetingService greetingService) { + this.greetingService = greetingService; + } + + @GetMapping + @Operation(summary = "Get a greeting with a count of todos") + public String getGreeting() { + return greetingService.greet(); + } +} diff --git a/src/main/java/lv/ctco/springboottemplate/greeting/GreetingService.java b/src/main/java/lv/ctco/springboottemplate/greeting/GreetingService.java new file mode 100644 index 0000000..b6609c0 --- /dev/null +++ b/src/main/java/lv/ctco/springboottemplate/greeting/GreetingService.java @@ -0,0 +1,25 @@ +package lv.ctco.springboottemplate.greeting; + +import lv.ctco.springboottemplate.features.todo.TodoService; +import org.springframework.stereotype.Service; + +@Service +public class GreetingService { + private final TodoService todoService; + + public GreetingService(TodoService todoService) { + this.todoService = todoService; + } + + public String greet() { + long pendingCount = todoService + .getAllTodos() + .stream() + .filter(t -> { + return !t.completed(); + }) + .count(); + + return "Hello from Spring! You have " + pendingCount + " open tasks."; + } +} diff --git a/src/test/java/lv/ctco/springboottemplate/features/greeting/GreetingServiceIntegrationTest.java b/src/test/java/lv/ctco/springboottemplate/features/greeting/GreetingServiceIntegrationTest.java index eda5f13..8e9ab0f 100644 --- a/src/test/java/lv/ctco/springboottemplate/features/greeting/GreetingServiceIntegrationTest.java +++ b/src/test/java/lv/ctco/springboottemplate/features/greeting/GreetingServiceIntegrationTest.java @@ -1,28 +1,38 @@ package lv.ctco.springboottemplate.features.greeting; +import lv.ctco.springboottemplate.features.todo.Todo; +import lv.ctco.springboottemplate.features.todo.TodoRepository; import lv.ctco.springboottemplate.features.todo.TodoService; +import lv.ctco.springboottemplate.greeting.GreetingService; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; import org.springframework.test.context.TestConstructor; +import org.testcontainers.containers.MongoDBContainer; +import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; +import java.util.List; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + /** * Integration test for {@link GreetingService}. * *

This test verifies that GreetingService correctly interacts with {@link TodoService} and * reflects the number of open (not completed) todos in the message. * - *

Initially marked {@link Disabled} to be enabled by the developer after implementation. */ @SpringBootTest -@Disabled("Enable after implementing GreetingService using TodoService") @Testcontainers @TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL) class GreetingServiceIntegrationTest { - /* - - @Container static MongoDBContainer mongoDBContainer = new MongoDBContainer("mongo:6.0.8"); + @Container + static MongoDBContainer mongoDBContainer = new MongoDBContainer("mongo:6.0.8"); @DynamicPropertySource static void setProperties(DynamicPropertyRegistry registry) { @@ -94,5 +104,4 @@ void should_ignore_null_todos_or_null_completed_flags() { // then assertThat(message).contains("1 open task"); } - */ } From 898be38334529d36eb77b3497c408f51a4a8ff95 Mon Sep 17 00:00:00 2001 From: Aleksandrs Semjonovs Date: Mon, 27 Oct 2025 12:13:01 +0200 Subject: [PATCH 2/3] Task 1 - apply spotless/formatting --- .../greeting/GreetingService.java | 26 +++++++++---------- .../GreetingServiceIntegrationTest.java | 12 +++------ 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/src/main/java/lv/ctco/springboottemplate/greeting/GreetingService.java b/src/main/java/lv/ctco/springboottemplate/greeting/GreetingService.java index b6609c0..c924a76 100644 --- a/src/main/java/lv/ctco/springboottemplate/greeting/GreetingService.java +++ b/src/main/java/lv/ctco/springboottemplate/greeting/GreetingService.java @@ -5,21 +5,21 @@ @Service public class GreetingService { - private final TodoService todoService; + private final TodoService todoService; - public GreetingService(TodoService todoService) { - this.todoService = todoService; - } + public GreetingService(TodoService todoService) { + this.todoService = todoService; + } - public String greet() { - long pendingCount = todoService - .getAllTodos() - .stream() - .filter(t -> { - return !t.completed(); + public String greet() { + long pendingCount = + todoService.getAllTodos().stream() + .filter( + t -> { + return !t.completed(); }) - .count(); + .count(); - return "Hello from Spring! You have " + pendingCount + " open tasks."; - } + return "Hello from Spring! You have " + pendingCount + " open tasks."; + } } diff --git a/src/test/java/lv/ctco/springboottemplate/features/greeting/GreetingServiceIntegrationTest.java b/src/test/java/lv/ctco/springboottemplate/features/greeting/GreetingServiceIntegrationTest.java index 8e9ab0f..f4defe1 100644 --- a/src/test/java/lv/ctco/springboottemplate/features/greeting/GreetingServiceIntegrationTest.java +++ b/src/test/java/lv/ctco/springboottemplate/features/greeting/GreetingServiceIntegrationTest.java @@ -1,11 +1,13 @@ package lv.ctco.springboottemplate.features.greeting; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +import java.util.List; import lv.ctco.springboottemplate.features.todo.Todo; import lv.ctco.springboottemplate.features.todo.TodoRepository; import lv.ctco.springboottemplate.features.todo.TodoService; import lv.ctco.springboottemplate.greeting.GreetingService; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.DynamicPropertyRegistry; @@ -15,24 +17,18 @@ import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; -import java.util.List; - -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; - /** * Integration test for {@link GreetingService}. * *

This test verifies that GreetingService correctly interacts with {@link TodoService} and * reflects the number of open (not completed) todos in the message. - * */ @SpringBootTest @Testcontainers @TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL) class GreetingServiceIntegrationTest { - @Container - static MongoDBContainer mongoDBContainer = new MongoDBContainer("mongo:6.0.8"); + @Container static MongoDBContainer mongoDBContainer = new MongoDBContainer("mongo:6.0.8"); @DynamicPropertySource static void setProperties(DynamicPropertyRegistry registry) { From 730e07a0f075f1977856f2c3166bb4c3f864c138 Mon Sep 17 00:00:00 2001 From: Aleksandrs Semjonovs Date: Sun, 9 Nov 2025 14:27:57 +0200 Subject: [PATCH 3/3] cr suggestion fix --- .../ctco/springboottemplate/greeting/GreetingService.java | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/main/java/lv/ctco/springboottemplate/greeting/GreetingService.java b/src/main/java/lv/ctco/springboottemplate/greeting/GreetingService.java index c924a76..c405f63 100644 --- a/src/main/java/lv/ctco/springboottemplate/greeting/GreetingService.java +++ b/src/main/java/lv/ctco/springboottemplate/greeting/GreetingService.java @@ -12,13 +12,7 @@ public GreetingService(TodoService todoService) { } public String greet() { - long pendingCount = - todoService.getAllTodos().stream() - .filter( - t -> { - return !t.completed(); - }) - .count(); + long pendingCount = todoService.getAllTodos().stream().filter(t -> !t.completed()).count(); return "Hello from Spring! You have " + pendingCount + " open tasks."; }