/**
   * Given that I want to execute a groovy business rule that uses the formatting api
   *
   * <p>When I call {@link ApplicationController#runOperation(ExecutionContext)}
   *
   * <p>Then I must check the resulting ExecutionContext with all the uses checked
   *
   * @throws Exception
   */
  @Test
  public void testFormattingAPIGroovyOperation() throws Exception {
    PlatformMessageRegistry pmr = new PlatformMessageRegistry();
    pmr.init();

    Locale ptBrLoc = new Locale("pt", "BR");
    windApp.addResourceBundle(
        ptBrLoc,
        new PropertyResourceBundle(
            getClass().getResourceAsStream("/wind-app-controller-messages-mock.properties")));
    windApp.setCurrentLocale(ptBrLoc);

    ExecutionContext<ModelReference> ctx = new ExecutionContext<ModelReference>();
    DomainObject dObj = findDomainObject("Conta", windApp);
    ctx.setOperation(dObj.getOperation("UsesOfFormattingAPI"));
    ctx.setUserContext(new CommonUserContext("TEST_SESSID"));
    ctx.setMeta(dObj);

    ctx = bean.runOperation(ctx);

    assertTrue((Boolean) ctx.getSession().get("executed"));

    assertNotNull(ctx.getSession().get("date_pt_BR"));
    assertNotNull(ctx.getSession().get("date_en_US"));
    assertNotNull(ctx.getSession().get("date_hour"));
    assertNotNull(ctx.getSession().get("clearHour"));

    assertNotNull(ctx.getSession().get("currency"));
    assertNotNull(ctx.getSession().get("percent"));
    assertNotNull(ctx.getSession().get("currency_en_US"));
    assertNotNull(ctx.getSession().get("percent_en_US"));
    assertEquals(BigDecimal.valueOf(150), ctx.getSession().get("percent_Of"));
  }
  /**
   * Given that I want to execute a groovy business rule that uses the persistence api
   *
   * <p>When I call {@link ApplicationController#runOperation(ExecutionContext)}
   *
   * <p>Then I must check the resulting ExecutionContext with all the uses checked
   *
   * @throws Exception
   */
  @Test
  public void testPersistenceAPIGroovyOperation() throws Exception {
    DomainObject dObj = findDomainObject("Conta", windApp);
    ModelReference instance = bean.createNewInstance(new CommonUserContext("TEST_SESSID"), dObj);
    IStorage storage = windApp.getBean(IStorage.BEAN_NAME, IStorage.class);
    Reflect.on(instance).set("nome", "Itau");
    Reflect.on(instance).set("tipo", "Credito");
    Reflect.on(instance).set("saldo", 154.54d);
    storage.save(instance);

    PlatformMessageRegistry pmr = new PlatformMessageRegistry();
    pmr.init();

    Locale ptBrLoc = new Locale("pt", "BR");
    windApp.addResourceBundle(
        ptBrLoc,
        new PropertyResourceBundle(
            getClass().getResourceAsStream("/wind-app-controller-messages-mock.properties")));
    windApp.setCurrentLocale(ptBrLoc);

    ExecutionContext<ModelReference> ctx = new ExecutionContext<ModelReference>();
    ctx.setOperation(dObj.getOperation("UsesOfPersistenceAPI"));
    ctx.setUserContext(new CommonUserContext("TEST_SESSID"));
    ctx.setMeta(dObj);

    ctx = bean.runOperation(ctx);

    List cList = (List) ctx.getSession().get("cList");
    assertNotNull(cList);
    assertEquals(1, cList.size());
  }
  /**
   * Given that I want to execute a groovy business rule that uses the message api
   *
   * <p>When I call {@link ApplicationController#runOperation(ExecutionContext)}
   *
   * <p>Then I must check the resulting ExecutionContext with all messages added by the rule
   *
   * @throws Exception
   */
  @Test
  public void testMessageAPIGroovyOperation() throws Exception {

    PlatformMessageRegistry pmr = new PlatformMessageRegistry();
    pmr.init();

    Locale ptBrLoc = new Locale("pt", "BR");
    windApp.addResourceBundle(
        ptBrLoc,
        new PropertyResourceBundle(
            getClass().getResourceAsStream("/wind-app-controller-messages-mock.properties")));
    windApp.setCurrentLocale(ptBrLoc);

    ExecutionContext<ModelReference> ctx = new ExecutionContext<ModelReference>();
    DomainObject dObj = findDomainObject("Conta", windApp);
    ctx.setOperation(dObj.getOperation("UsesOfMessageAPI"));
    ctx.setUserContext(new CommonUserContext("TEST_SESSID"));
    ctx.setMeta(dObj);

    ctx = bean.runOperation(ctx);

    assertTrue((Boolean) ctx.getSession().get("executed"));
    assertNotNull(ctx.getMessages());
    assertEquals(10, ctx.getMessages().size());
    assertEquals("this is a warn", ctx.getMessages().get(0).getI18nMessage());
    assertEquals(MessageKind.WARNING, ctx.getMessages().get(0).getKind());

    assertEquals("this is an info", ctx.getMessages().get(1).getI18nMessage());
    assertEquals(MessageKind.INFO, ctx.getMessages().get(1).getKind());

    assertEquals("this is an error", ctx.getMessages().get(2).getI18nMessage());
    assertEquals(MessageKind.ERROR, ctx.getMessages().get(2).getKind());

    assertEquals("this is a success", ctx.getMessages().get(3).getI18nMessage());
    assertEquals(MessageKind.SUCCESS, ctx.getMessages().get(3).getKind());

    assertEquals("this is a warn with PARAM", ctx.getMessages().get(4).getFormattedMessage());
    assertEquals(MessageKind.WARNING, ctx.getMessages().get(4).getKind());

    assertEquals("this is an info with PARAM", ctx.getMessages().get(5).getFormattedMessage());
    assertEquals(MessageKind.INFO, ctx.getMessages().get(5).getKind());

    assertEquals("this is an error with PARAM", ctx.getMessages().get(6).getFormattedMessage());
    assertEquals(MessageKind.ERROR, ctx.getMessages().get(6).getKind());

    assertEquals("this is a success with PARAM", ctx.getMessages().get(7).getFormattedMessage());
    assertEquals(MessageKind.SUCCESS, ctx.getMessages().get(7).getKind());

    assertEquals(
        "this is a success with a param and another one",
        ctx.getMessages().get(8).getFormattedMessage());
    assertEquals(MessageKind.SUCCESS, ctx.getMessages().get(8).getKind());

    assertEquals(
        "this is a message key used for tests", ctx.getMessages().get(9).getFormattedMessage());
    assertEquals(MessageKind.SUCCESS, ctx.getMessages().get(9).getKind());
  }
  /**
   * Given that I want to get a business rule executed.
   *
   * <p>When I call {@link ApplicationController#runOperation(ExecutionContext)}
   *
   * <p>Then I must check the resulting ExecutionContext
   */
  @Test
  public void testRunOperation() {
    ExecutionContext<ModelReference> ctx = new ExecutionContext<ModelReference>();
    DomainObject dObj = findDomainObject("Conta", windApp);
    ctx.setOperation(dObj.getOperation("SaveConta"));
    ctx.setUserContext(new CommonUserContext("TEST_SESSID"));

    ctx = bean.runOperation(ctx);

    assertEquals("Hello!", ctx.getSession().get("msg"));
    assertTrue((Boolean) ctx.getSession().get("executed"));
  }
  /**
   * Given that I want to validate the execution of a ExecutionContext
   *
   * <p>When I call {@link ApplicationController#processValidations(ExecutionContext)}
   *
   * <p>Then I must check the resulting ExecutionContext
   *
   * @throws Exception
   */
  @Test
  public void testProcessValidations() throws Exception {
    ExecutionContext<ModelReference> ctx = new ExecutionContext<ModelReference>();
    DomainObject dObj = findDomainObject("Conta", windApp);
    ctx.setOperation(dObj.getOperation("SaveConta"));
    ctx.setUserContext(new CommonUserContext("TEST_SESSID"));
    ctx.setMeta(dObj);

    // popula os dados submetidos a validacao
    ModelReference instance = (ModelReference) dObj.getObjectClass().newInstance();
    Reflect.on(instance).set("nome", "Credito Itau");
    Reflect.on(instance).set("tipo", "Credito");
    ctx.setInstance(instance);

    // TODO
    // ctx = bean.processValidations(ctx);

  }