<commit_before>/*
 * Copyright (C) 2017 B3Partners B.V.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package nl.b3p.viewer.config.services;

import nl.b3p.viewer.util.TestUtil;
import nl.b3p.web.WaitPageStatus;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Test;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.*;
import org.junit.Ignore;

/**
 *
 * @author <NAME> <EMAIL>
 */
public class TileServiceTest extends TestUtil{
    
    private TileService instance = new TileService();

    private static final String PDOK_WMTS = "http://geodata.nationaalgeoregister.nl/tiles/service/wmts?request=getcapabilities";
    private static final int PDOK_WMTS_LAYERCOUNT = 41;

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = null;
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
        
    public TileServiceTest() throws ParserConfigurationException {
        builder = factory.newDocumentBuilder();
    }
    

    /**
     * Test of loadFromUrl method, of class TileService.
     */
    @Test
    public void testLoadFromUrl() {
        String url = "http://www.openbasiskaart.nl/mapcache/tms/1.0.0/osm-nb@rd";
        Map params = new HashMap();
        params.put(TileService.PARAM_SERVICENAME, "osm");
        params.put(TileService.PARAM_RESOLUTIONS, "1,2,4");
        params.put(TileService.PARAM_SERVICEBBOX, "16,32,18,34");
        params.put(TileService.PARAM_CRS, "28992");
        params.put(TileService.PARAM_IMAGEEXTENSION, "png");
        params.put(TileService.PARAM_TILESIZE, 256);
        params.put(TileService.PARAM_TILINGPROTOCOL, "TMS");
        WaitPageStatus status = new WaitPageStatus();
        
        GeoService result = instance.loadFromUrl(url, params, status, entityManager);
        assertEquals("tiled", result.getProtocol());
        assertEquals(url, result.getUrl());
        
        TileService ts =(TileService)result;
        assertEquals("osm", ts.getTilingLayer().getName());
        assertEquals("TMS", ts.getTilingProtocol());
        assertEquals(ts.getUrl(), url);
        Layer l = ts.getTilingLayer();
        TileSet tileSet =l.getTileset();
        assertEquals(3, tileSet.getResolutions().size());
        assertEquals(256, tileSet.getHeight());
        assertEquals("osm", tileSet.getName());
        assertEquals("png",l.getDetails().get("image_extension").getValue());
        assertEquals(0, ts.getMatrixSets().size());
        JSONObject serviceObj = ts.toJSONObject(false, entityManager);
        assertTrue(serviceObj.has("matrixSets"));
        JSONArray matrixSets = serviceObj.getJSONArray("matrixSets");
        assertEquals(0, matrixSets.length());
        
    }

    
    @Test
    public void testLoadBRTWMTSFromURL() throws MalformedURLException {
        URL u = new URL(PDOK_WMTS);
        String url = u.toString();
        Map params = new HashMap();
        params.put(TileService.PARAM_TILINGPROTOCOL, "WMTS");
        params.put(TileService.PARAM_SERVICENAME, "Web Map Tile Service - GeoWebCache");
        WaitPageStatus status = new WaitPageStatus();
        
        GeoService result = instance.loadFromUrl(url, params, status, entityManager);
        assertEquals("https://geodata.nationaalgeoregister.nl/tiles/service/wmts?",result.getUrl());
        Layer topLayer = result.getTopLayer();
        assertEquals(PDOK_WMTS_LAYERCOUNT, topLayer.getChildren().size());
        
        Layer brt = topLayer.getChildren().get(0);
        assertEquals("brtachtergrondkaart", brt.getName());
        JSONArray styles = new JSONArray(brt.getDetails().get(Layer.DETAIL_WMS_STYLES).getValue());
        JSONObject style = (JSONObject)styles.get(0);
        assertEquals("",  style.getString("identifier"));
        assertEquals(1, brt.getBoundingBoxes().size());

        BoundingBox bbox = brt.getBoundingBoxes().get(new CoordinateReferenceSystem("urn:ogc:def:crs:EPSG::28992"));
        assertEquals(595401.9, bbox.getMaxx(), 0.1);
        assertEquals(903401.9, bbox.getMaxy(), 0.1);
        assertEquals(-370406.0, bbox.getMinx(), 0.1);
        assertEquals(5328.8, bbox.getMiny(), 0.1);
        
        JSONObject serviceObj = result.toJSONObject(false, entityManager);
        assertTrue(serviceObj.has("matrixSets"));
        JSONArray matrixSets = serviceObj.getJSONArray("matrixSets");
        assertEquals(4, matrixSets.length());
        JSONObject matrix = matrixSets.getJSONObject(1);
        JSONArray matrices = matrix.getJSONArray("matrices");
        assertEquals(15, matrices.length());
        assertTrue(serviceObj.has("layers"));
        JSONObject layers = serviceObj.getJSONObject("layers");
        JSONObject jsonLayer = layers.getJSONObject("brtachtergrondkaart");
        assertNotNull(jsonLayer);
        assertTrue(jsonLayer.has("bbox"));
    }
    
