diff --git a/GreetingController.java b/GreetingController.java new file mode 100644 index 0000000..2fea65e --- /dev/null +++ b/GreetingController.java @@ -0,0 +1,47 @@ +package week4; + +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")); + } +} 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) { + } +} 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(); + } +} 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)); + } +} 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); + } +}