Skip to content

Commit

Permalink
Fixed a few non deterministic tests in the repository
Browse files Browse the repository at this point in the history
Signed-off-by: Yug Vajani <[email protected]>
  • Loading branch information
Yug Vajani committed Dec 5, 2024
1 parent 297d54e commit 0e2e22b
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

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.*;

Expand Down Expand Up @@ -268,7 +269,7 @@ public void assertValidationThrowsExceptionContainingMessagesForAllRequiredConfi
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) {
Expand Down Expand Up @@ -352,7 +353,7 @@ public void assertValidationThrowsExceptionContainingMessagesForAllMinMaxConfigP
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) {
Expand Down Expand Up @@ -405,7 +406,7 @@ public void assertValidationThrowsExceptionContainingMessagesForMultipleInvalidT
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) {
Expand Down Expand Up @@ -513,7 +514,7 @@ public void assertValidationThrowsExceptionContainingMultipleVariousViolations()
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.openhab.core.thing.ThingUID;

import com.google.gson.Gson;
import com.google.gson.JsonParser;

/**
* {@link InboxEventFactoryTest} tests the {@link InboxEventFactory}.
Expand Down Expand Up @@ -71,7 +72,7 @@ public void inboxEventFactoryCreatesInboxAddedEventCorrectly() {

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()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParser;

/**
* Tests {@link Stream2JSONInputStream}.
Expand All @@ -52,7 +53,8 @@ public void shouldStreamSingleObjectToJSON() throws Exception {
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
Expand All @@ -62,7 +64,8 @@ public void shouldStreamCollectionStreamToJSON() throws Exception {
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

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.*;

Expand Down Expand Up @@ -41,6 +42,7 @@
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.
Expand Down Expand Up @@ -204,8 +206,9 @@ public void testAddElementToModel() throws IOException {

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
Expand All @@ -220,8 +223,9 @@ public void testUpdateElementInModel() throws IOException {

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.openhab.core.thing.link.dto.ItemChannelLinkDTO;

import com.google.gson.Gson;
import com.google.gson.JsonParser;

/**
* {@link LinkEventFactoryTest} tests the {@link LinkEventFactory}.
Expand Down Expand Up @@ -52,7 +53,7 @@ public void testCreateItemChannelLinkAddedEvent() {

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
Expand All @@ -73,7 +74,7 @@ public void testCreateItemChannelLinkRemovedEvent() {

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public void testValues() {
expectedValues.add(value1);

final Collection<@Nullable String> values = subject.values();
assertEquals(expectedValues, values);
assertEquals(new LinkedHashSet<>(expectedValues), new LinkedHashSet<>(values));
}

// use another different key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.openhab.core.types.UnDefType;

import com.google.gson.Gson;
import com.google.gson.JsonParser;

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

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());
Expand Down Expand Up @@ -189,7 +190,7 @@ public void testCreateStateEventOnOffType() {

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());
Expand Down

0 comments on commit 0e2e22b

Please sign in to comment.