@Test
public final void testExecuteValid() throws Exception
{
final Long tabId = 100L;
final Long gadgetDefinitionId = 9928L;
final Layout tabLayout = Layout.THREECOLUMNLEFTWIDEHEADER;
final Person person = context.mock(Person.class);
final List<Tab> tabList = Collections.singletonList(tab);
final List<Gadget> gadgets = new ArrayList<Gadget>();
// Set up expectations
context.checking(new Expectations()
{
{
allowing(actionContext).getParams();
will(returnValue(gadgetRequest));
allowing(gadgetRequest).getTabId();
will(returnValue(tabId));
allowing(gadgetRequest).getGadgetDefinitionUrl();
will(returnValue(GADGET_DEF_URL));
allowing(gadgetRequest).getUserPrefs();
will(returnValue(null));
// action has to load the tab by ID
allowing(tabMapper).findById(tabId);
will(returnValue(tab));
// then find-or-create the gadget definition
allowing(gadgetDefinitionMapper).findByUrl(GADGET_DEF_URL);
will(returnValue(gadgetDefinition));
// gadget definition ID is 9928
allowing(gadgetDefinition).getId();
will(returnValue(gadgetDefinitionId));
// and check what the last zone number is
allowing(tab).getTabLayout();
will(returnValue(tabLayout));
// setup the list of mocked gadgets
// ----------------
// -- Gadget #0 - zone 0, index 0
Gadget gadget0 = context.mock(Gadget.class, "gadget0");
exactly(1).of(gadget0).getZoneNumber();
will(returnValue(0));
// should never have to ask this gadget for its zone index
never(gadget0).getZoneIndex();
// ----------------
// ----------------
// -- Gadget #1 - <LAST ZONE>, index 0
Gadget gadget1 = context.mock(Gadget.class, "gadget1");
exactly(1).of(gadget1).getZoneNumber();
will(returnValue(tabLayout.getNumberOfZones() - 1));
// ----------------
// -- Gadget #2 - <LAST ZONE>, index 1
Gadget gadget2 = context.mock(Gadget.class, "gadget2");
// set the zone number - it MUST be requested once
exactly(1).of(gadget2).getZoneNumber();
will(returnValue(tabLayout.getNumberOfZones() - 1));
// add the gadgets to the collection
gadgets.add(gadget0);
gadgets.add(gadget1);
gadgets.add(gadget2);