Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions GreetingController.java
Original file line number Diff line number Diff line change
@@ -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"));
}
}
44 changes: 44 additions & 0 deletions TimeController.java
Original file line number Diff line number Diff line change
@@ -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) {
}
}
23 changes: 23 additions & 0 deletions TimeService.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
63 changes: 63 additions & 0 deletions WeatherControllerTests.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
25 changes: 25 additions & 0 deletions WeatherService.java
Original file line number Diff line number Diff line change
@@ -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<String> 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);
}
}