<commit_before>package geoscript.layer

import com.xebialabs.restito.server.StubServer
import geoscript.layer.io.GeoJSONWriter
import geoscript.proj.Projection
import org.glassfish.grizzly.http.Method
import org.glassfish.grizzly.http.util.HttpStatus
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder

import static com.xebialabs.restito.builder.stub.StubHttp.whenHttp
import static com.xebialabs.restito.builder.verify.VerifyHttp.verifyHttp
import static com.xebialabs.restito.semantics.Action.resourceContent
import static com.xebialabs.restito.semantics.Action.status
import static com.xebialabs.restito.semantics.Condition.get
import static com.xebialabs.restito.semantics.Condition.method
import static com.xebialabs.restito.semantics.Condition.uri
import static org.junit.Assert.*

/**
 * The VectorTiles Unit Test
 * @author <NAME>
 */
class VectorTilesTestCase {

    @Rule
    public TemporaryFolder folder = new TemporaryFolder()

    @Test
    void getGeoJsonTilesFromUrl() {
        StubServer server = new StubServer(8888).run()
        try {
            whenHttp(server).match(get("/1/0/0.json")).then(resourceContent("tile.json"), status(HttpStatus.OK_200))
            whenHttp(server).match(get("/1/1/0.json")).then(resourceContent("tile.json"), status(HttpStatus.OK_200))
            whenHttp(server).match(get("/1/0/1.json")).then(resourceContent("tile.json"), status(HttpStatus.OK_200))
            whenHttp(server).match(get("/1/1/1.json")).then(resourceContent("tile.json"), status(HttpStatus.OK_200))


            Pyramid pyramid = Pyramid.createGlobalMercatorPyramid()
            pyramid.origin = Pyramid.Origin.TOP_LEFT
            VectorTiles vectorTiles = new VectorTiles(
                    "States",
                    new URL("http://00.0.0.0:8888"),
                    pyramid,
                    "json",
                    proj: new Projection("EPSG:4326")
            )
            List<Layer> layers = vectorTiles.getLayers(new TileCursor<Tile>(vectorTiles, 1))
            assertEquals(1, layers.size())

            verifyHttp(server).once(method(Method.GET), uri("/1/0/0.json"))
            verifyHttp(server).once(method(Method.GET), uri("/1/1/0.json"))
            verifyHttp(server).once(method(Method.GET), uri("/1/0/1.json"))
            verifyHttp(server).once(method(Method.GET), uri("/1/1/1.json"))

        } finally {
            server.stop()
        }
    }

    @Test
    void getPbfTilesFromUrl() {
        StubServer server = new StubServer(8888).run()
        try {
            whenHttp(server).match(get("/1/0/0.pbf")).then(resourceContent("pbf/1/0/0.pbf"), status(HttpStatus.OK_200))
            whenHttp(server).match(get("/1/1/0.pbf")).then(resourceContent("pbf/1/1/0.pbf"), status(HttpStatus.OK_200))
            whenHttp(server).match(get("/1/0/1.pbf")).then(resourceContent("pbf/1/0/1.pbf"), status(HttpStatus.OK_200))
            whenHttp(server).match(get("/1/1/1.pbf")).then(resourceContent("pbf/1/1/1.pbf"), status(HttpStatus.OK_200))

            Pyramid pyramid = Pyramid.createGlobalMercatorPyramid()
            pyramid.origin = Pyramid.Origin.TOP_LEFT
            VectorTiles vectorTiles = new VectorTiles(
                    "World",
                    new URL("http://00.0.0.0:8888"),
                    pyramid,
                    "pbf"
            )
            List<Layer> layers = vectorTiles.getLayers(new TileCursor<Tile>(vectorTiles, 1))
            assertEquals(2, layers.size())

            verifyHttp(server).once(method(Method.GET), uri("/1/0/0.pbf"))
            verifyHttp(server).once(method(Method.GET), uri("/1/1/0.pbf"))
            verifyHttp(server).once(method(Method.GET), uri("/1/0/1.pbf"))
            verifyHttp(server).once(method(Method.GET), uri("/1/1/1.pbf"))

        } finally {
            server.stop()
        }
    }

