@Test public void testInvalidTargetZone() { final List<Gadget> gadgetsList = new ArrayList<Gadget>(); gadgetsList.add(testGadgetOne); gadgetsList.add(testGadgetTwo); gadgetsList.add(testGadgetThree); final Layout currentLayout = Layout.TWOCOLUMN; setupGadgetExpectations(); context.checking(new Expectations() { { oneOf(mockedContext).getParams(); will(returnValue(requestContext)); oneOf(requestContext).getCurrentTabId(); will(returnValue(1L)); oneOf(requestContext).getGadgetId(); will(returnValue(2L)); oneOf(requestContext).getTargetZoneIndex(); will(returnValue(2)); oneOf(requestContext).getTargetZoneNumber(); will(returnValue(4)); oneOf(tabMapperMock).findByGadgetId(2L); will(returnValue(sourceTabTemplateMock)); oneOf(tabMapperMock).findById(1L); will(returnValue(tabMock)); oneOf(tabMock).getTemplate(); will(returnValue(destinationTabTemplateMock)); oneOf(destinationTabTemplateMock).getTabLayout(); will(returnValue(currentLayout)); allowing(mockedContext).getState(); } }); boolean exceptionWasThrown = false; try { sut.validate(mockedContext); } catch (ValidationException vex) { exceptionWasThrown = true; assertEquals(1, vex.getErrors().size()); assertTrue(vex.getErrors().containsKey("invalidZone")); } assertTrue(exceptionWasThrown); context.assertIsSatisfied(); }
@Override public void validate(final PrincipalActionContext inActionContext) throws ValidationException { ValidationException vex = new ValidationException(); ReorderGadgetRequest request = (ReorderGadgetRequest) inActionContext.getParams(); Long targetTabId = request.getCurrentTabId(); Long gadgetId = request.getGadgetId(); Integer targetZoneNumber = request.getTargetZoneNumber(); Integer targetZoneIndex = request.getTargetZoneIndex(); TabTemplate sourceTemplate = tabMapper.findByGadgetId(gadgetId); Tab destinationTab = tabMapper.findById(targetTabId); if (destinationTab == null) { vex.addError("invalidTab", "Destination zone does not exist."); throw vex; } TabTemplate destinationTemplate = destinationTab.getTemplate(); Layout destinationLayout = destinationTemplate.getTabLayout(); inActionContext.getState().put("destinationTemplate", destinationTemplate); inActionContext.getState().put("sourceTemplate", sourceTemplate); if (targetZoneNumber + 1 > destinationLayout.getNumberOfZones()) { vex.addError("invalidZone", "ReorderGadgetAction told to move a gadget to a nonexistent zone."); throw vex; } if (sourceTemplate == null) { vex.addError("invalidGadget", "Gadget to be moved is invalid."); throw vex; } HashMap<Integer, List<Integer>> gadgetZoneIndexes = new HashMap<Integer, List<Integer>>(); for (Gadget currentGadget : destinationTemplate.getGadgets()) { if (gadgetZoneIndexes.containsKey(currentGadget.getZoneNumber())) { gadgetZoneIndexes.get(currentGadget.getZoneNumber()).add(currentGadget.getZoneIndex()); } else { ArrayList<Integer> currentZoneIndexes = new ArrayList<Integer>(); currentZoneIndexes.add(currentGadget.getZoneIndex()); gadgetZoneIndexes.put(currentGadget.getZoneNumber(), currentZoneIndexes); } } if (gadgetZoneIndexes.containsKey(targetZoneNumber)) { List<Integer> targetZoneIndexes = gadgetZoneIndexes.get(targetZoneNumber); Collections.sort(targetZoneIndexes); if (targetZoneIndex > targetZoneIndexes.get(targetZoneIndexes.size() - 1) + 1 || targetZoneIndex < targetZoneIndexes.get(0) - 1) { vex.addError("invalidZoneIndex", "Destination zone index is outside of the acceptable bounds for the target zone index."); throw vex; } } else if (targetZoneIndex != 0) { vex.addError("invalidZoneIndex", "Destination zone index should be zero when moving to an empty zone."); } }
ReorderGadgetValidation implements ValidationStrategy<PrincipalActionContext> { @Override public void validate(final PrincipalActionContext inActionContext) throws ValidationException { ValidationException vex = new ValidationException(); ReorderGadgetRequest request = (ReorderGadgetRequest) inActionContext.getParams(); Long targetTabId = request.getCurrentTabId(); Long gadgetId = request.getGadgetId(); Integer targetZoneNumber = request.getTargetZoneNumber(); Integer targetZoneIndex = request.getTargetZoneIndex(); TabTemplate sourceTemplate = tabMapper.findByGadgetId(gadgetId); Tab destinationTab = tabMapper.findById(targetTabId); if (destinationTab == null) { vex.addError("invalidTab", "Destination zone does not exist."); throw vex; } TabTemplate destinationTemplate = destinationTab.getTemplate(); Layout destinationLayout = destinationTemplate.getTabLayout(); inActionContext.getState().put("destinationTemplate", destinationTemplate); inActionContext.getState().put("sourceTemplate", sourceTemplate); if (targetZoneNumber + 1 > destinationLayout.getNumberOfZones()) { vex.addError("invalidZone", "ReorderGadgetAction told to move a gadget to a nonexistent zone."); throw vex; } if (sourceTemplate == null) { vex.addError("invalidGadget", "Gadget to be moved is invalid."); throw vex; } HashMap<Integer, List<Integer>> gadgetZoneIndexes = new HashMap<Integer, List<Integer>>(); for (Gadget currentGadget : destinationTemplate.getGadgets()) { if (gadgetZoneIndexes.containsKey(currentGadget.getZoneNumber())) { gadgetZoneIndexes.get(currentGadget.getZoneNumber()).add(currentGadget.getZoneIndex()); } else { ArrayList<Integer> currentZoneIndexes = new ArrayList<Integer>(); currentZoneIndexes.add(currentGadget.getZoneIndex()); gadgetZoneIndexes.put(currentGadget.getZoneNumber(), currentZoneIndexes); } } if (gadgetZoneIndexes.containsKey(targetZoneNumber)) { List<Integer> targetZoneIndexes = gadgetZoneIndexes.get(targetZoneNumber); Collections.sort(targetZoneIndexes); if (targetZoneIndex > targetZoneIndexes.get(targetZoneIndexes.size() - 1) + 1 || targetZoneIndex < targetZoneIndexes.get(0) - 1) { vex.addError("invalidZoneIndex", "Destination zone index is outside of the acceptable bounds for the target zone index."); throw vex; } } else if (targetZoneIndex != 0) { vex.addError("invalidZoneIndex", "Destination zone index should be zero when moving to an empty zone."); } } }
ReorderGadgetValidation implements ValidationStrategy<PrincipalActionContext> { @Override public void validate(final PrincipalActionContext inActionContext) throws ValidationException { ValidationException vex = new ValidationException(); ReorderGadgetRequest request = (ReorderGadgetRequest) inActionContext.getParams(); Long targetTabId = request.getCurrentTabId(); Long gadgetId = request.getGadgetId(); Integer targetZoneNumber = request.getTargetZoneNumber(); Integer targetZoneIndex = request.getTargetZoneIndex(); TabTemplate sourceTemplate = tabMapper.findByGadgetId(gadgetId); Tab destinationTab = tabMapper.findById(targetTabId); if (destinationTab == null) { vex.addError("invalidTab", "Destination zone does not exist."); throw vex; } TabTemplate destinationTemplate = destinationTab.getTemplate(); Layout destinationLayout = destinationTemplate.getTabLayout(); inActionContext.getState().put("destinationTemplate", destinationTemplate); inActionContext.getState().put("sourceTemplate", sourceTemplate); if (targetZoneNumber + 1 > destinationLayout.getNumberOfZones()) { vex.addError("invalidZone", "ReorderGadgetAction told to move a gadget to a nonexistent zone."); throw vex; } if (sourceTemplate == null) { vex.addError("invalidGadget", "Gadget to be moved is invalid."); throw vex; } HashMap<Integer, List<Integer>> gadgetZoneIndexes = new HashMap<Integer, List<Integer>>(); for (Gadget currentGadget : destinationTemplate.getGadgets()) { if (gadgetZoneIndexes.containsKey(currentGadget.getZoneNumber())) { gadgetZoneIndexes.get(currentGadget.getZoneNumber()).add(currentGadget.getZoneIndex()); } else { ArrayList<Integer> currentZoneIndexes = new ArrayList<Integer>(); currentZoneIndexes.add(currentGadget.getZoneIndex()); gadgetZoneIndexes.put(currentGadget.getZoneNumber(), currentZoneIndexes); } } if (gadgetZoneIndexes.containsKey(targetZoneNumber)) { List<Integer> targetZoneIndexes = gadgetZoneIndexes.get(targetZoneNumber); Collections.sort(targetZoneIndexes); if (targetZoneIndex > targetZoneIndexes.get(targetZoneIndexes.size() - 1) + 1 || targetZoneIndex < targetZoneIndexes.get(0) - 1) { vex.addError("invalidZoneIndex", "Destination zone index is outside of the acceptable bounds for the target zone index."); throw vex; } } else if (targetZoneIndex != 0) { vex.addError("invalidZoneIndex", "Destination zone index should be zero when moving to an empty zone."); } } ReorderGadgetValidation(final TabMapper inTabMapper); }
ReorderGadgetValidation implements ValidationStrategy<PrincipalActionContext> { @Override public void validate(final PrincipalActionContext inActionContext) throws ValidationException { ValidationException vex = new ValidationException(); ReorderGadgetRequest request = (ReorderGadgetRequest) inActionContext.getParams(); Long targetTabId = request.getCurrentTabId(); Long gadgetId = request.getGadgetId(); Integer targetZoneNumber = request.getTargetZoneNumber(); Integer targetZoneIndex = request.getTargetZoneIndex(); TabTemplate sourceTemplate = tabMapper.findByGadgetId(gadgetId); Tab destinationTab = tabMapper.findById(targetTabId); if (destinationTab == null) { vex.addError("invalidTab", "Destination zone does not exist."); throw vex; } TabTemplate destinationTemplate = destinationTab.getTemplate(); Layout destinationLayout = destinationTemplate.getTabLayout(); inActionContext.getState().put("destinationTemplate", destinationTemplate); inActionContext.getState().put("sourceTemplate", sourceTemplate); if (targetZoneNumber + 1 > destinationLayout.getNumberOfZones()) { vex.addError("invalidZone", "ReorderGadgetAction told to move a gadget to a nonexistent zone."); throw vex; } if (sourceTemplate == null) { vex.addError("invalidGadget", "Gadget to be moved is invalid."); throw vex; } HashMap<Integer, List<Integer>> gadgetZoneIndexes = new HashMap<Integer, List<Integer>>(); for (Gadget currentGadget : destinationTemplate.getGadgets()) { if (gadgetZoneIndexes.containsKey(currentGadget.getZoneNumber())) { gadgetZoneIndexes.get(currentGadget.getZoneNumber()).add(currentGadget.getZoneIndex()); } else { ArrayList<Integer> currentZoneIndexes = new ArrayList<Integer>(); currentZoneIndexes.add(currentGadget.getZoneIndex()); gadgetZoneIndexes.put(currentGadget.getZoneNumber(), currentZoneIndexes); } } if (gadgetZoneIndexes.containsKey(targetZoneNumber)) { List<Integer> targetZoneIndexes = gadgetZoneIndexes.get(targetZoneNumber); Collections.sort(targetZoneIndexes); if (targetZoneIndex > targetZoneIndexes.get(targetZoneIndexes.size() - 1) + 1 || targetZoneIndex < targetZoneIndexes.get(0) - 1) { vex.addError("invalidZoneIndex", "Destination zone index is outside of the acceptable bounds for the target zone index."); throw vex; } } else if (targetZoneIndex != 0) { vex.addError("invalidZoneIndex", "Destination zone index should be zero when moving to an empty zone."); } } ReorderGadgetValidation(final TabMapper inTabMapper); @Override void validate(final PrincipalActionContext inActionContext); }
ReorderGadgetValidation implements ValidationStrategy<PrincipalActionContext> { @Override public void validate(final PrincipalActionContext inActionContext) throws ValidationException { ValidationException vex = new ValidationException(); ReorderGadgetRequest request = (ReorderGadgetRequest) inActionContext.getParams(); Long targetTabId = request.getCurrentTabId(); Long gadgetId = request.getGadgetId(); Integer targetZoneNumber = request.getTargetZoneNumber(); Integer targetZoneIndex = request.getTargetZoneIndex(); TabTemplate sourceTemplate = tabMapper.findByGadgetId(gadgetId); Tab destinationTab = tabMapper.findById(targetTabId); if (destinationTab == null) { vex.addError("invalidTab", "Destination zone does not exist."); throw vex; } TabTemplate destinationTemplate = destinationTab.getTemplate(); Layout destinationLayout = destinationTemplate.getTabLayout(); inActionContext.getState().put("destinationTemplate", destinationTemplate); inActionContext.getState().put("sourceTemplate", sourceTemplate); if (targetZoneNumber + 1 > destinationLayout.getNumberOfZones()) { vex.addError("invalidZone", "ReorderGadgetAction told to move a gadget to a nonexistent zone."); throw vex; } if (sourceTemplate == null) { vex.addError("invalidGadget", "Gadget to be moved is invalid."); throw vex; } HashMap<Integer, List<Integer>> gadgetZoneIndexes = new HashMap<Integer, List<Integer>>(); for (Gadget currentGadget : destinationTemplate.getGadgets()) { if (gadgetZoneIndexes.containsKey(currentGadget.getZoneNumber())) { gadgetZoneIndexes.get(currentGadget.getZoneNumber()).add(currentGadget.getZoneIndex()); } else { ArrayList<Integer> currentZoneIndexes = new ArrayList<Integer>(); currentZoneIndexes.add(currentGadget.getZoneIndex()); gadgetZoneIndexes.put(currentGadget.getZoneNumber(), currentZoneIndexes); } } if (gadgetZoneIndexes.containsKey(targetZoneNumber)) { List<Integer> targetZoneIndexes = gadgetZoneIndexes.get(targetZoneNumber); Collections.sort(targetZoneIndexes); if (targetZoneIndex > targetZoneIndexes.get(targetZoneIndexes.size() - 1) + 1 || targetZoneIndex < targetZoneIndexes.get(0) - 1) { vex.addError("invalidZoneIndex", "Destination zone index is outside of the acceptable bounds for the target zone index."); throw vex; } } else if (targetZoneIndex != 0) { vex.addError("invalidZoneIndex", "Destination zone index should be zero when moving to an empty zone."); } } ReorderGadgetValidation(final TabMapper inTabMapper); @Override void validate(final PrincipalActionContext inActionContext);  }