Search in sources :

Example 1 with AbortWorkItemCommand

use of org.drools.core.command.runtime.process.AbortWorkItemCommand in project drools by kiegroup.

the class CommandSerializationTest method batchExecutionImplSerializationTest.

@Test
@Ignore
public void batchExecutionImplSerializationTest() throws Exception {
    DefaultFactHandle factHandle = new DefaultFactHandle(13, "entry-point-id", 42, 84, 400l, "fact");
    BatchExecutionCommandImpl batchCmd = new BatchExecutionCommandImpl();
    batchCmd.setLookup("lookup");
    {
        AbortWorkItemCommand cmd = new AbortWorkItemCommand(23l);
        batchCmd.addCommand(cmd);
    }
    {
        String externalForm = factHandle.toExternalForm();
        assertEquals("FactHandle string", externalForm, DisconnectedFactHandle.newFrom(factHandle).toExternalForm());
        DeleteCommand cmd = new DeleteCommand(factHandle);
        batchCmd.addCommand(cmd);
    }
    {
        GetGlobalCommand cmd = new GetGlobalCommand("global-id");
        cmd.setOutIdentifier("out-id");
        batchCmd.addCommand(cmd);
    }
    {
        SetGlobalCommand cmd = new SetGlobalCommand("global-id", new Integer(23));
        cmd.setOutIdentifier("out-id");
        batchCmd.addCommand(cmd);
    }
    {
        InsertElementsCommand cmd = new InsertElementsCommand();
        cmd.setEntryPoint("entry-point");
        cmd.setOutIdentifier("out-id");
        cmd.setReturnObject(true);
        Map<String, Object> mapObj = new HashMap<String, Object>();
        mapObj.put("key", "value");
        List<Object> objects = new ArrayList<Object>(1);
        objects.add(mapObj);
        cmd.setObjects(objects);
        batchCmd.addCommand(cmd);
    }
    {
        QueryCommand cmd = new QueryCommand();
        List<Object> args = new ArrayList<Object>(3);
        args.add("this");
        args.add(42);
        args.add("other");
        cmd.setArguments(args);
        cmd.setName("query-name");
        cmd.setOutIdentifier("out-id");
        batchCmd.addCommand(cmd);
    }
    {
        InsertObjectCommand cmd = new InsertObjectCommand();
        cmd.setEntryPoint("entry-point");
        cmd.setOutIdentifier("out-id");
        cmd.setReturnObject(true);
        cmd.setObject("object");
        batchCmd.addCommand(cmd);
    }
    {
        ModifyCommand cmd = new ModifyCommand();
        cmd.setFactHandle(DisconnectedFactHandle.newFrom(factHandle));
        List<Setter> setters = new ArrayList<Setter>(2);
        Setter setter = new Setter() {

            @Override
            public String getValue() {
                return "blue";
            }

            @Override
            public String getAccessor() {
                return "heart";
            }
        };
        setters.add(setter);
        setter = new Setter() {

            @Override
            public String getValue() {
                return "hot";
            }

            @Override
            public String getAccessor() {
                return "fingers";
            }
        };
        setters.add(setter);
        cmd.setSetters(setters);
        batchCmd.addCommand(cmd);
    }
    {
        GetObjectCommand cmd = new GetObjectCommand(factHandle, "out-id");
        batchCmd.addCommand(cmd);
    }
    // TODO: implement serialization for agenda filters
    {
        AgendaFilter[] filters = new AgendaFilter[4];
        filters[0] = new RuleNameEndsWithAgendaFilter("suffix", false);
        filters[1] = new RuleNameEqualsAgendaFilter("name", true);
        filters[2] = new RuleNameMatchesAgendaFilter("regexp", false);
        filters[3] = new RuleNameStartsWithAgendaFilter("prefix", false);
        for (AgendaFilter filter : filters) {
            FireAllRulesCommand cmd = new FireAllRulesCommand(randomString(), random.nextInt(1000), filter);
            batchCmd.addCommand(cmd);
        }
    }
    {
        AgendaFilter[] filters = new AgendaFilter[4];
        filters[0] = new RuleNameEndsWithAgendaFilter("suffix", false);
        filters[1] = new RuleNameEqualsAgendaFilter("name", true);
        filters[2] = new RuleNameMatchesAgendaFilter("regexp", false);
        filters[3] = new RuleNameStartsWithAgendaFilter("prefix", false);
        for (AgendaFilter filter : filters) {
            FireUntilHaltCommand cmd = new FireUntilHaltCommand(filter);
            batchCmd.addCommand(cmd);
        }
    }
    {
        Map<String, Object> results = new HashMap<String, Object>(1);
        List<String> resultValList = new ArrayList<String>(2);
        resultValList.add("yellow");
        resultValList.add("chances");
        results.put("list", resultValList);
        CompleteWorkItemCommand cmd = new CompleteWorkItemCommand(random.nextInt(1000), results);
        batchCmd.addCommand(cmd);
    }
    {
        ClassObjectFilter filter = new ClassObjectFilter(String.class);
        GetObjectsCommand cmd = new GetObjectsCommand(filter, "out-id");
        batchCmd.addCommand(cmd);
    }
    {
        AgendaGroupSetFocusCommand cmd = new AgendaGroupSetFocusCommand(randomString());
        batchCmd.addCommand(cmd);
    }
    {
        ClearActivationGroupCommand cmd = new ClearActivationGroupCommand(randomString());
        batchCmd.addCommand(cmd);
    }
    {
        ClearAgendaCommand cmd = new ClearAgendaCommand();
        batchCmd.addCommand(cmd);
    }
    {
        ClearAgendaGroupCommand cmd = new ClearAgendaGroupCommand(randomString());
        batchCmd.addCommand(cmd);
    }
    {
        ClearRuleFlowGroupCommand cmd = new ClearRuleFlowGroupCommand(randomString());
        batchCmd.addCommand(cmd);
    }
    BatchExecutionCommandImpl batchCmdCopy = roundTrip(batchCmd);
    assertEquals("Batch cmd lookup", batchCmd.getLookup(), batchCmdCopy.getLookup());
    assertEquals("Batch cmd num commands", batchCmd.getCommands().size(), batchCmdCopy.getCommands().size());
    // This code should use the utility in kie-test-util when it finally gets moved there..
    for (Command copyCmd : batchCmdCopy.getCommands()) {
        for (Command origCmd : batchCmd.getCommands()) {
            Class cmdClass = origCmd.getClass();
            if (copyCmd.getClass().equals(cmdClass)) {
                if (cmdClass.equals(DeleteCommand.class)) {
                    compareFactHandles(((DeleteCommand) origCmd).getFactHandle(), ((DeleteCommand) copyCmd).getFactHandle(), DeleteCommand.class);
                } else if (cmdClass.equals(FireAllRulesCommand.class)) {
                    AgendaFilter origFilter = ((FireAllRulesCommand) origCmd).getAgendaFilter();
                    AgendaFilter copyFilter = ((FireAllRulesCommand) copyCmd).getAgendaFilter();
                    if (!origFilter.getClass().equals(copyFilter.getClass())) {
                        continue;
                    }
                    Class agendaFilterClass = origFilter.getClass();
                    for (Field agendaFilterField : agendaFilterClass.getDeclaredFields()) {
                        agendaFilterField.setAccessible(true);
                        Object afFieldOrigVal = agendaFilterField.get(origFilter);
                        Object afFieldCopyVal = agendaFilterField.get(copyFilter);
                        if (afFieldOrigVal instanceof Pattern) {
                            afFieldOrigVal = ((Pattern) afFieldOrigVal).pattern();
                            afFieldCopyVal = ((Pattern) afFieldCopyVal).pattern();
                        }
                        assertEquals(agendaFilterClass.getSimpleName() + "." + agendaFilterField.getName(), afFieldOrigVal, afFieldCopyVal);
                    }
                    assertEquals(FireAllRulesCommand.class.getSimpleName() + ".max", ((FireAllRulesCommand) origCmd).getMax(), ((FireAllRulesCommand) copyCmd).getMax());
                    assertEquals(FireAllRulesCommand.class.getSimpleName() + ".outIdentifier", ((FireAllRulesCommand) origCmd).getOutIdentifier(), ((FireAllRulesCommand) copyCmd).getOutIdentifier());
                } else if (cmdClass.equals(FireUntilHaltCommand.class)) {
                    AgendaFilter origFilter = ((FireUntilHaltCommand) origCmd).getAgendaFilter();
                    AgendaFilter copyFilter = ((FireUntilHaltCommand) copyCmd).getAgendaFilter();
                    if (!origFilter.getClass().equals(copyFilter.getClass())) {
                        continue;
                    }
                    Class agendaFilterClass = origFilter.getClass();
                    for (Field agendaFilterField : agendaFilterClass.getDeclaredFields()) {
                        agendaFilterField.setAccessible(true);
                        Object afFieldOrigVal = agendaFilterField.get(origFilter);
                        Object afFieldCopyVal = agendaFilterField.get(copyFilter);
                        if (afFieldOrigVal instanceof Pattern) {
                            afFieldOrigVal = ((Pattern) afFieldOrigVal).pattern();
                            afFieldCopyVal = ((Pattern) afFieldCopyVal).pattern();
                        }
                        assertEquals(agendaFilterClass.getSimpleName() + "." + agendaFilterField.getName(), afFieldOrigVal, afFieldCopyVal);
                    }
                } else {
                    for (Field cmdField : cmdClass.getDeclaredFields()) {
                        cmdField.setAccessible(true);
                        if (Modifier.isTransient(cmdField.getModifiers())) {
                            continue;
                        }
                        Object origVal = cmdField.get(origCmd);
                        assertNotNull(cmdClass.getSimpleName() + "." + cmdField.getName(), origVal);
                        Object copyVal = cmdField.get(copyCmd);
                        assertNotNull("Round-tripped " + cmdClass.getSimpleName() + "." + cmdField.getName(), copyVal);
                        if (origVal instanceof FactHandle) {
                            compareFactHandles((FactHandle) origVal, (FactHandle) copyVal, cmdClass);
                        } else if (origVal instanceof ClassObjectSerializationFilter) {
                            assertEquals("Original compared to Round-tripped " + cmdClass.getSimpleName() + "." + cmdField.getName(), ((ClassObjectSerializationFilter) origVal).getClass(), ((ClassObjectSerializationFilter) copyVal).getClass());
                        } else if (origVal instanceof List) {
                            List origList = (List) origVal;
                            if (((List) copyVal).isEmpty()) {
                                assertTrue("Original compared to Round-tripped " + cmdClass.getSimpleName() + "." + cmdField.getName(), origList.isEmpty());
                            } else {
                                if (origList.get(0) instanceof Setter) {
                                    for (Object obj : (List) origVal) {
                                        assertTrue("Expected a " + Setter.class.getSimpleName() + " instance (not " + obj.getClass().getSimpleName() + " in " + cmdClass.getSimpleName() + "." + cmdField.getName(), obj instanceof Setter);
                                        Iterator<Object> iter = ((List) copyVal).iterator();
                                        while (iter.hasNext()) {
                                            Setter copySetter = (Setter) iter.next();
                                            if (((Setter) obj).getAccessor().equals(copySetter.getAccessor())) {
                                                assertEquals("Original compared to Round-tripped " + cmdClass.getSimpleName() + "." + cmdField.getName(), ((Setter) obj).getValue(), copySetter.getValue());
                                                iter.remove();
                                            }
                                        }
                                    }
                                    assertTrue("Original compared to Round-tripped " + cmdClass.getSimpleName() + "." + cmdField.getName(), ((List) copyVal).isEmpty());
                                } else if (origList.get(0) instanceof Map) {
                                    Map copyMap = (Map) ((List) copyVal).get(0);
                                    for (Object entry : ((Map) origList.get(0)).entrySet()) {
                                        assertTrue("Original compared to Round-tripped " + cmdClass.getSimpleName() + "." + cmdField.getName(), (copyMap).containsKey(((Entry) entry).getKey()));
                                    }
                                }
                            }
                        } else {
                            assertTrue("Original compared to Round-tripped " + cmdClass.getSimpleName() + "." + cmdField.getName(), origVal.equals(copyVal));
                        }
                    }
                }
            }
        }
    }
    // verify that BatchExecutionCommandImpl.commands has been filled with all
    // of the different types
    Field commandsField = BatchExecutionCommandImpl.class.getDeclaredField("commands");
    XmlElements xmlElemsAnno = commandsField.getAnnotation(XmlElements.class);
    List<Class> cmdTypes = new ArrayList<Class>(xmlElemsAnno.value().length);
    for (XmlElement xmlElem : xmlElemsAnno.value()) {
        cmdTypes.add(xmlElem.type());
    }
    // already thoroughly tested..
    cmdTypes.remove(SignalEventCommand.class);
    // already thoroughly tested..
    cmdTypes.remove(StartProcessCommand.class);
    for (Command cmd : batchCmd.getCommands()) {
        cmdTypes.remove(cmd.getClass());
    }
    String cmdInstName = cmdTypes.isEmpty() ? "null" : cmdTypes.get(0).getSimpleName();
    assertTrue("Please add a " + cmdInstName + " instance to the " + BatchExecutionCommandImpl.class.getSimpleName() + " commands!", cmdTypes.isEmpty());
// other tests for this as part of the REST integration tests..
}
Also used : DisconnectedFactHandle(org.drools.core.common.DisconnectedFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ArrayList(java.util.ArrayList) GetGlobalCommand(org.drools.core.command.runtime.GetGlobalCommand) RuleNameEqualsAgendaFilter(org.drools.core.base.RuleNameEqualsAgendaFilter) RuleNameStartsWithAgendaFilter(org.drools.core.base.RuleNameStartsWithAgendaFilter) Field(java.lang.reflect.Field) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) XmlElements(javax.xml.bind.annotation.XmlElements) BatchExecutionCommandImpl(org.drools.core.command.runtime.BatchExecutionCommandImpl) AbortWorkItemCommand(org.drools.core.command.runtime.process.AbortWorkItemCommand) ArrayList(java.util.ArrayList) List(java.util.List) RuleNameEndsWithAgendaFilter(org.drools.core.base.RuleNameEndsWithAgendaFilter) RuleNameStartsWithAgendaFilter(org.drools.core.base.RuleNameStartsWithAgendaFilter) AgendaFilter(org.kie.api.runtime.rule.AgendaFilter) RuleNameMatchesAgendaFilter(org.drools.core.base.RuleNameMatchesAgendaFilter) RuleNameEqualsAgendaFilter(org.drools.core.base.RuleNameEqualsAgendaFilter) ClassObjectSerializationFilter(org.drools.core.ClassObjectSerializationFilter) RuleNameEndsWithAgendaFilter(org.drools.core.base.RuleNameEndsWithAgendaFilter) RuleNameMatchesAgendaFilter(org.drools.core.base.RuleNameMatchesAgendaFilter) Pattern(java.util.regex.Pattern) CompleteWorkItemCommand(org.drools.core.command.runtime.process.CompleteWorkItemCommand) SetGlobalCommand(org.drools.core.command.runtime.SetGlobalCommand) ClassObjectFilter(org.drools.core.ClassObjectFilter) AbortWorkItemCommand(org.drools.core.command.runtime.process.AbortWorkItemCommand) CompleteWorkItemCommand(org.drools.core.command.runtime.process.CompleteWorkItemCommand) Command(org.kie.api.command.Command) StartProcessCommand(org.drools.core.command.runtime.process.StartProcessCommand) SetGlobalCommand(org.drools.core.command.runtime.SetGlobalCommand) GetGlobalCommand(org.drools.core.command.runtime.GetGlobalCommand) SignalEventCommand(org.drools.core.command.runtime.process.SignalEventCommand) Setter(org.kie.api.command.Setter) XmlElement(javax.xml.bind.annotation.XmlElement) HashMap(java.util.HashMap) Map(java.util.Map) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with AbortWorkItemCommand

