adding tests

Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
2024-09-17 09:24:24 +02:00
parent 432c086dc5
commit 0a2d419fb8
7 changed files with 168 additions and 13 deletions

View File

@@ -12,8 +12,8 @@ import org.springframework.web.client.RestClient;
public class AppConfig {
@Bean
public RestClient restClient() {
return RestClient.create();
public RestClient.Builder restClientBuilder() {
return RestClient.builder();
}
@Bean

View File

@@ -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.WorldTimeApiIpResponse;
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.val;
import org.springframework.cache.annotation.Cacheable;
@@ -20,13 +12,24 @@ import org.springframework.http.HttpStatusCode;
import org.springframework.stereotype.Service;
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
@RequiredArgsConstructor
@Slf4j
public class TimeZoneServiceImpl implements TimeZoneService {
private final RestClient restClient;
public TimeZoneServiceImpl(RestClient.Builder restClientBuilder) {
restClient = restClientBuilder.build();
}
@Override
public String getExternalIp(String ip) {
var result = ip;

View File

@@ -36,7 +36,7 @@ public class TimeZoneController {
}
@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 timezoneInfo = timeZoneService.getTimeZoneInfo(timezone);
val abbreviation = Objects.requireNonNullElse(timezoneInfo.dstInterval(), new TimeApiTimezoneZoneResponse.Interval(null)).dstName();

View File

@@ -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() {
}
}

View File

@@ -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");
}
}