From 90ab1c54ae01a64971d860e9507cdca8f23cfb87 Mon Sep 17 00:00:00 2001 From: natalietsvia-sys Date: Fri, 1 May 2026 21:15:56 +0300 Subject: [PATCH 1/6] Create GreetingController.java --- GreetingController.java | 47 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 GreetingController.java diff --git a/GreetingController.java b/GreetingController.java new file mode 100644 index 0000000..8c0e41a --- /dev/null +++ b/GreetingController.java @@ -0,0 +1,47 @@ +package com.example.demo; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.web.servlet.MockMvc; + +@SpringBootTest +@AutoConfigureMockMvc +class GreetingControllerTests { + + @Autowired + private MockMvc mockMvc; + + @Test + void helloReturnsHelloWorld() throws Exception { + mockMvc.perform(get("/hello")) + .andExpect(status().isOk()) + .andExpect(content().string("Hello World!")); + } + + @Test + void helloNameReturnsPersonalizedGreeting() throws Exception { + mockMvc.perform(get("/hello/Ada")) + .andExpect(status().isOk()) + .andExpect(content().string("Hello Ada!")); + } + + @Test + void addReturnsSum() throws Exception { + mockMvc.perform(get("/add/7/5")) + .andExpect(status().isOk()) + .andExpect(content().string("12")); + } + + @Test + void multiplyReturnsProduct() throws Exception { + mockMvc.perform(get("/multiply/7/5")) + .andExpect(status().isOk()) + .andExpect(content().string("35")); + } +} From 1774a7c2c99982b58da655e5690aba60453041c0 Mon Sep 17 00:00:00 2001 From: natalietsvia-sys Date: Fri, 1 May 2026 21:21:10 +0300 Subject: [PATCH 2/6] Update GreetingController.java --- GreetingController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GreetingController.java b/GreetingController.java index 8c0e41a..2fea65e 100644 --- a/GreetingController.java +++ b/GreetingController.java @@ -1,4 +1,4 @@ -package com.example.demo; +package week4; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; From eeacff4c3a7ed4ed39316b212ce3bd7c6c74b447 Mon Sep 17 00:00:00 2001 From: natalietsvia-sys Date: Fri, 1 May 2026 21:35:56 +0300 Subject: [PATCH 3/6] Create WeatherService.java --- WeatherService.java | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 WeatherService.java diff --git a/WeatherService.java b/WeatherService.java new file mode 100644 index 0000000..4d6bb08 --- /dev/null +++ b/WeatherService.java @@ -0,0 +1,25 @@ +package project4; + +import java.util.List; +import java.util.concurrent.ThreadLocalRandom; + +import org.springframework.stereotype.Service; + +@Service +class WeatherService { + + private static final List CONDITIONS = List.of("Sunny", "Rainy", "Cloudy", "Windy"); + + int getCurrentTemperature() { + return ThreadLocalRandom.current().nextInt(-10, 41); + } + + String getWeatherCondition() { + int conditionIndex = ThreadLocalRandom.current().nextInt(CONDITIONS.size()); + return CONDITIONS.get(conditionIndex); + } + + int getWindSpeed() { + return ThreadLocalRandom.current().nextInt(0, 101); + } +} From 0dabc586cb439744bb0fc337324d2b38d0ee0e62 Mon Sep 17 00:00:00 2001 From: natalietsvia-sys Date: Fri, 1 May 2026 21:40:58 +0300 Subject: [PATCH 4/6] Create WeatherControllerTests.java --- WeatherControllerTests.java | 63 +++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 WeatherControllerTests.java diff --git a/WeatherControllerTests.java b/WeatherControllerTests.java new file mode 100644 index 0000000..7f5654e --- /dev/null +++ b/WeatherControllerTests.java @@ -0,0 +1,63 @@ +package project4; + +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.test.context.bean.override.mockito.MockitoBean; +import org.springframework.test.web.servlet.MockMvc; + +@WebMvcTest(WeatherController.class) +class WeatherControllerTests { + + @Autowired + private MockMvc mockMvc; + + @MockitoBean + private WeatherService weatherService; + + @Test + void temperatureReturnsCurrentTemperature() throws Exception { + when(weatherService.getCurrentTemperature()).thenReturn(22); + + mockMvc.perform(get("/weather/temperature")) + .andExpect(status().isOk()) + .andExpect(content().string("22")); + } + + @Test + void conditionReturnsCurrentCondition() throws Exception { + when(weatherService.getWeatherCondition()).thenReturn("Sunny"); + + mockMvc.perform(get("/weather/condition")) + .andExpect(status().isOk()) + .andExpect(content().string("Sunny")); + } + + @Test + void windReturnsCurrentWindSpeed() throws Exception { + when(weatherService.getWindSpeed()).thenReturn(18); + + mockMvc.perform(get("/weather/wind")) + .andExpect(status().isOk()) + .andExpect(content().string("18")); + } + + @Test + void allReturnsCompleteWeatherInformation() throws Exception { + when(weatherService.getCurrentTemperature()).thenReturn(9); + when(weatherService.getWeatherCondition()).thenReturn("Cloudy"); + when(weatherService.getWindSpeed()).thenReturn(41); + + mockMvc.perform(get("/weather/all")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.temperature").value(9)) + .andExpect(jsonPath("$.condition").value("Cloudy")) + .andExpect(jsonPath("$.windSpeed").value(41)); + } +} From 197ff56cb0919b23f51eab677410f2b15ff817fc Mon Sep 17 00:00:00 2001 From: natalietsvia-sys Date: Fri, 1 May 2026 21:45:14 +0300 Subject: [PATCH 5/6] Create TimeService.java --- TimeService.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 TimeService.java diff --git a/TimeService.java b/TimeService.java new file mode 100644 index 0000000..38bddd8 --- /dev/null +++ b/TimeService.java @@ -0,0 +1,23 @@ +package project4; + +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.time.LocalTime; + +import org.springframework.stereotype.Service; + +@Service +class TimeService { + + LocalTime getCurrentTime() { + return LocalTime.now(); + } + + LocalDate getCurrentDate() { + return LocalDate.now(); + } + + DayOfWeek getCurrentDayOfWeek() { + return LocalDate.now().getDayOfWeek(); + } +} From da1fee3d22b6d3e0e51f4d33871a86b874b2f54a Mon Sep 17 00:00:00 2001 From: natalietsvia-sys Date: Fri, 1 May 2026 21:49:40 +0300 Subject: [PATCH 6/6] Create TimeController.java --- TimeController.java | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 TimeController.java diff --git a/TimeController.java b/TimeController.java new file mode 100644 index 0000000..20c512a --- /dev/null +++ b/TimeController.java @@ -0,0 +1,44 @@ +package project4; + +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.time.LocalTime; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +class TimeController { + + private final TimeService timeService; + + TimeController(TimeService timeService) { + this.timeService = timeService; + } + + @GetMapping("/time") + LocalTime getTime() { + return timeService.getCurrentTime(); + } + + @GetMapping("/date") + LocalDate getDate() { + return timeService.getCurrentDate(); + } + + @GetMapping("/day") + DayOfWeek getDayOfWeek() { + return timeService.getCurrentDayOfWeek(); + } + + @GetMapping("/all") + TimeInformation getAllTimeInformation() { + return new TimeInformation( + timeService.getCurrentTime(), + timeService.getCurrentDate(), + timeService.getCurrentDayOfWeek()); + } + + record TimeInformation(LocalTime time, LocalDate date, DayOfWeek dayOfWeek) { + } +}