    @Test
    void generateDirectoryOfPbfTiles() {
        // Setup
        File dir = folder.newFolder("states")
        dir.mkdir()
        Pyramid pyramid = Pyramid.createGlobalMercatorPyramid()
        pyramid.origin = Pyramid.Origin.TOP_LEFT
        VectorTiles vectorTiles = new VectorTiles(<commit_msg>Move stub server into before and after<commit_after>package geoscript.layer

import com.xebialabs.restito.server.StubServer
import geoscript.layer.io.GeoJSONWriter
import geoscript.proj.Projection
import org.glassfish.grizzly.http.Method
import org.glassfish.grizzly.http.util.HttpStatus
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder

import static com.xebialabs.restito.builder.stub.StubHttp.whenHttp
import static com.xebialabs.restito.builder.verify.VerifyHttp.verifyHttp
import static com.xebialabs.restito.semantics.Action.resourceContent
import static com.xebialabs.restito.semantics.Action.status
import static com.xebialabs.restito.semantics.Condition.get
import static com.xebialabs.restito.semantics.Condition.method
import static com.xebialabs.restito.semantics.Condition.uri
import static org.junit.Assert.*

/**
 * The VectorTiles Unit Test
 * @author <NAME>
 */
class VectorTilesTestCase {

    @Rule
    public TemporaryFolder folder = new TemporaryFolder()

    protected StubServer server

    @Before
    void start() {
        server = new StubServer(8888).run()
    }

    @After
    void stop() {
        server.stop()
        // Hack for Windows, which isn't shutting dow the server
        // fast enough
        try {
            Thread.sleep(200)
        } catch (InterruptedException e) {
            e.printStackTrace()
        }
    }

    @Test
    void getGeoJsonTilesFromUrl() {
        whenHttp(server).match(get("/1/0/0.json")).then(resourceContent("tile.json"), status(HttpStatus.OK_200))
        whenHttp(server).match(get("/1/1/0.json")).then(resourceContent("tile.json"), status(HttpStatus.OK_200))
        whenHttp(server).match(get("/1/0/1.json")).then(resourceContent("tile.json"), status(HttpStatus.OK_200))
        whenHttp(server).match(get("/1/1/1.json")).then(resourceContent("tile.json"), status(HttpStatus.OK_200))


        Pyramid pyramid = Pyramid.createGlobalMercatorPyramid()
        pyramid.origin = Pyramid.Origin.TOP_LEFT
        VectorTiles vectorTiles = new VectorTiles(
                "States",
                new URL("http://00.0.0.0:8888"),
                pyramid,
                "json",
                proj: new Projection("EPSG:4326")
        )
        List<Layer> layers = vectorTiles.getLayers(new TileCursor<Tile>(vectorTiles, 1))
        assertEquals(1, layers.size())

        verifyHttp(server).once(method(Method.GET), uri("/1/0/0.json"))
        verifyHttp(server).once(method(Method.GET), uri("/1/1/0.json"))
        verifyHttp(server).once(method(Method.GET), uri("/1/0/1.json"))
        verifyHttp(server).once(method(Method.GET), uri("/1/1/1.json"))
    }

    @Test
    void getPbfTilesFromUrl() {
        whenHttp(server).match(get("/1/0/0.pbf")).then(resourceContent("pbf/1/0/0.pbf"), status(HttpStatus.OK_200))
        whenHttp(server).match(get("/1/1/0.pbf")).then(resourceContent("pbf/1/1/0.pbf"), status(HttpStatus.OK_200))
        whenHttp(server).match(get("/1/0/1.pbf")).then(resourceContent("pbf/1/0/1.pbf"), status(HttpStatus.OK_200))
        whenHttp(server).match(get("/1/1/1.pbf")).then(resourceContent("pbf/1/1/1.pbf"), status(HttpStatus.OK_200))

        Pyramid pyramid = Pyramid.createGlobalMercatorPyramid()
        pyramid.origin = Pyramid.Origin.TOP_LEFT
        VectorTiles vectorTiles = new VectorTiles(
                "World",
                new URL("http://00.0.0.0:8888"),
                pyramid,
                "pbf"
        )
        List<Layer> layers = vectorTiles.getLayers(new TileCursor<Tile>(vectorTiles, 1))
        assertEquals(2, layers.size())

        verifyHttp(server).once(method(Method.GET), uri("/1/0/0.pbf"))
        verifyHttp(server).once(method(Method.GET), uri("/1/1/0.pbf"))
        verifyHttp(server).once(method(Method.GET), uri("/1/0/1.pbf"))
        verifyHttp(server).once(method(Method.GET), uri("/1/1/1.pbf"))
    }

    @Test
    void generateDirectoryOfPbfTiles() {
        // Setup
        File dir = folder.newFolder("states")
        dir.mkdir()
        Pyramid pyramid = Pyramid.createGlobalMercatorPyramid()
        pyramid.origin = Pyramid.Origin.TOP_LEFT
        VectorTiles vectorTiles = new VectorTiles(
{'fraction_non_alphanumeric': 0.14270437262357413, 'fraction_numerical': 0.027923003802281368, 'mean_word_length': 2.8451347647327547, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2493922', 'n_tokens_mistral': 2829, 'n_tokens_neox': 2700, 'n_words': 365}