From f15670650624a8f0e4be6ad54c24675b63bad709 Mon Sep 17 00:00:00 2001 From: Florian Barbin Date: Thu, 26 Aug 2021 15:51:57 +0200 Subject: [PATCH] [697] Add queryBasedInt into EditingContext GraphQL API. Bug: https://github.com/eclipse-sirius/sirius-components/issues/697 Signed-off-by: Florian Barbin --- .../sirius/web/emf/query/EMFQueryService.java | 64 +++++++++++++++ .../web/emf/query/EditingContextServices.java | 67 +++++++++++++++ .../web/emf/query/EMFQueryServiceTests.java | 82 +++++++++++++++++++ .../emf/services/EditingDomainFactory.java | 56 +++++++++++++ .../collaborative/api/IQueryService.java | 26 ++++++ .../handlers/QueryBasedIntEventHandler.java | 76 +++++++++++++++++ .../handlers/QueryBasedIntInput.java | 56 +++++++++++++ .../handlers/QueryBasedIntSuccessPayload.java | 53 ++++++++++++ .../src/main/resources/schema/core.graphqls | 1 + .../QueryBasedIntEventHandlerTests.java | 73 +++++++++++++++++ 10 files changed, 554 insertions(+) create mode 100644 backend/sirius-web-emf/src/main/java/org/eclipse/sirius/web/emf/query/EMFQueryService.java create mode 100644 backend/sirius-web-emf/src/main/java/org/eclipse/sirius/web/emf/query/EditingContextServices.java create mode 100644 backend/sirius-web-emf/src/test/java/org/eclipse/sirius/web/emf/query/EMFQueryServiceTests.java create mode 100644 backend/sirius-web-emf/src/test/java/org/eclipse/sirius/web/emf/services/EditingDomainFactory.java create mode 100644 backend/sirius-web-spring-collaborative/src/main/java/org/eclipse/sirius/web/spring/collaborative/api/IQueryService.java create mode 100644 backend/sirius-web-spring-collaborative/src/main/java/org/eclipse/sirius/web/spring/collaborative/handlers/QueryBasedIntEventHandler.java create mode 100644 backend/sirius-web-spring-collaborative/src/main/java/org/eclipse/sirius/web/spring/collaborative/handlers/QueryBasedIntInput.java create mode 100644 backend/sirius-web-spring-collaborative/src/main/java/org/eclipse/sirius/web/spring/collaborative/handlers/QueryBasedIntSuccessPayload.java create mode 100644 backend/sirius-web-spring-collaborative/src/test/java/org/eclipse/sirius/web/spring/collaborative/handlers/QueryBasedIntEventHandlerTests.java diff --git a/backend/sirius-web-emf/src/main/java/org/eclipse/sirius/web/emf/query/EMFQueryService.java b/backend/sirius-web-emf/src/main/java/org/eclipse/sirius/web/emf/query/EMFQueryService.java new file mode 100644 index 00000000000..6ea9d5bcfe5 --- /dev/null +++ b/backend/sirius-web-emf/src/main/java/org/eclipse/sirius/web/emf/query/EMFQueryService.java @@ -0,0 +1,64 @@ +/******************************************************************************* + * Copyright (c) 2021 Obeo. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.eclipse.sirius.web.emf.query; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; + +import org.eclipse.emf.ecore.EPackage; +import org.eclipse.sirius.web.core.api.ErrorPayload; +import org.eclipse.sirius.web.core.api.IEditingContext; +import org.eclipse.sirius.web.core.api.IPayload; +import org.eclipse.sirius.web.emf.services.IEditingContextEPackageService; +import org.eclipse.sirius.web.interpreter.AQLInterpreter; +import org.eclipse.sirius.web.interpreter.Result; +import org.eclipse.sirius.web.spring.collaborative.api.IQueryService; +import org.eclipse.sirius.web.spring.collaborative.handlers.QueryBasedIntInput; +import org.eclipse.sirius.web.spring.collaborative.handlers.QueryBasedIntSuccessPayload; +import org.springframework.stereotype.Service; + +/** + * A specific implementation of {@link IQueryService} for EMF. + * + * @author fbarbin + */ +@Service +public class EMFQueryService implements IQueryService { + + private final IEditingContextEPackageService editingContextEPackageService; + + public EMFQueryService(IEditingContextEPackageService editingContextEPackageService) { + this.editingContextEPackageService = Objects.requireNonNull(editingContextEPackageService); + } + + @Override + public IPayload execute(IEditingContext editingContext, QueryBasedIntInput input) { + List> classes = List.of(EditingContextServices.class); + List ePackages = this.editingContextEPackageService.getEPackages(editingContext.getId()); + + Map variables = new HashMap<>(); + variables.put(IEditingContext.EDITING_CONTEXT, editingContext); + + var interpreter = new AQLInterpreter(classes, ePackages); + String query = input.getQuery(); + Result result = interpreter.evaluateExpression(variables, query); + + if (result.asInt().isPresent()) { + return new QueryBasedIntSuccessPayload(input.getId(), result.asInt().getAsInt()); + } else { + return new ErrorPayload(input.getId(), "An error occured while evaluation the expression. Status : " + result.getStatus()); //$NON-NLS-1$ + } + } +} diff --git a/backend/sirius-web-emf/src/main/java/org/eclipse/sirius/web/emf/query/EditingContextServices.java b/backend/sirius-web-emf/src/main/java/org/eclipse/sirius/web/emf/query/EditingContextServices.java new file mode 100644 index 00000000000..f3e723a09b9 --- /dev/null +++ b/backend/sirius-web-emf/src/main/java/org/eclipse/sirius/web/emf/query/EditingContextServices.java @@ -0,0 +1,67 @@ +/******************************************************************************* + * Copyright (c) 2021 Obeo. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.eclipse.sirius.web.emf.query; + +import java.util.Collection; +import java.util.Collections; +import java.util.Optional; +import java.util.Spliterator; +import java.util.Spliterators; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; + +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.edit.domain.EditingDomain; +import org.eclipse.sirius.web.annotations.Immutable; +import org.eclipse.sirius.web.core.api.IEditingContext; +import org.eclipse.sirius.web.emf.services.EditingContext; + +/** + * An utility class providing various query services. + * + * @author fbarbin + */ +@Immutable +public final class EditingContextServices { + + private EditingContextServices() { + + } + + public static Collection eAllContents(IEditingContext editingContext) { + //@formatter:off + var resourceSet = Optional.of(editingContext) + .filter(EditingContext.class::isInstance) + .map(EditingContext.class::cast) + .map(EditingContext::getDomain) + .map(EditingDomain::getResourceSet) + .orElse(null); + //@formatter:on + if (resourceSet != null) { + //@formatter:off + return resourceSet.getResources().stream() + .flatMap(EditingContextServices::collectAllContent) + .collect(Collectors.toList()); + //@formatter:on + } else { + return Collections.emptyList(); + } + } + + private static Stream collectAllContent(Resource resource) { + Spliterator spliterator = Spliterators.spliteratorUnknownSize(resource.getAllContents(), Spliterator.ORDERED); + return StreamSupport.stream(spliterator, false); + } +} diff --git a/backend/sirius-web-emf/src/test/java/org/eclipse/sirius/web/emf/query/EMFQueryServiceTests.java b/backend/sirius-web-emf/src/test/java/org/eclipse/sirius/web/emf/query/EMFQueryServiceTests.java new file mode 100644 index 00000000000..9b1efb55bc4 --- /dev/null +++ b/backend/sirius-web-emf/src/test/java/org/eclipse/sirius/web/emf/query/EMFQueryServiceTests.java @@ -0,0 +1,82 @@ +/******************************************************************************* + * Copyright (c) 2021 Obeo. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.eclipse.sirius.web.emf.query; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.List; +import java.util.UUID; + +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.EPackage; +import org.eclipse.emf.ecore.EcoreFactory; +import org.eclipse.emf.ecore.EcorePackage; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl; +import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain; +import org.eclipse.sirius.web.core.api.IEditingContext; +import org.eclipse.sirius.web.core.api.IPayload; +import org.eclipse.sirius.web.emf.services.EditingContext; +import org.eclipse.sirius.web.emf.services.EditingDomainFactory; +import org.eclipse.sirius.web.emf.services.IEditingContextEPackageService; +import org.eclipse.sirius.web.spring.collaborative.api.IQueryService; +import org.eclipse.sirius.web.spring.collaborative.handlers.QueryBasedIntInput; +import org.eclipse.sirius.web.spring.collaborative.handlers.QueryBasedIntSuccessPayload; +import org.junit.jupiter.api.Test; + +/** + * Unit tests for {@link EMFQueryService}. + * + * @author fbarbin + */ +public class EMFQueryServiceTests { + + @Test + public void testEMFQueryService() { + + IEditingContext editingContext = this.createEditingContext(); + + IEditingContextEPackageService editingContextEPackageService = new IEditingContextEPackageService() { + + @Override + public List getEPackages(UUID editingContextId) { + return List.of(EcorePackage.eINSTANCE); + } + }; + IQueryService queryService = new EMFQueryService(editingContextEPackageService); + + QueryBasedIntInput input = new QueryBasedIntInput(UUID.randomUUID(), "aql:editingContext.eAllContents()->size()"); //$NON-NLS-1$ + IPayload payload = queryService.execute(editingContext, input); + assertTrue(payload instanceof QueryBasedIntSuccessPayload); + assertEquals(8, ((QueryBasedIntSuccessPayload) payload).getResult().intValue()); + } + + private IEditingContext createEditingContext() { + Resource resource = this.createResourceWith4Elements(); + Resource resource2 = this.createResourceWith4Elements(); + AdapterFactoryEditingDomain editingDomain = new EditingDomainFactory().create(resource, resource2); + return new EditingContext(UUID.randomUUID(), editingDomain); + } + + private Resource createResourceWith4Elements() { + Resource resource = new XMIResourceImpl(); + EPackage ePackage = EcoreFactory.eINSTANCE.createEPackage(); + EClass class1 = EcoreFactory.eINSTANCE.createEClass(); + EClass class2 = EcoreFactory.eINSTANCE.createEClass(); + EClass class3 = EcoreFactory.eINSTANCE.createEClass(); + ePackage.getEClassifiers().addAll(List.of(class1, class2, class3)); + resource.getContents().add(ePackage); + return resource; + } +} diff --git a/backend/sirius-web-emf/src/test/java/org/eclipse/sirius/web/emf/services/EditingDomainFactory.java b/backend/sirius-web-emf/src/test/java/org/eclipse/sirius/web/emf/services/EditingDomainFactory.java new file mode 100644 index 00000000000..2e909111e8c --- /dev/null +++ b/backend/sirius-web-emf/src/test/java/org/eclipse/sirius/web/emf/services/EditingDomainFactory.java @@ -0,0 +1,56 @@ +/******************************************************************************* + * Copyright (c) 2019, 2021 Obeo. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.eclipse.sirius.web.emf.services; + +import org.eclipse.emf.common.command.BasicCommandStack; +import org.eclipse.emf.ecore.EPackage; +import org.eclipse.emf.ecore.EcorePackage; +import org.eclipse.emf.ecore.impl.EPackageRegistryImpl; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.resource.ResourceSet; +import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; +import org.eclipse.emf.ecore.util.ECrossReferenceAdapter; +import org.eclipse.emf.ecore.util.EcoreAdapterFactory; +import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain; +import org.eclipse.emf.edit.provider.ComposedAdapterFactory; +import org.eclipse.emf.edit.provider.ReflectiveItemProviderAdapterFactory; + +/** + * Service used to create an editing domain for the unit tests. + * + * @author sbegaudeau + */ +public class EditingDomainFactory { + public AdapterFactoryEditingDomain create(Resource... resources) { + ComposedAdapterFactory composedAdapterFactory = new ComposedAdapterFactory(); + composedAdapterFactory.addAdapterFactory(new EcoreAdapterFactory()); + composedAdapterFactory.addAdapterFactory(new ReflectiveItemProviderAdapterFactory()); + + EPackage.Registry ePackageRegistry = new EPackageRegistryImpl(); + ePackageRegistry.put(EcorePackage.eINSTANCE.getNsURI(), EcorePackage.eINSTANCE); + + ResourceSet resourceSet = new ResourceSetImpl(); + for (Resource resource : resources) { + resourceSet.getResources().add(resource); + } + resourceSet.setPackageRegistry(ePackageRegistry); + resourceSet.eAdapters().add(new ECrossReferenceAdapter()); + + AdapterFactoryEditingDomain editingDomain = new AdapterFactoryEditingDomain(composedAdapterFactory, new BasicCommandStack(), resourceSet); + return editingDomain; + } + + public AdapterFactoryEditingDomain create() { + return this.create(new Resource[0]); + } +} diff --git a/backend/sirius-web-spring-collaborative/src/main/java/org/eclipse/sirius/web/spring/collaborative/api/IQueryService.java b/backend/sirius-web-spring-collaborative/src/main/java/org/eclipse/sirius/web/spring/collaborative/api/IQueryService.java new file mode 100644 index 00000000000..b255ef9ad5a --- /dev/null +++ b/backend/sirius-web-spring-collaborative/src/main/java/org/eclipse/sirius/web/spring/collaborative/api/IQueryService.java @@ -0,0 +1,26 @@ +/******************************************************************************* + * Copyright (c) 2021 Obeo. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.eclipse.sirius.web.spring.collaborative.api; + +import org.eclipse.sirius.web.core.api.IEditingContext; +import org.eclipse.sirius.web.core.api.IPayload; +import org.eclipse.sirius.web.spring.collaborative.handlers.QueryBasedIntInput; + +/** + * Common interface for services capable of executing a Query. + * + * @author fbarbin + */ +public interface IQueryService { + IPayload execute(IEditingContext editingContext, QueryBasedIntInput input); +} diff --git a/backend/sirius-web-spring-collaborative/src/main/java/org/eclipse/sirius/web/spring/collaborative/handlers/QueryBasedIntEventHandler.java b/backend/sirius-web-spring-collaborative/src/main/java/org/eclipse/sirius/web/spring/collaborative/handlers/QueryBasedIntEventHandler.java new file mode 100644 index 00000000000..648448de5b7 --- /dev/null +++ b/backend/sirius-web-spring-collaborative/src/main/java/org/eclipse/sirius/web/spring/collaborative/handlers/QueryBasedIntEventHandler.java @@ -0,0 +1,76 @@ +/******************************************************************************* + * Copyright (c) 2021 Obeo. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.eclipse.sirius.web.spring.collaborative.handlers; + +import java.util.Objects; + +import org.eclipse.sirius.web.core.api.ErrorPayload; +import org.eclipse.sirius.web.core.api.IEditingContext; +import org.eclipse.sirius.web.core.api.IInput; +import org.eclipse.sirius.web.core.api.IPayload; +import org.eclipse.sirius.web.spring.collaborative.api.ChangeDescription; +import org.eclipse.sirius.web.spring.collaborative.api.ChangeKind; +import org.eclipse.sirius.web.spring.collaborative.api.EventHandlerResponse; +import org.eclipse.sirius.web.spring.collaborative.api.IEditingContextEventHandler; +import org.eclipse.sirius.web.spring.collaborative.api.IQueryService; +import org.eclipse.sirius.web.spring.collaborative.api.Monitoring; +import org.eclipse.sirius.web.spring.collaborative.messages.ICollaborativeMessageService; +import org.springframework.stereotype.Service; + +import io.micrometer.core.instrument.Counter; +import io.micrometer.core.instrument.MeterRegistry; + +/** + * Handler used to execute query based int. + * + * @author fbarbin + */ +@Service +public class QueryBasedIntEventHandler implements IEditingContextEventHandler { + + private final IQueryService queryService; + + private final ICollaborativeMessageService messageService; + + private final Counter counter; + + public QueryBasedIntEventHandler(ICollaborativeMessageService messageService, MeterRegistry meterRegistry, IQueryService queryService) { + this.messageService = Objects.requireNonNull(messageService); + this.queryService = Objects.requireNonNull(queryService); + + // @formatter:off + this.counter = Counter.builder(Monitoring.EVENT_HANDLER) + .tag(Monitoring.NAME, this.getClass().getSimpleName()) + .register(meterRegistry); + // @formatter:on + } + + @Override + public boolean canHandle(IInput input) { + return input instanceof QueryBasedIntInput; + } + + @Override + public EventHandlerResponse handle(IEditingContext editingContext, IInput input) { + this.counter.increment(); + String message = this.messageService.invalidInput(input.getClass().getSimpleName(), QueryBasedIntInput.class.getSimpleName()); + ChangeDescription changeDescription = new ChangeDescription(ChangeKind.NOTHING, editingContext.getId()); + if (input instanceof QueryBasedIntInput) { + QueryBasedIntInput createChildInput = (QueryBasedIntInput) input; + IPayload payload = this.queryService.execute(editingContext, createChildInput); + return new EventHandlerResponse(changeDescription, payload); + } + ErrorPayload errorPayload = new ErrorPayload(input.getId(), message); + return new EventHandlerResponse(changeDescription, errorPayload); + } +} diff --git a/backend/sirius-web-spring-collaborative/src/main/java/org/eclipse/sirius/web/spring/collaborative/handlers/QueryBasedIntInput.java b/backend/sirius-web-spring-collaborative/src/main/java/org/eclipse/sirius/web/spring/collaborative/handlers/QueryBasedIntInput.java new file mode 100644 index 00000000000..ba741cffcf1 --- /dev/null +++ b/backend/sirius-web-spring-collaborative/src/main/java/org/eclipse/sirius/web/spring/collaborative/handlers/QueryBasedIntInput.java @@ -0,0 +1,56 @@ +/******************************************************************************* + * Copyright (c) 2021 Obeo. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.eclipse.sirius.web.spring.collaborative.handlers; + +import java.text.MessageFormat; +import java.util.Objects; +import java.util.UUID; + +import org.eclipse.sirius.web.core.api.IInput; + +/** + * The input object of the queryBasedIntEventHandler. + * + * @author fbarbin + */ +public final class QueryBasedIntInput implements IInput { + + private UUID id; + + private String query; + + public QueryBasedIntInput() { + // Used by Jackson + } + + public QueryBasedIntInput(UUID id, String query) { + this.id = Objects.requireNonNull(id); + this.query = Objects.requireNonNull(query); + } + + @Override + public UUID getId() { + return this.id; + } + + public String getQuery() { + return this.query; + } + + @Override + public String toString() { + String pattern = "{0} '{'id: {1}, query: {2}'}'"; //$NON-NLS-1$ + return MessageFormat.format(pattern, this.getClass().getSimpleName(), this.id, this.query); + } + +} diff --git a/backend/sirius-web-spring-collaborative/src/main/java/org/eclipse/sirius/web/spring/collaborative/handlers/QueryBasedIntSuccessPayload.java b/backend/sirius-web-spring-collaborative/src/main/java/org/eclipse/sirius/web/spring/collaborative/handlers/QueryBasedIntSuccessPayload.java new file mode 100644 index 00000000000..48c1a3b678f --- /dev/null +++ b/backend/sirius-web-spring-collaborative/src/main/java/org/eclipse/sirius/web/spring/collaborative/handlers/QueryBasedIntSuccessPayload.java @@ -0,0 +1,53 @@ +/******************************************************************************* + * Copyright (c) 2021 Obeo. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.eclipse.sirius.web.spring.collaborative.handlers; + +import java.text.MessageFormat; +import java.util.Objects; +import java.util.UUID; + +import org.eclipse.sirius.web.annotations.graphql.GraphQLObjectType; +import org.eclipse.sirius.web.core.api.IPayload; + +/** + * The payload of the queryBasedInt query. + * + * @author fbarbin + */ +@GraphQLObjectType +public final class QueryBasedIntSuccessPayload implements IPayload { + + private final UUID id; + + private final Integer result; + + public QueryBasedIntSuccessPayload(UUID id, Integer result) { + this.id = Objects.requireNonNull(id); + this.result = Objects.requireNonNull(result); + } + + @Override + public UUID getId() { + return this.id; + } + + public Integer getResult() { + return this.result; + } + + @Override + public String toString() { + String pattern = "{0} '{'id: {1}, result: {2}'}'"; //$NON-NLS-1$ + return MessageFormat.format(pattern, this.getClass().getSimpleName(), this.id, this.result); + } +} diff --git a/backend/sirius-web-spring-collaborative/src/main/resources/schema/core.graphqls b/backend/sirius-web-spring-collaborative/src/main/resources/schema/core.graphqls index b7ad7e7cd5b..8e55d27a395 100644 --- a/backend/sirius-web-spring-collaborative/src/main/resources/schema/core.graphqls +++ b/backend/sirius-web-spring-collaborative/src/main/resources/schema/core.graphqls @@ -20,6 +20,7 @@ type EditingContext { stereotypeDescriptions: EditingContextStereotypeDescriptionConnection! rootObjectCreationDescriptions(domainId: ID!, suggested: Boolean!): [ChildCreationDescription!]! childCreationDescriptions(classId: ID!): [ChildCreationDescription!]! + queryBasedInt(query: String!): Int } type Domain { diff --git a/backend/sirius-web-spring-collaborative/src/test/java/org/eclipse/sirius/web/spring/collaborative/handlers/QueryBasedIntEventHandlerTests.java b/backend/sirius-web-spring-collaborative/src/test/java/org/eclipse/sirius/web/spring/collaborative/handlers/QueryBasedIntEventHandlerTests.java new file mode 100644 index 00000000000..ac779da8109 --- /dev/null +++ b/backend/sirius-web-spring-collaborative/src/test/java/org/eclipse/sirius/web/spring/collaborative/handlers/QueryBasedIntEventHandlerTests.java @@ -0,0 +1,73 @@ +/******************************************************************************* + * Copyright (c) 2021 Obeo. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.eclipse.sirius.web.spring.collaborative.handlers; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.UUID; + +import org.eclipse.sirius.web.core.api.ErrorPayload; +import org.eclipse.sirius.web.core.api.IEditingContext; +import org.eclipse.sirius.web.core.api.IInput; +import org.eclipse.sirius.web.core.api.IPayload; +import org.eclipse.sirius.web.spring.collaborative.api.EventHandlerResponse; +import org.eclipse.sirius.web.spring.collaborative.api.IQueryService; +import org.junit.jupiter.api.Test; + +import io.micrometer.core.instrument.simple.SimpleMeterRegistry; + +/** + * Tests of the query based int event handler. + * + * @author fbarbin + */ +public class QueryBasedIntEventHandlerTests { + private static final int EXPECTED_RESULT_10 = 10; + + @Test + public void testQueryBasedInt() { + IQueryService queryService = new IQueryService() { + + @Override + public IPayload execute(IEditingContext editingContext, QueryBasedIntInput input) { + return new QueryBasedIntSuccessPayload(UUID.randomUUID(), EXPECTED_RESULT_10); + } + }; + + EventHandlerResponse response = this.handle(queryService); + assertThat(response.getPayload()).isInstanceOf(QueryBasedIntSuccessPayload.class); + assertThat(((QueryBasedIntSuccessPayload) response.getPayload()).getResult()).isEqualTo(EXPECTED_RESULT_10); + } + + @Test + public void testQueryBasedIntFailed() { + IQueryService queryService = new IQueryService() { + @Override + public IPayload execute(IEditingContext editingContext, QueryBasedIntInput input) { + return new ErrorPayload(UUID.randomUUID(), "An error occured"); //$NON-NLS-1$ + } + }; + EventHandlerResponse response = this.handle(queryService); + assertThat(response.getPayload()).isInstanceOf(ErrorPayload.class); + } + + EventHandlerResponse handle(IQueryService queryService) { + QueryBasedIntEventHandler queryBasedIntEventHandler = new QueryBasedIntEventHandler(new NoOpCollaborativeMessageService(), new SimpleMeterRegistry(), queryService); + IInput input = new QueryBasedIntInput(UUID.randomUUID(), "aql:self"); //$NON-NLS-1$ + assertThat(queryBasedIntEventHandler.canHandle(input)).isTrue(); + + IEditingContext editingContext = () -> UUID.randomUUID(); + EventHandlerResponse response = queryBasedIntEventHandler.handle(editingContext, input); + return response; + } +}