/**
   * 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());
  }
  /** @see #formattedMessage */
  public String getFormattedMessage() {
    // resolve msg somente na primeira vez
    if (formattedMessage == null && i18nMessage != null) {
      List<ResourceBundle> rbs = this.source.getApplication().getResouceBundle();
      if (rbs != null) {
        for (ResourceBundle rb : rbs) {
          try {
            String msg = rb.getString(i18nMessage);
            if (msg != null) {
              formattedMessage = MessageFormat.format(msg, param.toArray(new Object[] {}));
              break;
            }
          } catch (MissingResourceException mre) {
            // silently igonre
            continue;
          }
        }
      }
    }

    // verifica msgs da platforma
    if (StringUtils.isBlank(formattedMessage)) {
      List<ResourceBundleMessageSource> rbs =
          PlatformMessageRegistry.getInstance().getMessageBundles();
      if (rbs != null) {
        for (ResourceBundleMessageSource rb : rbs) {
          try {
            String msg =
                rb.getMessage(
                    i18nMessage,
                    param.toArray(new Object[] {}),
                    source.getApplication().getCurrentLocale());

            formattedMessage = msg;
            break;

          } catch (NoSuchMessageException nsme) {
            // silently igonre
            continue;
          }
        }
      }
    }

    if (StringUtils.isBlank(formattedMessage)) {
      formattedMessage = MessageFormat.format(i18nMessage, param.toArray(new Object[] {}));
    }

    return formattedMessage;
  }