Reputation: 2052
I'm inserting a Collection<Event>
inside a table, something like this
Collection<Event> eventCollection = service.insert(events);
Now, I need to test that Event
s in eventCollection
has certain attributes. I'm not sure about the order, in which, Event
s are stored in eventCollection
. Currently, this is how I'm doing the assertions.
assertTrue(
Iterables.get(eventCollection, 0).getPojo.getField1()
.equals("Some Value") &&
Iterables.get(eventCollection, 0).getPojo.getField2()
.equals("Some Value") &&
Iterables.get(eventCollection, 0).getPojo.getField3()
.equals("Some Value") &&
Iterables.get(eventCollection, 1).getPojo.getField1()
.equals("Some Value") &&
eventCollection, 1).getPojo.getField2()
.equals("Some Value") &&
Iterables.get(eventCollection, 1).getPojo.getField3()
.equals("Some Value"))
);
I know that there are two elements in eventCollection
, but I'm not sure what order they're in. Iterables.get
seem to work fine, but I'm wondering if there's any easier/shorter way to do this using stream api?
Edit: "Some Value"
doesn't always necessarily refer to String
.
Upvotes: 1
Views: 73
Reputation:
For maximum clarity I would suggest encapsulating the low-level predicate into a helper method, and using Stream.allMatch to do the check.
Helper function (assumed placed in class Context
, exact location does not matter):
public static boolean expectedValues(Event event) {
return event.getPojo().getField1().equals("Some Value") &&
event.getPojo().getField2().equals("Some Value") &&
event.getPojo().getField3().equals("Some Value");
}
The actual check, which is indeed much more self-explanatory:
assertTrue(eventCollection.stream().allMatch(Context::expectedValues));
Upvotes: 1