'Helper method which pre-populates the persistence_service with the
supplied ``dataset``, makes an API call to move the supplied
``dataset`` from ``origin`` to ``target`` and finally asserts that the
API call returned the expected result and that the persistence_service
has been updated.
:param Dataset dataset: The dataset which will be moved.
:param Deployment deployment: The deployment that contains the dataset.
:param UUID origin: The node UUID of the node that holds the
current primary manifestation of the ``dataset``.
:param UUID target: The node UUID of the node to which the
dataset will be moved.
:param if_matches: If true, send current hash in
``X-If-Configuration-Matches`` header.
:returns: A ``Deferred`` which fires when all assertions have been
executed.'

def _test_change_primary(self, dataset, deployment, origin, target, if_matches=False):

    expected_dataset_id = dataset.dataset_id
    expected_dataset = {u'dataset_id': expected_dataset_id, u'primary': unicode(target), u'metadata': {}, u'deleted': False}
    saving = self.persistence_service.save(deployment)
    headers = {}
    if if_matches:
        headers[IF_MATCHES_HEADER] = [self.persistence_service.configuration_hash()]
    def saved(ignored):
        creating = self.assertResult('POST', ('/configuration/datasets/%s' % (expected_dataset_id.encode('ascii'),)), {u'primary': unicode(target)}, OK, expected_dataset, additional_headers=headers)
        def got_result(result):
            deployment = self.persistence_service.get()
            for node in deployment.nodes.values():
                if (node.uuid == target):
                    dataset_ids = [(m.primary, m.dataset.dataset_id) for m in node.manifestations.values()]
                    self.assertIn((True, expected_dataset_id), dataset_ids)
                    break
            else:
                self.fail('Node    not    found.    {}'.format(target))
        creating.addCallback(got_result)
        return creating
    saving.addCallback(saved)
    return saving