use of org.drools.core.command.runtime.process.AbortWorkItemCommand in project drools by kiegroup.

the class CommandSerializationTest method batchExecutionImplSerializationTest.

@Test
public void batchExecutionImplSerializationTest() throws Exception {
    DefaultFactHandle factHandle = new DefaultFactHandle(13, "entry-point-id", 42, 84, 400l, "fact");
    BatchExecutionCommandImpl batchCmd = new BatchExecutionCommandImpl();
    batchCmd.setLookup("lookup");
    {
        AbortWorkItemCommand cmd = new AbortWorkItemCommand(23l);
        batchCmd.addCommand(cmd);
    }
    {
        String externalForm = factHandle.toExternalForm();
        assertEquals("FactHandle string", externalForm, DisconnectedFactHandle.newFrom(factHandle).toExternalForm());
        DeleteCommand cmd = new DeleteCommand(factHandle);
        batchCmd.addCommand(cmd);
    }
    {
        GetGlobalCommand cmd = new GetGlobalCommand("global-id");
        cmd.setOutIdentifier("out-id");
        batchCmd.addCommand(cmd);
    }
    {
        SetGlobalCommand cmd = new SetGlobalCommand("global-id", new Integer(23));
        cmd.setOutIdentifier("out-id");
        batchCmd.addCommand(cmd);
    }
    {
        InsertElementsCommand cmd = new InsertElementsCommand();
        cmd.setEntryPoint("entry-point");
        cmd.setOutIdentifier("out-id");
        cmd.setReturnObject(true);
        Map<String, Object> mapObj = new HashMap<String, Object>();
        mapObj.put("key", "value");
        List<Object> objects = new ArrayList<Object>(1);
        objects.add(mapObj);
        cmd.setObjects(objects);
        batchCmd.addCommand(cmd);
    }
    {
        QueryCommand cmd = new QueryCommand();
        List<Object> args = new ArrayList<Object>(3);
        args.add("this");
        args.add(42);
        args.add("other");
        cmd.setArguments(args);
        cmd.setName("query-name");
        cmd.setOutIdentifier("out-id");
        batchCmd.addCommand(cmd);
    }
    {
        InsertObjectCommand cmd = new InsertObjectCommand();
        cmd.setEntryPoint("entry-point");
        cmd.setOutIdentifier("out-id");
        cmd.setReturnObject(true);
        cmd.setObject("object");
        batchCmd.addCommand(cmd);
    }
    {
        ModifyCommand cmd = new ModifyCommand();
        cmd.setFactHandle(DisconnectedFactHandle.newFrom(factHandle));
        List<Setter> setters = new ArrayList<Setter>(2);
        Setter setter = new Setter() {

            @Override
            public String getValue() {
                return "blue";
            }

            @Override
            public String getAccessor() {
                return "heart";
            }
        };
        setters.add(setter);
        setter = new Setter() {

            @Override
            public String getValue() {
                return "hot";
            }

            @Override
            public String getAccessor() {
                return "fingers";
            }
        };
        setters.add(setter);
        cmd.setSetters(setters);
        batchCmd.addCommand(cmd);
    }
    {
        GetObjectCommand cmd = new GetObjectCommand(factHandle, "out-id");
        batchCmd.addCommand(cmd);
    }
    // TODO: implement serialization for agenda filters
    {
        AgendaFilter[] filters = new AgendaFilter[4];
        filters[0] = new RuleNameEndsWithAgendaFilter("suffix", false);
        filters[1] = new RuleNameEqualsAgendaFilter("name", true);
        filters[2] = new RuleNameMatchesAgendaFilter("regexp", false);
        filters[3] = new RuleNameStartsWithAgendaFilter("prefix", false);
        for (AgendaFilter filter : filters) {
            FireAllRulesCommand cmd = new FireAllRulesCommand(randomString(), random.nextInt(1000), filter);
            batchCmd.addCommand(cmd);
        }
    }
    {
        AgendaFilter[] filters = new AgendaFilter[4];
        filters[0] = new RuleNameEndsWithAgendaFilter("suffix", false);
        filters[1] = new RuleNameEqualsAgendaFilter("name", true);
        filters[2] = new RuleNameMatchesAgendaFilter("regexp", false);
        filters[3] = new RuleNameStartsWithAgendaFilter("prefix", false);
        for (AgendaFilter filter : filters) {
            FireUntilHaltCommand cmd = new FireUntilHaltCommand(filter);
            batchCmd.addCommand(cmd);
        }
    }
    {
        Map<String, Object> results = new HashMap<String, Object>(1);
        List<String> resultValList = new ArrayList<String>(2);
        resultValList.add("yellow");
        resultValList.add("chances");
        results.put("list", resultValList);
        CompleteWorkItemCommand cmd = new CompleteWorkItemCommand(random.nextInt(1000), results);
        batchCmd.addCommand(cmd);
    }
    {
        ClassObjectFilter filter = new ClassObjectFilter(String.class);
        GetObjectsCommand cmd = new GetObjectsCommand(filter, "out-id");
        batchCmd.addCommand(cmd);
    }
    {
        AgendaGroupSetFocusCommand cmd = new AgendaGroupSetFocusCommand(randomString());
        batchCmd.addCommand(cmd);
    }
    {
        ClearActivationGroupCommand cmd = new ClearActivationGroupCommand(randomString());
        batchCmd.addCommand(cmd);
    }
    {
        ClearAgendaCommand cmd = new ClearAgendaCommand();
        batchCmd.addCommand(cmd);
    }
    {
        ClearAgendaGroupCommand cmd = new ClearAgendaGroupCommand(randomString());
        batchCmd.addCommand(cmd);
    }
    {
        ClearRuleFlowGroupCommand cmd = new ClearRuleFlowGroupCommand(randomString());
        batchCmd.addCommand(cmd);
    }
    BatchExecutionCommandImpl batchCmdCopy = roundTrip(batchCmd);
    assertEquals("Batch cmd lookup", batchCmd.getLookup(), batchCmdCopy.getLookup());
    assertEquals("Batch cmd num commands", batchCmd.getCommands().size(), batchCmdCopy.getCommands().size());
    // This code should use the utility in kie-test-util when it finally gets moved there..
    for (Command copyCmd : batchCmdCopy.getCommands()) {
        for (Command origCmd : batchCmd.getCommands()) {
            Class cmdClass = origCmd.getClass();
            if (copyCmd.getClass().equals(cmdClass)) {
                if (cmdClass.equals(DeleteCommand.class)) {
                    compareFactHandles(((DeleteCommand) origCmd).getFactHandle(), ((DeleteCommand) copyCmd).getFactHandle(), DeleteCommand.class);
                } else if (cmdClass.equals(FireAllRulesCommand.class)) {
                    AgendaFilter origFilter = ((FireAllRulesCommand) origCmd).getAgendaFilter();
                    AgendaFilter copyFilter = ((FireAllRulesCommand) copyCmd).getAgendaFilter();
                    if (!origFilter.getClass().equals(copyFilter.getClass())) {
                        continue;
                    }
                    Class agendaFilterClass = origFilter.getClass();
                    for (Field agendaFilterField : agendaFilterClass.getDeclaredFields()) {
                        agendaFilterField.setAccessible(true);
                        Object afFieldOrigVal = agendaFilterField.get(origFilter);
                        Object afFieldCopyVal = agendaFilterField.get(copyFilter);
                        if (afFieldOrigVal instanceof Pattern) {
                            afFieldOrigVal = ((Pattern) afFieldOrigVal).pattern();
                            afFieldCopyVal = ((Pattern) afFieldCopyVal).pattern();
                        }
                        assertEquals(agendaFilterClass.getSimpleName() + "." + agendaFilterField.getName(), afFieldOrigVal, afFieldCopyVal);
                    }
                    assertEquals(FireAllRulesCommand.class.getSimpleName() + ".max", ((FireAllRulesCommand) origCmd).getMax(), ((FireAllRulesCommand) copyCmd).getMax());
                    assertEquals(FireAllRulesCommand.class.getSimpleName() + ".outIdentifier", ((FireAllRulesCommand) origCmd).getOutIdentifier(), ((FireAllRulesCommand) copyCmd).getOutIdentifier());
                } else if (cmdClass.equals(FireUntilHaltCommand.class)) {
                    AgendaFilter origFilter = ((FireUntilHaltCommand) origCmd).getAgendaFilter();
                    AgendaFilter copyFilter = ((FireUntilHaltCommand) copyCmd).getAgendaFilter();
                    if (!origFilter.getClass().equals(copyFilter.getClass())) {
                        continue;
                    }
                    Class agendaFilterClass = origFilter.getClass();
                    for (Field agendaFilterField : agendaFilterClass.getDeclaredFields()) {
                        agendaFilterField.setAccessible(true);
                        Object afFieldOrigVal = agendaFilterField.get(origFilter);
                        Object afFieldCopyVal = agendaFilterField.get(copyFilter);
                        if (afFieldOrigVal instanceof Pattern) {
                            afFieldOrigVal = ((Pattern) afFieldOrigVal).pattern();
                            afFieldCopyVal = ((Pattern) afFieldCopyVal).pattern();
                        }
                        assertEquals(agendaFilterClass.getSimpleName() + "." + agendaFilterField.getName(), afFieldOrigVal, afFieldCopyVal);
                    }
                } else {
                    for (Field cmdField : cmdClass.getDeclaredFields()) {
                        cmdField.setAccessible(true);
                        if (Modifier.isTransient(cmdField.getModifiers())) {
                            continue;
                        }
                        Object origVal = cmdField.get(origCmd);
                        assertNotNull(cmdClass.getSimpleName() + "." + cmdField.getName(), origVal);
                        Object copyVal = cmdField.get(copyCmd);
                        assertNotNull("Round-tripped " + cmdClass.getSimpleName() + "." + cmdField.getName(), copyVal);
                        if (origVal instanceof FactHandle) {
                            compareFactHandles((FactHandle) origVal, (FactHandle) copyVal, cmdClass);
                        } else if (origVal instanceof ClassObjectSerializationFilter) {
                            assertEquals("Original compared to Round-tripped " + cmdClass.getSimpleName() + "." + cmdField.getName(), ((ClassObjectSerializationFilter) origVal).getClass(), ((ClassObjectSerializationFilter) copyVal).getClass());
                        } else if (origVal instanceof List) {
                            List origList = (List) origVal;
                            if (((List) copyVal).isEmpty()) {
                                assertTrue("Original compared to Round-tripped " + cmdClass.getSimpleName() + "." + cmdField.getName(), origList.isEmpty());
                            } else {
                                if (origList.get(0) instanceof Setter) {
                                    for (Object obj : (List) origVal) {
                                        assertTrue("Expected a " + Setter.class.getSimpleName() + " instance (not " + obj.getClass().getSimpleName() + " in " + cmdClass.getSimpleName() + "." + cmdField.getName(), obj instanceof Setter);
                                        Iterator<Object> iter = ((List) copyVal).iterator();
                                        while (iter.hasNext()) {
                                            Setter copySetter = (Setter) iter.next();
                                            if (((Setter) obj).getAccessor().equals(copySetter.getAccessor())) {
                                                assertEquals("Original compared to Round-tripped " + cmdClass.getSimpleName() + "." + cmdField.getName(), ((Setter) obj).getValue(), copySetter.getValue());
                                                iter.remove();
                                            }
                                        }
                                    }
                                    assertTrue("Original compared to Round-tripped " + cmdClass.getSimpleName() + "." + cmdField.getName(), ((List) copyVal).isEmpty());
                                } else if (origList.get(0) instanceof Map) {
                                    Map copyMap = (Map) ((List) copyVal).get(0);
                                    for (Object entry : ((Map) origList.get(0)).entrySet()) {
                                        assertTrue("Original compared to Round-tripped " + cmdClass.getSimpleName() + "." + cmdField.getName(), (copyMap).containsKey(((Entry) entry).getKey()));
                                    }
                                }
                            }
                        } else {
                            assertTrue("Original compared to Round-tripped " + cmdClass.getSimpleName() + "." + cmdField.getName(), origVal.equals(copyVal));
                        }
                    }
                }
            }
        }
    }
    // verify that BatchExecutionCommandImpl.commands has been filled with all
    // of the different types
    Field commandsField = BatchExecutionCommandImpl.class.getDeclaredField("commands");
    XmlElements xmlElemsAnno = commandsField.getAnnotation(XmlElements.class);
    List<Class> cmdTypes = new ArrayList<Class>(xmlElemsAnno.value().length);
    for (XmlElement xmlElem : xmlElemsAnno.value()) {
        cmdTypes.add(xmlElem.type());
    }
    // already thoroughly tested..
    cmdTypes.remove(SignalEventCommand.class);
    // already thoroughly tested..
    cmdTypes.remove(StartProcessCommand.class);
    for (Command cmd : batchCmd.getCommands()) {
        cmdTypes.remove(cmd.getClass());
    }
// other tests for this as part of the REST integration tests..
}
Also used : DisconnectedFactHandle(org.drools.core.common.DisconnectedFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) FireAllRulesCommand(org.drools.core.command.runtime.rule.FireAllRulesCommand) AgendaGroupSetFocusCommand(org.drools.core.command.runtime.rule.AgendaGroupSetFocusCommand) GetObjectCommand(org.drools.core.command.runtime.rule.GetObjectCommand) ArrayList(java.util.ArrayList) ClearAgendaGroupCommand(org.drools.core.command.runtime.rule.ClearAgendaGroupCommand) GetObjectsCommand(org.drools.core.command.runtime.rule.GetObjectsCommand) ClearAgendaCommand(org.drools.core.command.runtime.rule.ClearAgendaCommand) ClearRuleFlowGroupCommand(org.drools.core.command.runtime.rule.ClearRuleFlowGroupCommand) GetGlobalCommand(org.drools.core.command.runtime.GetGlobalCommand) RuleNameEqualsAgendaFilter(org.drools.core.base.RuleNameEqualsAgendaFilter) RuleNameStartsWithAgendaFilter(org.drools.core.base.RuleNameStartsWithAgendaFilter) DeleteCommand(org.drools.core.command.runtime.rule.DeleteCommand) Field(java.lang.reflect.Field) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) XmlElements(javax.xml.bind.annotation.XmlElements) BatchExecutionCommandImpl(org.drools.core.command.runtime.BatchExecutionCommandImpl) AbortWorkItemCommand(org.drools.core.command.runtime.process.AbortWorkItemCommand) List(java.util.List) ArrayList(java.util.ArrayList) RuleNameEqualsAgendaFilter(org.drools.core.base.RuleNameEqualsAgendaFilter) RuleNameEndsWithAgendaFilter(org.drools.core.base.RuleNameEndsWithAgendaFilter) RuleNameStartsWithAgendaFilter(org.drools.core.base.RuleNameStartsWithAgendaFilter) AgendaFilter(org.kie.api.runtime.rule.AgendaFilter) RuleNameMatchesAgendaFilter(org.drools.core.base.RuleNameMatchesAgendaFilter) ClassObjectSerializationFilter(org.drools.core.ClassObjectSerializationFilter) RuleNameEndsWithAgendaFilter(org.drools.core.base.RuleNameEndsWithAgendaFilter) RuleNameMatchesAgendaFilter(org.drools.core.base.RuleNameMatchesAgendaFilter) Pattern(java.util.regex.Pattern) InsertElementsCommand(org.drools.core.command.runtime.rule.InsertElementsCommand) ModifyCommand(org.drools.core.command.runtime.rule.ModifyCommand) CompleteWorkItemCommand(org.drools.core.command.runtime.process.CompleteWorkItemCommand) FireUntilHaltCommand(org.drools.core.command.runtime.rule.FireUntilHaltCommand) SetGlobalCommand(org.drools.core.command.runtime.SetGlobalCommand) ClassObjectFilter(org.drools.core.ClassObjectFilter) ClearActivationGroupCommand(org.drools.core.command.runtime.rule.ClearActivationGroupCommand) FireAllRulesCommand(org.drools.core.command.runtime.rule.FireAllRulesCommand) GetObjectCommand(org.drools.core.command.runtime.rule.GetObjectCommand) ClearActivationGroupCommand(org.drools.core.command.runtime.rule.ClearActivationGroupCommand) GetObjectsCommand(org.drools.core.command.runtime.rule.GetObjectsCommand) ClearAgendaCommand(org.drools.core.command.runtime.rule.ClearAgendaCommand) Command(org.kie.api.command.Command) StartProcessCommand(org.drools.core.command.runtime.process.StartProcessCommand) AgendaGroupSetFocusCommand(org.drools.core.command.runtime.rule.AgendaGroupSetFocusCommand) SetGlobalCommand(org.drools.core.command.runtime.SetGlobalCommand) DeleteCommand(org.drools.core.command.runtime.rule.DeleteCommand) ClearAgendaGroupCommand(org.drools.core.command.runtime.rule.ClearAgendaGroupCommand) QueryCommand(org.drools.core.command.runtime.rule.QueryCommand) AbortWorkItemCommand(org.drools.core.command.runtime.process.AbortWorkItemCommand) ModifyCommand(org.drools.core.command.runtime.rule.ModifyCommand) InsertElementsCommand(org.drools.core.command.runtime.rule.InsertElementsCommand) FireUntilHaltCommand(org.drools.core.command.runtime.rule.FireUntilHaltCommand) CompleteWorkItemCommand(org.drools.core.command.runtime.process.CompleteWorkItemCommand) ClearRuleFlowGroupCommand(org.drools.core.command.runtime.rule.ClearRuleFlowGroupCommand) UpdateCommand(org.drools.core.command.runtime.rule.UpdateCommand) InsertObjectCommand(org.drools.core.command.runtime.rule.InsertObjectCommand) GetGlobalCommand(org.drools.core.command.runtime.GetGlobalCommand) SignalEventCommand(org.drools.core.command.runtime.process.SignalEventCommand) Setter(org.kie.api.command.Setter) QueryCommand(org.drools.core.command.runtime.rule.QueryCommand) XmlElement(javax.xml.bind.annotation.XmlElement) Map(java.util.Map) HashMap(java.util.HashMap) InsertObjectCommand(org.drools.core.command.runtime.rule.InsertObjectCommand) Test(org.junit.Test)

