@Override public List<MenuOptionDescription> listOptions() throws MenuServiceException { Gson gson = new Gson(); List<MenuOptionDescription> retval = new ArrayList<MenuOptionDescription>(); try { URL url = asURL("/menuOptions.json"); retval = gson.fromJson(createReader(url), new TypeToken<List<MenuOptionDescription>>(){}.getType()); } catch (URISyntaxException e) { throw new MenuServiceException("Problem finding menu options json file", e); } catch (FileNotFoundException e) { throw new MenuServiceException("No menu options json file found", e); } catch (Exception e) { throw new MenuServiceException("Unexpected error", e); } return retval; }
@Override public void saveMenuItem(String groupName, MenuItemDescription item) throws MenuServiceException { Map<String, List<MenuItemDescription>> items = listMenuItems(); addToMap(groupName, item, items); writeMenuItems(items); }
@Override public void deleteMenuItem(String groupName, MenuItemDescription item) throws MenuServiceException { Map<String, List<MenuItemDescription>> items = listMenuItems(); removeFromMap(groupName, item, items); writeMenuItems(items); }
private void writeMenuItems(Map<String, List<MenuItemDescription>> items) throws MenuServiceException { try { FormRepresentationEncoder encoder = FormEncodingFactory.getEncoder(); String json = encoder.encodeMenuItemsMap(items); URL url = asURL("/menuItems.json"); writeToURL(url, json); } catch (FormEncodingException e) { throw new MenuServiceException("Problem transforming menu items to json", e); } catch (URISyntaxException e) { throw new MenuServiceException("Problem finding menu items json file", e); } catch (FileNotFoundException e) { throw new MenuServiceException("No menu items json file found", e); } catch (IOException e) { throw new MenuServiceException("Problem writing menu items json file", e); } catch (Exception e) { throw new MenuServiceException("Unexpected error", e); } }
@Override public Map<String, List<MenuItemDescription>> listMenuItems() throws MenuServiceException { Map<String, List<MenuItemDescription>> retval = null; try { FormRepresentationDecoder decoder = FormEncodingFactory.getDecoder(); URL url = asURL("/menuItems.json"); String json = readURL(url); retval = decoder.decodeMenuItemsMap(json); } catch (FormEncodingException e) { throw new MenuServiceException("Problem parsing menu items json file", e); } catch (URISyntaxException e) { throw new MenuServiceException("Problem finding menu items json file", e); } catch (FileNotFoundException e) { throw new MenuServiceException("No menu items json file found", e); } catch (IOException e) { throw new MenuServiceException("Problem reading menu items json file", e); } catch (Exception e) { throw new MenuServiceException("Unexpected error", e); } return retval; }
@Test public void testListItemsOK() throws Exception { FSMenuService service = new FSMenuService(); Map<String, List<MenuItemDescription>> items = service.listMenuItems(); assertNotNull("items shouldn't be null", items); assertFalse("items shouldn't be empty", items.isEmpty()); for (String key : items.keySet()) { assertNotNull("items of key " + key + " shouldn't be null", items.get(key)); assertFalse("items of key " + key + " shouldn't be empty", items.get(key).isEmpty()); } } @Test
private void abstractTestListItemsProblem(final Class<?> exceptionType) throws Exception { FSMenuService service = createMockedService(exceptionType); try { service.listMenuItems(); fail("listOptions 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
@Test public void testGetFormBuilderProperties() throws Exception { FSMenuService service = new FSMenuService(); Map<String, String> props = service.getFormBuilderProperties(); assertNotNull("props shouldn't be null", props); assertFalse("props shouldn't be empty", props.isEmpty()); }
@Test public void testListOptionsOK() throws Exception { FSMenuService service = new FSMenuService(); List<MenuOptionDescription> options = service.listOptions(); assertNotNull("options shouldn't be null", options); assertFalse("options shouldn't be empty", options.isEmpty()); } @Test
@Override protected void writeToURL(URL url, String json) throws FileNotFoundException, IOException { if (exceptionType != null) { if (exceptionType.equals(FileNotFoundException.class)) throw new FileNotFoundException(url.toExternalForm()); if (exceptionType.equals(IOException.class)) throw new IOException(url.toExternalForm()); throw new NullPointerException(); } super.writeToURL(url, json); } };
@Override protected URL asURL(String path) throws URISyntaxException { if (exceptionType != null && exceptionType.equals(URISyntaxException.class)) throw new URISyntaxException(path, "mocking"); return super.asURL(path); } @Override
@Override protected Reader createReader(URL url) throws FileNotFoundException, IOException { if (exceptionType != null) { if (exceptionType.equals(FileNotFoundException.class)) throw new FileNotFoundException(url.toExternalForm()); if (exceptionType.equals(IOException.class)) throw new IOException(url.toExternalForm()); throw new NullPointerException(); } return super.createReader(url); } @Override
@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 protected String readURL(URL url) throws FileNotFoundException, IOException { if (exceptionType != null) { if (exceptionType.equals(FileNotFoundException.class)) throw new FileNotFoundException(url.toExternalForm()); if (exceptionType.equals(IOException.class)) throw new IOException(url.toExternalForm()); throw new NullPointerException(); } return super.readURL(url); } @Override
private void abstractTestListOptionsProblem(final Class<?> exceptionType) throws Exception { FSMenuService service = createMockedService(exceptionType); try { service.listOptions(); fail("listOptions 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)); } }
private void abstractTestListValidationsProblem(final Class<?> exceptionType) throws Exception { FSMenuService service = createMockedService(exceptionType); try { service.listValidations(); fail("listOptions 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 public void deleteMenuItem(String groupName, MenuItemDescription item) throws MenuServiceException { Map<String, List<MenuItemDescription>> items = listMenuItems(); removeFromMap(groupName, item, items); writeMenuItems(items); }
private void writeMenuItems(Map<String, List<MenuItemDescription>> items) throws MenuServiceException { try { FormRepresentationEncoder encoder = FormEncodingFactory.getEncoder(); String json = encoder.encodeMenuItemsMap(items); URL url = asURL("/menuItems.json"); writeToURL(url, json); } catch (FormEncodingException e) { throw new MenuServiceException("Problem transforming menu items to json", e); } catch (URISyntaxException e) { throw new MenuServiceException("Problem finding menu items json file", e); } catch (FileNotFoundException e) { throw new MenuServiceException("No menu items json file found", e); } catch (IOException e) { throw new MenuServiceException("Problem writing menu items json file", e); } catch (Exception e) { throw new MenuServiceException("Unexpected error", e); } }
@Override public Map<String, List<MenuItemDescription>> listMenuItems() throws MenuServiceException { Map<String, List<MenuItemDescription>> retval = null; try { FormRepresentationDecoder decoder = FormEncodingFactory.getDecoder(); URL url = asURL("/menuItems.json"); String json = readURL(url); retval = decoder.decodeMenuItemsMap(json); } catch (FormEncodingException e) { throw new MenuServiceException("Problem parsing menu items json file", e); } catch (URISyntaxException e) { throw new MenuServiceException("Problem finding menu items json file", e); } catch (FileNotFoundException e) { throw new MenuServiceException("No menu items json file found", e); } catch (IOException e) { throw new MenuServiceException("Problem reading menu items json file", e); } catch (Exception e) { throw new MenuServiceException("Unexpected error", e); } return retval; }
@Test public void testListItemsOK() throws Exception { FSMenuService service = new FSMenuService(); Map<String, List<MenuItemDescription>> items = service.listMenuItems(); assertNotNull("items shouldn't be null", items); assertFalse("items shouldn't be empty", items.isEmpty()); for (String key : items.keySet()) { assertNotNull("items of key " + key + " shouldn't be null", items.get(key)); assertFalse("items of key " + key + " shouldn't be empty", items.get(key).isEmpty()); } } @Test