Commit 768054b9 authored by Noel Alonso's avatar Noel Alonso
Browse files

Añade mapping dto a model y model a dto + tests

parent e686dc3f
Loading
Loading
Loading
Loading
+37 −0
Original line number Diff line number Diff line
package es.redmic.viewlib.usersettings.mapper;

/*-
 * #%L
 * view-lib
 * %%
 * 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.mapstruct.Mapper;

import es.redmic.usersettingslib.dto.SettingsDTO;
import es.redmic.usersettingslib.model.Settings;
import es.redmic.viewlib.common.mapper.es2dto.DataCollectionESMapper;

@Mapper
// @ConditionalOnProperty(name = "redmic.user-settings.enabled", havingValue =
// "true")
public abstract class SettingsESMapper extends DataCollectionESMapper<SettingsDTO, Settings> {

	public abstract SettingsDTO map(Settings model);

	public abstract Settings map(SettingsDTO dto);
}
+82 −0
Original line number Diff line number Diff line
package es.redmic.viewlib.usersettings.mapper;

/*-
 * #%L
 * view-lib
 * %%
 * 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.Test;
import org.junit.runner.RunWith;
import org.mapstruct.factory.Mappers;
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.models.es.common.dto.JSONCollectionDTO;
import es.redmic.models.es.data.common.model.DataSearchWrapper;
import es.redmic.testutils.utils.JsonToBeanTestUtil;
import es.redmic.usersettingslib.dto.SettingsDTO;
import es.redmic.usersettingslib.model.Settings;

@RunWith(MockitoJUnitRunner.class)
public class SettingsMapperTest {

	// @formatter:off

	String modelPath = "/data/model/settings.json",
			dtoToSavePath = "/data/dto/settings.json",
			searchWrapperPath = "/data/model/searchWrapperSettingsESModel.json",
			searchDTOPath = "/data/dto/searchWrapperSettingsDTO.json";

	// @formatter:on

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

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

		Settings modelOut = Mappers.getMapper(SettingsESMapper.class).map(dtoIn);

		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, Settings.class);

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

		JSONCollectionDTO searchDTO = Mappers.getMapper(SettingsESMapper.class).map(searchWrapperModel);

		String searchDTOString = JsonToBeanTestUtil.writeValueAsString(searchDTO);

		JSONAssert.assertEquals(expected, searchDTOString, false);
	}
}
+27 −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": "settings-a31bd54e-836e-4942-b10b-8d7bdbd6f196",
			"name": "prueba",
			"shared": false,
			"service": "atlas",
			"userId": "7",
			"selection": ["atlas-b31bd54e-936e-5942-c10b-9d7bdbd6f194"],
			"inserted": "2017-05-23T12:35:12.052+00:00",
			"updated": "2017-05-23T12:35:12.052+00:00",
			"accessed": "2017-05-23T12:35:12.052+00:00"
		}
	]
}
 No newline at end of file
+11 −0
Original line number Diff line number Diff line
{
	"id": "settings-a31bd54e-836e-4942-b10b-8d7bdbd6f196",
	"name": "prueba",
	"shared": false,
	"service": "atlas",
	"userId": "7",
	"selection": ["atlas-b31bd54e-936e-5942-c10b-9d7bdbd6f194"],
	"inserted": "2017-05-23T12:35:12.052+00:00",
	"updated": "2017-05-23T12:35:12.052+00:00",
	"accessed": "2017-05-23T12:35:12.052+00:00"
}
 No newline at end of file
+33 −0
Original line number Diff line number Diff line
{
	"took": 1,
	"timed_out": false,
	"_shards": {
		"total": 5,
		"successful": 5,
		"failed": 0
	},
	"hits": {
		"total": 1,
		"max_score": null,
		"hits": [
			{
				"_index": "settings",
				"_type": "_doc",
				"_id": "settings-a31bd54e-836e-4942-b10b-8d7bdbd6f196",
				"_version": 1,
				"_score": 1,
				"_source": {
					"id": "settings-a31bd54e-836e-4942-b10b-8d7bdbd6f196",
					"name": "prueba",
					"shared": false,
					"service": "atlas",
					"userId": "7",
					"selection": ["atlas-b31bd54e-936e-5942-c10b-9d7bdbd6f194"],
					"inserted": "2017-05-23T12:35:12.052+00:00",
					"updated": "2017-05-23T12:35:12.052+00:00",
					"accessed": "2017-05-23T12:35:12.052+00:00"
				}
			}
		]
	}
}
 No newline at end of file
Loading