Schema { @Nullable static KeyspaceMetadata getKeyspaceMetadata(Session session, String keyspace) { Cluster cluster = session.getCluster(); com.datastax.driver.core.Metadata metadata = cluster.getMetadata(); for (Host node : metadata.getAllHosts()) { VersionNumber version = node.getCassandraVersion(); if (version == null) throw new RuntimeException("node had no version: " + node); if (VersionNumber.parse("3.11.3").compareTo(version) > 0) { throw new RuntimeException(String.format( "Host %s is running Cassandra %s, but minimum version is 3.11.3", node.getHostId(), node.getCassandraVersion())); } } return metadata.getKeyspace(keyspace); } Schema();   }
@Test public void getKeyspaceMetadata_failsWhenVersionLessThan3_11_3() { Session session = mock(Session.class); Cluster cluster = mock(Cluster.class); Metadata metadata = mock(Metadata.class); Host host = mock(Host.class); when(session.getCluster()).thenReturn(cluster); when(cluster.getMetadata()).thenReturn(metadata); when(metadata.getAllHosts()).thenReturn(Collections.singleton(host)); when(host.getHostId()).thenReturn(UUID.fromString("11111111-1111-1111-1111-111111111111")); when(host.getCassandraVersion()).thenReturn(VersionNumber.parse("3.11.2")); assertThatThrownBy(() -> Schema.getKeyspaceMetadata(session, "zipkin2")) .isInstanceOf(RuntimeException.class) .hasMessage( "Host 11111111-1111-1111-1111-111111111111 is running Cassandra 3.11.2, but minimum version is 3.11.3"); }
@Test public void getKeyspaceMetadata_failsWhenOneVersionLessThan3_11_3() { Session session = mock(Session.class); Cluster cluster = mock(Cluster.class); Metadata metadata = mock(Metadata.class); Host host1 = mock(Host.class); Host host2 = mock(Host.class); Set<Host> hosts = new LinkedHashSet<>(); hosts.add(host1); hosts.add(host2); when(session.getCluster()).thenReturn(cluster); when(cluster.getMetadata()).thenReturn(metadata); when(metadata.getAllHosts()).thenReturn(hosts); when(host1.getHostId()).thenReturn(UUID.fromString("11111111-1111-1111-1111-111111111111")); when(host2.getHostId()).thenReturn(UUID.fromString("22222222-2222-2222-2222-222222222222")); when(host1.getCassandraVersion()).thenReturn(VersionNumber.parse("3.11.3")); when(host2.getCassandraVersion()).thenReturn(VersionNumber.parse("3.11.2")); assertThatThrownBy(() -> Schema.getKeyspaceMetadata(session, "zipkin2")) .isInstanceOf(RuntimeException.class) .hasMessage("Host 22222222-2222-2222-2222-222222222222 is running Cassandra 3.11.2, but minimum version is 3.11.3"); }
@Test public void getKeyspaceMetadata_passesWhenVersion3_11_3AndKeyspaceMetadataIsNotNull() { Session session = mock(Session.class); Cluster cluster = mock(Cluster.class); Metadata metadata = mock(Metadata.class); Host host = mock(Host.class); KeyspaceMetadata keyspaceMetadata = mock(KeyspaceMetadata.class); when(session.getCluster()).thenReturn(cluster); when(cluster.getMetadata()).thenReturn(metadata); when(metadata.getAllHosts()).thenReturn(Collections.singleton(host)); when(host.getCassandraVersion()).thenReturn(VersionNumber.parse("3.11.3")); when(metadata.getKeyspace("zipkin2")).thenReturn(keyspaceMetadata); assertThat(Schema.getKeyspaceMetadata(session, "zipkin2")) .isSameAs(keyspaceMetadata); }
@Test public void getKeyspaceMetadata_passesWhenVersion3_11_4AndKeyspaceMetadataIsNotNull() { Session session = mock(Session.class); Cluster cluster = mock(Cluster.class); Metadata metadata = mock(Metadata.class); Host host = mock(Host.class); KeyspaceMetadata keyspaceMetadata = mock(KeyspaceMetadata.class); when(session.getCluster()).thenReturn(cluster); when(cluster.getMetadata()).thenReturn(metadata); when(metadata.getAllHosts()).thenReturn(Collections.singleton(host)); when(host.getCassandraVersion()).thenReturn(VersionNumber.parse("3.11.4")); when(metadata.getKeyspace("zipkin2")).thenReturn(keyspaceMetadata); assertThat(Schema.getKeyspaceMetadata(session, "zipkin2")) .isSameAs(keyspaceMetadata); }