Search in sources :

Example 6 with IdentityPlaceholderResolverStrategy

use of org.drools.serialization.protobuf.marshalling.IdentityPlaceholderResolverStrategy in project drools by kiegroup.

the class ObjectMarshallingStrategyStoreTest method testThrowErrorWhenExistMultipleMarshallingStrategiesWithSameName.

@Test
public void testThrowErrorWhenExistMultipleMarshallingStrategiesWithSameName() throws IOException, ClassNotFoundException {
    Environment env = EnvironmentFactory.newEnvironment();
    final Thing entityOne = new Thing(1, "Object 1");
    final Thing entityTwo = new Thing(2, "Object 2");
    Collection srcItems = new ArrayList();
    srcItems.add(entityOne);
    srcItems.add(entityTwo);
    ObjectMarshallingStrategy[] strats = new ObjectMarshallingStrategy[] { new IdentityPlaceholderResolverStrategy(new ObjectMarshallingStrategyAcceptor() {

        @Override
        public boolean accept(Object object) {
            return entityOne.equals(object);
        }
    }, Collections.singletonMap(entityOne.id, (Object) entityOne)), new IdentityPlaceholderResolverStrategy(new ObjectMarshallingStrategyAcceptor() {

        @Override
        public boolean accept(Object object) {
            return entityTwo.equals(object);
        }
    }, Collections.singletonMap(entityTwo.id, (Object) entityTwo)) };
    env.set(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES, strats);
    KieSessionConfiguration ksc = SessionConfiguration.newInstance();
    final KieBaseConfiguration kbconf = RuleBaseFactory.newKnowledgeBaseConfiguration();
    kbconf.setOption(EventProcessingOption.STREAM);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(RuleBaseFactory.newRuleBase(kbconf));
    KieSession ks = kbase.newKieSession(ksc, env);
    ks.insert(entityOne);
    ks.insert(entityTwo);
    try {
        ProtobufMarshaller marshaller = (ProtobufMarshaller) MarshallerFactory.newMarshaller(kbase, strats);
        // Here ocurrs the bug that shows that NamedObjectMarshallingStrategies are required.
        Assert.fail("A runtime error must be thrown while found strategies with same name");
    } catch (RuntimeException re) {
        Assert.assertTrue(re.getMessage().contains("Multiple"));
    }
}
Also used : ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) ArrayList(java.util.ArrayList) KieBaseConfiguration(org.kie.api.KieBaseConfiguration) IdentityPlaceholderResolverStrategy(org.drools.serialization.protobuf.marshalling.IdentityPlaceholderResolverStrategy) Environment(org.kie.api.runtime.Environment) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) ObjectMarshallingStrategyAcceptor(org.kie.api.marshalling.ObjectMarshallingStrategyAcceptor) Test(org.junit.Test)

Aggregations

IdentityPlaceholderResolverStrategy (org.drools.serialization.protobuf.marshalling.IdentityPlaceholderResolverStrategy)6 Test (org.junit.Test)6 Environment (org.kie.api.runtime.Environment)6 KieSession (org.kie.api.runtime.KieSession)6 ArrayList (java.util.ArrayList)5 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)3 Cheese (org.drools.mvel.compiler.Cheese)3 KieBase (org.kie.api.KieBase)3 Collection (java.util.Collection)2 List (java.util.List)2 Person (org.drools.mvel.compiler.Person)2 IteratorToList (org.drools.mvel.integrationtests.IteratorToList)2 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)2 ObjectMarshallingStrategy (org.kie.api.marshalling.ObjectMarshallingStrategy)2 ObjectMarshallingStrategyAcceptor (org.kie.api.marshalling.ObjectMarshallingStrategyAcceptor)2 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)1 Cell (org.drools.testcoverage.common.model.Cell)1