Commit ba557421 authored by Noel Alonso's avatar Noel Alonso
Browse files

Añade comprobación de que el padre existe + tests

Antes de guardar/editar una capa, es necesario comprobar que la
categoría a la que se está asociando existe.
parent 43d70c14
Loading
Loading
Loading
Loading
+28 −1
Original line number Diff line number Diff line
package es.redmic.atlasview.service.layer;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.mapstruct.factory.Mappers;

/*-
@@ -29,7 +31,9 @@ import es.redmic.atlaslib.dto.layer.LayerDTO;
import es.redmic.atlasview.mapper.layer.LayerESMapper;
import es.redmic.atlasview.model.layer.Layer;
import es.redmic.atlasview.model.layer.LayerWMS;
import es.redmic.atlasview.repository.category.CategoryESRepository;
import es.redmic.atlasview.repository.layer.LayerESRepository;
import es.redmic.exception.common.ExceptionType;
import es.redmic.models.es.common.dto.EventApplicationResult;
import es.redmic.models.es.common.dto.JSONCollectionDTO;
import es.redmic.models.es.common.query.dto.MgetDTO;
@@ -43,19 +47,42 @@ import es.redmic.viewlib.data.service.RDataService;
@Service
public class LayerESService extends RDataService<Layer, LayerDTO, SimpleQueryDTO> {

	protected static Logger logger = LogManager.getLogger();

	LayerESRepository repository;

	CategoryESRepository categoryRepository;

	@Autowired
	public LayerESService(LayerESRepository repository) {
	public LayerESService(LayerESRepository repository, CategoryESRepository categoryRepository) {
		super(repository);
		this.repository = repository;
		this.categoryRepository = categoryRepository;
	}

	public EventApplicationResult save(Layer model, String parentId) {

		try {
			categoryRepository.findById(parentId);
		} catch (Exception e) {
			logger.error("Categoría con id ",
					parentId + " no encontrada. Imposible guardar una capa asociada a una categoría que no existe");
			return new EventApplicationResult(ExceptionType.ES_PARENT_NOT_EXIST_ERROR.toString(), "parentId", parentId);
		}

		return repository.save(model, parentId);
	}

	public EventApplicationResult update(Layer model, String parentId) {

		try {
			categoryRepository.findById(parentId);
		} catch (Exception e) {
			logger.error("Categoría con id ",
					parentId + " no encontrada. Imposible modificar una capa asociada a una categoría que no existe");
			return new EventApplicationResult(ExceptionType.ES_PARENT_NOT_EXIST_ERROR.toString(), "parentId", parentId);
		}

		return repository.update(model, parentId);
	}

+63 −5
Original line number Diff line number Diff line
@@ -142,7 +142,7 @@ public class LayerEventHandlerTest extends DocumentationViewBaseTest {
	}

	@Test
	public void sendLayerCreatedEvent_SaveItem_IfEventIsOk() throws Exception {
	public void sendCreateLayerEvent_SaveItem_IfEventIsOk() throws Exception {

		CreateLayerEvent event = getCreateLayerEvent();

@@ -225,7 +225,7 @@ public class LayerEventHandlerTest extends DocumentationViewBaseTest {
	}

	@Test(expected = ItemNotFoundException.class)
	public void sendLayerDeleteEvent_callDelete_IfEventIsOk() throws Exception {
	public void sendDeleteLayerEvent_callDelete_IfEventIsOk() throws Exception {

		DeleteLayerEvent event = getDeleteLayerEvent();

@@ -247,7 +247,7 @@ public class LayerEventHandlerTest extends DocumentationViewBaseTest {
	}

	@Test
	public void sendLayerCreatedEvent_PublishCreateLayerFailedEvent_IfNoConstraintsFulfilled() throws Exception {
	public void sendCreateLayerEvent_PublishCreateLayerFailedEvent_IfNoConstraintsFulfilled() throws Exception {

		CreateLayerEvent event = getCreateLayerEvent();

@@ -279,7 +279,36 @@ public class LayerEventHandlerTest extends DocumentationViewBaseTest {
	}

	@Test
	public void sendLayerUpdateEvent_PublishUpdateLayerFailedEvent_IfNoConstraintsFulfilled() throws Exception {
	public void sendCreateLayerEvent_PublishCreateLayerFailedEvent_IfParentNotExist() throws Exception {

		CreateLayerEvent event = getCreateLayerEvent();

		String newParentId = "dddd";

		event.getLayer().getParent().setId(newParentId);

		ListenableFuture<SendResult<String, Event>> future = kafkaTemplate.send(LAYER_TOPIC, event.getAggregateId(),
				event);
		future.addCallback(new SendListener());

		Event fail = (Event) blockingQueue.poll(50, TimeUnit.SECONDS);

		assertNotNull(fail);
		assertEquals(LayerEventTypes.CREATE_FAILED.toString(), fail.getType());

		CreateLayerFailedEvent createLayerFailedEvent = (CreateLayerFailedEvent) fail;

		Map<String, String> arguments = createLayerFailedEvent.getArguments();
		assertNotNull(arguments);

		assertEquals(1, arguments.size());

		assertNotNull(arguments.get("parentId"));
		assertEquals(newParentId, arguments.get("parentId"));
	}

	@Test
	public void sendUpdateLayerEvent_PublishUpdateLayerFailedEvent_IfNoConstraintsFulfilled() throws Exception {

		UpdateLayerEvent event = getUpdateLayerEvent();

@@ -327,7 +356,36 @@ public class LayerEventHandlerTest extends DocumentationViewBaseTest {
	}

	@Test
	public void sendLayerDeleteEvent_PublishDeleteLayerFailedEvent_IfNoConstraintsFulfilled() throws Exception {
	public void sendUpdateLayerEvent_PublishCreateLayerFailedEvent_IfParentNotExist() throws Exception {

		UpdateLayerEvent event = getUpdateLayerEvent();

		String newParentId = "dddd";

		event.getLayer().getParent().setId(newParentId);

		ListenableFuture<SendResult<String, Event>> future = kafkaTemplate.send(LAYER_TOPIC, event.getAggregateId(),
				event);
		future.addCallback(new SendListener());

		Event fail = (Event) blockingQueue.poll(50, TimeUnit.SECONDS);

		assertNotNull(fail);
		assertEquals(LayerEventTypes.UPDATE_FAILED.toString(), fail.getType());

		UpdateLayerFailedEvent updateLayerFailedEvent = (UpdateLayerFailedEvent) fail;

		Map<String, String> arguments = updateLayerFailedEvent.getArguments();
		assertNotNull(arguments);

		assertEquals(1, arguments.size());

		assertNotNull(arguments.get("parentId"));
		assertEquals(newParentId, arguments.get("parentId"));
	}

	@Test
	public void sendDeleteLayerEvent_PublishDeleteLayerFailedEvent_IfNoConstraintsFulfilled() throws Exception {

		// TODO: Implementar cuando se metan las referencias en la vista.
		assertTrue(true);