|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 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.search; |
| 14 | + |
| 15 | +import static org.assertj.core.api.Assertions.assertThat; |
| 16 | + |
| 17 | +import com.jayway.jsonpath.JsonPath; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.UUID; |
| 22 | + |
| 23 | +import org.eclipse.sirius.web.AbstractIntegrationTests; |
| 24 | +import org.eclipse.sirius.web.application.views.search.dto.SearchResult; |
| 25 | +import org.eclipse.sirius.web.application.views.search.dto.SearchSuccessPayload; |
| 26 | +import org.eclipse.sirius.web.data.PapayaIdentifiers; |
| 27 | +import org.eclipse.sirius.web.tests.data.GivenSiriusWebServer; |
| 28 | +import org.eclipse.sirius.web.tests.graphql.SearchQueryRunner; |
| 29 | +import org.eclipse.sirius.web.tests.services.api.IGivenInitialServerState; |
| 30 | +import org.junit.jupiter.api.BeforeEach; |
| 31 | +import org.junit.jupiter.api.DisplayName; |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | +import org.springframework.beans.factory.annotation.Autowired; |
| 34 | +import org.springframework.boot.test.context.SpringBootTest; |
| 35 | +import org.springframework.transaction.annotation.Transactional; |
| 36 | + |
| 37 | +/** |
| 38 | + * Integration tests for the Search view. |
| 39 | + * |
| 40 | + * @author pcdavid |
| 41 | + */ |
| 42 | +@Transactional |
| 43 | +@SuppressWarnings("checkstyle:MultipleStringLiterals") |
| 44 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
| 45 | +public class SearchIntegrationTests extends AbstractIntegrationTests { |
| 46 | + |
| 47 | + @Autowired |
| 48 | + private IGivenInitialServerState givenInitialServerState; |
| 49 | + |
| 50 | + @Autowired |
| 51 | + private SearchQueryRunner searchQueryRunner; |
| 52 | + |
| 53 | + @BeforeEach |
| 54 | + public void beforeEach() { |
| 55 | + this.givenInitialServerState.initialize(); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + @GivenSiriusWebServer |
| 60 | + @DisplayName("Given a project, when we execute a basic search, then all the matching semantic elements are returned") |
| 61 | + public void givenProjectWhenWeExecuteBasicSearchThenAllMatchingElementsAreReturned() { |
| 62 | + List<String> matches = this.search(PapayaIdentifiers.PAPAYA_EDITING_CONTEXT_ID, "sirius-web", false, false, false, false); |
| 63 | + assertThat(matches).containsExactlyInAnyOrder( |
| 64 | + "sirius-web-tests-data", |
| 65 | + "sirius-web-domain", |
| 66 | + "sirius-web-application", |
| 67 | + "sirius-web-infrastructure", |
| 68 | + "sirius-web-starter", |
| 69 | + "sirius-web" |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + @GivenSiriusWebServer |
| 75 | + @DisplayName("Given a project, when we execute a whole word search, then only the matching semantic elements are returned") |
| 76 | + public void givenProjectWhenWeExecuteWholeWordSearchThenOnlyMatchingElementsAreReturned() { |
| 77 | + // "Command" appear multiple times as part of "Command1" and "Command2" |
| 78 | + List<String> matches = this.search(PapayaIdentifiers.PAPAYA_EDITING_CONTEXT_ID, "Command", false, /* mathcWholeWord */ false, false, false); |
| 79 | + assertThat(matches).containsExactly("Command1 over HTTP", "Command2 over null", "Command1", "Command2"); |
| 80 | + // but never as a distinct whole word. |
| 81 | + matches = this.search(PapayaIdentifiers.PAPAYA_EDITING_CONTEXT_ID, "Command", false, /* mathcWholeWord */ true, false, false); |
| 82 | + assertThat(matches).isEmpty(); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + @GivenSiriusWebServer |
| 87 | + @DisplayName("Given a project, when we execute a case insensitive search, then all the matching semantic elements are returned") |
| 88 | + public void givenProjectWhenWeExecuteCaseInsensitiveSearchThenAllMatchingElementsAreReturned() { |
| 89 | + List<String> matches = this.search(PapayaIdentifiers.PAPAYA_EDITING_CONTEXT_ID, "project creation", /* matchCase */ false, false, false, false); |
| 90 | + assertThat(matches).containsExactlyInAnyOrder("Project Creation"); |
| 91 | + |
| 92 | + matches = this.search(PapayaIdentifiers.PAPAYA_EDITING_CONTEXT_ID, "project creation", /* matchCase */ true, false, false, false); |
| 93 | + assertThat(matches).isEmpty(); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + @GivenSiriusWebServer |
| 98 | + @DisplayName("Given a project, when we execute a search including attributes, then all the matching semantic elements are returned") |
| 99 | + public void givenProjectWhenWeExecuteSearchIncludingAttributesThenAllMatchingElementsAreReturned() { |
| 100 | + // There are no matches in object names/labels |
| 101 | + List<String> matches = this.search(PapayaIdentifiers.PAPAYA_EDITING_CONTEXT_ID, "2024-02", false, false, false, /* searchInAttributes */ false); |
| 102 | + assertThat(matches).isEmpty(); |
| 103 | + // but there are in several object attributes |
| 104 | + matches = this.search(PapayaIdentifiers.PAPAYA_EDITING_CONTEXT_ID, "2024-02", false, false, false, /* searchInAttributes */ true); |
| 105 | + // Note: the matches are in these object's attributes, but we return only the objects themselves |
| 106 | + assertThat(matches).containsExactlyInAnyOrder("2024.3.0", "2024.5.0"); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + @GivenSiriusWebServer |
| 111 | + @DisplayName("Given a project, when we execute a search with regular expression, then all the matching semantic elements are returned") |
| 112 | + public void givenProjectWhenWeExecuteSearchWithRegexpThenAllMatchingElementsAreReturned() { |
| 113 | + List<String> matches = this.search(PapayaIdentifiers.PAPAYA_EDITING_CONTEXT_ID, "^sirius-web-([dt]|ap{2}).*[an]$", false, false, /* useRegularExpressions */ false, false); |
| 114 | + assertThat(matches).isEmpty(); |
| 115 | + matches = this.search(PapayaIdentifiers.PAPAYA_EDITING_CONTEXT_ID, "^sirius-web-([dt]|ap{2}).*[an]$", false, false, /* useRegularExpressions */ true, false); |
| 116 | + assertThat(matches).containsExactlyInAnyOrder( |
| 117 | + "sirius-web-tests-data", |
| 118 | + "sirius-web-domain", |
| 119 | + "sirius-web-application" |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + private List<String> search(UUID editingContextId, String text, boolean matchCase, boolean matchWholeWord, boolean useRegularExpressions, boolean searchInAttributes) { |
| 124 | + // The SearchQuery object must be passed as a plain Map here |
| 125 | + var queryMap = Map.of( |
| 126 | + "text", text, |
| 127 | + "matchCase", matchCase, |
| 128 | + "matchWholeWord", matchWholeWord, |
| 129 | + "useRegularExpression", useRegularExpressions, |
| 130 | + "searchInAttributes", searchInAttributes |
| 131 | + ); |
| 132 | + Map<String, Object> variables = Map.of( |
| 133 | + "editingContextId", editingContextId.toString(), |
| 134 | + "query", queryMap |
| 135 | + ); |
| 136 | + var result = this.searchQueryRunner.run(variables); |
| 137 | + |
| 138 | + String payloadTypename = JsonPath.read(result, "$.data.viewer.editingContext.search.__typename"); |
| 139 | + assertThat(payloadTypename).isEqualTo(SearchSuccessPayload.class.getSimpleName()); |
| 140 | + |
| 141 | + String resultTypename = JsonPath.read(result, "$.data.viewer.editingContext.search.result.__typename"); |
| 142 | + assertThat(resultTypename).isEqualTo(SearchResult.class.getSimpleName()); |
| 143 | + |
| 144 | + return JsonPath.read(result, "$.data.viewer.editingContext.search.result.matches[*].label"); |
| 145 | + } |
| 146 | +} |
0 commit comments