@@ -0,0 +1,67 @@
|
||||
package dev.mars3142.fhq.timezone_service.timezone.service.impl;
|
||||
|
||||
import lombok.val;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.client.RestClientTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.client.MockRestServiceServer;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
|
||||
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
|
||||
|
||||
@DisplayName("Testing Timezone Service Default Implementation")
|
||||
@RunWith(SpringRunner.class)
|
||||
@RestClientTest(TimeZoneServiceImpl.class)
|
||||
class TimeZoneServiceImplTest {
|
||||
|
||||
@Autowired
|
||||
private TimeZoneServiceImpl timeZoneService;
|
||||
|
||||
@Autowired
|
||||
private MockRestServiceServer server;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
server.reset();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Return same ip")
|
||||
void getSameIp() {
|
||||
val ip = timeZoneService.getExternalIp("8.8.8.8");
|
||||
assertThat(ip).isEqualTo("8.8.8.8");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Return custom ip")
|
||||
void getCustomIp() {
|
||||
server.expect(requestTo("https://api.ipify.org?format=json"))
|
||||
.andRespond(withSuccess("""
|
||||
{"ip":"8.8.8.8"}
|
||||
""", MediaType.APPLICATION_JSON));
|
||||
val ip = timeZoneService.getExternalIp("127.0.0.1");
|
||||
assertThat(ip).isEqualTo("8.8.8.8");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getTimeZoneInfoByIp() {
|
||||
}
|
||||
|
||||
@Test
|
||||
void getTimeZoneInfo() {
|
||||
}
|
||||
|
||||
@Test
|
||||
void getPosixTimeZone() {
|
||||
}
|
||||
|
||||
@Test
|
||||
void getLocations() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package dev.mars3142.fhq.timezone_service.timezone.web.controllers;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
@DisplayName("Testing Timezone RestController")
|
||||
public class TimeZoneControllerTest {
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Autowired
|
||||
private TestRestTemplate restTemplate;
|
||||
|
||||
@Disabled
|
||||
@Test
|
||||
@DisplayName("local timezone is Europe/Berlin")
|
||||
void getTimeZone() throws Exception {
|
||||
assertThat(restTemplate.getForObject("http://localhost:" + port + "/v1/timezone", String.class))
|
||||
.contains("Europe/Berlin");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Europe/Moscow is in European timezone list")
|
||||
void checkLocationMoscow() throws Exception {
|
||||
assertThat(restTemplate.getForObject("http://localhost:" + port + "/v1/timezone/Europe", String.class))
|
||||
.contains("Europe/Moscow").contains("64");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Europe/Hamburg is not in European timezone list")
|
||||
void checkLocationHamburg() throws Exception {
|
||||
assertThat(restTemplate.getForObject("http://localhost:" + port + "/v1/timezone/Europe", String.class))
|
||||
.doesNotContain("Europe/Hamburg");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Hawaii is not a valide timezone list")
|
||||
void checkLocationHawaii() throws Exception {
|
||||
assertThat(restTemplate.getForObject("http://localhost:" + port + "/v1/timezone/Hawaii", String.class))
|
||||
.contains("Not Found");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Europe/Paris has a valid timezone")
|
||||
void getTimeZoneForLocation() throws Exception {
|
||||
assertThat(restTemplate.getForObject("http://localhost:" + port + "/v1/timezone/Europe/Paris", String.class))
|
||||
.contains("Europe/Paris");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user