@Test public void serialization() throws Exception { AllocationCommands commands = new AllocationCommands( new AllocateAllocationCommand(new ShardId("test", 1), "node1", true), new MoveAllocationCommand(new ShardId("test", 3), "node2", "node3"), new CancelAllocationCommand(new ShardId("test", 4), "node5", true)); BytesStreamOutput bytes = new BytesStreamOutput(); AllocationCommands.writeTo(commands, bytes); AllocationCommands sCommands = AllocationCommands.readFrom(new BytesStreamInput(bytes.bytes())); assertThat(sCommands.commands().size(), equalTo(3)); assertThat( ((AllocateAllocationCommand) (sCommands.commands().get(0))).shardId(), equalTo(new ShardId("test", 1))); assertThat( ((AllocateAllocationCommand) (sCommands.commands().get(0))).node(), equalTo("node1")); assertThat( ((AllocateAllocationCommand) (sCommands.commands().get(0))).allowPrimary(), equalTo(true)); assertThat( ((MoveAllocationCommand) (sCommands.commands().get(1))).shardId(), equalTo(new ShardId("test", 3))); assertThat( ((MoveAllocationCommand) (sCommands.commands().get(1))).fromNode(), equalTo("node2")); assertThat(((MoveAllocationCommand) (sCommands.commands().get(1))).toNode(), equalTo("node3")); assertThat( ((CancelAllocationCommand) (sCommands.commands().get(2))).shardId(), equalTo(new ShardId("test", 4))); assertThat(((CancelAllocationCommand) (sCommands.commands().get(2))).node(), equalTo("node5")); assertThat( ((CancelAllocationCommand) (sCommands.commands().get(2))).allowPrimary(), equalTo(true)); }
@Test public void xContent() throws Exception { String commands = "{\n" + " \"commands\" : [\n" + " {\"allocate\" : {\"index\" : \"test\", \"shard\" : 1, \"node\" : \"node1\", \"allow_primary\" : true}}\n" + " ,{\"move\" : {\"index\" : \"test\", \"shard\" : 3, \"from_node\" : \"node2\", \"to_node\" : \"node3\"}} \n" + " ,{\"cancel\" : {\"index\" : \"test\", \"shard\" : 4, \"node\" : \"node5\", \"allow_primary\" : true}} \n" + " ]\n" + "}\n"; XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(commands); // move two tokens, parser expected to be "on" `commands` field parser.nextToken(); parser.nextToken(); AllocationCommands sCommands = AllocationCommands.fromXContent(parser); assertThat(sCommands.commands().size(), equalTo(3)); assertThat( ((AllocateAllocationCommand) (sCommands.commands().get(0))).shardId(), equalTo(new ShardId("test", 1))); assertThat( ((AllocateAllocationCommand) (sCommands.commands().get(0))).node(), equalTo("node1")); assertThat( ((AllocateAllocationCommand) (sCommands.commands().get(0))).allowPrimary(), equalTo(true)); assertThat( ((MoveAllocationCommand) (sCommands.commands().get(1))).shardId(), equalTo(new ShardId("test", 3))); assertThat( ((MoveAllocationCommand) (sCommands.commands().get(1))).fromNode(), equalTo("node2")); assertThat(((MoveAllocationCommand) (sCommands.commands().get(1))).toNode(), equalTo("node3")); assertThat( ((CancelAllocationCommand) (sCommands.commands().get(2))).shardId(), equalTo(new ShardId("test", 4))); assertThat(((CancelAllocationCommand) (sCommands.commands().get(2))).node(), equalTo("node5")); assertThat( ((CancelAllocationCommand) (sCommands.commands().get(2))).allowPrimary(), equalTo(true)); }