AttachCommand extends TopologyCommand { public void setSourceAddress(InetSocketAddress sourceAddress) { this.sourceAddress = sourceAddress; }  void setSourceAddress(InetSocketAddress sourceAddress); void setRestartWaitTime(Measure<TimeUnit> restartWaitTime); void setRestartDelay(Measure<TimeUnit> restartDelay); @Override void validate();  }
@Test public void test_validate_failures() { AttachCommand command = newCommand(); command.setSourceAddress(InetSocketAddress.createUnresolved("localhost", 9410)); command.setOperationType(NODE); command.setDestinationAddress(InetSocketAddress.createUnresolved("localhost", 9410)); assertThat( command::run, is(throwing(instanceOf(IllegalArgumentException.class)).andMessage(is(equalTo("The destination and the source endpoints must not be the same"))))); }
@Test public void test_attach_node_validation_fail_src_activated() { when(topologyServiceMock("localhost", 9411).isActivated()).thenReturn(true); AttachCommand command = newCommand(); command.setSourceAddress(createUnresolved("localhost", 9411)); command.setOperationType(NODE); command.setDestinationAddress(createUnresolved("localhost", 9410)); assertThat( command::run, is(throwing(instanceOf(IllegalArgumentException.class)).andMessage(is(containsString("cannot be attached since it is part of an existing cluster"))))); }
@Test public void test_attach_node_validation_fail_src_multiNodeStripe() { NodeContext nodeContext = new NodeContext(newTestCluster("my-cluster", new Stripe().addNodes(node1, node2)), node1.getUID()); when(topologyServiceMock("localhost", 9411).getUpcomingNodeContext()).thenReturn(nodeContext); AttachCommand command = newCommand(); command.setSourceAddress(createUnresolved("localhost", 9411)); command.setOperationType(NODE); command.setDestinationAddress(createUnresolved("localhost", 9410)); assertThat( command::run, is(throwing(instanceOf(IllegalArgumentException.class)).andMessage(is(containsString("is part of a stripe containing more than 1 nodes"))))); }
@Test public void test_attach_node_validation_fail_clusterSettingsMismatch() { NodeContext nodeContext = new NodeContext(newTestCluster("my-cluster", new Stripe().addNode(node1)).setFailoverPriority(consistency()), node1.getUID()); when(topologyServiceMock("localhost", 9411).getUpcomingNodeContext()).thenReturn(nodeContext); AttachCommand command = newCommand(); command.setSourceAddress(createUnresolved("localhost", 9411)); command.setOperationType(NODE); command.setDestinationAddress(createUnresolved("localhost", 9410)); assertThat( command::run, is(throwing(instanceOf(IllegalArgumentException.class)).andMessage(is(containsString( "Mismatch found in failover-priority setting"))))); }
@Test public void test_attach_node_ok() throws IOException { DynamicConfigService mock10 = dynamicConfigServiceMock("localhost", 9410); DynamicConfigService mock11 = dynamicConfigServiceMock("localhost", 9411); AttachCommand command = newCommand(); command.setSourceAddress(createUnresolved("localhost", 9411)); command.setOperationType(NODE); command.setDestinationAddress(createUnresolved("localhost", 9410)); command.run(); verify(mock10).setUpcomingCluster(newCluster.capture()); verify(mock11).setUpcomingCluster(newCluster.capture()); List<Cluster> allValues = newCluster.getAllValues(); assertThat(allValues, hasSize(2)); assertThat(allValues.get(0), is(equalTo(allValues.get(1)))); Testing.replaceUIDs(allValues.get(0)); assertThat( objectMapper.writeValueAsString(allValues.get(0)), allValues.get(0), is(equalTo(objectMapper.readValue(getClass().getResource("/cluster1.json"), Cluster.class)))); Cluster cluster = allValues.get(0); assertThat(cluster.getStripes(), hasSize(1)); assertThat(cluster.getNodes(), hasSize(2)); }
@Test public void test_attach_stripe_validation_fail_src_activated() { when(topologyServiceMock("localhost", 9411).isActivated()).thenReturn(true); AttachCommand command = newCommand(); command.setSourceAddress(createUnresolved("localhost", 9411)); command.setOperationType(STRIPE); command.setDestinationAddress(createUnresolved("localhost", 9410)); assertThat( command::run, is(throwing(instanceOf(IllegalArgumentException.class)).andMessage(is(containsString("cannot be attached since it is part of an existing cluster"))))); }
@Test public void test_attach_stripe_validation_fail_src_multiStripeCluster() { NodeContext nodeContext = new NodeContext(newTestCluster("my-cluster", new Stripe().addNode(node1), new Stripe().addNode(node2)), node1.getUID()); when(topologyServiceMock("localhost", 9411).getUpcomingNodeContext()).thenReturn(nodeContext); AttachCommand command = newCommand(); command.setSourceAddress(createUnresolved("localhost", 9411)); command.setOperationType(STRIPE); command.setDestinationAddress(createUnresolved("localhost", 9410)); assertThat( command::run, is(throwing(instanceOf(IllegalArgumentException.class)).andMessage(is(containsString("is part of a cluster containing more than 1 stripes"))))); }
@Test public void test_attach_stripe_ok() throws IOException { nodeContext1.getStripe().setName("stripe2"); DynamicConfigService mock10 = dynamicConfigServiceMock("localhost", 9410); DynamicConfigService mock11 = dynamicConfigServiceMock("localhost", 9411); AttachCommand command = newCommand(); command.setSourceAddress(createUnresolved("localhost", 9411)); command.setOperationType(STRIPE); command.setDestinationAddress(createUnresolved("localhost", 9410)); command.run(); verify(mock10).setUpcomingCluster(newCluster.capture()); verify(mock11).setUpcomingCluster(newCluster.capture()); List<Cluster> allValues = newCluster.getAllValues(); assertThat(allValues, hasSize(2)); assertThat(allValues.get(0), is(equalTo(allValues.get(1)))); Testing.replaceUIDs(allValues.get(0)); assertThat( objectMapper.writeValueAsString(allValues.get(0)), allValues.get(0), is(equalTo(objectMapper.readValue(getClass().getResource("/cluster2.json"), Cluster.class)))); Cluster cluster = allValues.get(0); assertThat(cluster.getStripes(), hasSize(2)); assertThat(cluster.getStripes().get(0).getNodes(), hasSize(1)); assertThat(cluster.getStripes().get(1).getNodes(), hasSize(1)); assertThat(cluster.getNodes(), hasSize(2)); }
@Test public void test_attach_stripe_validation_fail_clusterSettingsMismatch() { NodeContext nodeContext = new NodeContext(newTestCluster("my-cluster", new Stripe().addNode(node1)).setFailoverPriority(consistency()), node1.getUID()); when(topologyServiceMock("localhost", 9411).getUpcomingNodeContext()).thenReturn(nodeContext); AttachCommand command = newCommand(); command.setSourceAddress(createUnresolved("localhost", 9411)); command.setOperationType(STRIPE); command.setDestinationAddress(createUnresolved("localhost", 9410)); assertThat( command::run, is(throwing(instanceOf(IllegalArgumentException.class)).andMessage(is(containsString( "Mismatch found in failover-priority setting"))))); }