RaftConfigurationLoader { public static RaftConfiguration loadFromFile(String configFile) throws IOException { File config = new File(configFile); if (!config.exists() || !config.canRead()) { throw new IOException(String.format("%s cannot be opened", config)); } ObjectMapper mapper = new ObjectMapper(); RaftConfiguration configuration = mapper.readValue(config, RaftConfiguration.class); return validate(configuration); } private  RaftConfigurationLoader(); static RaftConfiguration loadFromFile(String configFile); static RaftConfiguration validate(RaftConfiguration configuration);  }
@Test public void shouldDeserializeConfiguration() throws IOException, RaftConfigurationException { String configFilePath = Resources.getResource("fixtures/config.required_fields_only.good.json").getPath(); RaftConfiguration configuration = RaftConfigurationLoader.loadFromFile(configFilePath); assertThat(WRITER.writeValueAsString(configuration), configuration, equalTo(RaftConfigurationFixture.RAFT_REQUIRED_FIELDS_ONLY_CONFIGURATION)); }
@Test public void shouldDeserializeConfigurationWithRequiredFieldsOnlyAndEmptyPassword() throws IOException, RaftConfigurationException { String configFilePath = Resources.getResource("fixtures/config.required_fields_only_empty_password.good.json").getPath(); RaftConfiguration configuration = RaftConfigurationLoader.loadFromFile(configFilePath); assertThat(WRITER.writeValueAsString(configuration), configuration, equalTo(RaftConfigurationFixture.RAFT_REQUIRED_FIELDS_ONLY_EMPTY_PASSWORD_CONFIGURATION)); }
@Test public void shouldDeserializeConfigurationWithRequiredFieldsOnlyAndNoPassword() throws IOException, RaftConfigurationException { String configFilePath = Resources.getResource("fixtures/config.required_fields_only_no_password.good.json").getPath(); RaftConfiguration configuration = RaftConfigurationLoader.loadFromFile(configFilePath); assertThat(WRITER.writeValueAsString(configuration), configuration, equalTo(RaftConfigurationFixture.RAFT_REQUIRED_FIELDS_ONLY_NO_PASSWORD_CONFIGURATION)); }
@Test public void shouldDeserializeConfigurationWithMinimalFieldsAndSnapshotsDisabledExplicitly() throws IOException, RaftConfigurationException { String configFilePath = Resources.getResource("fixtures/config.minimal_fields_snapshots_disabled_explicitly.good.json").getPath(); RaftConfiguration configuration = RaftConfigurationLoader.loadFromFile(configFilePath); assertThat(WRITER.writeValueAsString(configuration), configuration, equalTo(RaftConfigurationFixture.RAFT_MINIMAL_FIELDS_SNAPSHOTS_DISABLED_EXPLICITLY_CONFIGURATION)); }
@Test public void shouldDeserializeConfigurationWithMinimalFieldsAndSnapshotsDisabledExplicitlyWithAdditionalSnapshotFields() throws IOException, RaftConfigurationException { String configFilePath = Resources.getResource("fixtures/config.minimal_fields_snapshots_disabled_explicitly_with_additional_snapshot_fields.good.json").getPath(); RaftConfiguration configuration = RaftConfigurationLoader.loadFromFile(configFilePath); assertThat(WRITER.writeValueAsString(configuration), configuration, equalTo(RaftConfigurationFixture.RAFT_MINIMAL_FIELDS_SNAPSHOTS_DISABLED_EXPLICITLY_WITH_ADDITIONAL_SNAPSHOT_FIELDS_CONFIGURATION)); }
@Test public void shouldDeserializeConfigurationWithMinimalFieldsAndSnapshotsEnabledAndSnapshotDirectoryOnly() throws IOException, RaftConfigurationException { String configFilePath = Resources.getResource("fixtures/config.minimal_fields_snapshots_enabled_with_snapshot_directory_only.good.json").getPath(); RaftConfiguration configuration = RaftConfigurationLoader.loadFromFile(configFilePath); assertThat(WRITER.writeValueAsString(configuration), configuration, equalTo(RaftConfigurationFixture.RAFT_MINIMAL_FIELDS_SNAPSHOTS_ENABLED_WITH_SNAPSHOT_DIRECTORY_ONLY_CONFIGURATION)); }
@Test public void shouldDeserializeConfigurationWithMinimalFieldsAndSnapshotsEnabledAndSnapshotCheckIntervalOnly() throws IOException, RaftConfigurationException { String configFilePath = Resources.getResource("fixtures/config.minimal_fields_snapshots_enabled_with_snapshot_check_interval_only.good.json").getPath(); RaftConfiguration configuration = RaftConfigurationLoader.loadFromFile(configFilePath); assertThat(WRITER.writeValueAsString(configuration), configuration, equalTo(RaftConfigurationFixture.RAFT_MINIMAL_FIELDS_SNAPSHOTS_ENABLED_WITH_SNAPSHOT_CHECK_INTERVAL_ONLY_CONFIGURATION)); }
@Test public void shouldDeserializeConfigurationWithAllOptionalFields() throws IOException, RaftConfigurationException { String configFilePath = Resources.getResource("fixtures/config.all_fields.good.json").getPath(); RaftConfiguration configuration = RaftConfigurationLoader.loadFromFile(configFilePath); assertThat(WRITER.writeValueAsString(configuration), configuration, equalTo(RaftConfigurationFixture.RAFT_ALL_FIELDS_CONFIGURATION)); }
@Test public void shouldFailToLoaConfigurationThatFailsValidation() throws IOException, RaftConfigurationException { String configFilePath = Resources.getResource("fixtures/config.bad.1.json").getPath(); expectedException.expect(RaftConfigurationException.class); RaftConfigurationLoader.loadFromFile(configFilePath); }
@Test public void shouldFailToLoadConfigurationThatHasUnexpectedJsonFields() throws IOException, RaftConfigurationException { String configFilePath = Resources.getResource("fixtures/config.bad.2.json").getPath(); expectedException.expect(UnrecognizedPropertyException.class); RaftConfigurationLoader.loadFromFile(configFilePath); }
@Test public void shouldFailToLoadConfigurationThatIsMissingMemberFields() throws IOException, RaftConfigurationException { String configFilePath = Resources.getResource("fixtures/config.missing_member_fields.bad.json").getPath(); expectedException.expect(JsonMappingException.class); RaftConfigurationLoader.loadFromFile(configFilePath); }
@Test public void shouldFailToLoadConfigurationThatHasInvalidMemberEndpoint() throws IOException, RaftConfigurationException { String configFilePath = Resources.getResource("fixtures/config.invalid_member_endpoint.bad.json").getPath(); expectedException.expect(JsonMappingException.class); RaftConfigurationLoader.loadFromFile(configFilePath); }
@Test public void shouldFailToLoadConfigurationThatHasInvalidMinEntriesToSnapshot() throws IOException, RaftConfigurationException { String configFilePath = Resources.getResource("fixtures/config.minimal_fields_invalid_min_entries_to_snapshot.bad.json").getPath(); expectedException.expect(RaftConfigurationException.class); RaftConfigurationLoader.loadFromFile(configFilePath); }
@Test public void shouldFailToLoadConfigurationThatHasZeroMinEntriesToSnapshot() throws IOException, RaftConfigurationException { String configFilePath = Resources.getResource("fixtures/config.minimal_fields_zero_min_entries_to_snapshot.bad.json").getPath(); expectedException.expect(RaftConfigurationException.class); RaftConfigurationLoader.loadFromFile(configFilePath); }
@Test public void shouldFailToLoadConfigurationThatHasGreaterThanIntMaxMinusOneMinEntriesToSnapshot() throws IOException, RaftConfigurationException { String configFilePath = Resources.getResource("fixtures/config.minimal_fields_greater_than_int_max_minus_one_min_entries_to_snapshot.bad.json").getPath(); expectedException.expect(RaftConfigurationException.class); RaftConfigurationLoader.loadFromFile(configFilePath); }
@Test public void shouldFailToLoadConfigurationThatInvalidSnapshotCheckInterval() throws IOException, RaftConfigurationException { String configFilePath = Resources.getResource("fixtures/config.minimal_fields_invalid_snapshot_check_interval.bad.json").getPath(); expectedException.expect(RaftConfigurationException.class); RaftConfigurationLoader.loadFromFile(configFilePath); }