TemplateFileReader { public byte[] readTemplate(final String templatesName) { byte[] result; Map<String, byte[]> templatesMap = resourceHolder.getTemplates(); if (templatesMap.containsKey(templatesName)) { result = templatesMap.get(templatesName); } else { result = readTemplateFromFileSystem(templatesName); resourceHolder.addTemplate(templatesName, result); } return result; }  byte[] readTemplate(final String templatesName);  }
@Test public void testReadTemplateShouldReturnWithResourceWhenStubResourceHolderContainsTemplate() { given(stubResourceHolder.getTemplates()).willReturn(templatesMap); given(templatesMap.containsKey(RESOURCE_NAME)).willReturn(true); given(templatesMap.get(RESOURCE_NAME)).willReturn(RESOURCE_NAME.getBytes()); byte[] actual = underTest.readTemplate(RESOURCE_NAME); assertEquals(actual, RESOURCE_NAME.getBytes()); }
@Test public void testReadTemplateShouldReturnWithResourceWhenStubResourceHolderDoesntContainTemplateYet() throws IOException { given(stubResourceHolder.getTemplates()).willReturn(templatesMap); given(templatesMap.containsKey(RESOURCE_NAME)).willReturn(false); given(fileFactory.createFile(Mockito.anyString())).willReturn(file); given(fileUtils.getFileAsByteArray(file)).willReturn(RESOURCE_NAME.getBytes()); byte[] actual = underTest.readTemplate(RESOURCE_NAME); Mockito.verify(stubResourceHolder).addTemplate(RESOURCE_NAME, actual); assertEquals(actual, RESOURCE_NAME.getBytes()); }
@Test(expectedExceptions = DescriptorValidationFailedException.class) public void testReadTemplateShouldThrowExceptionWhenTheSpecifiedTemplateDoesntExist() throws IOException { given(stubResourceHolder.getTemplates()).willReturn(templatesMap); given(templatesMap.containsKey(RESOURCE_NAME)).willReturn(false); given(fileFactory.createFile(Mockito.anyString())).willReturn(file); given(fileUtils.getFileAsByteArray(file)).willThrow(new IOException()); underTest.readTemplate(RESOURCE_NAME); }