aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-testing-harness/src/main/java/org/sonar/test/EventAssert.java
blob: c206703bcbc4fd95064f0fffcc384b494eb07289 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * SonarQube
 * Copyright (C) 2009-2025 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.assertj.core.api.Assertions.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;
  }
}