@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; }
@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 testListItemsEncodingProblem() throws Exception { FSMenuService service = createMockedService(null); FormRepresentationDecoder decoder = EasyMock.createMock(FormRepresentationDecoder.class); FormEncodingFactory.register(FormEncodingFactory.getEncoder(), decoder); FormEncodingException exception = new FormEncodingException("Something going wrong"); EasyMock.expect(decoder.decodeMenuItemsMap(EasyMock.anyObject(String.class))).andThrow(exception).once(); EasyMock.replay(decoder); 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 FormEncodingException", cause instanceof FormEncodingException); } EasyMock.verify(decoder); } @Test
@Test public void testListItemsEncodingProblem() throws Exception { FSMenuService service = createMockedService(null); FormRepresentationDecoder decoder = EasyMock.createMock(FormRepresentationDecoder.class); FormEncodingFactory.register(FormEncodingFactory.getEncoder(), decoder); FormEncodingException exception = new FormEncodingException("Something going wrong"); EasyMock.expect(decoder.decodeMenuItemsMap(EasyMock.anyObject(String.class))).andThrow(exception).once(); EasyMock.replay(decoder); 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 FormEncodingException", cause instanceof FormEncodingException); } EasyMock.verify(decoder); } @Test