aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-testing-harness/src
diff options
context:
space:
mode:
authorDamien Urruty <damien.urruty@sonarsource.com>2022-02-24 14:51:07 +0100
committersonartech <sonartech@sonarsource.com>2022-02-25 20:02:53 +0000
commit0253f591bd017cb9a9c8e365fa074a7bc4b0a2a0 (patch)
treee9b2794b0904a2aa999273c1d1075e6aba86e08b /sonar-testing-harness/src
parent9828d4922ec6064d0c7814b285a639ef1f838de4 (diff)
downloadsonarqube-0253f591bd017cb9a9c8e365fa074a7bc4b0a2a0.tar.gz
sonarqube-0253f591bd017cb9a9c8e365fa074a7bc4b0a2a0.zip
SONAR-15919 Rename RuleSetChangedEvent and fix payload format
Diffstat (limited to 'sonar-testing-harness/src')
-rw-r--r--sonar-testing-harness/src/main/java/org/sonar/test/EventAssert.java123
-rw-r--r--sonar-testing-harness/src/test/java/org/sonar/test/EventAssertTest.java111
-rw-r--r--sonar-testing-harness/src/test/resources/org/sonar/test/EventAssertTest/sample.json1
3 files changed, 235 insertions, 0 deletions
diff --git a/sonar-testing-harness/src/main/java/org/sonar/test/EventAssert.java b/sonar-testing-harness/src/main/java/org/sonar/test/EventAssert.java
new file mode 100644
index 00000000000..5de4aa1ba23
--- /dev/null
+++ b/sonar-testing-harness/src/main/java/org/sonar/test/EventAssert.java
@@ -0,0 +1,123 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2022 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.test;
+
+import java.net.URL;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+
+import static org.junit.Assert.fail;
+import static org.sonar.test.JsonAssert.assertJson;
+
+/**
+ * Assertion methods to compare server-sent events messages.
+ *
+ * <h3>Usage</h3>
+ * <pre>
+ * String actual = "";
+ * String expected = "event: E\ndata: D";
+ * EventAssert.assertEvent(actual).hasType("E");
+ * </pre>
+ *
+ * @since 9.4
+ */
+public class EventAssert {
+
+ private static final String EVENT = "event";
+ private static final String DATA = "data";
+ private static final String ID = "id";
+ private static final String RETRY = "retry";
+
+ private static final Set<String> ALLOWED_FIELDS = new HashSet<>(Arrays.asList(EVENT, DATA, ID, RETRY));
+
+ private final String eventPayload;
+
+ private EventAssert(String eventPayload) {
+ this.eventPayload = eventPayload;
+ }
+
+ public static EventAssert assertThatEvent(String eventPayload) {
+ return new EventAssert(eventPayload);
+ }
+
+ public EventAssert isValid() {
+ extractFields();
+ return this;
+ }
+
+ public EventAssert hasField(String name) {
+ isValid();
+ if (!extractFields().containsKey(name)) {
+ fail("Expected event to contain field '" + name + "'. Actual event was: '" + eventPayload + "'");
+ }
+ return this;
+ }
+
+ public EventAssert hasType(String value) {
+ return hasField(EVENT, value);
+ }
+
+ public EventAssert hasData(String value) {
+ return hasField(DATA, value);
+ }
+
+ public EventAssert hasField(String name, String value) {
+ isValid();
+ hasField(name);
+ String actual = extractFields().get(name);
+ if (!Objects.equals(actual, value)) {
+ fail("Expected field '" + name + "' to contain '" + value + "' but was '" + actual + "'");
+ }
+ return this;
+ }
+
+ public EventAssert hasJsonData(URL url) {
+ isValid();
+ hasField(DATA);
+ assertJson(extractFields().get(DATA))
+ .withStrictArrayOrder()
+ .isSimilarTo(url);
+ return this;
+ }
+
+ private Map<String, String> extractFields() {
+ Map<String, String> fields = new HashMap<>();
+ Arrays.stream(eventPayload.split("\n")).forEach(line -> {
+ String trimmed = line.trim();
+ if (!trimmed.isEmpty()) {
+ int fieldDelimiterIndex = line.indexOf(':');
+ if (fieldDelimiterIndex != -1) {
+ String fieldName = line.substring(0, fieldDelimiterIndex);
+ if (!ALLOWED_FIELDS.contains(fieldName)) {
+ fail("Unknown field in event: '" + fieldName + "'");
+ }
+ fields.put(fieldName, line.substring(fieldDelimiterIndex + 1).trim());
+ } else {
+ fail("Invalid line in event: '" + line + "'");
+ }
+ }
+ });
+ return fields;
+ }
+}
diff --git a/sonar-testing-harness/src/test/java/org/sonar/test/EventAssertTest.java b/sonar-testing-harness/src/test/java/org/sonar/test/EventAssertTest.java
new file mode 100644
index 00000000000..ac2cce1c2dc
--- /dev/null
+++ b/sonar-testing-harness/src/test/java/org/sonar/test/EventAssertTest.java
@@ -0,0 +1,111 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2022 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.test;
+
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.sonar.test.EventAssert.assertThatEvent;
+
+public class EventAssertTest {
+ @Test
+ public void isValid_no_field() {
+ assertThatThrownBy(() -> assertThatEvent("line without field").isValid())
+ .isInstanceOf(AssertionError.class)
+ .hasMessage("Invalid line in event: 'line without field'");
+ }
+ @Test
+ public void isValid_unknown_field() {
+ assertThatThrownBy(() -> assertThatEvent("field: value").isValid())
+ .isInstanceOf(AssertionError.class)
+ .hasMessage("Unknown field in event: 'field'");
+ }
+
+ @Test
+ public void isValid_correct() {
+ assertThatEvent("").isValid();
+ assertThatEvent("\n\n").isValid();
+ assertThatEvent("data: D").isValid();
+ assertThatEvent("event: E\ndata: D").isValid();
+ }
+
+ @Test
+ public void hasField_invalid() {
+ assertThatThrownBy(() -> assertThatEvent("line without field").hasField("event"))
+ .isInstanceOf(AssertionError.class)
+ .hasMessage("Invalid line in event: 'line without field'");
+ }
+
+ @Test
+ public void hasField_no_field() {
+ assertThatThrownBy(() -> assertThatEvent("event: E").hasField("data"))
+ .isInstanceOf(AssertionError.class)
+ .hasMessage("Expected event to contain field 'data'. Actual event was: 'event: E'");
+ }
+
+ @Test
+ public void hasField_correct() {
+ assertThatEvent("event: E\ndata: D").hasField("data");
+ assertThatEvent("event: E\ndata: D").hasField("event");
+ }
+
+ @Test
+ public void hasType_invalid() {
+ assertThatThrownBy(() -> assertThatEvent("line without field").hasType("E"))
+ .isInstanceOf(AssertionError.class)
+ .hasMessage("Invalid line in event: 'line without field'");
+ }
+
+ @Test
+ public void hasType_without_type() {
+ assertThatThrownBy(() -> assertThatEvent("data: D").hasType("E"))
+ .isInstanceOf(AssertionError.class)
+ .hasMessage("Expected event to contain field 'event'. Actual event was: 'data: D'");
+ }
+
+ @Test
+ public void hasType_correct() {
+ assertThatEvent("event: E\ndata: D").hasType("E");
+ }
+
+ @Test
+ public void hasData_invalid() {
+ assertThatThrownBy(() -> assertThatEvent("line without field").hasData("D"))
+ .isInstanceOf(AssertionError.class)
+ .hasMessage("Invalid line in event: 'line without field'");
+ }
+
+ @Test
+ public void hasData_correct() {
+ assertThatEvent("data:D").hasData("D");
+ }
+
+ @Test
+ public void hasJsonData_invalid() {
+ assertThatThrownBy(() -> assertThatEvent("line without field").hasJsonData(getClass().getResource("EventAssertTest/sample.json")))
+ .isInstanceOf(AssertionError.class)
+ .hasMessage("Invalid line in event: 'line without field'");
+ }
+
+ @Test
+ public void hasJsonData_correct() {
+ assertThatEvent("data: {}").hasJsonData(getClass().getResource("EventAssertTest/sample.json"));
+ }
+}
diff --git a/sonar-testing-harness/src/test/resources/org/sonar/test/EventAssertTest/sample.json b/sonar-testing-harness/src/test/resources/org/sonar/test/EventAssertTest/sample.json
new file mode 100644
index 00000000000..0967ef424bc
--- /dev/null
+++ b/sonar-testing-harness/src/test/resources/org/sonar/test/EventAssertTest/sample.json
@@ -0,0 +1 @@
+{}