18
.run/ApplicationTest.run.xml
Normal file
18
.run/ApplicationTest.run.xml
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="ApplicationTest" type="JUnit" factoryName="JUnit">
|
||||||
|
<module name="timezone-service" />
|
||||||
|
<extension name="coverage">
|
||||||
|
<pattern>
|
||||||
|
<option name="PATTERN" value="dev.mars3142.fhq.timezone_service.*" />
|
||||||
|
<option name="ENABLED" value="true" />
|
||||||
|
</pattern>
|
||||||
|
</extension>
|
||||||
|
<option name="PACKAGE_NAME" value="dev.mars3142" />
|
||||||
|
<option name="MAIN_CLASS_NAME" value="" />
|
||||||
|
<option name="METHOD_NAME" value="" />
|
||||||
|
<option name="TEST_OBJECT" value="package" />
|
||||||
|
<method v="2">
|
||||||
|
<option name="Make" enabled="true" />
|
||||||
|
</method>
|
||||||
|
</configuration>
|
||||||
|
</component>
|
11
pom.xml
11
pom.xml
@@ -39,7 +39,6 @@
|
|||||||
<groupId>org.springframework.modulith</groupId>
|
<groupId>org.springframework.modulith</groupId>
|
||||||
<artifactId>spring-modulith-starter-core</artifactId>
|
<artifactId>spring-modulith-starter-core</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.projectlombok</groupId>
|
<groupId>org.projectlombok</groupId>
|
||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
@@ -64,6 +63,16 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.platform</groupId>
|
||||||
|
<artifactId>junit-platform-runner</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@@ -12,8 +12,8 @@ import org.springframework.web.client.RestClient;
|
|||||||
public class AppConfig {
|
public class AppConfig {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public RestClient restClient() {
|
public RestClient.Builder restClientBuilder() {
|
||||||
return RestClient.create();
|
return RestClient.builder();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
@@ -5,14 +5,6 @@ import dev.mars3142.fhq.timezone_service.timezone.domain.entities.response.Ipify
|
|||||||
import dev.mars3142.fhq.timezone_service.timezone.domain.entities.response.TimeApiTimezoneZoneResponse;
|
import dev.mars3142.fhq.timezone_service.timezone.domain.entities.response.TimeApiTimezoneZoneResponse;
|
||||||
import dev.mars3142.fhq.timezone_service.timezone.domain.entities.response.WorldTimeApiIpResponse;
|
import dev.mars3142.fhq.timezone_service.timezone.domain.entities.response.WorldTimeApiIpResponse;
|
||||||
import dev.mars3142.fhq.timezone_service.timezone.service.TimeZoneService;
|
import dev.mars3142.fhq.timezone_service.timezone.service.TimeZoneService;
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.stream.Stream;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import lombok.val;
|
import lombok.val;
|
||||||
import org.springframework.cache.annotation.Cacheable;
|
import org.springframework.cache.annotation.Cacheable;
|
||||||
@@ -20,13 +12,24 @@ import org.springframework.http.HttpStatusCode;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.client.RestClient;
|
import org.springframework.web.client.RestClient;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class TimeZoneServiceImpl implements TimeZoneService {
|
public class TimeZoneServiceImpl implements TimeZoneService {
|
||||||
|
|
||||||
private final RestClient restClient;
|
private final RestClient restClient;
|
||||||
|
|
||||||
|
public TimeZoneServiceImpl(RestClient.Builder restClientBuilder) {
|
||||||
|
restClient = restClientBuilder.build();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getExternalIp(String ip) {
|
public String getExternalIp(String ip) {
|
||||||
var result = ip;
|
var result = ip;
|
||||||
|
@@ -36,7 +36,7 @@ public class TimeZoneController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("{area}/{location}")
|
@GetMapping("{area}/{location}")
|
||||||
public TimeZoneResponse getTimeZone(@PathVariable String area, @PathVariable String location) {
|
public TimeZoneResponse getTimeZoneForLocation(@PathVariable String area, @PathVariable String location) {
|
||||||
val timezone = area + "/" + location;
|
val timezone = area + "/" + location;
|
||||||
val timezoneInfo = timeZoneService.getTimeZoneInfo(timezone);
|
val timezoneInfo = timeZoneService.getTimeZoneInfo(timezone);
|
||||||
val abbreviation = Objects.requireNonNullElse(timezoneInfo.dstInterval(), new TimeApiTimezoneZoneResponse.Interval(null)).dstName();
|
val abbreviation = Objects.requireNonNullElse(timezoneInfo.dstInterval(), new TimeApiTimezoneZoneResponse.Interval(null)).dstName();
|
||||||
|
@@ -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