SystemFlagsDataArchive { public static SystemFlagsDataArchive fromDirectory(Path directory) { Path root = directory.toAbsolutePath(); Path flagsDirectory = directory.resolve("flags"); if (!Files.isDirectory(flagsDirectory)) { throw new IllegalArgumentException("Sub-directory 'flags' does not exist: " + flagsDirectory); } try (Stream<Path> directoryStream = Files.walk(root)) { Builder builder = new Builder(); directoryStream.forEach(absolutePath -> { Path relativePath = root.relativize(absolutePath); if (!Files.isDirectory(absolutePath) && relativePath.startsWith("flags")) { String rawData = uncheck(() -> Files.readString(absolutePath, StandardCharsets.UTF_8)); addFile(builder, rawData, relativePath); } }); return builder.build(); } catch (IOException e) { throw new UncheckedIOException(e); } } private  SystemFlagsDataArchive(Map<FlagId, Map<String, FlagData>> files); static SystemFlagsDataArchive fromZip(InputStream rawIn); static SystemFlagsDataArchive fromDirectory(Path directory); void toZip(OutputStream out); Set<FlagData> flagData(FlagsTarget target); void validateAllFilesAreForTargets(SystemName currentSystem, Set<FlagsTarget> targets);  }
@Test public void retrieves_correct_flag_data_for_target() { var archive = SystemFlagsDataArchive.fromDirectory(Paths.get("src/test/resources/system-flags/")); assertArchiveReturnsCorrectTestFlagDataForTarget(archive); }
@Test public void supports_multi_level_flags_directory() { var archive = SystemFlagsDataArchive.fromDirectory(Paths.get("src/test/resources/system-flags-multi-level/")); assertFlagDataHasValue(archive, MY_TEST_FLAG, mainControllerTarget, "default"); }
@Test public void duplicated_flagdata_is_detected() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("contains redundant flag data for id 'my-test-flag' already set in another directory!"); var archive = SystemFlagsDataArchive.fromDirectory(Paths.get("src/test/resources/system-flags-multi-level-with-duplicated-flagdata/")); }
@Test public void empty_files_are_handled_as_no_flag_data_for_target() { var archive = SystemFlagsDataArchive.fromDirectory(Paths.get("src/test/resources/system-flags/")); assertNoFlagData(archive, FLAG_WITH_EMPTY_DATA, mainControllerTarget); assertFlagDataHasValue(archive, FLAG_WITH_EMPTY_DATA, prodUsWestCfgTarget, "main.prod.us-west-1"); assertNoFlagData(archive, FLAG_WITH_EMPTY_DATA, prodUsEast3CfgTarget); assertFlagDataHasValue(archive, FLAG_WITH_EMPTY_DATA, devUsEast1CfgTarget, "main"); }
@Test public void throws_exception_on_non_json_file() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("Only JSON files are allowed in 'flags/' directory (found 'flags/my-test-flag/file-name-without-dot-json')"); SystemFlagsDataArchive.fromDirectory(Paths.get("src/test/resources/system-flags-with-invalid-file-name/")); }
@Test public void throws_on_unknown_field() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage( "flags/my-test-flag/main.prod.us-west-1.json contains unknown non-comment fields: after removing any comment fields the JSON is:\n" + " {\"id\":\"my-test-flag\",\"rules\":[{\"condition\":[{\"type\":\"whitelist\",\"dimension\":\"hostname\",\"values\":[\"foo.com\"]}],\"value\":\"default\"}]}\n" + "but deserializing this ended up with a JSON that are missing some of the fields:\n" + " {\"id\":\"my-test-flag\",\"rules\":[{\"value\":\"default\"}]}\n" + "See https: SystemFlagsDataArchive.fromDirectory(Paths.get("src/test/resources/system-flags-with-unknown-field-name/")); }