Skip to content

Commit 50cd6ef

Browse files
author
Yug Vajani
committed
Fixed a few non deterministic tests in the repository
Signed-off-by: Yug Vajani <[email protected]>
1 parent 297d54e commit 50cd6ef

File tree

7 files changed

+25
-14
lines changed

7 files changed

+25
-14
lines changed

bundles/org.openhab.core.config.core/src/test/java/org/openhab/core/config/core/internal/validation/ConfigDescriptionValidatorTest.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import static org.hamcrest.CoreMatchers.is;
1616
import static org.hamcrest.MatcherAssert.assertThat;
17+
import static org.hamcrest.Matchers.containsInAnyOrder;
1718
import static org.mockito.ArgumentMatchers.any;
1819
import static org.mockito.Mockito.*;
1920

@@ -268,7 +269,7 @@ public void assertValidationThrowsExceptionContainingMessagesForAllRequiredConfi
268269
params.put(DECIMAL_REQUIRED_PARAM_NAME, null);
269270
ConfigValidationException exception = Assertions.assertThrows(ConfigValidationException.class,
270271
() -> configDescriptionValidator.validate(params, CONFIG_DESCRIPTION_URI));
271-
assertThat(getConfigValidationMessages(exception), is(expected));
272+
assertThat(getConfigValidationMessages(exception), containsInAnyOrder(expected.toArray()));
272273
}
273274

274275
void assertMissingRequired(String parameterName) {
@@ -352,7 +353,7 @@ public void assertValidationThrowsExceptionContainingMessagesForAllMinMaxConfigP
352353
params.put(DECIMAL_MAX_PARAM_NAME, DECIMAL_MAX_VIOLATED);
353354
ConfigValidationException exception = Assertions.assertThrows(ConfigValidationException.class,
354355
() -> configDescriptionValidator.validate(params, CONFIG_DESCRIPTION_URI));
355-
assertThat(getConfigValidationMessages(exception), is(expected));
356+
assertThat(getConfigValidationMessages(exception), containsInAnyOrder(expected.toArray()));
356357
}
357358

358359
void assertMinMax(String parameterName, Object value, MessageKey msgKey, String minMax) {
@@ -405,7 +406,7 @@ public void assertValidationThrowsExceptionContainingMessagesForMultipleInvalidT
405406
params.put(DECIMAL_PARAM_NAME, INVALID);
406407
ConfigValidationException exception = Assertions.assertThrows(ConfigValidationException.class,
407408
() -> configDescriptionValidator.validate(params, CONFIG_DESCRIPTION_URI));
408-
assertThat(getConfigValidationMessages(exception), is(expected));
409+
assertThat(getConfigValidationMessages(exception), containsInAnyOrder(expected.toArray()));
409410
}
410411

411412
void assertType(String parameterName, Type type) {
@@ -513,7 +514,7 @@ public void assertValidationThrowsExceptionContainingMultipleVariousViolations()
513514
params.put(DECIMAL_MAX_PARAM_NAME, DECIMAL_MAX_VIOLATED);
514515
ConfigValidationException exception = Assertions.assertThrows(ConfigValidationException.class,
515516
() -> configDescriptionValidator.validate(params, CONFIG_DESCRIPTION_URI));
516-
assertThat(getConfigValidationMessages(exception), is(expected));
517+
assertThat(getConfigValidationMessages(exception), containsInAnyOrder(expected.toArray()));
517518
}
518519

519520
@Test

bundles/org.openhab.core.config.discovery/src/test/java/org/openhab/core/config/discovery/inbox/events/InboxEventFactoryTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.openhab.core.thing.ThingUID;
2626

2727
import com.google.gson.Gson;
28+
import com.google.gson.JsonParser;
2829

