From 974b90e5f1f351bd3fad0697aa07ab2f02dfa9cd Mon Sep 17 00:00:00 2001 From: jericks Date: Tue, 31 May 2016 20:11:20 -0700 Subject: [PATCH] Improve test coverage --- build.gradle | 1 + .../org/cugos/mbtilesserver/Rest.groovy | 14 --- .../mbtilesserver/RestReadOnlyTest.groovy | 90 ++++++++++++++++--- .../mbtilesserver/RestReadWriteTest.groovy | 90 ++++++++++++++++--- 4 files changed, 153 insertions(+), 42 deletions(-) diff --git a/build.gradle b/build.gradle index 3ba44e9..c76a66e 100755 --- a/build.gradle +++ b/build.gradle @@ -76,6 +76,7 @@ dependencies { } testCompile("junit:junit") testCompile("org.springframework.boot:spring-boot-starter-test") + testCompile("com.jayway.jsonpath:json-path:0.8.1") } task wrapper(type: Wrapper) { diff --git a/src/main/groovy/org/cugos/mbtilesserver/Rest.groovy b/src/main/groovy/org/cugos/mbtilesserver/Rest.groovy index f56f99e..ab2c16a 100755 --- a/src/main/groovy/org/cugos/mbtilesserver/Rest.groovy +++ b/src/main/groovy/org/cugos/mbtilesserver/Rest.groovy @@ -73,20 +73,6 @@ class Rest { } } - @RequestMapping(value = "tile/{z}/{x}/{y}", method = RequestMethod.PUT) - @ResponseBody - HttpEntity updateTile(@PathVariable int z, @PathVariable int x, @PathVariable int y, @RequestParam MultipartFile file) throws IOException { - if (config.readOnly) { - new ResponseEntity(HttpStatus.METHOD_NOT_ALLOWED) - } else { - ImageTile tile = config.mbtiles.get(z, x, y) - tile.data = file.bytes - config.mbtiles.put(tile) - byte[] bytes = tile.data - createHttpEntity(bytes, config.mbtiles.metadata.get("format", "png")) - } - } - @RequestMapping(value = "tile/{z}/{x}/{y}", method = RequestMethod.DELETE) @ResponseBody HttpEntity deleteTile(@PathVariable int z, @PathVariable int x, @PathVariable int y) throws IOException { diff --git a/src/test/groovy/org/cugos/mbtilesserver/RestReadOnlyTest.groovy b/src/test/groovy/org/cugos/mbtilesserver/RestReadOnlyTest.groovy index 9e10132..084d768 100644 --- a/src/test/groovy/org/cugos/mbtilesserver/RestReadOnlyTest.groovy +++ b/src/test/groovy/org/cugos/mbtilesserver/RestReadOnlyTest.groovy @@ -12,11 +12,13 @@ import org.springframework.test.context.TestPropertySource import org.springframework.test.context.junit4.SpringJUnit4ClassRunner import org.springframework.test.context.web.WebAppConfiguration import org.springframework.test.web.servlet.MockMvc +import org.springframework.test.web.servlet.MvcResult import org.springframework.web.context.WebApplicationContext +import static org.hamcrest.Matchers.* +import static org.junit.Assert.assertTrue import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.* -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.* import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup @RunWith(SpringJUnit4ClassRunner) @@ -55,12 +57,16 @@ class RestReadOnlyTest { @Test void raster() { - mockMvc.perform(get("/raster/400/400/-156.533203,3.688855,-50.712891,56.800878")).andExpect(status().isOk()) + MvcResult result = mockMvc.perform(get("/raster/400/400/-156.533203,3.688855,-50.712891,56.800878")) + .andExpect(status().isOk()).andReturn() + assertTrue result.response.contentAsByteArray.length > 0 } @Test void tile() { - mockMvc.perform(get("/tile/0/0/0")).andExpect(status().isOk()) + MvcResult result = mockMvc.perform(get("/tile/0/0/0")) + .andExpect(status().isOk()).andReturn() + assertTrue result.response.contentAsByteArray.length > 0 } @Test @@ -69,12 +75,6 @@ class RestReadOnlyTest { mockMvc.perform(fileUpload("/tile/6/0/0").file("file", bytes)).andExpect(status().is4xxClientError()) } - @Test - void updateTile() { - byte[] bytes = getClass().getClassLoader().getResource("tile.png").bytes - mockMvc.perform(fileUpload("/tile/6/0/0").file("file", bytes)).andExpect(status().is4xxClientError()) - } - @Test void deleteTile() { byte[] bytes = getClass().getClassLoader().getResource("tile.png").bytes @@ -83,22 +83,84 @@ class RestReadOnlyTest { @Test void tileCounts() { - mockMvc.perform(get("/tile/counts")).andExpect(status().isOk()) + mockMvc.perform(get("/tile/counts")) + .andExpect(status().isOk()) + .andExpect(jsonPath('$', hasSize(4))) + .andExpect(jsonPath('$[0].zoom', is(0))) + .andExpect(jsonPath('$[0].tiles', is(1))) + .andExpect(jsonPath('$[0].total', is(1))) + .andExpect(jsonPath('$[0].percent', is(1.0 as Double))) + .andExpect(jsonPath('$[1].zoom', is(1))) + .andExpect(jsonPath('$[1].tiles', is(4))) + .andExpect(jsonPath('$[1].total', is(4))) + .andExpect(jsonPath('$[1].percent', is(1.0 as Double))) + .andExpect(jsonPath('$[2].zoom', is(2))) + .andExpect(jsonPath('$[2].tiles', is(16))) + .andExpect(jsonPath('$[2].total', is(16))) + .andExpect(jsonPath('$[2].percent', is(1.0 as Double))) + .andExpect(jsonPath('$[3].zoom', is(3))) + .andExpect(jsonPath('$[3].tiles', is(64))) + .andExpect(jsonPath('$[3].total', is(64))) + .andExpect(jsonPath('$[3].percent', is(1.0 as Double))) } @Test void pyramid() { - mockMvc.perform(get("/pyramid")).andExpect(status().isOk()) + mockMvc.perform(get("/pyramid")) + .andExpect(status().isOk()) + .andExpect(jsonPath('$.proj', is('EPSG:3857'))) + .andExpect(jsonPath('$.bounds.minX', closeTo(-2.0036395147881314E7 as Double, 0.001))) + .andExpect(jsonPath('$.bounds.minY', closeTo(-2.0037471205137067E7 as Double, 0.001))) + .andExpect(jsonPath('$.bounds.maxX', closeTo(2.0036395147881314E7 as Double, 0.001))) + .andExpect(jsonPath('$.bounds.maxY', closeTo(2.003747120513706E7 as Double, 0.001))) + .andExpect(jsonPath('$.origin', is('BOTTOM_LEFT'))) + .andExpect(jsonPath('$.tileSize.width', is(256))) + .andExpect(jsonPath('$.tileSize.height', is(256))) + .andExpect(jsonPath('$.grids', hasSize(20))) + .andExpect(jsonPath('$.grids[0].z', is(0))) + .andExpect(jsonPath('$.grids[0].width', is(1))) + .andExpect(jsonPath('$.grids[0].height', is(1))) + .andExpect(jsonPath('$.grids[0].xres', is(156412.0 as Double))) + .andExpect(jsonPath('$.grids[0].yres', is(156412.0 as Double))) + .andExpect(jsonPath('$.grids[19].z', is(19))) + .andExpect(jsonPath('$.grids[19].width', is(524288))) + .andExpect(jsonPath('$.grids[19].height', is(524288))) + .andExpect(jsonPath('$.grids[19].xres', closeTo(0.29833221435546875 as Double, 0.00001))) + .andExpect(jsonPath('$.grids[19].yres', closeTo(0.29833221435546875 as Double, 0.00001))) } @Test void metadata() { - mockMvc.perform(get("/metadata")).andExpect(status().isOk()) + mockMvc.perform(get("/metadata")) + .andExpect(status().isOk()) + .andExpect(jsonPath('$.type', is('base_layer'))) + .andExpect(jsonPath('$.name', is('countries'))) + .andExpect(jsonPath('$.description', is('countries'))) + .andExpect(jsonPath('$.format', is('png'))) + .andExpect(jsonPath('$.version', is('1.0'))) + .andExpect(jsonPath('$.attribution', is('Created with GeoScript'))) + .andExpect(jsonPath('$.bounds', is('-179.99,-85.0511,179.99,85.0511'))) } @Test void gdal() { - mockMvc.perform(get("/gdal")).andExpect(status().isOk()) + mockMvc.perform(get("/gdal")) + .andExpect(status().isOk()) + .andExpect(xpath("/GDAL_WMS/Service/@name").string("TMS")) + .andExpect(xpath("/GDAL_WMS/Service/ServerUrl").string(startsWith('http://'))) + .andExpect(xpath("/GDAL_WMS/Service/ServerUrl").string(containsString('tile/${z}/${x}/${y}'))) + .andExpect(xpath("/GDAL_WMS/DataWindow/UpperLeftX").number(-20037508.34)) + .andExpect(xpath("/GDAL_WMS/DataWindow/UpperLeftY").number(20037508.34)) + .andExpect(xpath("/GDAL_WMS/DataWindow/LowerRightX").number(20037508.34)) + .andExpect(xpath("/GDAL_WMS/DataWindow/LowerRightY").number(-20037508.34)) + .andExpect(xpath("/GDAL_WMS/DataWindow/TileLevel").number(3)) + .andExpect(xpath("/GDAL_WMS/DataWindow/TileCountX").number(1)) + .andExpect(xpath("/GDAL_WMS/DataWindow/TileCountY").number(1)) + .andExpect(xpath("/GDAL_WMS/DataWindow/YOrigin").string('bottom')) + .andExpect(xpath("/GDAL_WMS/Projection").string('EPSG:3857')) + .andExpect(xpath("/GDAL_WMS/BlockSizeX").string('256')) + .andExpect(xpath("/GDAL_WMS/BlockSizeY").string('256')) + .andExpect(xpath("/GDAL_WMS/BandsCount").string('3')) } } diff --git a/src/test/groovy/org/cugos/mbtilesserver/RestReadWriteTest.groovy b/src/test/groovy/org/cugos/mbtilesserver/RestReadWriteTest.groovy index 1155658..02a8513 100644 --- a/src/test/groovy/org/cugos/mbtilesserver/RestReadWriteTest.groovy +++ b/src/test/groovy/org/cugos/mbtilesserver/RestReadWriteTest.groovy @@ -12,8 +12,11 @@ import org.springframework.test.context.TestPropertySource import org.springframework.test.context.junit4.SpringJUnit4ClassRunner import org.springframework.test.context.web.WebAppConfiguration import org.springframework.test.web.servlet.MockMvc +import org.springframework.test.web.servlet.MvcResult import org.springframework.web.context.WebApplicationContext +import static org.junit.Assert.assertTrue +import static org.hamcrest.Matchers.* import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.* import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.* import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup @@ -54,12 +57,16 @@ class RestReadWriteTest { @Test void raster() { - mockMvc.perform(get("/raster/400/400/-156.533203,3.688855,-50.712891,56.800878")).andExpect(status().isOk()) + MvcResult result = mockMvc.perform(get("/raster/400/400/-156.533203,3.688855,-50.712891,56.800878")) + .andExpect(status().isOk()).andReturn() + assertTrue result.response.contentAsByteArray.length > 0 } @Test void tile() { - mockMvc.perform(get("/tile/0/0/0")).andExpect(status().isOk()) + MvcResult result = mockMvc.perform(get("/tile/0/0/0")) + .andExpect(status().isOk()).andReturn() + assertTrue result.response.contentAsByteArray.length > 0 } @Test @@ -70,14 +77,6 @@ class RestReadWriteTest { .andExpect(content().bytes(bytes)) } - @Test - void updateTile() { - byte[] bytes = getClass().getClassLoader().getResource("tile.png").bytes - mockMvc.perform(fileUpload("/tile/6/0/0").file("file", bytes)) - .andExpect(status().isOk()) - .andExpect(content().bytes(bytes)) - } - @Test void deleteTile() { byte[] bytes = getClass().getClassLoader().getResource("tile.png").bytes @@ -92,22 +91,85 @@ class RestReadWriteTest { @Test void tileCounts() { - mockMvc.perform(get("/tile/counts")).andExpect(status().isOk()) + mockMvc.perform(get("/tile/counts")) + .andExpect(status().isOk()) + .andExpect(jsonPath('$', hasSize(4))) + .andExpect(jsonPath('$[0].zoom', is(0))) + .andExpect(jsonPath('$[0].tiles', is(1))) + .andExpect(jsonPath('$[0].total', is(1))) + .andExpect(jsonPath('$[0].percent', is(1.0 as Double))) + .andExpect(jsonPath('$[1].zoom', is(1))) + .andExpect(jsonPath('$[1].tiles', is(4))) + .andExpect(jsonPath('$[1].total', is(4))) + .andExpect(jsonPath('$[1].percent', is(1.0 as Double))) + .andExpect(jsonPath('$[2].zoom', is(2))) + .andExpect(jsonPath('$[2].tiles', is(16))) + .andExpect(jsonPath('$[2].total', is(16))) + .andExpect(jsonPath('$[2].percent', is(1.0 as Double))) + .andExpect(jsonPath('$[3].zoom', is(3))) + .andExpect(jsonPath('$[3].tiles', is(64))) + .andExpect(jsonPath('$[3].total', is(64))) + .andExpect(jsonPath('$[3].percent', is(1.0 as Double))) } @Test void pyramid() { - mockMvc.perform(get("/pyramid")).andExpect(status().isOk()) + mockMvc.perform(get("/pyramid")) + .andExpect(status().isOk()) + .andExpect(jsonPath('$.proj', is('EPSG:3857'))) + .andExpect(jsonPath('$.bounds.minX', closeTo(-2.0036395147881314E7 as Double, 0.001))) + .andExpect(jsonPath('$.bounds.minY', closeTo(-2.0037471205137067E7 as Double, 0.001))) + .andExpect(jsonPath('$.bounds.maxX', closeTo(2.0036395147881314E7 as Double, 0.001))) + .andExpect(jsonPath('$.bounds.maxY', closeTo(2.003747120513706E7 as Double, 0.001))) + .andExpect(jsonPath('$.origin', is('BOTTOM_LEFT'))) + .andExpect(jsonPath('$.tileSize.width', is(256))) + .andExpect(jsonPath('$.tileSize.height', is(256))) + .andExpect(jsonPath('$.grids', hasSize(20))) + .andExpect(jsonPath('$.grids[0].z', is(0))) + .andExpect(jsonPath('$.grids[0].width', is(1))) + .andExpect(jsonPath('$.grids[0].height', is(1))) + .andExpect(jsonPath('$.grids[0].xres', is(156412.0 as Double))) + .andExpect(jsonPath('$.grids[0].yres', is(156412.0 as Double))) + .andExpect(jsonPath('$.grids[19].z', is(19))) + .andExpect(jsonPath('$.grids[19].width', is(524288))) + .andExpect(jsonPath('$.grids[19].height', is(524288))) + .andExpect(jsonPath('$.grids[19].xres', closeTo(0.29833221435546875 as Double, 0.00001))) + .andExpect(jsonPath('$.grids[19].yres', closeTo(0.29833221435546875 as Double, 0.00001))) } @Test void metadata() { - mockMvc.perform(get("/metadata")).andExpect(status().isOk()) + mockMvc.perform(get("/metadata")) + .andExpect(status().isOk()) + .andExpect(jsonPath('$.type', is('base_layer'))) + .andExpect(jsonPath('$.name', is('countries'))) + .andExpect(jsonPath('$.description', is('countries'))) + .andExpect(jsonPath('$.format', is('png'))) + .andExpect(jsonPath('$.version', is('1.0'))) + .andExpect(jsonPath('$.attribution', is('Created with GeoScript'))) + .andExpect(jsonPath('$.bounds', is('-179.99,-85.0511,179.99,85.0511'))) } @Test void gdal() { - mockMvc.perform(get("/gdal")).andExpect(status().isOk()) + mockMvc.perform(get("/gdal")) + .andExpect(status().isOk()) + .andExpect(xpath("/GDAL_WMS/Service/@name").string("TMS")) + .andExpect(xpath("/GDAL_WMS/Service/ServerUrl").string(startsWith('http://'))) + .andExpect(xpath("/GDAL_WMS/Service/ServerUrl").string(containsString('tile/${z}/${x}/${y}'))) + .andExpect(xpath("/GDAL_WMS/DataWindow/UpperLeftX").number(-20037508.34)) + .andExpect(xpath("/GDAL_WMS/DataWindow/UpperLeftY").number(20037508.34)) + .andExpect(xpath("/GDAL_WMS/DataWindow/LowerRightX").number(20037508.34)) + .andExpect(xpath("/GDAL_WMS/DataWindow/LowerRightY").number(-20037508.34)) + .andExpect(xpath("/GDAL_WMS/DataWindow/TileLevel").number(3)) + .andExpect(xpath("/GDAL_WMS/DataWindow/TileCountX").number(1)) + .andExpect(xpath("/GDAL_WMS/DataWindow/TileCountY").number(1)) + .andExpect(xpath("/GDAL_WMS/DataWindow/YOrigin").string('bottom')) + .andExpect(xpath("/GDAL_WMS/Projection").string('EPSG:3857')) + .andExpect(xpath("/GDAL_WMS/BlockSizeX").string('256')) + .andExpect(xpath("/GDAL_WMS/BlockSizeY").string('256')) + .andExpect(xpath("/GDAL_WMS/BandsCount").string('3')) } + }