@Test
void writeFile_preserve_order() throws IOException {

    Random random = new Random();
    for (int i = 0; i < 10; i++) {
        String testFile = TEST_FILE + i;
        //generates 20 beans between 0 and 20
        Map<String, TestBean> originalBeans = new LinkedHashMap<>();
        for (int j = 0; j < 20; j++) {
            int num = random.nextInt(20) + 10;
            originalBeans.put("bean_" + num, new TestBean("bean_" + num));
        }
        this.jsonTools.setOutputType(YML_FILE);
        WriterHelper.writeFile(testProjectDir, documentFileUtils, jsonTools, originalBeans, testFile, TestBean.class);
        // check file existing and permissions
        final File propertiesFile = checkFileBasics(testFile, YML_FILE);
        // read and check contents
        final LinkedHashMap<String, TestBean> originalReloaded = jsonTools.getObjectMapper().readValue(propertiesFile, TestBean.MAPPING);
        // validate the order they were written in
        Iterator<Map.Entry<String, TestBean>> reloadedIterator = originalReloaded.entrySet().iterator();
        originalBeans.forEach((b, v) -> assertEquals(reloadedIterator.next().getKey(), b));
        //generates 20 beans between 0 and 40
        Map<String, TestBean> newBeans = new LinkedHashMap<>();
        for (int j = 0; j < 20; j++) {
            int num = random.nextInt(40);
            newBeans.put("bean_" + num, new TestBean("bean_" + num));
        }
        //write new beans
        WriterHelper.writeFile(testProjectDir, documentFileUtils, jsonTools, newBeans, testFile, TestBean.class);
        // read and check contents
        final LinkedHashMap<String, TestBean> mergedBeans = jsonTools.getObjectMapper().readValue(propertiesFile, TestBean.MAPPING);
        //validate that original order is preserved and that new beans are added to the end
        Iterator<Map.Entry<String, TestBean>> mergedBeanIterator = mergedBeans.entrySet().iterator();
        originalBeans.forEach((b, v) -> assertEquals(mergedBeanIterator.next().getKey(), b));
        newBeans.entrySet().stream().filter(e -> !originalBeans.containsKey(e.getKey())).forEach(e -> assertEquals(mergedBeanIterator.next().getKey(), e.getKey()));
    }
}