Home

Advertisement

Back on 2005 I wrote about an practical use of what Fowler describes as "Human Interface" operations.

Yesterday I was unit testing some code of my work and I started with a top down approach regarding the way I wanted to write the testing code.

Basically I wanted to have a collection of Command objects that would change my input instances (to a Rule Engine) so I could mix and match easily different updates to trigger different rules, at each unit test (lines 9-10).

I also wanted to make several requests to the Rule engine, before validate the results, since a lot of rules are just fired after other ones being fired on previous invocations (lines 12-13).

But what I wanted the most was to easily express the way I would get results from different iterations (using ranges); and summarize elements and apply operations on the resulting sets of data (lines 15-17).

01:public class TerminalModelTest extends CommandTests
02:{
03:    @Test
04:    public void execute()
05:    {
06:        Transaction t = new Transaction();
07:        t.setAuthorizationDate(new Date());
08:
09:        TransactionUpdateCommand mcc = new SetMccRamdomCommand(5001);
10:        TransactionUpdateCommand codePosPDV = new SetTerminalCodePosPDV("xxxxxx12");
11:        
12:        RuleServiceInvoker commands = new RuleServiceInvoker();
13:        CommandsResultSet results = commands.run(t, 10, mcc, codePosPDV);
14:
15:        assertTrue(results.iteration(0, 4).intProfileVar(3).incrementedEachTime(1));
16:        assertEquals(0, results.iteration(5).intProfileVar(3));
17:        assertTrue(results.iteration(6, 10).intProfileVar(3).incrementedEachTime(1));
18:    }
19:}

To better understand the assertions above one must know that some arrays of integers, double, Dates are sent back and forth to the Rule Engine, inside the engine the end user sees those array slots as proper named structured data, but at this level, where we poor developers reside they are just arrays, and the class that holds them all is the one called 'Profile', so now I can say that I wanted in the line 17:

  • Extract the results from the iterations (or requests) 6 to 10;
  • From those iterations work only with the integer array of values, specifically the ones on the position 3;
  • Check if the difference between each one of those are of 1;

All those methods in place I could start with their implementation, and bellow is the result (with the omission of some unrelated code ;):

Here is the code to run the commands upon the input instances and an special mention to the inner private class 'LocalReturn' just because java does not have tuples or return statements with multiple values (of course one can always return an array, but it's not that beautiful).

public class RuleServiceInvoker
{
    CommandsResultSet run(Transaction t, int times, Command... commands)
    {
        CommandsResultSet results = new CommandsResultSet();
        for (int i = 0; i < times; i++) {
            ...
            for (TransactionUpdateCommand command : commands) {
                command.execute(t);
            }

            input.setTransaction(t);
            LocalReturn ruleServiceReturn = invokeAndKeep[OmittedName2]Variables(input);
            results.add(ruleServiceReturn.output);
            ...
        }
        return results;
    }
    ...
    private class LocalReturn {
        [OmittedName1]Input input;
        [OmittedName1]Output output;
        public LocalReturn([OmittedName1]Input input, [OmittedName1]Output output) {
            this.input = input;
            this.output = output;
        }
    }
}

Bellow the 3 container classes, so I could work exactly with the data I needed for each assertion:

public class CommandsResultSet
{
    private List<ComandsOutputIteration> iterations = new LinkedList<ComandsOutputIteration>();

    public ComandsOutputIterationSet iteration(int start, int end)
    {
        ComandsOutputIterationSet set = new ComandsOutputIterationSet();
        for (int i = start; i < end; i++) {
            set.add(iterations.get(i));
        }
        return set;
    }

    public ComandsOutputIteration iteration(int idx)
    {
        return iterations.get(idx);
    }

    public void add([OmittedName1]Output output) {
        iterations.add(new ComandsOutputIteration(output));
    }
}

public class ComandsOutputIterationSet
{
    private List<ComandsOutputIteration> iterations = new LinkedList<ComandsOutputIteration>();

    public IntProfileVarSet intProfileVar(int idx)
    {
        IntProfileVarSet set = new IntProfileVarSet();
        for (ComandsOutputIteration it : iterations) {
            set.add(it.intProfileVar(idx));
        }
        return set;
    }

    public void add(ComandsOutputIteration iteration) { iterations.add(iteration); }
}

public class ComandsOutputIteration  
{
    private [OmittedName1]Output output;

    public ComandsOutputIteration([OmittedName1]Output output) {
        this.output = output;
    }

    public int intProfileVar(int idx) {
        return output.get[OmittedName2]ProfileVariables().getIntegerValues()[idx];
    }
}
Here an interesting spot where I could use a bit of recursion, at the 'diff' method:
public class IntProfileVarSet
{
    private List<Integer> ints = new LinkedList<Integer>();

    public boolean incrementedEachTime(int diff) {
        if (ints.size() < 2) {
            throw new RuntimeException("Less than 2 elements! No increment can be verified!");
        }
        return diff(0, 1, diff);
    }

    private boolean diff(int current, int next, int diff) {
        if (ints.get(next) - ints.get(current) == diff) {
            return next + 1 == ints.size() ? true : diff(next, next + 1, diff);
        }
        return false;
    }

    public void add(int intProfileVar) { ints.add(intProfileVar); }
}

This top down approach to design certain operation is most pleasant since you already start exactly where you want to get in the end :-)

*The [OmittedNameX]s where placed to protect the Business of this client.



Comment Form

From:
(will be screened)
Help(will be screened)
Identity URL: 
(will be screened)
Username:
Password:
Don't have an account? Create one now.
Subject:
No HTML allowed in subject
   Help
Message:
 
Notice! This user has turned on the option that logs your IP address when posting. Help

Latest Month

November 2009
S M T W T F S
1234567
891011121314
15161718192021
22232425262728
2930     

Tags

Powered by LiveJournal.com
Designed by Tiffany Chow