@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>(); 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)); allowing(tabMapper).findById(tabId); will(returnValue(tab)); allowing(gadgetDefinitionMapper).findByUrl(GADGET_DEF_URL); will(returnValue(gadgetDefinition)); allowing(gadgetDefinition).getId(); will(returnValue(gadgetDefinitionId)); allowing(tab).getTabLayout(); will(returnValue(tabLayout)); Gadget gadget0 = context.mock(Gadget.class, "gadget0"); exactly(1).of(gadget0).getZoneNumber(); will(returnValue(0)); never(gadget0).getZoneIndex(); Gadget gadget1 = context.mock(Gadget.class, "gadget1"); exactly(1).of(gadget1).getZoneNumber(); will(returnValue(tabLayout.getNumberOfZones() - 1)); Gadget gadget2 = context.mock(Gadget.class, "gadget2"); exactly(1).of(gadget2).getZoneNumber(); will(returnValue(tabLayout.getNumberOfZones() - 1)); gadgets.add(gadget0); gadgets.add(gadget1); gadgets.add(gadget2); oneOf(gadget0).getZoneIndex(); will(returnValue(0)); oneOf(gadget0).setZoneIndex(1); oneOf(person).getId(); will(returnValue(0L)); atLeast(1).of(tab).getGadgets(); will(returnValue(gadgets)); allowing(actionContext).getPrincipal(); will(returnValue(principal)); allowing(principal).getAccountId(); will(returnValue("username")); allowing(personMapper).findByAccountId("username"); will(returnValue(person)); oneOf(person).getTabs(TabGroupType.START); will(returnValue(tabList)); exactly(1).of(tabMapper).flush(); allowing(deleteCacheKeysMapper).execute(with(any(Set.class))); } }); Gadget actual = (Gadget) sut.execute(actionContext); assertEquals(gadgetDefinitionId, (Long) actual.getGadgetDefinition().getId()); assertEquals(0, actual.getZoneIndex()); assertEquals(0, actual.getZoneNumber()); context.assertIsSatisfied(); }
@Override public Serializable execute(final PrincipalActionContext inActionContext) throws ExecutionException { AddGadgetRequest request = (AddGadgetRequest) inActionContext.getParams(); Long tabId = request.getTabId(); String gadgetDefUrl = request.getGadgetDefinitionUrl(); Person owner = personMapper.findByAccountId(inActionContext.getPrincipal().getAccountId()); Tab tab = owner.getTabs(TabGroupType.START).get(0); if (null != tabId) { tab = tabMapper.findById(tabId); } try { GadgetDefinition gadgetDef; if (gadgetDefUrl.startsWith("{") && gadgetDefUrl.substring(gadgetDefUrl.length() - 1).equals("}")) { gadgetDef = gadgetDefinitionMapper.findByUUID(gadgetDefUrl.substring(1, gadgetDefUrl.length() - 1)); } else { gadgetDef = gadgetDefinitionMapper.findByUrl(gadgetDefUrl); } if (null == gadgetDef) { throw new ExecutionException("Unable to instantiate gadgetDef."); } shiftGadget(tab.getGadgets()); Gadget gadget = new Gadget(gadgetDef, 0, 0, owner, request.getUserPrefs() == null ? "" : request .getUserPrefs()); tab.getGadgets().add(gadget); tabMapper.flush(); deleteKeysMapper.execute(Collections.singleton(CacheKeys.PERSON_PAGE_PROPERTIES_BY_ID + owner.getId())); return gadget; } catch (NoResultException ex) { log.error("Could not add Gadget because tab not found: " + owner.getUniqueId()); throw new ExecutionException("Could not add Gadget because tab not found: " + owner.getUniqueId()); } }
AddGadgetExecution implements ExecutionStrategy<PrincipalActionContext> { @Override public Serializable execute(final PrincipalActionContext inActionContext) throws ExecutionException { AddGadgetRequest request = (AddGadgetRequest) inActionContext.getParams(); Long tabId = request.getTabId(); String gadgetDefUrl = request.getGadgetDefinitionUrl(); Person owner = personMapper.findByAccountId(inActionContext.getPrincipal().getAccountId()); Tab tab = owner.getTabs(TabGroupType.START).get(0); if (null != tabId) { tab = tabMapper.findById(tabId); } try { GadgetDefinition gadgetDef; if (gadgetDefUrl.startsWith("{") && gadgetDefUrl.substring(gadgetDefUrl.length() - 1).equals("}")) { gadgetDef = gadgetDefinitionMapper.findByUUID(gadgetDefUrl.substring(1, gadgetDefUrl.length() - 1)); } else { gadgetDef = gadgetDefinitionMapper.findByUrl(gadgetDefUrl); } if (null == gadgetDef) { throw new ExecutionException("Unable to instantiate gadgetDef."); } shiftGadget(tab.getGadgets()); Gadget gadget = new Gadget(gadgetDef, 0, 0, owner, request.getUserPrefs() == null ? "" : request .getUserPrefs()); tab.getGadgets().add(gadget); tabMapper.flush(); deleteKeysMapper.execute(Collections.singleton(CacheKeys.PERSON_PAGE_PROPERTIES_BY_ID + owner.getId())); return gadget; } catch (NoResultException ex) { log.error("Could not add Gadget because tab not found: " + owner.getUniqueId()); throw new ExecutionException("Could not add Gadget because tab not found: " + owner.getUniqueId()); } } }
AddGadgetExecution implements ExecutionStrategy<PrincipalActionContext> { @Override public Serializable execute(final PrincipalActionContext inActionContext) throws ExecutionException { AddGadgetRequest request = (AddGadgetRequest) inActionContext.getParams(); Long tabId = request.getTabId(); String gadgetDefUrl = request.getGadgetDefinitionUrl(); Person owner = personMapper.findByAccountId(inActionContext.getPrincipal().getAccountId()); Tab tab = owner.getTabs(TabGroupType.START).get(0); if (null != tabId) { tab = tabMapper.findById(tabId); } try { GadgetDefinition gadgetDef; if (gadgetDefUrl.startsWith("{") && gadgetDefUrl.substring(gadgetDefUrl.length() - 1).equals("}")) { gadgetDef = gadgetDefinitionMapper.findByUUID(gadgetDefUrl.substring(1, gadgetDefUrl.length() - 1)); } else { gadgetDef = gadgetDefinitionMapper.findByUrl(gadgetDefUrl); } if (null == gadgetDef) { throw new ExecutionException("Unable to instantiate gadgetDef."); } shiftGadget(tab.getGadgets()); Gadget gadget = new Gadget(gadgetDef, 0, 0, owner, request.getUserPrefs() == null ? "" : request .getUserPrefs()); tab.getGadgets().add(gadget); tabMapper.flush(); deleteKeysMapper.execute(Collections.singleton(CacheKeys.PERSON_PAGE_PROPERTIES_BY_ID + owner.getId())); return gadget; } catch (NoResultException ex) { log.error("Could not add Gadget because tab not found: " + owner.getUniqueId()); throw new ExecutionException("Could not add Gadget because tab not found: " + owner.getUniqueId()); } } AddGadgetExecution(final TabMapper inTabMapper, final PersonMapper inPersonMapper,
            final GadgetDefinitionMapper inGadgetDefinitionMapper,
            final DomainMapper<Set<String>, Boolean> inDeleteKeysMapper); }
