private void assertStatelessRegions(
      final OperatorDef stateless1,
      final OperatorDef stateless2,
      final Map<String, OperatorDef> optimizedOperators,
      final Collection<Entry<Port, Port>> optimizedConnections,
      final List<RegionDef> statelessRegions) {
    for (RegionDef region : statelessRegions) {
      if (region.getOperatorCount() != 2) {
        continue;
      }

      final List<OperatorDef> operators = region.getOperators();

      final OperatorDef optimizedStateless1 = operators.get(0);
      final OperatorDef optimizedStateless2 = operators.get(1);

      assertEquals(StatelessOperator2.class, optimizedStateless1.operatorClazz());
      assertEquals(StatelessOperator2.class, optimizedStateless2.operatorClazz());

      assertTrue(optimizedOperators.containsKey(optimizedStateless1.id()));
      assertTrue(optimizedOperators.containsKey(optimizedStateless2.id()));

      assertTrue(
          optimizedConnections.contains(
              new SimpleEntry<>(
                  new Port(optimizedStateless1.id(), 0), new Port(optimizedStateless2.id(), 0))));
    }

    assertFalse(optimizedOperators.containsKey(stateless1.id()));
    assertFalse(optimizedOperators.containsKey(stateless2.id()));
  }
  private void assertStatelessRegions(
      final OperatorDef stateless1,
      final OperatorDef stateless2,
      final OperatorDef stateless3,
      final Map<String, OperatorDef> optimizedOperators,
      final Collection<Entry<Port, Port>> optimizedConnections,
      final List<RegionDef> statelessRegions,
      final boolean expectedNonMatchingStatelessRegion) {
    boolean stateful1ConnectionExists = false, stateful2ConnectionExists = false;
    for (RegionDef region : statelessRegions) {
      if (region.getOperatorCount() != 3) {
        assertTrue(expectedNonMatchingStatelessRegion);
        continue;
      }

      final List<OperatorDef> operators = region.getOperators();

      final OperatorDef optimizedStateless1 = operators.get(0);
      final OperatorDef optimizedStateless2 = operators.get(1);
      final OperatorDef optimizedStateless3 = operators.get(2);

      assertEquals(StatelessOperator.class, optimizedStateless1.operatorClazz());
      assertEquals(StatelessOperator.class, optimizedStateless2.operatorClazz());
      assertEquals(StatelessOperator.class, optimizedStateless3.operatorClazz());

      assertTrue(optimizedOperators.containsKey(optimizedStateless1.id()));
      assertTrue(optimizedOperators.containsKey(optimizedStateless2.id()));
      assertTrue(optimizedOperators.containsKey(optimizedStateless3.id()));

      final Collection<Port> stateful1DownstreamConnections =
          getDownstream(optimizedConnections, new Port("stateful1", 0));
      final Collection<Port> stateful2DownstreamConnections =
          getDownstream(optimizedConnections, new Port("stateful2", 0));
      assertEquals(1, stateful1DownstreamConnections.size());
      assertEquals(1, stateful2DownstreamConnections.size());
      final Port stateful1DownstreamPort = stateful1DownstreamConnections.iterator().next();
      if (stateful1DownstreamPort.operatorId.equals(optimizedStateless1.id())) {
        stateful1ConnectionExists = true;
      } else {
        final Port stateful2DownstreamPort = stateful2DownstreamConnections.iterator().next();
        if (stateful2DownstreamPort.operatorId.equals(optimizedStateless1.id())) {
          stateful2ConnectionExists = true;
        }
      }

      assertTrue(
          optimizedConnections.contains(
              new SimpleEntry<>(
                  new Port(optimizedStateless1.id(), 0), new Port(optimizedStateless2.id(), 0))));
      assertTrue(
          optimizedConnections.contains(
              new SimpleEntry<>(
                  new Port(optimizedStateless2.id(), 0), new Port(optimizedStateless3.id(), 0))));
      assertTrue(
          optimizedConnections.contains(
              new SimpleEntry<>(new Port(optimizedStateless3.id(), 0), new Port("stateful3", 0))));
      assertTrue(
          optimizedConnections.contains(
              new SimpleEntry<>(new Port(optimizedStateless3.id(), 0), new Port("stateful4", 0))));
    }

    assertTrue(stateful1ConnectionExists);
    assertTrue(stateful2ConnectionExists);

    assertFalse(optimizedOperators.containsKey(stateless1.id()));
    assertFalse(optimizedOperators.containsKey(stateless2.id()));
    assertFalse(optimizedOperators.containsKey(stateless3.id()));
  }