Commit 39ca7d0e authored by Noel Alonso's avatar Noel Alonso
Browse files

Añade mapper + tests

parent 17d3951b
Loading
Loading
Loading
Loading
+104 −0
Original line number Diff line number Diff line
package es.redmic.atlasview.mapper.layer;

/*-
 * #%L
 * Atlas-query-endpoint
 * %%
 * Copyright (C) 2019 REDMIC Project / Server
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import org.springframework.stereotype.Component;

import es.redmic.atlaslib.dto.layer.ActivityDTO;
import es.redmic.atlaslib.dto.layer.ContactDTO;
import es.redmic.atlaslib.dto.layer.LatLonBoundingBoxDTO;
import es.redmic.atlaslib.dto.layer.LayerDTO;
import es.redmic.atlaslib.dto.layer.ProtocolDTO;
import es.redmic.atlaslib.dto.layer.StyleLayerDTO;
import es.redmic.atlaslib.dto.themeinspire.ThemeInspireDTO;
import es.redmic.atlasview.model.layer.Contact;
import es.redmic.atlasview.model.layer.LatLonBoundingBox;
import es.redmic.atlasview.model.layer.Layer;
import es.redmic.atlasview.model.layer.Protocol;
import es.redmic.atlasview.model.layer.StyleLayer;
import es.redmic.atlasview.model.themeinspire.ThemeInspire;
import es.redmic.models.es.administrative.model.ActivityCompact;
import ma.glasnost.orika.CustomMapper;
import ma.glasnost.orika.MappingContext;

@Component
public class LayerESMapper extends CustomMapper<Layer, LayerDTO> {

	@Override
	public void mapAtoB(Layer a, LayerDTO b, MappingContext context) {

		if (a.getStyleLayer() != null) {
			b.setStyleLayer(mapperFacade.map(a.getStyleLayer(), StyleLayerDTO.class));
		}

		if (a.getContact() != null) {
			b.setContact(mapperFacade.map(a.getContact(), ContactDTO.class));
		}

		if (a.getActivities() != null) {
			b.setActivities(mapperFacade.mapAsList(a.getActivities(), ActivityDTO.class));
		}

		if (a.getThemeInspire() != null) {
			b.setThemeInspire(mapperFacade.map(a.getThemeInspire(), ThemeInspireDTO.class));
		}

		if (a.getLatLonBoundsImage() != null) {
			b.setLatLonBoundsImage(mapperFacade.map(a.getLatLonBoundsImage(), LatLonBoundingBoxDTO.class));
		}

		if (a.getProtocols() != null) {
			b.setProtocols(mapperFacade.mapAsList(a.getProtocols(), ProtocolDTO.class));
		}

		super.mapAtoB(a, b, context);
	}

	@Override
	public void mapBtoA(LayerDTO b, Layer a, MappingContext context) {

		if (b.getStyleLayer() != null) {
			a.setStyleLayer(mapperFacade.map(b.getStyleLayer(), StyleLayer.class));
		}

		if (b.getContact() != null) {
			a.setContact(mapperFacade.map(b.getContact(), Contact.class));
		}

		if (b.getActivities() != null) {
			a.setActivities(mapperFacade.mapAsList(b.getActivities(), ActivityCompact.class));
		}

		if (b.getThemeInspire() != null) {
			a.setThemeInspire(mapperFacade.map(b.getThemeInspire(), ThemeInspire.class));
		}

		if (b.getLatLonBoundsImage() != null) {
			a.setLatLonBoundsImage(mapperFacade.map(b.getLatLonBoundsImage(), LatLonBoundingBox.class));
		}

		if (b.getProtocols() != null) {
			a.setProtocols(mapperFacade.mapAsList(b.getProtocols(), Protocol.class));
		}

		super.mapBtoA(b, a, context);
	}
}
+107 −0
Original line number Diff line number Diff line
package es.redmic.test.atlasview.unit.mapper;

/*-
 * #%L
 * Atlas-query-endpoint
 * %%
 * Copyright (C) 2019 REDMIC Project / Server
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import java.io.IOException;

import org.json.JSONException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.junit.MockitoJUnitRunner;
import org.skyscreamer.jsonassert.JSONAssert;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;

import es.redmic.atlaslib.dto.layer.LayerDTO;
import es.redmic.atlasview.config.MapperScanBean;
import es.redmic.atlasview.mapper.layer.LayerESMapper;
import es.redmic.atlasview.model.layer.Layer;
import es.redmic.models.es.common.dto.JSONCollectionDTO;
import es.redmic.models.es.data.common.model.DataSearchWrapper;
import es.redmic.testutils.utils.JsonToBeanTestUtil;
import es.redmic.viewlib.common.mapper.es2dto.DataCollectionESMapper;
import es.redmic.viewlib.common.mapper.es2dto.DataItemESMapper;

@RunWith(MockitoJUnitRunner.class)
public class LayerMapperTest {

	@InjectMocks
	LayerESMapper mapper;

	@InjectMocks
	DataCollectionESMapper dataCollectionMapper;

	@InjectMocks
	DataItemESMapper dataItemMapper;

	protected MapperScanBean factory = new MapperScanBean().build();

	// @formatter:off

	String modelPath = "/data/model/layer/layer.json",
			dtoToSavePath = "/data/dto/layer/layer.json",
			searchWrapperPath = "/data/model/layer/searchWrapperLayerESModel.json",
			searchDTOPath = "/data/dto/layer/searchWrapperLayerDTO.json";

	// @formatter:on

	@Before
	public void setupTest() throws IOException {

		factory.addMapper(mapper);
		factory.addMapper(dataCollectionMapper);
		factory.addMapper(dataItemMapper);
	}

	@Test
	public void mapperDtoToModel() throws JsonParseException, JsonMappingException, IOException, JSONException {

		LayerDTO dtoIn = (LayerDTO) JsonToBeanTestUtil.getBean(dtoToSavePath, LayerDTO.class);

		Layer modelOut = factory.getMapperFacade().map(dtoIn, Layer.class);

		String modelStringExpected = JsonToBeanTestUtil.getJsonString(modelPath);
		String modelString = JsonToBeanTestUtil.writeValueAsString(modelOut);

		JSONAssert.assertEquals(modelStringExpected, modelString, false);
	}

	@Test
	public void mapperSearchWrapperToDto() throws JsonParseException, JsonMappingException, IOException, JSONException {

		JavaType type = JsonToBeanTestUtil.getParametizedType(DataSearchWrapper.class, Layer.class);

		DataSearchWrapper<?> searchWrapperModel = (DataSearchWrapper<?>) JsonToBeanTestUtil.getBean(searchWrapperPath,
				type);
		String expected = JsonToBeanTestUtil.getJsonString(searchDTOPath);

		JSONCollectionDTO searchDTO = factory.getMapperFacade().map(searchWrapperModel.getHits(),
				JSONCollectionDTO.class);

		String searchDTOString = JsonToBeanTestUtil.writeValueAsString(searchDTO);

		JSONAssert.assertEquals(expected, searchDTOString, false);
	}
}
+90 −0
Original line number Diff line number Diff line
{
	"id": "635",
	"name": "SpeciesInCell100m",
	"description": "Registros de especies representados en cuadrículas de cuadrículas100x100m",
	"alias": "Especies en cuadrículas 100x100m",
	"atlas": true,
	"themeInspire": {
		"code": "sd",
		"name": "Distribución de las especies",
		"id": "32",
		"name_en": "Species distribution"
	},
	"refresh": 0,
	"title": "Especies en cuadrículas 100x100m",
	"abstractLayer": "Registros de especies representados en cuadrículas de cuadrículas 100x100m",
	"keyword": [
		"100m"
	],
	"srs": [
		"EPSG:3857",
		"EPSG:4326",
		"EPSG:32628",
		"CRS:84"
	],
	"styleLayer": {
		"name": "sd:citationgrid100",
		"format": "image/png",
		"url": "https://atlas.redmic.es/geoserver/sd/ows?service=WMS&request=GetLegendGraphic&format=image%2Fpng&width=20&height=20&layer=SpeciesInCell100m",
		"title": null,
		"abstractStyle": null
	},
	"contact": {
		"name": "José Andrés Sevilla",
		"organization": "Observatorio Ambiental Granadilla",
		"contactPosition": "Technical GIS",
		"email": "gis@oag-fundacion.org",
		"phone": "+34922298700",
		"fax": "",
		"address": "Edf. Puerto Ciudad S/C de Tenerife 38101 España "
	},
	"activities": [
		{
			"path": "root.1.12",
			"name": "Banco de datos de especies marinas de Canarias (DIADEMA)",
			"id": "12"
		}
	],
	"urlSource": "https://atlas.redmic.es/geoserver/sd/wms",
	"queryable": true,
	"formats": [
		"image/png",
		"application/atom+xml",
		"application/json;type=geojson",
		"application/json;type=topojson",
		"application/json;type=utfgrid",
		"application/vnd.google-earth.kml+xml",
		"application/vnd.google-earth.kml+xml;mode=networklink",
		"application/vnd.google-earth.kmz",
		"application/vnd.mapbox-vector-tile",
		"image/png; mode=8bit",
		"text/html; subtype=openlayers",
		"text/html; subtype=openlayers2",
		"text/html; subtype=openlayers3"
	],
	"image": "/api/mediastorage/photobank/layers/635.png",
	"latLonBoundsImage": {
		"minY": 28.56990337371826,
		"minX": -16.3326358795166,
		"maxY": 28.58088970184326,
		"maxX": -16.3216495513916
	},
	"protocols": [
		{
			"type": "WMS",
			"url": "https://atlas.redmic.es/geoserver/sd/wms"
		}
	],
	"geometry": {
		"type": "Polygon",
		"coordinates": [
			[
				[-18.1745567321777, 27.6111183166504],
				[-18.1745567321777, 29.4221172332764],
				[-13.3011913299561, 29.4221172332764],
				[-13.3011913299561, 27.6111183166504],
				[-18.1745567321777, 27.6111183166504]
			]
		]
	}
}
 No newline at end of file
+106 −0
Original line number Diff line number Diff line
{
	"total": 1,
	"_meta": {
		"max_score": null
	},
	"_aggs": {
		
	},
	"data": [
		{
			"_meta": {
				"score": 1.0,
				"version": 1,
				"highlight": null
			},
			"id": "635",
			"name": "SpeciesInCell100m",
			"description": "Registros de especies representados en cuadrículas de cuadrículas100x100m",
			"alias": "Especies en cuadrículas 100x100m",
			"atlas": true,
			"themeInspire": {
				"code": "sd",
				"name": "Distribución de las especies",
				"id": "32",
				"name_en": "Species distribution"
			},
			"title": "Especies en cuadrículas 100x100m",
			"refresh": 0,
			"abstractLayer": "Registros de especies representados en cuadrículas de cuadrículas 100x100m",
			"keyword": [
				"100m"
			],
			"srs": [
				"EPSG:3857",
				"EPSG:4326",
				"EPSG:32628",
				"CRS:84"
			],
			"styleLayer": {
				"name": "sd:citationgrid100",
				"format": "image/png",
				"url": "https://atlas.redmic.es/geoserver/sd/ows?service=WMS&request=GetLegendGraphic&format=image%2Fpng&width=20&height=20&layer=SpeciesInCell100m",
				"title": null,
				"abstractStyle": null
			},
			"contact": {
				"name": "José Andrés Sevilla",
				"organization": "Observatorio Ambiental Granadilla",
				"contactPosition": "Technical GIS",
				"email": "gis@oag-fundacion.org",
				"phone": "+34922298700",
				"fax": "",
				"address": "Edf. Puerto Ciudad S/C de Tenerife 38101 España "
			},
			"activities": [
				{
					"path": "root.1.12",
					"name": "Banco de datos de especies marinas de Canarias (DIADEMA)",
					"id": "12"
				}
			],
			"urlSource": "https://atlas.redmic.es/geoserver/sd/wms",
			"queryable": true,
			"formats": [
				"image/png",
				"application/atom+xml",
				"application/json;type=geojson",
				"application/json;type=topojson",
				"application/json;type=utfgrid",
				"application/vnd.google-earth.kml+xml",
				"application/vnd.google-earth.kml+xml;mode=networklink",
				"application/vnd.google-earth.kmz",
				"application/vnd.mapbox-vector-tile",
				"image/png; mode=8bit",
				"text/html; subtype=openlayers",
				"text/html; subtype=openlayers2",
				"text/html; subtype=openlayers3"
			],
			"image": "/api/mediastorage/photobank/layers/635.png",
			"latLonBoundsImage": {
				"minY": 28.56990337371826,
				"minX": -16.3326358795166,
				"maxY": 28.58088970184326,
				"maxX": -16.3216495513916
			},
			"protocols": [
				{
					"type": "WMS",
					"url": "https://atlas.redmic.es/geoserver/sd/wms"
				}
			],
			"geometry": {
				"type": "Polygon",
				"coordinates": [
					[
						[-18.1745567321777, 27.6111183166504],
						[-18.1745567321777, 29.4221172332764],
						[-13.3011913299561, 29.4221172332764],
						[-13.3011913299561, 27.6111183166504],
						[-18.1745567321777, 27.6111183166504]
					]
				]
			}
		}
	]
}
 No newline at end of file
+90 −0
Original line number Diff line number Diff line
{
	"id": "635",
	"name": "SpeciesInCell100m",
	"description": "Registros de especies representados en cuadrículas de cuadrículas100x100m",
	"alias": "Especies en cuadrículas 100x100m",
	"atlas": true,
	"themeInspire": {
		"code": "sd",
		"name": "Distribución de las especies",
		"id": "32",
		"name_en": "Species distribution"
	},
	"refresh": 0,
	"title": "Especies en cuadrículas 100x100m",
	"abstractLayer": "Registros de especies representados en cuadrículas de cuadrículas 100x100m",
	"keyword": [
		"100m"
	],
	"srs": [
		"EPSG:3857",
		"EPSG:4326",
		"EPSG:32628",
		"CRS:84"
	],
	"styleLayer": {
		"name": "sd:citationgrid100",
		"format": "image/png",
		"url": "https://atlas.redmic.es/geoserver/sd/ows?service=WMS&request=GetLegendGraphic&format=image%2Fpng&width=20&height=20&layer=SpeciesInCell100m",
		"title": null,
		"abstractStyle": null
	},
	"contact": {
		"name": "José Andrés Sevilla",
		"organization": "Observatorio Ambiental Granadilla",
		"contactPosition": "Technical GIS",
		"email": "gis@oag-fundacion.org",
		"phone": "+34922298700",
		"fax": "",
		"address": "Edf. Puerto Ciudad S/C de Tenerife 38101 España "
	},
	"activities": [
		{
			"path": "root.1.12",
			"name": "Banco de datos de especies marinas de Canarias (DIADEMA)",
			"id": 12
		}
	],
	"urlSource": "https://atlas.redmic.es/geoserver/sd/wms",
	"queryable": true,
	"formats": [
		"image/png",
		"application/atom+xml",
		"application/json;type=geojson",
		"application/json;type=topojson",
		"application/json;type=utfgrid",
		"application/vnd.google-earth.kml+xml",
		"application/vnd.google-earth.kml+xml;mode=networklink",
		"application/vnd.google-earth.kmz",
		"application/vnd.mapbox-vector-tile",
		"image/png; mode=8bit",
		"text/html; subtype=openlayers",
		"text/html; subtype=openlayers2",
		"text/html; subtype=openlayers3"
	],
	"image": "/api/mediastorage/photobank/layers/635.png",
	"latLonBoundsImage": {
		"minY": 28.56990337371826,
		"minX": -16.3326358795166,
		"maxY": 28.58088970184326,
		"maxX": -16.3216495513916
	},
	"protocols": [
		{
			"type": "WMS",
			"url": "https://atlas.redmic.es/geoserver/sd/wms"
		}
	],
	"geometry": {
		"type": "Polygon",
		"coordinates": [
			[
				[-18.1745567321777, 27.6111183166504],
				[-18.1745567321777, 29.4221172332764],
				[-13.3011913299561, 29.4221172332764],
				[-13.3011913299561, 27.6111183166504],
				[-18.1745567321777, 27.6111183166504]
			]
		]
	}
}
 No newline at end of file
Loading