forked from RedHatInsights/event-schemas-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsoleCloudEventParserTest.java
182 lines (140 loc) · 8.24 KB
/
ConsoleCloudEventParserTest.java
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package com.redhat.cloud.event.parser;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.redhat.cloud.event.apps.advisor.v1.AdvisorRecommendations;
import com.redhat.cloud.event.apps.exportservice.v1.ResourceRequest;
import com.redhat.cloud.event.core.v1.Notification;
import com.redhat.cloud.event.parser.exceptions.ConsoleCloudEventValidationException;
import com.redhat.cloud.event.parser.modules.LocalDateTimeModule;
import com.redhat.cloud.event.parser.modules.OffsetDateTimeModule;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;
import java.util.TreeMap;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class ConsoleCloudEventParserTest {
@JsonIgnoreProperties(ignoreUnknown = true)
public static class AdvisorCloudEvent extends GenericConsoleCloudEvent<AdvisorRecommendations> {
}
static class SortingNodeFactory extends JsonNodeFactory {
@Override
public ObjectNode objectNode() {
return new ObjectNode(this, new TreeMap<>());
}
}
@Test
public void shouldParseCorrectCloudEvents() throws IOException {
ConsoleCloudEventParser consoleCloudEventParser = new ConsoleCloudEventParser();
ConsoleCloudEvent consoleCloudEvent = consoleCloudEventParser.fromJsonString(readSchema("cloud-events/advisor.json"));
assertEquals("com.redhat.console.advisor.new-recommendations", consoleCloudEvent.getType());
assertEquals("org123", consoleCloudEvent.getOrgId());
assertNull(consoleCloudEvent.getAccountId());
}
@Test
public void shouldValidateCorrectCloudEvents() throws IOException {
ConsoleCloudEventParser consoleCloudEventParser = new ConsoleCloudEventParser();
assertDoesNotThrow(() -> consoleCloudEventParser.validate(readSchema("cloud-events/advisor.json")));
assertDoesNotThrow(() -> consoleCloudEventParser.validate(readSchema("cloud-events/policies.json")));
}
@Test
public void shouldFailWithWrongDataschema() throws IOException {
ConsoleCloudEventParser consoleCloudEventParser = new ConsoleCloudEventParser();
assertThrows(
ConsoleCloudEventValidationException.class,
() -> consoleCloudEventParser.validate(readSchema("cloud-events/advisor-with-wrong-dataschema.json"))
);
}
@Test
public void shouldParseWithCustomClass() throws IOException {
ConsoleCloudEventParser consoleCloudEventParser = new ConsoleCloudEventParser();
AdvisorCloudEvent advisorEvent = consoleCloudEventParser.fromJsonString(readSchema("cloud-events/advisor.json"), AdvisorCloudEvent.class);
assertEquals("com.redhat.console.advisor.new-recommendations", advisorEvent.getType());
assertEquals("org123", advisorEvent.getOrgId());
assertNull(advisorEvent.getAccountId());
assertEquals("rhel8desktop", advisorEvent.getData().getSystem().getDisplayName());
assertEquals("insights_core_egg_not_up2date|INSIGHTS_CORE_EGG_NOT_UP2DATE", advisorEvent.getData().getAdvisorRecommendations()[0].getRuleID());
}
@Test
public void shouldParseAndSerialize() throws IOException {
ConsoleCloudEventParser consoleCloudEventParser = new ConsoleCloudEventParser();
String original = readSchema("cloud-events/advisor.json");
ConsoleCloudEvent consoleCloudEvent = consoleCloudEventParser.fromJsonString(original);
String other = consoleCloudEventParser.toJson(consoleCloudEvent);
assertNotNull(other);
ObjectMapper mapper = new ObjectMapper()
.setNodeFactory(new SortingNodeFactory())
.registerModule(new LocalDateTimeModule());
assertEquals(mapper.readTree(original), mapper.readTree(other));
assertTrue(other.contains("https://console.redhat.com/api/schemas/events/v1/events.json"));
}
@Test
public void shouldParseAndSerializeWithCustomClass() throws IOException {
ConsoleCloudEventParser consoleCloudEventParser = new ConsoleCloudEventParser();
String original = readSchema("cloud-events/advisor.json");
AdvisorCloudEvent consoleCloudEvent = consoleCloudEventParser.fromJsonString(original, AdvisorCloudEvent.class);
String other = consoleCloudEventParser.toJson(consoleCloudEvent);
assertNotNull(other);
ObjectMapper mapper = new ObjectMapper()
.setNodeFactory(new SortingNodeFactory())
.registerModule(new OffsetDateTimeModule())
.registerModule(new LocalDateTimeModule());
// It correctly parses dates with offsets as 2021-03-13T18:44:00+00:00 but writes them as "2021-03-13T18:44:00Z"
original = original.replace("+00:00", "Z");
assertEquals(mapper.readTree(original), mapper.readTree(other));
assertTrue(other.contains("https://console.redhat.com/api/schemas/events/v1/events.json"));
}
@Test
public void getWithClassFailsIfNotCompatible() throws IOException {
ConsoleCloudEventParser consoleCloudEventParser = new ConsoleCloudEventParser();
String original = readSchema("cloud-events/advisor.json");
ConsoleCloudEvent consoleCloudEvent = consoleCloudEventParser.fromJsonString(original);
assertFalse(consoleCloudEvent.getData(Notification.class).isPresent());
}
@Test
public void getWithClassWorksIfCompatibleData() throws IOException {
ConsoleCloudEventParser consoleCloudEventParser = new ConsoleCloudEventParser();
String original = readSchema("cloud-events/notification-recipients.json");
ConsoleCloudEvent consoleCloudEvent = consoleCloudEventParser.fromJsonString(original);
Notification notification = consoleCloudEvent.getData(Notification.class).orElseThrow();
assertNotNull(notification);
assertNotNull(notification.getNotificationRecipients());
assertTrue(notification.getNotificationRecipients().getOnlyAdmins());
assertFalse(notification.getNotificationRecipients().getIgnoreUserPreferences());
assertEquals(3, notification.getNotificationRecipients().getUsers().length);
assertArrayEquals(new String[]{ "foo", "bar", "x" }, notification.getNotificationRecipients().getUsers());
}
@Test
public void shouldFailOnNonCompliantCloudEvents() {
ConsoleCloudEventParser consoleCloudEventParser = new ConsoleCloudEventParser();
assertThrows(RuntimeException.class, () -> consoleCloudEventParser.fromJsonString(readSchema("cloud-events/advisor-invalid.json")));
}
@Test
public void shouldFailOnInvalidJson() throws IOException {
ConsoleCloudEventParser consoleCloudEventParser = new ConsoleCloudEventParser();
assertThrows(RuntimeException.class, () -> consoleCloudEventParser.fromJsonString("hello world"));
}
@Test
public void shouldReturnEmptyOptional() throws IOException {
ConsoleCloudEventParser consoleCloudEventParser = new ConsoleCloudEventParser();
final ConsoleCloudEvent cloudEvent = consoleCloudEventParser.fromJsonString(readSchema("cloud-events/export-request-empty.json"));
final Optional<ResourceRequest> exportRequest = cloudEvent.getData(ResourceRequest.class);
Assertions.assertTrue(exportRequest.isEmpty());
}
private String readSchema(String path) throws IOException {
InputStream content = ConsoleCloudEventParserTest.class.getClassLoader().getResourceAsStream(path);
assertNotNull(content, "could not load example cloud event for advisor");
return new String(content.readAllBytes(), UTF_8);
}
}