package org.batfish.question;

import java.util.List;

import org.batfish.main.BatfishException;
import org.batfish.main.BatfishLogger;
import org.batfish.main.Settings;

public class Assertion implements Statement {

   private final String _assertionText;

   private final BooleanExpr _expr;

   private List<Statement> _onErrorStatements;

   public Assertion(BooleanExpr expr, String assertionText,
         List<Statement> onErrorStatements) {
      _expr = expr;
      _assertionText = assertionText;
      _onErrorStatements = onErrorStatements;
   }

   @Override
   public void execute(Environment environment, BatfishLogger logger,
         Settings settings) {
      environment.setAssertions(true);
      boolean pass = _expr.evaluate(environment);
      environment.incrementAssertionCount();
      if (pass) {
         String successMessage = "Assertion: " + _assertionText
               + " succeeded with context: " + environment.toString() + "\n";
         logger.info(successMessage);
      }
      else {
         environment.setUnsafe(true);
         environment.incrementFailedAssertionCount();
         String debugErrorMessage = "Assertion: " + _assertionText
               + " failed with context: " + environment.toString() + "\n";
         if (settings.getExitOnFirstError()) {
            executeOnErrorStatements(environment, logger, settings);
            throw new BatfishException(debugErrorMessage);
         }
         else {
            logger.debug(debugErrorMessage);
            executeOnErrorStatements(environment, logger, settings);
         }
      }
   }

   private void executeOnErrorStatements(Environment environment,
         BatfishLogger logger, Settings settings) {
      for (Statement statement : _onErrorStatements) {
         statement.execute(environment, logger, settings);
      }
   }

}

@@ -1447,32 +1447,167 @@
          else %7B%0A
+            if (_onErrorStatements.size() == 0) %7B%0A               logger.output(debugErrorMessage);%0A            %7D%0A            else %7B%0A   
             logg
@@ -1627,32 +1627,46 @@
 gErrorMessage);%0A
+            %7D%0A
             exec

print environment for failed assertion at 'output' log level if there are no onfailure statements