+
+ List<FlowEntity> sortedFlows = sortFlows(flows);
+ List<FlowEntity> sortedExpectedFlows = sortFlows(expectedFlowsAsNewArrayList);
+
+ // FYI: This containsExactlyElementsIn() assumes that FlowEntity, and everything in it,
+ // has correctly working equals() implementations. assertEqualBeans() does not assume
+ // that, and would work even without equals, because it only uses property reflection.
+ // Normally this will lead to the same result, but if one day it doesn't (because of
+ // a bug in an equals() implementation somewhere), then it's worth to keep this diff
+ // in mind.
+
+ // FTR: This use of G Truth and then catch AssertionError and using assertEqualBeans iff NOK
+ // (thus discarding the message from G Truth) is a bit of a hack, but it works well...
+ // If you're tempted to improve this, please remember that correctly re-implementing
+ // containsExactlyElementsIn (or Hamcrest's similar containsInAnyOrder) isn't a 1 line
+ // trivia... e.g. a.containsAll(b) && b.containsAll(a) isn't sufficient, because it
+ // won't work for duplicates (which we frequently have here); and ordering before is
+ // not viable because FlowEntity is not Comparable, and Comparator based on hashCode
+ // is not a good idea (different instances can have same hashCode), and e.g. on
+ // System#identityHashCode even less so.
+ try {
+ assertThat(sortedFlows).containsExactlyElementsIn(sortedExpectedFlows);
+ } catch (AssertionError e) {
+ // We LOG the AssertionError just for clarity why containsExactlyElementsIn() failed
+ LOG.warn("assert containsExactlyElementsIn() failed", e);
+ // We LOG the expected and actual flow in case of a failed assertion
+ // because, even though that is typically just a HUGE String that's
+ // hard to read (the diff printed subsequently by assertEqualBeans
+ // is, much, more readable), there are cases when looking more closely
+ // at the full toString() output of the flows is still useful, so:
+ LOG.warn("assert failed [order ignored!]; expected flows: {}", sortedExpectedFlows);
+ LOG.warn("assert failed [order ignored!]; actual flows : {}", sortedFlows);
+ // The point of this is basically just that our assertEqualBeans output,
+ // in case of a comparison failure, is *A LOT* more clearly readable
+ // than what G Truth (or Hamcrest) can do based on toString.
+ assertEqualBeans(sortedExpectedFlows, sortedFlows);
+ }
+ }
+
+ private List<FlowEntity> sortFlows(Iterable<FlowEntity> flowsToSort) {
+ List<FlowEntity> sortedFlows = Lists.newArrayList(flowsToSort);
+ Collections.sort(sortedFlows,
+ (flow1, flow2) -> ComparisonChain.start()
+ .compare(flow1.getTableId(), flow2.getTableId())
+ .compare(flow1.getPriority(), flow2.getPriority())
+ .compare(flow1.getFlowId(), flow2.getFlowId())
+ .result());
+ return sortedFlows;
+ }
+
+ @Override
+ public synchronized void installFlow(FlowEntity flowEntity) {
+ getOrNewFlows().add(flowEntity);
+ }
+
+ @Override
+ public synchronized CheckedFuture<Void, TransactionCommitFailedException> installFlow(BigInteger dpId,
+ FlowEntity flowEntity) {
+ installFlow(flowEntity);
+ return Futures.immediateCheckedFuture(null);
+ }
+
+ @Override
+ public synchronized CheckedFuture<Void, TransactionCommitFailedException> removeFlow(BigInteger dpnId,
+ FlowEntity flowEntity) {
+ getOrNewFlows().remove(flowEntity);
+ return Futures.immediateCheckedFuture(null);
+ }
+
+ @Override
+ public synchronized void batchedAddFlow(BigInteger dpId, FlowEntity flowEntity) {
+ getOrNewFlows().add(flowEntity);