2930
/**
3031
* {@link InboxEventFactoryTest} tests the {@link InboxEventFactory}.
@@ -71,7 +72,7 @@ public void inboxEventFactoryCreatesInboxAddedEventCorrectly() {
7172

7273
assertThat(event.getType(), is(INBOX_ADDED_EVENT_TYPE));
7374
assertThat(event.getTopic(), is(INBOX_ADDED_EVENT_TOPIC));
74-
assertThat(event.getPayload(), is(INBOX_ADDED_EVENT_PAYLOAD));
75+
assertThat(JsonParser.parseString(event.getPayload()), is(JsonParser.parseString(INBOX_ADDED_EVENT_PAYLOAD)));
7576
assertThat(event.getDiscoveryResult(), not(nullValue()));
7677
assertThat(event.getDiscoveryResult().thingUID, is(THING_UID.getAsString()));
7778
}

bundles/org.openhab.core.io.rest/src/test/java/org/openhab/core/io/rest/Stream2JSONInputStreamTest.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import com.google.gson.Gson;
2929
import com.google.gson.GsonBuilder;
30+
import com.google.gson.JsonParser;
3031

3132
/**
3233
* Tests {@link Stream2JSONInputStream}.
@@ -52,7 +53,8 @@ public void shouldStreamSingleObjectToJSON() throws Exception {
5253
List<DummyObject> dummyList = List.of(dummyObject);
5354
Stream2JSONInputStream collection2InputStream = new Stream2JSONInputStream(Stream.of(dummyObject));
5455

55-
assertThat(inputStreamToString(collection2InputStream), is(GSON.toJson(dummyList)));
56+
assertThat(JsonParser.parseString(inputStreamToString(collection2InputStream)),
57+
is(JsonParser.parseString(GSON.toJson(dummyList))));
5658
}
5759

5860
@Test
@@ -62,7 +64,8 @@ public void shouldStreamCollectionStreamToJSON() throws Exception {
6264
List<DummyObject> dummyCollection = List.of(dummyObject1, dummyObject2);
6365
Stream2JSONInputStream collection2InputStream = new Stream2JSONInputStream(dummyCollection.stream());
6466

65-
assertThat(inputStreamToString(collection2InputStream), is(GSON.toJson(dummyCollection)));
67+
assertThat(JsonParser.parseString(inputStreamToString(collection2InputStream)),
68+
is(JsonParser.parseString(GSON.toJson(dummyCollection))));
6669
}
6770

6871
private String inputStreamToString(InputStream in) throws IOException {

bundles/org.openhab.core.model.yaml/src/test/java/org/openhab/core/model/yaml/internal/YamlModelRepositoryImplTest.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import static org.hamcrest.MatcherAssert.assertThat;
1616
import static org.hamcrest.Matchers.*;
17+
import static org.mockito.ArgumentMatchers.any;
1718
import static org.mockito.ArgumentMatchers.eq;
1819
import static org.mockito.Mockito.*;
1920

@@ -41,6 +42,7 @@
4142
import org.openhab.core.model.yaml.test.FirstTypeDTO;
4243
import org.openhab.core.model.yaml.test.SecondTypeDTO;
4344
import org.openhab.core.service.WatchService;
45+
import org.yaml.snakeyaml.Yaml;
4446

4547
/**
4648
* The {@link YamlModelRepositoryImplTest} contains tests for the {@link YamlModelRepositoryImpl} class.
@@ -204,8 +206,9 @@ public void testAddElementToModel() throws IOException {
204206

205207
String actualFileContent = Files.readString(fullModelPath);
206208
String expectedFileContent = Files.readString(SOURCE_PATH.resolve("addToModelExpectedContent.yaml"));
209+
Yaml yaml = new Yaml();
207210

208-
assertThat(actualFileContent, is(expectedFileContent.replaceAll("\r\n", "\n")));
211+
assertThat(yaml.load(actualFileContent), equalTo(yaml.load(expectedFileContent.replaceAll("\r\n", "\n"))));
209212
}
210213

211214
@Test
@@ -220,8 +223,9 @@ public void testUpdateElementInModel() throws IOException {
220223

221224
String actualFileContent = Files.readString(fullModelPath);
222225
String expectedFileContent = Files.readString(SOURCE_PATH.resolve("updateInModelExpectedContent.yaml"));
226+
Yaml yaml = new Yaml();
223227

224-
assertThat(actualFileContent, is(expectedFileContent.replaceAll("\r\n", "\n")));
228+
assertThat(yaml.load(actualFileContent), equalTo(yaml.load(expectedFileContent.replaceAll("\r\n", "\n"))));
225229
}
226230

227231
@Test

bundles/org.openhab.core.thing/src/test/java/org/openhab/core/thing/link/events/LinkEventFactoryTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.openhab.core.thing.link.dto.ItemChannelLinkDTO;
2525

2626
import com.google.gson.Gson;
27+
import com.google.gson.JsonParser;
2728

2829
/**
2930
* {@link LinkEventFactoryTest} tests the {@link LinkEventFactory}.
@@ -52,7 +53,7 @@ public void testCreateItemChannelLinkAddedEvent() {
5253

5354
assertEquals(ItemChannelLinkAddedEvent.TYPE, event.getType());
5455
assertEquals(LINK_ADDED_EVENT_TOPIC, event.getTopic());
55-
assertEquals(LINK_EVENT_PAYLOAD, event.getPayload());
56+
assertEquals(JsonParser.parseString(LINK_EVENT_PAYLOAD), JsonParser.parseString(event.getPayload()));
5657
}
5758

5859
@Test
@@ -73,7 +74,7 @@ public void testCreateItemChannelLinkRemovedEvent() {
7374

7475
assertEquals(ItemChannelLinkRemovedEvent.TYPE, event.getType());
7576
assertEquals(LINK_REMOVED_EVENT_TOPIC, event.getTopic());
76-
assertEquals(LINK_EVENT_PAYLOAD, event.getPayload());
77+
assertEquals(JsonParser.parseString(LINK_EVENT_PAYLOAD), JsonParser.parseString(event.getPayload()));
7778
}
7879

7980
@Test

bundles/org.openhab.core/src/test/java/org/openhab/core/cache/ExpiringCacheMapTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public void testValues() {
149149
expectedValues.add(value1);
150150

151151
final Collection<@Nullable String> values = subject.values();
152-
assertEquals(expectedValues, values);
152+
assertEquals(new LinkedHashSet<>(expectedValues), new LinkedHashSet<>(values));
153153
}
154154

155155
// use another different key

bundles/org.openhab.core/src/test/java/org/openhab/core/items/events/ItemEventFactoryTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.openhab.core.types.UnDefType;
3232

3333
import com.google.gson.Gson;
34+
import com.google.gson.JsonParser;
3435

3536
/**
3637
* {@link ItemEventFactoryTest} tests the {@link ItemEventFactory}.
@@ -96,7 +97,7 @@ public void testCreateCommandEventOnOffType() throws Exception {
9697

9798
assertEquals(ITEM_COMMAND_EVENT_TYPE, event.getType());
9899
assertEquals(ITEM_COMMAND_EVENT_TOPIC, event.getTopic());
99-
assertEquals(ITEM_COMMAND_EVENT_PAYLOAD, event.getPayload());
100+
assertEquals(JsonParser.parseString(ITEM_COMMAND_EVENT_PAYLOAD), JsonParser.parseString(event.getPayload()));
100101
assertEquals(ITEM_NAME, event.getItemName());
101102
assertEquals(SOURCE, event.getSource());
102103
assertEquals(OnOffType.class, event.getItemCommand().getClass());
@@ -189,7 +190,7 @@ public void testCreateStateEventOnOffType() {
189190

190191
assertThat(event.getType(), is(ITEM_STATE_EVENT_TYPE));
191192
assertThat(event.getTopic(), is(ITEM_STATE_EVENT_TOPIC));
192-
assertThat(event.getPayload(), is(ITEM_STATE_EVENT_PAYLOAD));
193+
assertThat(JsonParser.parseString(event.getPayload()), is(JsonParser.parseString(ITEM_STATE_EVENT_PAYLOAD)));
193194
assertThat(event.getItemName(), is(ITEM_NAME));
194195
assertThat(event.getSource(), is(SOURCE));
195196
assertEquals(OnOffType.class, event.getItemState().getClass());

0 commit comments

Comments
 (0)