Skip to content

Commit

Permalink
[4414] Return valid JSON on commit changes REST APIs
Browse files Browse the repository at this point in the history
Bug: #4414
Signed-off-by: Axel RICHARD <[email protected]>
  • Loading branch information
AxelRICHARD committed Jan 22, 2025
1 parent 35b3475 commit 57f012c
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Before this contribution, migration participants were executed if the project be
Now they are executed for some data if the data was coming from a studio too.
- https://github.com/eclipse-sirius/sirius-web/issues/4422[#4422] [diagram] Prevent application crash when an edge path is malformed during node moves
- https://github.com/eclipse-sirius/sirius-web/issues/4437[#4437] [diagram] Fix selection dialog not opening

- https://github.com/eclipse-sirius/sirius-web/issues/4414[#4414] [sirius-web] Provide a default behavior to return valid JSON on `/api/rest/projects/{projectId}/commits/{commitId}/changes` REST APIs.

=== New Features

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2024 Obeo.
* Copyright (c) 2024, 2025 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
Expand Down Expand Up @@ -45,6 +45,8 @@ public void serialize(Object value, JsonGenerator gen, SerializerProvider serial
.findFirst();
if (optionalDelegate.isPresent()) {
optionalDelegate.get().serialize(value, gen, serializers);
} else {
gen.writeNull();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*******************************************************************************
* Copyright (c) 2025 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.application.controllers.projects;

import java.util.UUID;

import org.eclipse.sirius.web.AbstractIntegrationTests;
import org.eclipse.sirius.web.data.TestIdentifiers;
import org.eclipse.sirius.web.tests.data.GivenSiriusWebServer;
import org.eclipse.sirius.web.tests.services.api.IGivenInitialServerState;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.transaction.annotation.Transactional;

/**
* Integration tests of the project-data-versioning commits REST controller.
*
* @author arichard
*/
@Transactional
@SuppressWarnings("checkstyle:MultipleStringLiterals")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class CommitChangesNoPayloadRestControllerIntegrationTests extends AbstractIntegrationTests {

@Autowired
private IGivenInitialServerState givenInitialServerState;

@LocalServerPort
private String port;

private String getHTTPBaseUrl() {
return "http://localhost:" + this.port;
}

@BeforeEach
public void beforeEach() {
this.givenInitialServerState.initialize();
}

@Test
@GivenSiriusWebServer
@DisplayName("Given the Sirius Web REST API, when we ask for commit changes of a project, then it should return these commit changes with a null payload")
public void givenSiriusWebRestAPIWhenWeAskForCommitChangesOfAProjectThenItShouldReturnTheseCommitChangesWithANullPayload() {
var webTestClient = WebTestClient.bindToServer()
.baseUrl(this.getHTTPBaseUrl())
.build();

var uri = String.format("/api/rest/projects/%s/commits/%s/changes", TestIdentifiers.ECORE_SAMPLE_PROJECT, TestIdentifiers.ECORE_SAMPLE_PROJECT);
webTestClient
.get()
.uri(uri)
.exchange()
.expectStatus()
.isOk()
.expectBody()
.jsonPath("$[0].@type").isEqualTo("DataVersion")
.jsonPath("$[0].identity.@id").isEqualTo(TestIdentifiers.EPACKAGE_OBJECT.toString())
.jsonPath("$[0].identity.@type").isEqualTo("DataIdentity")
.jsonPath("$[0].payload").isEqualTo(null)
.jsonPath("$[1].@type").isEqualTo("DataVersion")
.jsonPath("$[1].identity.@id").isEqualTo(TestIdentifiers.ECLASS_OBJECT.toString())
.jsonPath("$[0].identity.@type").isEqualTo("DataIdentity")
.jsonPath("$[1].payload").isEqualTo(null);
}


@Test
@GivenSiriusWebServer
@DisplayName("Given the Sirius Web REST API, when we ask for a specific commit change, then it should return this commit change with a null payload")
public void givenSiriusWebRestAPIWhenWeAskForASpecificCommitChangeThenItShouldReturnThisCommitChangeWithANullPayload() {
var webTestClient = WebTestClient.bindToServer()
.baseUrl(this.getHTTPBaseUrl())
.build();

var changeId = UUID.nameUUIDFromBytes((TestIdentifiers.ECORE_SAMPLE_PROJECT + TestIdentifiers.EPACKAGE_OBJECT).getBytes());
var uri = String.format("/api/rest/projects/%s/commits/%s/changes/%s", TestIdentifiers.ECORE_SAMPLE_PROJECT, TestIdentifiers.ECORE_SAMPLE_PROJECT, changeId);
webTestClient
.get()
.uri(uri)
.exchange()
.expectStatus()
.isOk()
.expectBody()
.jsonPath("$.@type").isEqualTo("DataVersion")
.jsonPath("$.identity.@id").isEqualTo(TestIdentifiers.EPACKAGE_OBJECT.toString())
.jsonPath("$.identity.@type").isEqualTo("DataIdentity")
.jsonPath("$.payload").isEqualTo(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
*/
@Transactional
@SuppressWarnings("checkstyle:MultipleStringLiterals")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = { "sirius.web.test.enabled=rest-commit-payload" })
public class CommitRestControllerIntegrationTests extends AbstractIntegrationTests {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2024 Obeo.
* Copyright (c) 2024, 2025 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
Expand All @@ -20,6 +20,8 @@
import org.eclipse.emf.ecore.EObject;
import org.eclipse.sirius.web.EObjectJsonSerializer;
import org.eclipse.sirius.web.application.project.data.versioning.services.api.IRestDataVersionPayloadSerializerService;
import org.eclipse.sirius.web.services.OnRestCommitPayloadTests;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Service;

/**
Expand All @@ -29,6 +31,7 @@
* @author arichard
*/
@Service
@Conditional(OnRestCommitPayloadTests.class)
public class RestDataVersionPayloadSerializerTestService implements IRestDataVersionPayloadSerializerService {

private EObjectJsonSerializer eObjectJsonSerializer;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*******************************************************************************
* Copyright (c) 2025 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.services;

import java.util.Arrays;

import org.eclipse.sirius.web.application.configurationproperties.SiriusWebProperties;
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
* Custom condition used to detect that a REST Commit Payload related test is running.
*
* @author arichard
*/
public class OnRestCommitPayloadTests extends SpringBootCondition {

@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
var message = ConditionMessage.forCondition(this.getClass().getSimpleName()).notAvailable("sirius.web.test.enabled");
ConditionOutcome outcome = ConditionOutcome.noMatch(message);

var siriusWebTestEnabled = context.getEnvironment().getProperty("sirius.web.test.enabled", "");
var siriusWebTestEnabledFeatures = Arrays.stream(siriusWebTestEnabled.split(",")).map(String::trim).toList();

if (siriusWebTestEnabledFeatures.contains("*")) {
message = ConditionMessage.forCondition(this.getClass().getSimpleName()).available("sirius.web.test.enabled=" + SiriusWebProperties.EVERYTHING);
outcome = ConditionOutcome.match(message);
} else if (siriusWebTestEnabledFeatures.contains("rest-commit-payload")) {
message = ConditionMessage.forCondition(this.getClass().getSimpleName()).available("sirius.web.test.enabled=" + SiriusWebProperties.STUDIO);
outcome = ConditionOutcome.match(message);
}

return outcome;
}
}

0 comments on commit 57f012c

Please sign in to comment.