AddGadgetExecution implements ExecutionStrategy<PrincipalActionContext> { @Override public Serializable execute(final PrincipalActionContext inActionContext) throws ExecutionException { AddGadgetRequest request = (AddGadgetRequest) inActionContext.getParams(); Long tabId = request.getTabId(); String gadgetDefUrl = request.getGadgetDefinitionUrl(); Person owner = personMapper.findByAccountId(inActionContext.getPrincipal().getAccountId()); Tab tab = owner.getTabs(TabGroupType.START).get(0); if (null != tabId) { tab = tabMapper.findById(tabId); } try { GadgetDefinition gadgetDef; if (gadgetDefUrl.startsWith("{") && gadgetDefUrl.substring(gadgetDefUrl.length() - 1).equals("}")) { gadgetDef = gadgetDefinitionMapper.findByUUID(gadgetDefUrl.substring(1, gadgetDefUrl.length() - 1)); } else { gadgetDef = gadgetDefinitionMapper.findByUrl(gadgetDefUrl); } if (null == gadgetDef) { throw new ExecutionException("Unable to instantiate gadgetDef."); } shiftGadget(tab.getGadgets()); Gadget gadget = new Gadget(gadgetDef, 0, 0, owner, request.getUserPrefs() == null ? "" : request .getUserPrefs()); tab.getGadgets().add(gadget); tabMapper.flush(); deleteKeysMapper.execute(Collections.singleton(CacheKeys.PERSON_PAGE_PROPERTIES_BY_ID + owner.getId())); return gadget; } catch (NoResultException ex) { log.error("Could not add Gadget because tab not found: " + owner.getUniqueId()); throw new ExecutionException("Could not add Gadget because tab not found: " + owner.getUniqueId()); } } AddGadgetExecution(final TabMapper inTabMapper, final PersonMapper inPersonMapper,
            final GadgetDefinitionMapper inGadgetDefinitionMapper,
            final DomainMapper<Set<String>, Boolean> inDeleteKeysMapper); @Override Serializable execute(final PrincipalActionContext inActionContext); }
AddGadgetExecution implements ExecutionStrategy<PrincipalActionContext> { @Override public Serializable execute(final PrincipalActionContext inActionContext) throws ExecutionException { AddGadgetRequest request = (AddGadgetRequest) inActionContext.getParams(); Long tabId = request.getTabId(); String gadgetDefUrl = request.getGadgetDefinitionUrl(); Person owner = personMapper.findByAccountId(inActionContext.getPrincipal().getAccountId()); Tab tab = owner.getTabs(TabGroupType.START).get(0); if (null != tabId) { tab = tabMapper.findById(tabId); } try { GadgetDefinition gadgetDef; if (gadgetDefUrl.startsWith("{") && gadgetDefUrl.substring(gadgetDefUrl.length() - 1).equals("}")) { gadgetDef = gadgetDefinitionMapper.findByUUID(gadgetDefUrl.substring(1, gadgetDefUrl.length() - 1)); } else { gadgetDef = gadgetDefinitionMapper.findByUrl(gadgetDefUrl); } if (null == gadgetDef) { throw new ExecutionException("Unable to instantiate gadgetDef."); } shiftGadget(tab.getGadgets()); Gadget gadget = new Gadget(gadgetDef, 0, 0, owner, request.getUserPrefs() == null ? "" : request .getUserPrefs()); tab.getGadgets().add(gadget); tabMapper.flush(); deleteKeysMapper.execute(Collections.singleton(CacheKeys.PERSON_PAGE_PROPERTIES_BY_ID + owner.getId())); return gadget; } catch (NoResultException ex) { log.error("Could not add Gadget because tab not found: " + owner.getUniqueId()); throw new ExecutionException("Could not add Gadget because tab not found: " + owner.getUniqueId()); } } AddGadgetExecution(final TabMapper inTabMapper, final PersonMapper inPersonMapper,
            final GadgetDefinitionMapper inGadgetDefinitionMapper,
            final DomainMapper<Set<String>, Boolean> inDeleteKeysMapper); @Override Serializable execute(final PrincipalActionContext inActionContext);  }