|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2024, 2025 Obeo. |
| 3 | + * This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v2.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + * |
| 10 | + * Contributors: |
| 11 | + * Obeo - initial API and implementation |
| 12 | + *******************************************************************************/ |
| 13 | +package org.eclipse.sirius.web.application.controllers.projects; |
| 14 | + |
| 15 | +import static org.assertj.core.api.Assertions.assertThat; |
| 16 | +import static org.assertj.core.api.Assertions.fail; |
| 17 | + |
| 18 | +import java.io.ByteArrayOutputStream; |
| 19 | +import java.io.IOException; |
| 20 | +import java.nio.charset.StandardCharsets; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.List; |
| 23 | +import java.util.zip.ZipInputStream; |
| 24 | + |
| 25 | +import org.eclipse.sirius.web.AbstractIntegrationTests; |
| 26 | +import org.eclipse.sirius.web.data.PapayaIdentifiers; |
| 27 | +import org.eclipse.sirius.web.tests.data.GivenSiriusWebServer; |
| 28 | +import org.eclipse.sirius.web.tests.services.api.IGivenInitialServerState; |
| 29 | +import org.junit.jupiter.api.BeforeEach; |
| 30 | +import org.junit.jupiter.api.DisplayName; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | +import org.springframework.beans.factory.annotation.Autowired; |
| 33 | +import org.springframework.boot.test.context.SpringBootTest; |
| 34 | +import org.springframework.boot.test.web.client.TestRestTemplate; |
| 35 | +import org.springframework.boot.test.web.server.LocalServerPort; |
| 36 | +import org.springframework.core.io.Resource; |
| 37 | +import org.springframework.http.HttpEntity; |
| 38 | +import org.springframework.http.HttpHeaders; |
| 39 | +import org.springframework.http.HttpMethod; |
| 40 | +import org.springframework.http.HttpStatus; |
| 41 | +import org.springframework.http.MediaType; |
| 42 | +import org.springframework.http.ResponseEntity; |
| 43 | +import org.springframework.transaction.annotation.Transactional; |
| 44 | +import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper; |
| 45 | + |
| 46 | +/** |
| 47 | + * Integration tests of the project download controllers for a project with dependencies. |
| 48 | + * |
| 49 | + * @author sbegaudeau |
| 50 | + */ |
| 51 | +@Transactional |
| 52 | +@SuppressWarnings("checkstyle:MultipleStringLiterals") |
| 53 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
| 54 | +public class ProjectWithDependenciesDownloadControllerIntegrationTests extends AbstractIntegrationTests { |
| 55 | + |
| 56 | + @LocalServerPort |
| 57 | + private int port; |
| 58 | + |
| 59 | + @Autowired |
| 60 | + private IGivenInitialServerState givenInitialServerState; |
| 61 | + |
| 62 | + @BeforeEach |
| 63 | + public void beforeEach() { |
| 64 | + this.givenInitialServerState.initialize(); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + @GivenSiriusWebServer |
| 69 | + @DisplayName("Given a project with dependencies, when the download of the project is requested, then the manifest is exported") |
| 70 | + public void givenProjectWithDependenciesWhenTheDownloadOfProjectIsRequestedThenTheManifestIsExported() { |
| 71 | + var response = this.download(PapayaIdentifiers.PAPAYA_SAMPLE_PROJECT, null); |
| 72 | + |
| 73 | + try (ZipInputStream inputStream = new ZipInputStream(response.getBody().getInputStream())) { |
| 74 | + HashMap<String, ByteArrayOutputStream> zipEntries = this.toZipEntries(inputStream); |
| 75 | + assertThat(zipEntries).isNotEmpty().containsKey("Papaya Sample/manifest.json"); |
| 76 | + |
| 77 | + String manifestContentExpected = """ |
| 78 | + { |
| 79 | + "natures":[ |
| 80 | + "siriusComponents://nature?kind=papaya" |
| 81 | + ], |
| 82 | + "documentIdsToName":{ |
| 83 | + "a4495c9c-d00c-4f0e-a591-1176d102a4a1":"Sirius Web Architecture", |
| 84 | + "56a66774-cbff-499f-a9fa-6b6600c86064":"GraphQL", |
| 85 | + "a0473b31-912f-4d99-8b41-3d44a8a1b238":"Sirius Web Project", |
| 86 | + "8139fdb7-bb71-4bca-b50b-9170870bbc0d":"Sirius Web Architecture" |
| 87 | + }, |
| 88 | + "metamodels":[ |
| 89 | + "domain://buck", |
| 90 | + "http://www.eclipse.org/emf/2002/Ecore", |
| 91 | + "https://www.eclipse.org/sirius-web/papaya" |
| 92 | + ], |
| 93 | + "representations":{ |
| 94 | + "dd0080f8-430d-441f-99a4-f46c7d9b28ef":{ |
| 95 | + "targetObjectURI":"sirius:///a4495c9c-d00c-4f0e-a591-1176d102a4a1#569d3f9b-2a43-4254-b609-511258251d96", |
| 96 | + "type":"siriusComponents://representation?type=Table", |
| 97 | + "descriptionURI":"papaya_package_table_description" |
| 98 | + } |
| 99 | + }, |
| 100 | + "dependencies":[ |
| 101 | + "6f24a044-1605-484d-96c3-553ff6bc184d" |
| 102 | + ] |
| 103 | + } |
| 104 | + """; |
| 105 | + |
| 106 | + String manifestContent = zipEntries.get("Papaya Sample/manifest.json").toString(StandardCharsets.UTF_8); |
| 107 | + var objectMapper = new ObjectMapper(); |
| 108 | + assertThat(objectMapper.readTree(manifestContent)).isEqualTo(objectMapper.readTree(manifestContentExpected)); |
| 109 | + } catch (IOException exception) { |
| 110 | + fail(exception.getMessage()); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private ResponseEntity<Resource> download(String projectId, String name) { |
| 115 | + var uri = "http://localhost:" + this.port + "/api/projects/" + projectId; |
| 116 | + if (name != null) { |
| 117 | + uri += "?name=" + name; |
| 118 | + } |
| 119 | + |
| 120 | + HttpHeaders headers = new HttpHeaders(); |
| 121 | + headers.setAccept(List.of(MediaType.parseMediaType("application/zip"))); |
| 122 | + HttpEntity<String> entity = new HttpEntity<>(null, headers); |
| 123 | + |
| 124 | + var response = new TestRestTemplate().exchange(uri, HttpMethod.GET, entity, Resource.class); |
| 125 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); |
| 126 | + |
| 127 | + return response; |
| 128 | + } |
| 129 | + |
| 130 | + private HashMap<String, ByteArrayOutputStream> toZipEntries(ZipInputStream inputStream) { |
| 131 | + HashMap<String, ByteArrayOutputStream> zipEntries = new HashMap<>(); |
| 132 | + |
| 133 | + try { |
| 134 | + var zipEntry = inputStream.getNextEntry(); |
| 135 | + while (zipEntry != null) { |
| 136 | + if (!zipEntry.isDirectory()) { |
| 137 | + String name = zipEntry.getName(); |
| 138 | + ByteArrayOutputStream entryBaos = new ByteArrayOutputStream(); |
| 139 | + inputStream.transferTo(entryBaos); |
| 140 | + zipEntries.put(name, entryBaos); |
| 141 | + } |
| 142 | + zipEntry = inputStream.getNextEntry(); |
| 143 | + } |
| 144 | + } catch (IOException exception) { |
| 145 | + fail(exception.getMessage()); |
| 146 | + } |
| 147 | + |
| 148 | + return zipEntries; |
| 149 | + } |
| 150 | + |
| 151 | +} |
0 commit comments