use of org.hamcrest.TypeSafeMatcher in project mod-circulation-storage by folio-org.
the class JsonMatchers method hasSameProperties.
/**
* Checks whether the object being asserted has the same values for properties
* as expectedProperties has.
*
* This is equivalent of {@code expectedProperties.map().containsAll(actual.map())}.
*
* @param expectedProperties - expected properties and values to be present.
* @return true only if all the properties from expectedProperties are present
* and has the same value in actual object.
*/
public static Matcher<JsonObject> hasSameProperties(JsonObject expectedProperties) {
return new TypeSafeMatcher<JsonObject>() {
@Override
protected boolean matchesSafely(JsonObject actual) {
if (actual == null || actual.size() < expectedProperties.size()) {
return false;
}
for (Map.Entry<String, Object> expectedProperty : expectedProperties) {
final String propertyKey = expectedProperty.getKey();
final Object expectedValue = expectedProperty.getValue();
final Object actualValue = actual.getValue(propertyKey);
if (!Objects.equals(expectedValue, actualValue)) {
return false;
}
}
return true;
}
@Override
public void describeTo(Description description) {
description.appendText("Json object has following properties").appendValue(expectedProperties.toString());
}
};
}
use of org.hamcrest.TypeSafeMatcher in project openhab-addons by openhab.
the class ModbusPollerThingHandlerTest method testPollUnregistrationOnDispose.
@Test
public void testPollUnregistrationOnDispose() throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
PollTask pollTask = Mockito.mock(PollTask.class);
doReturn(pollTask).when(comms).registerRegularPoll(notNull(), eq(150l), eq(0L), notNull(), notNull());
Configuration pollerConfig = new Configuration();
pollerConfig.put("refresh", 150L);
pollerConfig.put("start", 5);
pollerConfig.put("length", 13);
pollerConfig.put("type", "coil");
poller = createPollerThingBuilder("poller").withConfiguration(pollerConfig).withBridge(endpoint.getUID()).build();
addThing(poller);
verifyEndpointBasicInitInteraction();
// verify registration
final AtomicReference<ModbusReadCallback> callbackRef = new AtomicReference<>();
verify(mockedModbusManager).newModbusCommunicationInterface(argThat(new TypeSafeMatcher<ModbusSlaveEndpoint>() {
@Override
public void describeTo(Description description) {
description.appendText("correct endpoint");
}
@Override
protected boolean matchesSafely(ModbusSlaveEndpoint endpoint) {
return checkEndpoint(endpoint);
}
}), any());
verify(comms).registerRegularPoll(argThat(new TypeSafeMatcher<ModbusReadRequestBlueprint>() {
@Override
public void describeTo(Description description) {
}
@Override
protected boolean matchesSafely(ModbusReadRequestBlueprint request) {
return checkRequest(request, ModbusReadFunctionCode.READ_COILS);
}
}), eq(150l), eq(0L), argThat(new TypeSafeMatcher<ModbusReadCallback>() {
@Override
public void describeTo(Description description) {
}
@Override
protected boolean matchesSafely(ModbusReadCallback callback) {
callbackRef.set(callback);
return true;
}
}), notNull());
verifyNoMoreInteractions(mockedModbusManager);
// reset call counts for easy assertions
reset(mockedModbusManager);
// remove the thing
disposeThing(poller);
// 1) should first unregister poll task
verify(comms).unregisterRegularPoll(eq(pollTask));
verifyNoMoreInteractions(mockedModbusManager);
}
use of org.hamcrest.TypeSafeMatcher in project openhab-addons by openhab.
the class ModbusPollerThingHandlerTest method testPollingGeneric.
public void testPollingGeneric(String type, ModbusReadFunctionCode expectedFunctionCode) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
PollTask pollTask = Mockito.mock(PollTask.class);
doReturn(pollTask).when(comms).registerRegularPoll(notNull(), eq(150l), eq(0L), notNull(), notNull());
Configuration pollerConfig = new Configuration();
pollerConfig.put("refresh", 150L);
pollerConfig.put("start", 5);
pollerConfig.put("length", 13);
pollerConfig.put("type", type);
poller = createPollerThingBuilder("poller").withConfiguration(pollerConfig).withBridge(endpoint.getUID()).build();
addThing(poller);
assertThat(poller.getStatusInfo().toString(), poller.getStatus(), is(equalTo(ThingStatus.ONLINE)));
verifyEndpointBasicInitInteraction();
verify(mockedModbusManager).newModbusCommunicationInterface(argThat(new TypeSafeMatcher<ModbusSlaveEndpoint>() {
@Override
public void describeTo(Description description) {
description.appendText("correct endpoint (");
}
@Override
protected boolean matchesSafely(ModbusSlaveEndpoint endpoint) {
return checkEndpoint(endpoint);
}
}), any());
verify(comms).registerRegularPoll(argThat(new TypeSafeMatcher<ModbusReadRequestBlueprint>() {
@Override
public void describeTo(Description description) {
description.appendText("correct request");
}
@Override
protected boolean matchesSafely(ModbusReadRequestBlueprint request) {
return checkRequest(request, expectedFunctionCode);
}
}), eq(150l), eq(0L), notNull(), notNull());
verifyNoMoreInteractions(mockedModbusManager);
}
use of org.hamcrest.TypeSafeMatcher in project fdb-record-layer by FoundationDB.
the class FDBRecordStoreIndexTest method markManyDisabled.
@Test
public void markManyDisabled() throws Exception {
final Index[] indexes = new Index[100];
for (int i = 0; i < indexes.length; i++) {
indexes[i] = new Index(String.format("index-%d", i), "str_value_indexed");
}
final RecordMetaDataHook hook = metaData -> {
final RecordTypeBuilder recordType = metaData.getRecordType("MySimpleRecord");
for (int i = 0; i < indexes.length; i++) {
metaData.addIndex(recordType, indexes[i]);
}
};
// Such timing problems take several tries to show up.
for (int j = 0; j < 10; j++) {
try (FDBRecordContext context = openContext()) {
openSimpleRecordStore(context, hook);
List<CompletableFuture<Boolean>> futures = new ArrayList<>();
List<Index> shouldBeDisabled = new ArrayList<>();
// Expected contract: only use isIndexXXX and markIndexXXX and wait for all futures when done.
for (int i = 0; i < indexes.length; i++) {
if ((i % 2 == 0) || (i == 99 && recordStore.isIndexDisabled(indexes[i]))) {
futures.add(recordStore.markIndexDisabled(indexes[i]));
shouldBeDisabled.add(indexes[i]);
}
}
AsyncUtil.whenAll(futures).join();
for (Index index : shouldBeDisabled) {
assertThat(index, new TypeSafeMatcher<Index>() {
@Override
protected boolean matchesSafely(Index item) {
return recordStore.isIndexDisabled(index);
}
@Override
public void describeTo(Description description) {
description.appendText("a disabled index");
}
});
}
}
}
}
use of org.hamcrest.TypeSafeMatcher in project mod-inventory by folio-org.
the class ResponseMatchers method hasValidationError.
public static Matcher<Response> hasValidationError(String expectedMessage, String expectedKey, String expectedValue) {
return new TypeSafeMatcher<Response>() {
@Override
protected boolean matchesSafely(Response response) {
if (response.getStatusCode() != 422) {
return false;
}
if (!response.getContentType().startsWith(ContentType.APPLICATION_JSON)) {
return false;
}
try {
JsonArray errors = response.getJson().getJsonArray("errors");
if (errors != null && errors.size() == 1) {
JsonObject error = errors.getJsonObject(0);
JsonArray parameters = error.getJsonArray("parameters");
if (parameters != null && parameters.size() == 1) {
String message = error.getString("message");
String key = parameters.getJsonObject(0).getString("key");
String value = parameters.getJsonObject(0).getString("value");
return Objects.equals(expectedMessage, message) && Objects.equals(expectedKey, key) && Objects.equals(expectedValue, value);
}
}
return false;
} catch (DecodeException ex) {
return false;
}
}
@Override
public void describeTo(Description description) {
description.appendText("Response has 422 status and 'message' - ").appendValue(expectedMessage).appendText(", 'key' - ").appendValue(expectedKey).appendText(" and 'value' - ").appendValue(expectedValue);
}
@Override
protected void describeMismatchSafely(Response response, Description mismatchDescription) {
mismatchDescription.appendText("Status: ").appendValue(response.getStatusCode()).appendText(", body: ");
if (response.getContentType().startsWith(ContentType.APPLICATION_JSON)) {
mismatchDescription.appendValue(response.getJson());
} else {
mismatchDescription.appendValue(response.getBody());
}
}
};
}
Aggregations