LibrarySpecificationReader { public static Optional<LibrarySpecification> readSpecification(final File file) { if (file == null || !file.isFile() || !file.exists()) { return Optional.empty(); } try { final JAXBContext jaxbContext = JAXBContext.newInstance(LibrarySpecification.class); final Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); final LibrarySpecification spec = (LibrarySpecification) jaxbUnmarshaller.unmarshal(file); if (spec.getSpecificationVersion() >= 2) { spec.getKeywordsStream() .filter(kwSpec -> kwSpec.getDeprecatedState() == null) .forEach(kwSpec -> kwSpec.setDeprecated(false)); } return Optional.ofNullable(spec); } catch (final JAXBException e) { return Optional.empty(); } }  static Optional<LibrarySpecification> readSpecification(final File file);  }
@Test public void testReadingIncorrectFile() throws Exception { assertThat(LibrarySpecificationReader.readSpecification(null)).isNotPresent(); final File folder = new File(tempDir, "folder"); folder.mkdir(); assertThat(LibrarySpecificationReader.readSpecification(folder)).isNotPresent(); assertThat(LibrarySpecificationReader.readSpecification(new File("not_existing_file"))).isNotPresent(); final File file = new File(tempDir, "file"); file.createNewFile(); assertThat(LibrarySpecificationReader.readSpecification(file)).isNotPresent(); }
@Test public void testReadingCorrectFile() throws Exception { final File file = new File(tempDir, "libspec"); final String content = "<keywordspec name=\"TestLib\" format=\"ROBOT\">" + "<version>1.0</version><scope>global</scope>" + "<doc>Documentation for test library ``TestLib``.</doc>" + "<kw name=\"Some Keyword\"><arguments><arg>a</arg><arg>b</arg></arguments><doc></doc></kw>" + "</keywordspec>"; Files.write(content.getBytes(), file); assertThat(LibrarySpecificationReader.readSpecification(file)).hasValueSatisfying(spec -> { assertThat(spec.getConstructor()).isNull(); assertThat(spec.getDescriptor()).isNull(); assertThat(spec.isModified()).isFalse(); assertThat(spec.getName()).isEqualTo("TestLib"); assertThat(spec.getFormat()).isEqualTo("ROBOT"); assertThat(spec.getVersion()).isEqualTo("1.0"); assertThat(spec.getScope()).isEqualTo("global"); assertThat(spec.getDocumentation()).isEqualTo("Documentation for test library ``TestLib``."); assertThat(spec.getKeywordNames()).containsExactly("Some Keyword"); }); }