protected void removeFromMap(String groupName, MenuItemDescription item, Map<String, List<MenuItemDescription>> items) { String group = groupName == null ? "Custom" : groupName; List<MenuItemDescription> customItems = items.get(group); if (customItems == null) { customItems = new ArrayList<MenuItemDescription>(); } MenuItemDescription serverItem = null; for (MenuItemDescription subItem : customItems) { if (subItem.getName().equals(item.getName())) { serverItem = subItem; break; } } customItems.remove(serverItem); if (customItems.isEmpty()) { items.remove(group); } else { items.put(group, customItems); } }
@Override @SuppressWarnings("unchecked") public void setDataMap(Map<String, Object> data) { this.className = (String) data.get("className"); this.name = (String) data.get("name"); this.iconUrl = (String) data.get("iconUrl"); List<Object> effectsMap = (List<Object>) data.get("effects"); this.effects.clear(); if (effectsMap != null) { for (Object objEffect : effectsMap) { Map<String, Object> effectDataMap = (Map<String, Object>) objEffect; FormEffectDescription effect = new FormEffectDescription(); effect.setDataMap(effectDataMap); this.effects.add(effect); } } List<Object> allowedEventsList = (List<Object>) data.get("allowedEvents"); if (allowedEventsList != null) { this.allowedEvents.clear(); for (Object obj : allowedEventsList) { this.allowedEvents.add(obj.toString()); } } this.itemRepresentationMap = (Map<String, Object>) data.get("itemRepresentationMap"); }
@Override public Map<String, Object> getDataMap() { Map<String, Object> data = new HashMap<String, Object>(); data.put("className", this.className); data.put("name", this.name); data.put("itemRepresentationMap", itemRepresentationMap == null ? null : itemRepresentationMap); if (this.effects == null) { data.put("effects", null); } else { List<Object> effectsMap = new ArrayList<Object>(); for (FormEffectDescription effect : this.effects) { effectsMap.add(effect.getDataMap()); } data.put("effects", effectsMap); } data.put("allowedEvents", this.allowedEvents); data.put("iconUrl", this.iconUrl); return data; }
private void abstractTestSaveMenuItemProblem(final Class<?> exceptionType) throws Exception { FSMenuService service = createMockedService(exceptionType); MenuItemDescription sampleDescription = new MenuItemDescription(); sampleDescription.setAllowedEvents(new ArrayList<String>()); sampleDescription.setEffects(new ArrayList<FormEffectDescription>()); FormItemRepresentation item = MockFormHelper.createMockForm("form", "param1").getFormItems().iterator().next(); sampleDescription.setItemRepresentationMap(item.getDataMap()); sampleDescription.setIconUrl("https://www.google.com/images/srpr/logo3w.png"); sampleDescription.setName("name"); try { service.saveMenuItem("group", sampleDescription); fail("saveMenuItem shouldn't succeed"); } catch (MenuServiceException e) { assertNotNull("e shouldn't be null", e); Throwable cause = e.getCause(); assertNotNull("cause shouldn't be null", cause); assertTrue("cause should be a " + exceptionType.getName(), cause.getClass().equals(exceptionType)); } } @Test
@Override @SuppressWarnings("unchecked") public Map<String, List<MenuItemDescription>> decodeMenuItemsMap(String json) throws FormEncodingException { JsonObject jsonObj = new JsonParser().parse(json).getAsJsonObject(); Map<String, Object> dataMap = asMap(jsonObj); Map<String, List<MenuItemDescription>> retval = null; if (dataMap != null) { retval = new HashMap<String, List<MenuItemDescription>>(); for (Map.Entry<String, Object> entry : dataMap.entrySet()) { List<MenuItemDescription> itemsList = new ArrayList<MenuItemDescription>(); String key = entry.getKey(); Object obj = entry.getValue(); if (obj != null) { List<Object> itemsMapList = (List<Object>) obj; for (Object itemObj : itemsMapList) { Map<String, Object> itemDescMap = (Map<String, Object>) itemObj; MenuItemDescription desc = new MenuItemDescription(); desc.setDataMap(itemDescMap); itemsList.add(desc); } } retval.put(key, itemsList); } } return retval; }
@Override public void setDataMap(Map<String, Object> dataMap) throws FormEncodingException { FormBuilderDTOHelper helper = new FormBuilderDTOHelper(dataMap); this.html = helper.getString("html"); this.commandClass = helper.getString("commandClass"); List<FormBuilderDTOHelper> subItems = helper.getListOfDtoHelpers("subMenu"); this.subMenu.clear(); if (subItems != null) { for (FormBuilderDTOHelper subHelper : subItems) { MenuOptionDescription option = new MenuOptionDescription(); option.setDataMap(subHelper.getMap()); this.subMenu.add(option); } } } }
@Override public Map<String, Object> getDataMap() { FormBuilderDTOHelper helper = new FormBuilderDTOHelper(); helper.setString("html", html); helper.setString("commandClass", commandClass); if (subMenu != null) { List<Object> subItems = new ArrayList<Object>(); for (MenuOptionDescription option : subMenu) { subItems.add(option.getDataMap()); } helper.setList("subMenu", subItems); } return helper.getMap(); }
@Test public void testListValidationsOK() throws Exception { FSMenuService service = new FSMenuService(); List<ValidationDescription> validations = service.listValidations(); assertNotNull("validations shouldn't be null", validations); assertFalse("validations should'nt be empty", validations.isEmpty()); for (ValidationDescription desc : validations) { assertNotNull("validations shouldn't contain null elements", desc); assertNotNull("validation className shouldn't be null", desc.getClassName()); assertFalse("validation className shouldn't be empty", "".equals(desc.getClassName())); } }
@Override public String encodeMenuItemsMap(Map<String, List<MenuItemDescription>> items) throws FormEncodingException { Map<String, Object> dataMap = new HashMap<String, Object>(); if (items == null) { return "null"; } for (Map.Entry<String, List<MenuItemDescription>> entry : items.entrySet()) { List<Map<String, Object>> itemMap = new ArrayList<Map<String, Object>>(); for (MenuItemDescription desc : entry.getValue()) { itemMap.add(desc.getDataMap()); } dataMap.put(entry.getKey(), itemMap); } return jsonFromMap(dataMap); }
private void abstractTestSaveMenuItemProblem(final Class<?> exceptionType) throws Exception { FSMenuService service = createMockedService(exceptionType); MenuItemDescription sampleDescription = new MenuItemDescription(); sampleDescription.setAllowedEvents(new ArrayList<String>()); sampleDescription.setEffects(new ArrayList<FormEffectDescription>()); FormItemRepresentation item = MockFormHelper.createMockForm("form", "param1").getFormItems().iterator().next(); sampleDescription.setItemRepresentationMap(item.getDataMap()); sampleDescription.setIconUrl("https://www.google.com/images/srpr/logo3w.png"); sampleDescription.setName("name"); try { service.saveMenuItem("group", sampleDescription); fail("saveMenuItem shouldn't succeed"); } catch (MenuServiceException e) { assertNotNull("e shouldn't be null", e); Throwable cause = e.getCause(); assertNotNull("cause shouldn't be null", cause); assertTrue("cause should be a " + exceptionType.getName(), cause.getClass().equals(exceptionType)); } } @Test
@Override @SuppressWarnings("unchecked") public Map<String, List<MenuItemDescription>> decodeMenuItemsMap(String json) throws FormEncodingException { JsonObject jsonObj = new JsonParser().parse(json).getAsJsonObject(); Map<String, Object> dataMap = asMap(jsonObj); Map<String, List<MenuItemDescription>> retval = null; if (dataMap != null) { retval = new HashMap<String, List<MenuItemDescription>>(); for (Map.Entry<String, Object> entry : dataMap.entrySet()) { List<MenuItemDescription> itemsList = new ArrayList<MenuItemDescription>(); String key = entry.getKey(); Object obj = entry.getValue(); if (obj != null) { List<Object> itemsMapList = (List<Object>) obj; for (Object itemObj : itemsMapList) { Map<String, Object> itemDescMap = (Map<String, Object>) itemObj; MenuItemDescription desc = new MenuItemDescription(); desc.setDataMap(itemDescMap); itemsList.add(desc); } } retval.put(key, itemsList); } } return retval; }
@Override public void setDataMap(Map<String, Object> dataMap) throws FormEncodingException { FormBuilderDTOHelper helper = new FormBuilderDTOHelper(dataMap); this.html = helper.getString("html"); this.commandClass = helper.getString("commandClass"); List<FormBuilderDTOHelper> subItems = helper.getListOfDtoHelpers("subMenu"); this.subMenu.clear(); if (subItems != null) { for (FormBuilderDTOHelper subHelper : subItems) { MenuOptionDescription option = new MenuOptionDescription(); option.setDataMap(subHelper.getMap()); this.subMenu.add(option); } } } }
@Override @SuppressWarnings("unchecked") public void setDataMap(Map<String, Object> data) { this.className = (String) data.get("className"); this.name = (String) data.get("name"); this.iconUrl = (String) data.get("iconUrl"); List<Object> effectsMap = (List<Object>) data.get("effects"); this.effects.clear(); if (effectsMap != null) { for (Object objEffect : effectsMap) { Map<String, Object> effectDataMap = (Map<String, Object>) objEffect; FormEffectDescription effect = new FormEffectDescription(); effect.setDataMap(effectDataMap); this.effects.add(effect); } } List<Object> allowedEventsList = (List<Object>) data.get("allowedEvents"); if (allowedEventsList != null) { this.allowedEvents.clear(); for (Object obj : allowedEventsList) { this.allowedEvents.add(obj.toString()); } } this.itemRepresentationMap = (Map<String, Object>) data.get("itemRepresentationMap"); }
@Override public Map<String, Object> getDataMap() { FormBuilderDTOHelper helper = new FormBuilderDTOHelper(); helper.setString("html", html); helper.setString("commandClass", commandClass); if (subMenu != null) { List<Object> subItems = new ArrayList<Object>(); for (MenuOptionDescription option : subMenu) { subItems.add(option.getDataMap()); } helper.setList("subMenu", subItems); } return helper.getMap(); }
@Test public void testListValidationsOK() throws Exception { FSMenuService service = new FSMenuService(); List<ValidationDescription> validations = service.listValidations(); assertNotNull("validations shouldn't be null", validations); assertFalse("validations should'nt be empty", validations.isEmpty()); for (ValidationDescription desc : validations) { assertNotNull("validations shouldn't contain null elements", desc); assertNotNull("validation className shouldn't be null", desc.getClassName()); assertFalse("validation className shouldn't be empty", "".equals(desc.getClassName())); } }
@Override public Map<String, Object> getDataMap() { Map<String, Object> data = new HashMap<String, Object>(); data.put("className", this.className); data.put("name", this.name); data.put("itemRepresentationMap", itemRepresentationMap == null ? null : itemRepresentationMap); if (this.effects == null) { data.put("effects", null); } else { List<Object> effectsMap = new ArrayList<Object>(); for (FormEffectDescription effect : this.effects) { effectsMap.add(effect.getDataMap()); } data.put("effects", effectsMap); } data.put("allowedEvents", this.allowedEvents); data.put("iconUrl", this.iconUrl); return data; }
protected void removeFromMap(String groupName, MenuItemDescription item, Map<String, List<MenuItemDescription>> items) { String group = groupName == null ? "Custom" : groupName; List<MenuItemDescription> customItems = items.get(group); if (customItems == null) { customItems = new ArrayList<MenuItemDescription>(); } MenuItemDescription serverItem = null; for (MenuItemDescription subItem : customItems) { if (subItem.getName().equals(item.getName())) { serverItem = subItem; break; } } customItems.remove(serverItem); if (customItems.isEmpty()) { items.remove(group); } else { items.put(group, customItems); } }
@Override public String encodeMenuItemsMap(Map<String, List<MenuItemDescription>> items) throws FormEncodingException { Map<String, Object> dataMap = new HashMap<String, Object>(); if (items == null) { return "null"; } for (Map.Entry<String, List<MenuItemDescription>> entry : items.entrySet()) { List<Map<String, Object>> itemMap = new ArrayList<Map<String, Object>>(); for (MenuItemDescription desc : entry.getValue()) { itemMap.add(desc.getDataMap()); } dataMap.put(entry.getKey(), itemMap); } return jsonFromMap(dataMap); }
@Test public void testSaveMenuItemEncodingProblem() throws Exception { FSMenuService service = createMockedService(null); MenuItemDescription sampleDescription = new MenuItemDescription(); sampleDescription.setAllowedEvents(new ArrayList<String>()); sampleDescription.setEffects(new ArrayList<FormEffectDescription>()); FormItemRepresentation item = MockFormHelper.createMockForm("form", "param1").getFormItems().iterator().next(); sampleDescription.setItemRepresentationMap(item.getDataMap()); sampleDescription.setName("name"); FormRepresentationEncoder encoder = EasyMock.createMock(FormRepresentationEncoder.class); FormEncodingException exception = new FormEncodingException(); @SuppressWarnings("unchecked") Map<String, List<MenuItemDescription>> anyObject = EasyMock.anyObject(Map.class); EasyMock.expect(encoder.encodeMenuItemsMap(anyObject)).andThrow(exception).once(); FormEncodingFactory.register(encoder, FormEncodingFactory.getDecoder()); EasyMock.replay(encoder); try { service.saveMenuItem("group", sampleDescription); fail("saveMenuItem shouldn't succeed"); } catch (MenuServiceException e) { assertNotNull("e shouldn't be null", e); Throwable cause = e.getCause(); assertNotNull("cause shouldn't be null", cause); assertTrue("cause should be a FormEncodingException", cause instanceof FormEncodingException); } EasyMock.verify(encoder); } // THIS TEST REQUIRES THE REAL GWT Components so it should be executed in the showcase
@Test public void testSaveMenuItemEncodingProblem() throws Exception { FSMenuService service = createMockedService(null); MenuItemDescription sampleDescription = new MenuItemDescription(); sampleDescription.setAllowedEvents(new ArrayList<String>()); sampleDescription.setEffects(new ArrayList<FormEffectDescription>()); FormItemRepresentation item = MockFormHelper.createMockForm("form", "param1").getFormItems().iterator().next(); sampleDescription.setItemRepresentationMap(item.getDataMap()); sampleDescription.setName("name"); FormRepresentationEncoder encoder = EasyMock.createMock(FormRepresentationEncoder.class); FormEncodingException exception = new FormEncodingException(); @SuppressWarnings("unchecked") Map<String, List<MenuItemDescription>> anyObject = EasyMock.anyObject(Map.class); EasyMock.expect(encoder.encodeMenuItemsMap(anyObject)).andThrow(exception).once(); FormEncodingFactory.register(encoder, FormEncodingFactory.getDecoder()); EasyMock.replay(encoder); try { service.saveMenuItem("group", sampleDescription); fail("saveMenuItem shouldn't succeed"); } catch (MenuServiceException e) { assertNotNull("e shouldn't be null", e); Throwable cause = e.getCause(); assertNotNull("cause shouldn't be null", cause); assertTrue("cause should be a FormEncodingException", cause instanceof FormEncodingException); } EasyMock.verify(encoder); } // THIS TEST REQUIRES THE REAL GWT Components so it should be executed in the showcase