@Test
public void migrationWithDocumentOwnerMismatch() throws Throwable {

    // this test dirties destination host, requires clean up
    clearSourceAndDestInProcessPeers = true;
    // create example services in the source node-group and make sure that each peer
    // is owner of at-least one example service.
    int peersWithDocs;
    int iteration = 0;
    do {
        ++iteration;
        createExampleDocuments(this.exampleSourceFactory, getSourceHost(), this.serviceCount, false);
        ServiceDocumentQueryResult results = this.host.getExpandedFactoryState(this.exampleSourceFactory);
        peersWithDocs = results.documents.values().stream()
                .map(doc -> (Utils.fromJson(doc, ServiceDocument.class)).documentOwner)
                .distinct()
                .collect(Collectors.toList())
                .size();
    } while (peersWithDocs != this.nodeCount);
    // disable peer synchronization on each source host.
    Iterator<VerificationHost> peerIt = this.host.getInProcessHostMap().values().iterator();
    VerificationHost peerNode = null;
    while (peerIt.hasNext()) {
        VerificationHost h = peerIt.next();
        // ensure owner node of the factory still alive
        if (!h.isOwner(ExampleService.FACTORY_LINK, ServiceUriPaths.DEFAULT_NODE_SELECTOR)) {
            peerNode = h;
        }
        h.setPeerSynchronizationEnabled(false);
    }
    assertNotNull(peerNode);
    // Stop the last peerNode
    this.host.setNodeGroupQuorum(this.nodeCount - 1);
    this.host.stopHost(peerNode);
    this.host.waitForNodeGroupConvergence(this.nodeCount - 1);
    // Kick-off migration
    MigrationTaskService.State migrationState = validMigrationState(
            ExampleService.FACTORY_LINK);
    String[] out = new String[1];
    Operation op = Operation.createPost(this.destinationFactoryUri)
            .setBody(migrationState)
            .setReferer(this.host.getUri());
    out[0] = getDestinationHost().getTestRequestSender().sendAndWait(op).getBody(State.class).documentSelfLink;
    // Wait for the migration task to finish
    State waitForServiceCompletion = waitForServiceCompletion(out[0], getDestinationHost());
    assertEquals(TaskStage.FINISHED, waitForServiceCompletion.taskInfo.stage);
    // validate that the new factory got all the documents.
    ServiceDocumentQueryResult result = this.host
            .getFactoryState(UriUtils.buildUri(
                    getDestinationHost(), ExampleService.FACTORY_LINK));
    // all documents migrated
    assertEquals(this.serviceCount * iteration, result.documentLinks.size());
}