MetadataServiceImpl implements MetadataServerSideService { @Override public Path saveMetadata(final Path path, final Metadata metadata, final String comment) { try (final InputStream inputStream = ioService.newInputStream(Paths.convert(path), StandardOpenOption.READ)) { return Paths.convert(ioService.write(Paths.convert(path), IOUtils.toByteArray(inputStream), setUpAttributes(path, metadata), commentedOptionFactory.makeCommentedOption(comment))); } catch (IOException e) { throw ExceptionUtilities.handleException(e); } } MetadataServiceImpl(); @Inject  MetadataServiceImpl(@Named("ioStrategy") IOService ioService,
                               @Named("configIO") IOService configIOService,
                               CommentedOptionFactory commentedOptionFactory,
                               SessionInfo sessionInfo); @Override Path saveMetadata(final Path path,
                             final Metadata metadata,
                             final String comment); @Override Metadata getMetadata(final Path pathToResource); @Override Metadata getMetadata(org.uberfire.java.nio.file.Path path); @Override List<String> getTags(final Path resource); @Override List<String> getTags(final org.uberfire.java.nio.file.Path resource); @Override Map<String, Object> configAttrs(final Map<String, Object> _attrs,
                                           final Metadata metadata); @Override Map<String, Object> setUpAttributes(final Path path,
                                               final Metadata metadata);  }
@Test public void testSaveMetaData() throws IOException { final InputStream is = spy(new ByteArrayInputStream("hello".getBytes(StandardCharsets.UTF_8))); doReturn(is).when(ioService).newInputStream(any(), any()); final CommentedOption comment = new CommentedOption("comment"); doReturn(comment).when(commentedOptionFactory).makeCommentedOption("comment"); service.saveMetadata(Paths.convert(path), new Metadata(), "comment"); verify(ioService).write(any(org.uberfire.java.nio.file.Path.class), any(byte[].class), anyMap(), eq(comment)); verify(is).close(); }
@Test(expected = GenericPortableException.class) public void testSaveMetaDataException() throws IOException { final InputStream is = new ByteArrayInputStream("hello".getBytes(StandardCharsets.UTF_8)); try { doReturn(is).when(ioService).newInputStream(any(), any()); doThrow(IOException.class).when(ioService).write(any(org.uberfire.java.nio.file.Path.class), any(byte[].class), anyMap(), any()); service.saveMetadata(Paths.convert(path), new Metadata(), "comment"); } finally { is.close(); } }