Aggregations

Field (java.lang.reflect.Field)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Pattern (java.util.regex.Pattern)2 XmlElement (javax.xml.bind.annotation.XmlElement)2 XmlElements (javax.xml.bind.annotation.XmlElements)2 ClassObjectFilter (org.drools.core.ClassObjectFilter)2 ClassObjectSerializationFilter (org.drools.core.ClassObjectSerializationFilter)2 RuleNameEndsWithAgendaFilter (org.drools.core.base.RuleNameEndsWithAgendaFilter)2 RuleNameEqualsAgendaFilter (org.drools.core.base.RuleNameEqualsAgendaFilter)2 RuleNameMatchesAgendaFilter (org.drools.core.base.RuleNameMatchesAgendaFilter)2 RuleNameStartsWithAgendaFilter (org.drools.core.base.RuleNameStartsWithAgendaFilter)2 BatchExecutionCommandImpl (org.drools.core.command.runtime.BatchExecutionCommandImpl)2 GetGlobalCommand (org.drools.core.command.runtime.GetGlobalCommand)2 SetGlobalCommand (org.drools.core.command.runtime.SetGlobalCommand)2 AbortWorkItemCommand (org.drools.core.command.runtime.process.AbortWorkItemCommand)2 CompleteWorkItemCommand (org.drools.core.command.runtime.process.CompleteWorkItemCommand)2 SignalEventCommand (org.drools.core.command.runtime.process.SignalEventCommand)2