Fixed a few non deterministic tests in the repository (#4475)

Signed-off-by: Yug Vajani <yvajani2@illinois.edu>
pull/4693/head
Yug Vajani 2025-04-02 13:01:21 -05:00 committed by GitHub
parent a259a549d3
commit 6e87152b90
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 25 additions and 14 deletions

View File

@ -14,6 +14,7 @@ package org.openhab.core.config.core.internal.validation;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
@ -268,7 +269,7 @@ public class ConfigDescriptionValidatorTest {
params.put(DECIMAL_REQUIRED_PARAM_NAME, null);
ConfigValidationException exception = Assertions.assertThrows(ConfigValidationException.class,
() -> configDescriptionValidator.validate(params, CONFIG_DESCRIPTION_URI));
assertThat(getConfigValidationMessages(exception), is(expected));
assertThat(getConfigValidationMessages(exception), containsInAnyOrder(expected.toArray()));
}
void assertMissingRequired(String parameterName) {
@ -352,7 +353,7 @@ public class ConfigDescriptionValidatorTest {
params.put(DECIMAL_MAX_PARAM_NAME, DECIMAL_MAX_VIOLATED);
ConfigValidationException exception = Assertions.assertThrows(ConfigValidationException.class,
() -> configDescriptionValidator.validate(params, CONFIG_DESCRIPTION_URI));
assertThat(getConfigValidationMessages(exception), is(expected));
assertThat(getConfigValidationMessages(exception), containsInAnyOrder(expected.toArray()));
}
void assertMinMax(String parameterName, Object value, MessageKey msgKey, String minMax) {
@ -405,7 +406,7 @@ public class ConfigDescriptionValidatorTest {
params.put(DECIMAL_PARAM_NAME, INVALID);
ConfigValidationException exception = Assertions.assertThrows(ConfigValidationException.class,
() -> configDescriptionValidator.validate(params, CONFIG_DESCRIPTION_URI));
assertThat(getConfigValidationMessages(exception), is(expected));
assertThat(getConfigValidationMessages(exception), containsInAnyOrder(expected.toArray()));
}
void assertType(String parameterName, Type type) {
@ -513,7 +514,7 @@ public class ConfigDescriptionValidatorTest {
params.put(DECIMAL_MAX_PARAM_NAME, DECIMAL_MAX_VIOLATED);
ConfigValidationException exception = Assertions.assertThrows(ConfigValidationException.class,
() -> configDescriptionValidator.validate(params, CONFIG_DESCRIPTION_URI));
assertThat(getConfigValidationMessages(exception), is(expected));
assertThat(getConfigValidationMessages(exception), containsInAnyOrder(expected.toArray()));
}
@Test

View File

@ -25,6 +25,7 @@ import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.ThingUID;
import com.google.gson.Gson;
import com.google.gson.JsonParser;
/**
* {@link InboxEventFactoryTest} tests the {@link InboxEventFactory}.
@ -71,7 +72,7 @@ public class InboxEventFactoryTest {
assertThat(event.getType(), is(INBOX_ADDED_EVENT_TYPE));
assertThat(event.getTopic(), is(INBOX_ADDED_EVENT_TOPIC));
assertThat(event.getPayload(), is(INBOX_ADDED_EVENT_PAYLOAD));
assertThat(JsonParser.parseString(event.getPayload()), is(JsonParser.parseString(INBOX_ADDED_EVENT_PAYLOAD)));
assertThat(event.getDiscoveryResult(), not(nullValue()));
assertThat(event.getDiscoveryResult().thingUID, is(THING_UID.getAsString()));
}

View File

@ -27,6 +27,7 @@ import org.openhab.core.library.types.DateTimeType;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParser;
/**
* Tests {@link Stream2JSONInputStream}.
@ -52,7 +53,8 @@ public class Stream2JSONInputStreamTest {
List<DummyObject> dummyList = List.of(dummyObject);
Stream2JSONInputStream collection2InputStream = new Stream2JSONInputStream(Stream.of(dummyObject));
assertThat(inputStreamToString(collection2InputStream), is(GSON.toJson(dummyList)));
assertThat(JsonParser.parseString(inputStreamToString(collection2InputStream)),
is(JsonParser.parseString(GSON.toJson(dummyList))));
}
@Test
@ -62,7 +64,8 @@ public class Stream2JSONInputStreamTest {
List<DummyObject> dummyCollection = List.of(dummyObject1, dummyObject2);
Stream2JSONInputStream collection2InputStream = new Stream2JSONInputStream(dummyCollection.stream());
assertThat(inputStreamToString(collection2InputStream), is(GSON.toJson(dummyCollection)));
assertThat(JsonParser.parseString(inputStreamToString(collection2InputStream)),
is(JsonParser.parseString(GSON.toJson(dummyCollection))));
}
private String inputStreamToString(InputStream in) throws IOException {

View File

@ -14,6 +14,7 @@ package org.openhab.core.model.yaml.internal;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;
@ -41,6 +42,7 @@ import org.openhab.core.model.yaml.YamlModelListener;
import org.openhab.core.model.yaml.test.FirstTypeDTO;
import org.openhab.core.model.yaml.test.SecondTypeDTO;
import org.openhab.core.service.WatchService;
import org.yaml.snakeyaml.Yaml;
/**
* The {@link YamlModelRepositoryImplTest} contains tests for the {@link YamlModelRepositoryImpl} class.
@ -204,8 +206,9 @@ public class YamlModelRepositoryImplTest {
String actualFileContent = Files.readString(fullModelPath);
String expectedFileContent = Files.readString(SOURCE_PATH.resolve("addToModelExpectedContent.yaml"));
Yaml yaml = new Yaml();
assertThat(actualFileContent, is(expectedFileContent.replaceAll("\r\n", "\n")));
assertThat(yaml.load(actualFileContent), equalTo(yaml.load(expectedFileContent.replaceAll("\r\n", "\n"))));
}
@Test
@ -220,8 +223,9 @@ public class YamlModelRepositoryImplTest {
String actualFileContent = Files.readString(fullModelPath);
String expectedFileContent = Files.readString(SOURCE_PATH.resolve("updateInModelExpectedContent.yaml"));
Yaml yaml = new Yaml();
assertThat(actualFileContent, is(expectedFileContent.replaceAll("\r\n", "\n")));
assertThat(yaml.load(actualFileContent), equalTo(yaml.load(expectedFileContent.replaceAll("\r\n", "\n"))));
}
@Test

View File

@ -24,6 +24,7 @@ import org.openhab.core.thing.link.ItemChannelLink;
import org.openhab.core.thing.link.dto.ItemChannelLinkDTO;
import com.google.gson.Gson;
import com.google.gson.JsonParser;
/**
* {@link LinkEventFactoryTest} tests the {@link LinkEventFactory}.
@ -52,7 +53,7 @@ public class LinkEventFactoryTest {
assertEquals(ItemChannelLinkAddedEvent.TYPE, event.getType());
assertEquals(LINK_ADDED_EVENT_TOPIC, event.getTopic());
assertEquals(LINK_EVENT_PAYLOAD, event.getPayload());
assertEquals(JsonParser.parseString(LINK_EVENT_PAYLOAD), JsonParser.parseString(event.getPayload()));
}
@Test
@ -73,7 +74,7 @@ public class LinkEventFactoryTest {
assertEquals(ItemChannelLinkRemovedEvent.TYPE, event.getType());
assertEquals(LINK_REMOVED_EVENT_TOPIC, event.getTopic());
assertEquals(LINK_EVENT_PAYLOAD, event.getPayload());
assertEquals(JsonParser.parseString(LINK_EVENT_PAYLOAD), JsonParser.parseString(event.getPayload()));
}
@Test

View File

@ -149,7 +149,7 @@ public class ExpiringCacheMapTest {
expectedValues.add(value1);
final Collection<@Nullable String> values = subject.values();
assertEquals(expectedValues, values);
assertEquals(new LinkedHashSet<>(expectedValues), new LinkedHashSet<>(values));
}
// use another different key

View File

@ -31,6 +31,7 @@ import org.openhab.core.types.State;
import org.openhab.core.types.UnDefType;
import com.google.gson.Gson;
import com.google.gson.JsonParser;
/**
* {@link ItemEventFactoryTest} tests the {@link ItemEventFactory}.
@ -96,7 +97,7 @@ public class ItemEventFactoryTest {
assertEquals(ITEM_COMMAND_EVENT_TYPE, event.getType());
assertEquals(ITEM_COMMAND_EVENT_TOPIC, event.getTopic());
assertEquals(ITEM_COMMAND_EVENT_PAYLOAD, event.getPayload());
assertEquals(JsonParser.parseString(ITEM_COMMAND_EVENT_PAYLOAD), JsonParser.parseString(event.getPayload()));
assertEquals(ITEM_NAME, event.getItemName());
assertEquals(SOURCE, event.getSource());
assertEquals(OnOffType.class, event.getItemCommand().getClass());
@ -189,7 +190,7 @@ public class ItemEventFactoryTest {
assertThat(event.getType(), is(ITEM_STATE_EVENT_TYPE));
assertThat(event.getTopic(), is(ITEM_STATE_EVENT_TOPIC));
assertThat(event.getPayload(), is(ITEM_STATE_EVENT_PAYLOAD));
assertThat(JsonParser.parseString(event.getPayload()), is(JsonParser.parseString(ITEM_STATE_EVENT_PAYLOAD)));
assertThat(event.getItemName(), is(ITEM_NAME));
assertThat(event.getSource(), is(SOURCE));
assertEquals(OnOffType.class, event.getItemState().getClass());