diff --git a/pom.xml b/pom.xml index 023925c..ee35625 100644 --- a/pom.xml +++ b/pom.xml @@ -117,6 +117,16 @@ 5.11.4 test + + org.springframework.boot + spring-boot-configuration-processor + true + + + org.springframework.boot + spring-boot-configuration-processor + true + diff --git a/src/main/java/osm/surveyor/task/plateau/Plateau.java b/src/main/java/osm/surveyor/task/plateau/Plateau.java new file mode 100644 index 0000000..e949b48 --- /dev/null +++ b/src/main/java/osm/surveyor/task/plateau/Plateau.java @@ -0,0 +1,19 @@ +package osm.surveyor.task.plateau; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +import lombok.Data; + +@Component +@ConfigurationProperties(prefix = "app.plateau") +@Data +public class Plateau { + + private String dir; // app.plateau.dir=plateau-data + + public String getDirPathName() { + return dir; + } + +} diff --git a/src/main/java/osm/surveyor/task/plateau/PlateauController.java b/src/main/java/osm/surveyor/task/plateau/PlateauController.java index 6f6533a..45b918c 100644 --- a/src/main/java/osm/surveyor/task/plateau/PlateauController.java +++ b/src/main/java/osm/surveyor/task/plateau/PlateauController.java @@ -1,62 +1,25 @@ package osm.surveyor.task.plateau; -import java.nio.file.*; - -import org.springframework.beans.factory.annotation.Value; -import org.springframework.core.io.PathResource; -import org.springframework.http.HttpMethod; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.client.RestTemplate; @RestController public class PlateauController { - @Value("${task-bldg.osm-data.url}") - private String url; // task-bldg.osm-data.url=http://surveyor.mydns.jp/osm-data + + private PlateauService plateauService; - @Value("${task-bldg.plateau.dir}") - private String dir; // task-bldg.plateau.dir=plateau-data + public PlateauController(PlateauService plateauService) { + this.plateauService = plateauService; + } + + @GetMapping(value = "/plateau") + public Boolean getPlateauStatus() throws Exception { + return plateauService.getPlateauStatus(); + } - @GetMapping(value = "/plateau/{citycode}}") + @GetMapping(value = "/plateau/{citycode}") public String getPlateau(@PathVariable("citycode") String citycode) throws Exception { - try { - Path base = Path.of(dir); - if (Files.exists(base)) { - Files.createDirectory(base); - } - Path city = Path.of(dir, citycode); - if (Files.exists(city)) { - Files.createDirectory(city); - } - return "OK"; - } - catch (Exception e) { - return e.toString(); - } - - /* - Path path = Path.of(dir.toString(), fileName); - PathResource resource = new PathResource(path); - - try { - RestTemplate restTemplate = new RestTemplate(); - System.out.println(String.format("INFO: httpGet(%s)", url)); - ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, null, String.class); - HttpStatus httpStatus = response.getStatusCode(); - if (httpStatus.isError()) { - System.out.println("ERROE: Can not access '"+ url +"'"); - throw new Exception("ERROE: Can not access '"+ url +"'"); - } - String body = response.getBody(); - return body; - } - catch (Exception e) { - return e.toString(); - } - */ + return plateauService.getPlateau(citycode); } } \ No newline at end of file diff --git a/src/main/java/osm/surveyor/task/plateau/PlateauService.java b/src/main/java/osm/surveyor/task/plateau/PlateauService.java new file mode 100644 index 0000000..5b7a75b --- /dev/null +++ b/src/main/java/osm/surveyor/task/plateau/PlateauService.java @@ -0,0 +1,68 @@ +package osm.surveyor.task.plateau; + +import java.nio.file.Files; +import java.nio.file.Path; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +@Service +public class PlateauService { + + @Value("${task-bldg.plateau.dir}") + private String dir; // task-bldg.plateau.dir=plateau-data + + public Boolean getPlateauStatus() throws Exception { + try { + Path base = Path.of(dir); + if (Files.exists(base)) { + return Boolean.valueOf(true); + } + else { + return Boolean.valueOf(false); + } + } + catch (Exception e) { + return Boolean.valueOf(false); + } + + } + + public String getPlateau(String citycode) { + try { + Path base = Path.of(dir); + if (Files.exists(base)) { + Files.createDirectory(base); + } + Path city = Path.of(dir, citycode); + if (Files.exists(city)) { + Files.createDirectory(city); + } + return "OK"; + } + catch (Exception e) { + return e.toString(); + } + + /* + Path path = Path.of(dir.toString(), fileName); + PathResource resource = new PathResource(path); + + try { + RestTemplate restTemplate = new RestTemplate(); + System.out.println(String.format("INFO: httpGet(%s)", url)); + ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, null, String.class); + HttpStatus httpStatus = response.getStatusCode(); + if (httpStatus.isError()) { + System.out.println("ERROE: Can not access '"+ url +"'"); + throw new Exception("ERROE: Can not access '"+ url +"'"); + } + String body = response.getBody(); + return body; + } + catch (Exception e) { + return e.toString(); + } + */ + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 060effe..d01fbd3 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -20,4 +20,4 @@ # task-bldg orignal properties task-bldg.osm-data.url=http://surveyor.mydns.jp/osm-data -task-bldg.plateau.dir=plateau-data +app.plateau.dir=plateau-data diff --git a/src/test/java/osm/surveyor/task/plateau/PlateauControllerTest.java b/src/test/java/osm/surveyor/task/plateau/PlateauControllerTest.java index 81852a4..87971f5 100644 --- a/src/test/java/osm/surveyor/task/plateau/PlateauControllerTest.java +++ b/src/test/java/osm/surveyor/task/plateau/PlateauControllerTest.java @@ -1,12 +1,7 @@ package osm.surveyor.task.plateau; -import static org.assertj.core.api.Assertions.assertThat; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.junit.jupiter.api.Assertions.assertTrue; -import java.io.IOException; -import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; @@ -17,26 +12,19 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.client.AutoConfigureWebClient; -import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.core.io.ClassPathResource; -import org.springframework.http.HttpHeaders; import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import org.springframework.util.FileSystemUtils; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; @SpringBootTest -@WebMvcTest(PlateauController.class) +@AutoConfigureMockMvc class PlateauControllerTest { @Autowired - private MockMvc mvc; private final String rootUrl = "/plateau"; private static final String dirStr = "plateau-data"; // task-bldg.plateau.dir=plateau-data @@ -60,16 +48,14 @@ @Test @DisplayName("'./plateau/01100' is Sapporo 2020") - void testGetPlateau() throws IOException { - String urlPath = "/01100"; - - MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(rootUrl + urlPath)) - .andExpect(status().isOk()) - .andExpect(content().json("OK")) - .andReturn(); - + void testGetPlateau(@Autowired MockMvc mvc) throws Exception { + mvc.perform( + MockMvcRequestBuilders.get(rootUrl+"/01100")) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.content().string("HelloWorld")); + Path base = Path.of(dirStr); boolean exists = Files.exists(base); - assertThat(exists); + assertTrue(exists); } } diff --git a/src/test/java/osm/surveyor/task/plateau/PlateauServiceTest.java b/src/test/java/osm/surveyor/task/plateau/PlateauServiceTest.java new file mode 100644 index 0000000..85c2a58 --- /dev/null +++ b/src/test/java/osm/surveyor/task/plateau/PlateauServiceTest.java @@ -0,0 +1,50 @@ +package osm.surveyor.task.plateau; + +import static org.junit.jupiter.api.Assertions.*; + +import java.nio.file.Path; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +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.util.FileSystemUtils; + +@SpringBootTest +class PlateauServiceTest { + + @Autowired + PlateauService service; + + private static final String dirStr = "plateau-data"; // task-bldg.plateau.dir=plateau-data + + @BeforeAll + static void setUpBeforeClass() throws Exception { + Path dir = Path.of(dirStr); + FileSystemUtils.deleteRecursively(dir); + } + + @AfterAll + static void tearDownAfterClass() throws Exception { + } + + @BeforeEach + void setUp() throws Exception { + } + + @AfterEach + void tearDown() throws Exception { + } + + @Test + @DisplayName("'PlateauService.getPlateauStatus() ['./plateau-data' is not exists.]") + void testGetPlateauStatus() throws Exception { + Boolean ret = service.getPlateauStatus(); + assertFalse(ret); + } + +} diff --git a/src/test/java/osm/surveyor/task/plateau/PlateauTest.java b/src/test/java/osm/surveyor/task/plateau/PlateauTest.java new file mode 100644 index 0000000..6fa6817 --- /dev/null +++ b/src/test/java/osm/surveyor/task/plateau/PlateauTest.java @@ -0,0 +1,37 @@ +package osm.surveyor.task.plateau; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class PlateauTest { + + private final Plateau plateau = new Plateau(); + + @BeforeAll + static void setUpBeforeClass() throws Exception { + } + + @AfterAll + static void tearDownAfterClass() throws Exception { + } + + @BeforeEach + void setUp() throws Exception { + } + + @AfterEach + void tearDown() throws Exception { + } + + @Test + void testGetDirPathName() { + String ret = plateau.getDirPathName(); + assertEquals("plateau-data", ret); + } + +}