Generator { public void generate() throws GeneratorException { try { SkillModel skillModel = skillModelSupplier.getSkillModel(); if (!destdir.exists()) { destdir.mkdirs(); } for (Locale locale : locales) { File out = getDestFile(locale.toLanguageTag() + ".json"); writer.writeValue(out, renderer.render(skillModel, locale)); } } catch (IOException | RuntimeException ex) { throw new GeneratorException(ex); } } Generator(SkillModelSupplier skillModelSupplier, File destdir, List<Locale> locales);  Generator(ObjectWriter writer, SkillModelRenderer renderer, SkillModelSupplier skillModelSupplier, File destdir, List<Locale> locales); void generate();  }
@Test public void testWriteToEmptyDir() throws GeneratorException, IOException { Mockito.when(mockDir.exists()).thenReturn(false); underTest.generate(); Mockito.verify(mockDir).mkdirs(); Mockito.verify(mockWriter).writeValue(mockFile, mockModel); }
@Test public void testWriteToDir() throws GeneratorException, IOException { Mockito.when(mockDir.exists()).thenReturn(true); underTest.generate(); Mockito.verify(mockDir, Mockito.never()).mkdirs(); Mockito.verify(mockWriter).writeValue(mockFile, mockModel); }
@Test public void testLocales() throws GeneratorException, IOException { File mockEnFile = Mockito.mock(File.class); File mockFrFile = Mockito.mock(File.class); Generator generator = new Generator(mockWriter, mockRenderer, mockApplication, mockDir, Arrays.asList(en_US, fr_FR)) { @Override protected File getDestFile(String name) { if (name.equals("en-US.json")) return mockEnFile; if (name.equals("fr-FR.json")) return mockFrFile; fail("expected en-US.json or fr-FR.json"); return null; } }; InteractionModelEnvelope mockEnModel = InteractionModelEnvelope.builder() .withInteractionModel(InteractionModel.builder().build()) .build(); InteractionModelEnvelope mockFrModel = InteractionModelEnvelope.builder().build(); Mockito.when(mockRenderer.render(mockSkill, en_US)).thenReturn(mockEnModel); Mockito.when(mockRenderer.render(mockSkill, fr_FR)).thenReturn(mockFrModel); Mockito.when(mockDir.exists()) .thenReturn(true) .thenReturn(false); generator.generate(); Mockito.verify(mockWriter).writeValue(mockEnFile, mockEnModel); Mockito.verify(mockWriter).writeValue(mockFrFile, mockFrModel); }
@Test public void testDefaultToAllInvocationNameLocales() throws GeneratorException, IOException { File mockEnFile = Mockito.mock(File.class); File mockFrFile = Mockito.mock(File.class); Generator generator = new Generator(mockWriter, mockRenderer, mockApplication, mockDir, null) { @Override protected File getDestFile(String name) { if (name.equals("en-US.json")) return mockEnFile; if (name.equals("fr-FR.json")) return mockFrFile; fail("expected en-US.json or fr-FR.json"); return null; } }; InteractionModelEnvelope mockEnModel = InteractionModelEnvelope.builder() .withInteractionModel(InteractionModel.builder().build()) .build(); InteractionModelEnvelope mockFrModel = InteractionModelEnvelope.builder().build(); Mockito.when(mockRenderer.render(mockSkill, en_US)).thenReturn(mockEnModel); Mockito.when(mockRenderer.render(mockSkill, fr_FR)).thenReturn(mockFrModel); Mockito.when(mockDir.exists()) .thenReturn(true) .thenReturn(false); generator.generate(); Mockito.verify(mockWriter).writeValue(mockEnFile, mockEnModel); Mockito.verify(mockWriter).writeValue(mockFrFile, mockFrModel); }
@Test(expected = GeneratorException.class) public void testIOExceptionOnWrite() throws GeneratorException, IOException { Mockito.doThrow(new IOException()).when(mockWriter).writeValue(mockFile, mockModel); Mockito.when(mockDir.exists()).thenReturn(true); underTest.generate(); Mockito.verify(mockWriter).writeValue(mockFile, mockModel); }
@Test(expected = GeneratorException.class) public void testRuntimeExceptionOnRender() throws GeneratorException, IOException { Mockito.when(mockRenderer.render(mockSkill, en_US)).thenThrow(new IllegalArgumentException("test")); Mockito.when(mockDir.exists()).thenReturn(true); underTest.generate(); Mockito.verify(mockWriter).writeValue(mockFile, mockModel); }