    @Test
    public void testLoadArcGisWMTSFromURL() throws MalformedURLException {
        URL u = new URL("http://tiles.arcgis.com/tiles/nSZVuSZjHpEZZbRo/arcgis/rest/services/Historische_tijdreis_1950/MapServer/WMTS?request=getcapabilities");
        String url = u.toString();
        Map params = new HashMap();
        params.put(TileService.PARAM_TILINGPROTOCOL, "WMTS");
        
        WaitPageStatus status = new WaitPageStatus();
        
        GeoService result = instance.loadFromUrl(url, params, status, entityManager);
        assertEquals("http://tiles.arcgis.com/tiles/nSZVuSZjHpEZZbRo/arcgis/rest/services/Historische_tijdreis_1950/MapServer/WMTS?",result.getUrl());
        Layer topLayer = result.getTopLayer();
        assertEquals(1, topLayer.getChildren().size());
        
        Layer tijdreis = topLayer.getChildren().get(0);
        assertEquals("Historische_tijdreis_1950", tijdreis.getName());
        JSONArray styles = new JSONArray(tijdreis.getDetails().get(Layer.DETAIL_WMS_STYLES).getValue());
        JSONObject style = (JSONObject)styles.get(0);
        assertEquals("default",  style.getString("identifier"));<commit_msg>fix tiles.arcgis.com WMTS test (#1978)<commit_after>/*
 * Copyright (C) 2017 B3Partners B.V.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package nl.b3p.viewer.config.services;

import nl.b3p.viewer.util.TestUtil;
import nl.b3p.web.WaitPageStatus;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Test;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.*;
import org.junit.Ignore;

/**
 *
 * @author <NAME> <EMAIL>
 */
public class TileServiceTest extends TestUtil{
    
    private TileService instance = new TileService();

    private static final String PDOK_WMTS = "http://geodata.nationaalgeoregister.nl/tiles/service/wmts?request=getcapabilities";
    private static final int PDOK_WMTS_LAYERCOUNT = 41;

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = null;
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
        
    public TileServiceTest() throws ParserConfigurationException {
        builder = factory.newDocumentBuilder();
    }
    

    /**
     * Test of loadFromUrl method, of class TileService.
     */
    @Test
    public void testLoadFromUrl() {
        String url = "http://www.openbasiskaart.nl/mapcache/tms/1.0.0/osm-nb@rd";
        Map params = new HashMap();
        params.put(TileService.PARAM_SERVICENAME, "osm");
        params.put(TileService.PARAM_RESOLUTIONS, "1,2,4");
        params.put(TileService.PARAM_SERVICEBBOX, "16,32,18,34");
        params.put(TileService.PARAM_CRS, "28992");
        params.put(TileService.PARAM_IMAGEEXTENSION, "png");
        params.put(TileService.PARAM_TILESIZE, 256);
        params.put(TileService.PARAM_TILINGPROTOCOL, "TMS");
        WaitPageStatus status = new WaitPageStatus();
        
        GeoService result = instance.loadFromUrl(url, params, status, entityManager);
        assertEquals("tiled", result.getProtocol());
        assertEquals(url, result.getUrl());
        
        TileService ts =(TileService)result;
        assertEquals("osm", ts.getTilingLayer().getName());
        assertEquals("TMS", ts.getTilingProtocol());
        assertEquals(ts.getUrl(), url);
        Layer l = ts.getTilingLayer();
        TileSet tileSet =l.getTileset();
        assertEquals(3, tileSet.getResolutions().size());
        assertEquals(256, tileSet.getHeight());
        assertEquals("osm", tileSet.getName());
        assertEquals("png",l.getDetails().get("image_extension").getValue());
        assertEquals(0, ts.getMatrixSets().size());
        JSONObject serviceObj = ts.toJSONObject(false, entityManager);
        assertTrue(serviceObj.has("matrixSets"));
        JSONArray matrixSets = serviceObj.getJSONArray("matrixSets");
        assertEquals(0, matrixSets.length());
        
    }

    
    @Test
    public void testLoadBRTWMTSFromURL() throws MalformedURLException {
        URL u = new URL(PDOK_WMTS);
        String url = u.toString();
        Map params = new HashMap();
        params.put(TileService.PARAM_TILINGPROTOCOL, "WMTS");
        params.put(TileService.PARAM_SERVICENAME, "Web Map Tile Service - GeoWebCache");
        WaitPageStatus status = new WaitPageStatus();
        
        GeoService result = instance.loadFromUrl(url, params, status, entityManager);
        assertEquals("https://geodata.nationaalgeoregister.nl/tiles/service/wmts?",result.getUrl());
        Layer topLayer = result.getTopLayer();
        assertEquals(PDOK_WMTS_LAYERCOUNT, topLayer.getChildren().size());
        
        Layer brt = topLayer.getChildren().get(0);
        assertEquals("brtachtergrondkaart", brt.getName());
        JSONArray styles = new JSONArray(brt.getDetails().get(Layer.DETAIL_WMS_STYLES).getValue());
        JSONObject style = (JSONObject)styles.get(0);
        assertEquals("",  style.getString("identifier"));
        assertEquals(1, brt.getBoundingBoxes().size());

        BoundingBox bbox = brt.getBoundingBoxes().get(new CoordinateReferenceSystem("urn:ogc:def:crs:EPSG::28992"));
        assertEquals(595401.9, bbox.getMaxx(), 0.1);
        assertEquals(903401.9, bbox.getMaxy(), 0.1);
        assertEquals(-370406.0, bbox.getMinx(), 0.1);
        assertEquals(5328.8, bbox.getMiny(), 0.1);
        
        JSONObject serviceObj = result.toJSONObject(false, entityManager);
        assertTrue(serviceObj.has("matrixSets"));
        JSONArray matrixSets = serviceObj.getJSONArray("matrixSets");
        assertEquals(4, matrixSets.length());
        JSONObject matrix = matrixSets.getJSONObject(1);
        JSONArray matrices = matrix.getJSONArray("matrices");
        assertEquals(15, matrices.length());
        assertTrue(serviceObj.has("layers"));
        JSONObject layers = serviceObj.getJSONObject("layers");
        JSONObject jsonLayer = layers.getJSONObject("brtachtergrondkaart");
        assertNotNull(jsonLayer);
        assertTrue(jsonLayer.has("bbox"));
    }
    
    @Test
    public void testLoadArcGisWMTSFromURL() throws MalformedURLException {
        URL u = new URL("https://tiles.arcgis.com/tiles/nSZVuSZjHpEZZbRo/arcgis/rest/services/Historische_tijdreis_1950/MapServer/WMTS?request=getcapabilities");
        String url = u.toString();
        Map params = new HashMap();
        params.put(TileService.PARAM_TILINGPROTOCOL, "WMTS");
        
        WaitPageStatus status = new WaitPageStatus();
        
        GeoService result = instance.loadFromUrl(url, params, status, entityManager);
        assertEquals("https://tiles.arcgis.com/tiles/nSZVuSZjHpEZZbRo/arcgis/rest/services/Historische_tijdreis_1950/MapServer/WMTS?",result.getUrl());
        Layer topLayer = result.getTopLayer();
        assertEquals(1, topLayer.getChildren().size());
        
        Layer tijdreis = topLayer.getChildren().get(0);
        assertEquals("Historische_tijdreis_1950", tijdreis.getName());
        JSONArray styles = new JSONArray(tijdreis.getDetails().get(Layer.DETAIL_WMS_STYLES).getValue());
        JSONObject style = (JSONObject)styles.get(0);
        assertEquals("default",  style.getString("identifier"));
{'fraction_non_alphanumeric': 0.11915281576548145, 'fraction_numerical': 0.014306753629286767, 'mean_word_length': 3.580790234500482, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, 'xml': 14}, 'pii_count': 0, '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': '5048383', 'n_tokens_mistral': 4335, 'n_tokens_neox': 4217, 'n_words': 849}