### Question:
PublicationReader { public <T> T read(String f, Class<T> classOfT) throws StageConfigNotFoundException, StageConfigFileInvalidFormatException, StageConfigFileIOException { try { final Reader reader = new FileReader(f); return read(reader, classOfT); } catch (JsonIOException e) { throw new StageConfigFileIOException(e.getMessage(), e); } catch (FileNotFoundException e) { throw new StageConfigNotFoundException(e.getMessage(), e); } catch (JsonSyntaxException e) { throw new StageConfigFileInvalidFormatException(e.getMessage(), e); } }  T read(String f, Class<T> classOfT); T read(Reader r, Class<T> classOfT);  }### Answer:
@Test(expected = StageConfigFileInvalidFormatException.class) public void testPublicationReaderWithWrongFormattedFileShouldThrowException() throws Exception { String filePath = "src/test/resources/invalid.json"; assertTrue(new File(filePath).exists()); new PublicationReader().read(filePath, Publication.class); }
@Test public void testReaderWithUsernameAndPasswordFromSystemPropertiesShouldSuccess() throws StageConfigFileIOException, StageConfigNotFoundException, StageConfigFileInvalidFormatException { String filePath = "src/test/resources/stage_config.json"; System.clearProperty("axway.username"); System.clearProperty("axway.password"); assertTrue(new File(filePath).exists()); String username = "property username"; String password = "property password"; System.setProperty("axway.username", username); System.setProperty("axway.password", password); final Publication publication = new PublicationReader().read(filePath, Publication.class); assertThat(publication.getUsername(), is(username)); assertThat(publication.getPassword(), is(password)); }
@Test public void testReaderWithUsernameAndPasswordInConfigFileShouldSuccess() { final Publication publication = new Publication().setPort(443).setHost("Test Host").setClientAppsMode(Publication.MODE.IGNORE) .setUsername("abcd").setPassword("efgh").setForce(false).setIgnoreAdminAccount(false) .setIgnoreQuotas(true).setClientOrgsMode(Publication.MODE.IGNORE).setApis( Collections.singletonList(new Api().setApiSpecification("api spec").setApiConfig("apiconfig"))); final String publicationString1 = new Gson().toJson(publication); StringReader stringReader = new StringReader(publicationString1); final Publication publication2 = new PublicationReader().read(stringReader, Publication.class); assertThat(publication2.getUsername(), is(publication.getUsername())); assertThat(publication2.getPassword(), is(publication.getPassword())); }
@Test public void testPublicationReader() throws Exception { final String filePath = "src/test/resources/stage_config.json"; final Publication readObject = new PublicationReader().read(filePath, Publication.class); assertThat(readObject, notNullValue()); assertThat(readObject.getHost(), is("https: assertThat(readObject.getPort(), is(443)); assertThat(readObject.getApis().size(), is(2)); readObject.getApis().forEach(e -> { assertThat(e.getApiConfig(), notNullValue()); assertThat(e.getApiSpecification(), notNullValue()); }); }
@Test(expected = StageConfigNotFoundException.class) public void testPublicationReaderWithNonExistingFileShouldThrowException() throws StageConfigFileIOException, StageConfigNotFoundException, StageConfigFileInvalidFormatException { final String filePath = "not_exists.json"; assertFalse(new File(filePath).exists()); new PublicationReader().read(filePath, Publication.class); }