Package org.drools.guvnor.server.util

Source Code of org.drools.guvnor.server.util.BRDRTPersistenceTest

/**
* Copyright 2010 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.drools.guvnor.server.util;

import java.util.HashSet;

import junit.framework.TestCase;

import org.drools.ide.common.client.modeldriven.brl.ActionFieldValue;
import org.drools.ide.common.client.modeldriven.brl.ActionInsertFact;
import org.drools.ide.common.client.modeldriven.brl.FactPattern;
import org.drools.ide.common.client.modeldriven.brl.FreeFormLine;
import org.drools.ide.common.client.modeldriven.brl.IAction;
import org.drools.ide.common.client.modeldriven.brl.IPattern;
import org.drools.ide.common.client.modeldriven.brl.BaseSingleFieldConstraint;
import org.drools.ide.common.client.modeldriven.brl.SingleFieldConstraint;
import org.drools.ide.common.client.modeldriven.dt.TemplateModel;
import org.drools.ide.common.server.util.BRLPersistence;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BRDRTPersistenceTest extends TestCase {
  private static final Logger log = LoggerFactory.getLogger(BRDRTPersistenceTest.class);
  private BRLPersistence p;

  @Override
  protected void setUp() throws Exception {
    p = BRDRTPersistence.getInstance();
  }

  @Override
  protected void tearDown() throws Exception {
    p = null;
  }
 
  public void testGenerateEmptyDRL() {
    String expected =
        "rule \"null_0\"\n" +
        "  dialect \"mvel\"\n" +
        "  when\n" +
        "  then\n" +
        "end";

    final String drl = p.marshal(new TemplateModel());
    log.info("drl :\n{}", drl);

    assertNotNull(drl);
    assertEquals(expected, drl);
  }

  public void testEmptyData() {
    String expected =
        "rule \"with composite_0\"\n" +
        "  dialect \"mvel\"\n" +
        "  when\n" +
        "    $p : Person( name == name_na )\n" +
        "  then\n" +
        "end";
        TemplateModel m = new TemplateModel();
        m.name = "with composite";
        m.lhs = new IPattern[1];
        m.rhs = new IAction[0];

        FactPattern fp = new FactPattern("Person");
        fp.boundName = "$p";
       
        SingleFieldConstraint sfc = new SingleFieldConstraint("name");
        sfc.setFieldName("name");
        sfc.setValue("name");
        sfc.setOperator("==");
         
        sfc.setConstraintValueType(BaseSingleFieldConstraint.TYPE_TEMPLATE);
        fp.addConstraint(sfc);
       
        m.lhs[0] = fp;
        final String drl = p.marshal(m);
    log.info("drl :\n{}", drl);
        assertNotNull(drl);
        assertEquals(expected, drl);
  }

  public void testFreeFormLine() {
    String expected =
      "rule \"with composite_1\"\n" +
      "  dialect \"mvel\"\n" +
      "  when\n" +
      "    $p : Person( name == \"diegoll\" )\n" +
      "    Cheese(type == \"Gouda\", price < 17)\n" +
      "  then\n" +
      "    Person fact0 = new Person();\n" +
      "    fact0.setAge( 87 );\n" +
      "    insert(fact0 );\n" +
      "end\n" +
      "\n" +
      "rule \"with composite_0\"\n" +
      "  dialect \"mvel\"\n" +
      "  when\n" +
      "    $p : Person( name == \"baunax\" )\n" +
      "    Cheese(type == \"Cheddar\", price < 23)\n" +
      "  then\n" +
      "    Person fact0 = new Person();\n" +
      "    fact0.setAge( 34 );\n" +
      "    insert(fact0 );\n" +
      "end";

    TemplateModel m = new TemplateModel();
    m.name = "with composite";
    m.lhs = new IPattern[2];
    m.rhs = new IAction[1];

    FactPattern fp = new FactPattern("Person");
    fp.boundName = "$p";

    SingleFieldConstraint sfc = new SingleFieldConstraint("name");
    sfc.setFieldName("name");
    sfc.setValue("name");
    sfc.setOperator("==");

    sfc.setConstraintValueType(BaseSingleFieldConstraint.TYPE_TEMPLATE);
    fp.addConstraint(sfc);

    m.lhs[0] = fp;
   
    FreeFormLine ffl = new FreeFormLine();
    ffl.text = "Cheese(type == @{type}, price < @{price})";
   
    m.lhs[1] = ffl;

    ActionInsertFact aif = new ActionInsertFact("Person");
    ActionFieldValue afv = new ActionFieldValue("age", "age", "");
    afv.nature = ActionFieldValue.TYPE_TEMPLATE;

    aif.addFieldValue(afv);
    m.rhs[0] = aif;

    m.addRow(new String[] {"\"baunax\"", "\"Cheddar\"", "23", "34"});
    m.addRow(new String[] {"\"diegoll\"", "\"Gouda\"", "17", "87"});
    final String drl = p.marshal(m);
    log.info("drl :\n{}", drl);

    assertNotNull(drl);
    assertEquals(expected, drl);
  }

  public void testEmptyDataWithRHS() {
    String expected =
        "rule \"with composite_1\"\n" +
        "  dialect \"mvel\"\n" +
        "  when\n" +
        "    $p : Person( name == \"diegoll\" )\n" +
        "  then\n" +
        "    Person fact0 = new Person();\n" +
        "    fact0.setAge( 87 );\n" +
        "    insert(fact0 );\n" +
        "end\n" +
        "\n" +
        "rule \"with composite_0\"\n" +
        "  dialect \"mvel\"\n" +
        "  when\n" +
        "    $p : Person( name == \"baunax\" )\n" +
        "  then\n" +
        "    Person fact0 = new Person();\n" +
        "    fact0.setAge( 34 );\n" +
        "    insert(fact0 );\n" +
        "end";

    TemplateModel m = new TemplateModel();
        m.name = "with composite";
        m.lhs = new IPattern[1];
        m.rhs = new IAction[1];

        FactPattern fp = new FactPattern("Person");
        fp.boundName = "$p";
       
        SingleFieldConstraint sfc = new SingleFieldConstraint("name");
        sfc.setFieldName("name");
        sfc.setValue("name");
        sfc.setOperator("==");
         
        sfc.setConstraintValueType(BaseSingleFieldConstraint.TYPE_TEMPLATE);
        fp.addConstraint(sfc);
       
        m.lhs[0] = fp;
       
        ActionInsertFact aif = new ActionInsertFact("Person");
        ActionFieldValue afv = new ActionFieldValue("age", "age", "");
        afv.nature = ActionFieldValue.TYPE_TEMPLATE;
       
        aif.addFieldValue(afv);
        m.rhs[0] = aif;
       
        m.addRow(new String[] {"\"baunax\"", "34"});
        m.addRow(new String[] {"\"diegoll\"", "87"});
        final String drl = p.marshal(m);
    log.info("drl :\n{}", drl);
   
        assertNotNull(drl);
        assertEquals(expected, drl);
  }
 
  public void testWithData() {
    String expected =
        "rule \"with composite_1\"\n" +
        "  dialect \"mvel\"\n" +
        "  when\n" +
        "    $p : Person( name == \"diegoll\" )\n" +
        "  then\n" +
        "end\n" +
        "\n" +
        "rule \"with composite_0\"\n" +
        "  dialect \"mvel\"\n" +
        "  when\n" +
        "    $p : Person( name == \"baunax\" )\n" +
        "  then\n" +
        "end";
   
        TemplateModel m = new TemplateModel();
        m.name = "with composite";
        m.lhs = new IPattern[1];
        m.rhs = new IAction[0];

        FactPattern fp = new FactPattern("Person");
        fp.boundName = "$p";
       
        SingleFieldConstraint sfc = new SingleFieldConstraint("name");
        sfc.setFieldName("name");
        sfc.setValue("name");
        sfc.setOperator("==");
         
        sfc.setConstraintValueType(BaseSingleFieldConstraint.TYPE_TEMPLATE);
        fp.addConstraint(sfc);
       
        m.lhs[0] = fp;
       
        m.addRow(new String[] {"\"baunax\""});
        m.addRow(new String[] {"\"diegoll\""});
       
        final String drl = p.marshal(m);
    log.info("drl :\n{}", drl);
        assertNotNull(drl);
        assertEquals(expected, drl);

  }
 
  public void testRemoveWithData() {
    String expected =
        "rule \"with composite_1\"\n" +
        "  dialect \"mvel\"\n" +
        "  when\n" +
        "    $p : Person( name == \"diegoll\" )\n" +
        "  then\n" +
        "end\n" +
        "\n" +
        "rule \"with composite_0\"\n" +
        "  dialect \"mvel\"\n" +
        "  when\n" +
        "    $p : Person( name == \"baunax\" )\n" +
        "  then\n" +
        "end";
   
        TemplateModel m = new TemplateModel();
        m.name = "with composite";
        m.lhs = new IPattern[1];
        m.rhs = new IAction[0];

        FactPattern fp = new FactPattern("Person");
        fp.boundName = "$p";
       
        SingleFieldConstraint sfc = new SingleFieldConstraint("name");
        sfc.setFieldName("name");
        sfc.setValue("name");
        sfc.setOperator("==");
         
        sfc.setConstraintValueType(BaseSingleFieldConstraint.TYPE_TEMPLATE);
        fp.addConstraint(sfc);
       
        m.lhs[0] = fp;
       
        m.addRow(new String[] {"\"baunax\""});
        m.addRow(new String[] {"\"diegoll\""});
        String id1 = m.addRow(new String[] {"\"diegoll1\""});
        String id2 = m.addRow(new String[] {"\"diegoll2\""});
       
        m.removeRowById(id1);
        m.removeRowById(id2);
       
        final String drl = p.marshal(m);
    log.info("drl :\n{}", drl);
        assertNotNull(drl);
        assertEquals(expected, drl);

  }
 
  public void testWithDataAndSync() {
        TemplateModel m = new TemplateModel();
        m.name = "with composite";
        m.lhs = new IPattern[1];
        m.rhs = new IAction[0];

        FactPattern fp = new FactPattern("Person");
        fp.boundName = "$p";
       
        SingleFieldConstraint sfc = new SingleFieldConstraint("name");
        sfc.setFieldName("name");
        sfc.setValue("name");
        sfc.setOperator("==");
        sfc.setConstraintValueType(BaseSingleFieldConstraint.TYPE_TEMPLATE);
       
        fp.addConstraint(sfc);
       
        sfc = new SingleFieldConstraint("age");
        sfc.setFieldName("age");
        sfc.setValue("age");
        sfc.setOperator("==");
        sfc.setConstraintValueType(BaseSingleFieldConstraint.TYPE_TEMPLATE);
       
        fp.addConstraint(sfc);
       
        m.lhs[0] = fp;

        m.putInSync();
        HashSet<String> expected = new HashSet<String>();
        expected.add("name");
        expected.add("age");
        expected.add(TemplateModel.ID_COLUMN_NAME);
        assertEquals(expected, m.getTable().keySet());
       
        fp.removeConstraint(1);
        m.putInSync();
       
        expected.remove("age");
        expected.add(TemplateModel.ID_COLUMN_NAME);
        assertEquals(expected, m.getTable().keySet());
       
        fp.addConstraint(sfc);
        m.putInSync();
       
        expected.add("age");
        expected.add(TemplateModel.ID_COLUMN_NAME);
        assertEquals(expected, m.getTable().keySet());
       
  }
}
TOP

Related Classes of org.drools.guvnor.server.util.BRDRTPersistenceTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.