KafkaVersion implements Comparable<KafkaVersion> { public static KafkaVersion parseKafkaVersions(Reader reader, Map<String, KafkaVersion> mapOfVersions) throws IOException, IllegalArgumentException { YAMLMapper mapper = new YAMLMapper(); List<KafkaVersion> kafkaVersions = mapper.readValue(reader, new TypeReference<List<KafkaVersion>>() { }); KafkaVersion defaultVersion = null; for (KafkaVersion kafkaVersion: kafkaVersions) { if (mapOfVersions.put(kafkaVersion.version, kafkaVersion) != null) { throw new IllegalArgumentException("Duplicate version (" + kafkaVersion.version + ") listed in kafka-versions file"); } if (kafkaVersion.isDefault) { if (defaultVersion == null) { defaultVersion = kafkaVersion; } else { throw new IllegalArgumentException( "Multiple Kafka versions (" + defaultVersion.version() + " and " + kafkaVersion.version() + ") are set as default"); } } } if (defaultVersion != null) { return defaultVersion; } else { throw new RuntimeException("No Kafka version was configured as the default"); } } @JsonCreator  KafkaVersion(@JsonProperty("version") String version,
                        @JsonProperty("protocol") String protocolVersion,
                        @JsonProperty("format") String messageVersion,
                        @JsonProperty("zookeeper") String zookeeperVersion,
                        @JsonProperty("default") boolean isDefault,
                        @JsonProperty("unsupported-features") String unsupportedFeatures); static KafkaVersion parseKafkaVersions(Reader reader, Map<String, KafkaVersion> mapOfVersions); @Override String toString(); String version(); String protocolVersion(); String messageVersion(); String zookeeperVersion(); boolean isDefault(); String unsupportedFeatures(); @Override int compareTo(KafkaVersion o); int compareVersion(String version); static int compareDottedVersions(String version1, String version2); @Override int hashCode(); @Override boolean equals(Object o);  }
@Test public void duplicateVersionTest() throws IOException { assertThrows(IllegalArgumentException.class, () -> { Map<String, KafkaVersion> map = new HashMap<>(); KafkaVersion defaultVersion = KafkaVersion.parseKafkaVersions( new StringReader( KafkaVersionTestUtils.getPreviousVersionYaml(false) + KafkaVersionTestUtils.getPreviousVersionYaml(false) + KafkaVersionTestUtils.getLatestVersionYaml(true) ), map); }); }
@Test public void noDefaultTest() throws IOException { assertThrows(RuntimeException.class, () -> { Map<String, KafkaVersion> map = new HashMap<>(); KafkaVersion defaultVersion = KafkaVersion.parseKafkaVersions( new StringReader( KafkaVersionTestUtils.getPreviousVersionYaml(false) + KafkaVersionTestUtils.getLatestVersionYaml(false)), map); }); }
@Test public void multipleDefaultTest() throws IOException { assertThrows(IllegalArgumentException.class, () -> { Map<String, KafkaVersion> map = new HashMap<>(); KafkaVersion defaultVersion = KafkaVersion.parseKafkaVersions( new StringReader( KafkaVersionTestUtils.getPreviousVersionYaml(true) + KafkaVersionTestUtils.getLatestVersionYaml(true) ), map); }); }