SessionDeserializer implements Deserializer<List<Session>, String> { @Override public Optional<List<Session>> deserialize(String data) { final ArrayList<Session> sessions = new ArrayList<>(); try { final JSONArray array = new JSONArray(data); for (int i = 0; i < array.length(); i++) { final JSONObject jsonObject = array.getJSONObject(i); String name = jsonObject.getString("name"); final ArrayList<FileToOpen> fileToOpens = new ArrayList<>(); final JSONArray filesArray = jsonObject.optJSONArray("filesToOpen"); if (filesArray != null){ for (int j = 0; j < filesArray.length(); j++) { final JSONObject filesToOpen = filesArray.getJSONObject(j); String uri = filesToOpen.getString("uri"); Level level = Level.parse(filesToOpen.getString("level")); OpenMode openMode = OpenMode.valueOf(filesToOpen.getString("openMode")); String logImporter = filesToOpen.optString("logImporter", null); fileToOpens.add(new FileToOpen(uri, openMode, level, Optional.ofNullable(logImporter))); } } sessions.add(new Session(name, fileToOpens)); } } catch (JSONException e) { LOGGER.error("Can't deserialize sessions: ", e); Optional.empty(); } LOGGER.info("Returning deserialized sessions: " + sessions.size()); return Optional.of(sessions); }  @Override Optional<List<Session>> deserialize(String data);  }
@Test(dataProvider = "serialize") public void testDeserialize(String testName, List<Session> sessionList, String jsonFileName) throws Exception { String json = IOUtils.toString(this.getClass().getResourceAsStream(jsonFileName), "UTF-8"); Optional<List<Session>> actual = new SessionDeserializer().deserialize(json); System.out.println(actual); assertTrue(actual.isPresent(), testName); actual.ifPresent(l -> { assertThat(l) .extracting("name") .containsOnly(sessionList.stream().map(Session::getName).toArray()); for (int i = 0; i < sessionList.size(); i++) { assertEquals(l.get(i).getFilesToOpen(), sessionList.get(i).getFilesToOpen(), "Files " + l.get(i).getFilesToOpen() + " not equal to " + sessionList.get(i).getFilesToOpen()); } }); }