text:
import mock
import oslo_messaging as messaging
from oslo_serialization import jsonutils
from oslo_utils.fixture import uuidsentinel as uuids
import six

from nova.compute import power_state
from nova.compute import rpcapi as compute_rpcapi
from nova.compute import vm_states
from nova.conductor.tasks import live_migrate
from nova import context as nova_context
from nova import exception
from nova.network import model as network_model
from nova import objects
from nova.scheduler.client import query
from nova.scheduler.client import report
from nova.scheduler import utils as scheduler_utils
from nova import servicegroup
from nova import test
from nova.tests.unit import fake_instance

fake_limits1 = objects.SchedulerLimits()
fake_selection1 = objects.Selection(service_host="host1", nodename="node1",
        cell_uuid=uuids.cell, limits=fake_limits1,
        compute_node_uuid=uuids.compute_node1)
fake_limits2 = objects.SchedulerLimits()
fake_selection2 = objects.Selection(service_host="host2", nodename="node2",
        cell_uuid=uuids.cell, limits=fake_limits2,
        compute_node_uuid=uuids.compute_node2)

class LiveMigrationTaskTestCase(test.NoDBTestCase):
    def setUp(self):
        super(LiveMigrationTaskTestCase, self).setUp()
        self.context = nova_context.get_admin_context()
        self.instance_host = "host"
        self.instance_uuid = uuids.instance
        self.instance_image = "image_ref"
        db_instance = fake_instance.fake_db_instance(
                host=self.instance_host,
                uuid=self.instance_uuid,
                power_state=power_state.RUNNING,
                vm_state = vm_states.ACTIVE,
                memory_mb=512,
                image_ref=self.instance_image)
        self.instance = objects.Instance._from_db_object(
                self.context, objects.Instance(), db_instance)
        self.instance.system_metadata = {'image_hw_disk_bus': 'scsi'}
        self.instance.numa_topology = None
        self.instance.pci_requests = None
        self.destination = "destination"
        self.block_migration = "bm"
        self.disk_over_commit = "doc"
        self.migration = objects.Migration()
        self.fake_spec = objects.RequestSpec()
        self._generate_task()

        _p = mock.patch('nova.compute.utils.heal_reqspec_is_bfv')
        self.heal_reqspec_is_bfv_mock = _p.start()
        self.addCleanup(_p.stop)

        _p = mock.patch('nova.objects.RequestSpec.ensure_network_metadata')
        self.ensure_network_metadata_mock = _p.start()
        self.addCleanup(_p.stop)

        _p = mock.patch(
            'nova.network.neutron.API.'
            'get_requested_resource_for_instance',
            return_value=[])
        self.mock_get_res_req = _p.start()
        self.addCleanup(_p.stop)

    def _generate_task(self):
        self.task = live_migrate.LiveMigrationTask(self.context,
            self.instance, self.destination, self.block_migration,
            self.disk_over_commit, self.migration, compute_rpcapi.ComputeAPI(),
            servicegroup.API(), query.SchedulerQueryClient(),
            report.SchedulerReportClient(), self.fake_spec)

    @mock.patch('nova.availability_zones.get_host_availability_zone',
                return_value='fake-az')
    def test_execute_with_destination(self, mock_get_az):
        dest_node = objects.ComputeNode(hypervisor_hostname='dest_node')
        with test.nested(
            mock.patch.object(self.task, '_check_host_is_up'),
            mock.patch.object(self.task, '_check_requested_destination',
                              return_value=(mock.sentinel.source_node,
                                            dest_node)),
            mock.patch.object(scheduler_utils,
                              'claim_resources_on_destination'),
            mock.patch.object(self.migration, 'save'),
            mock.patch.object(self.task.compute_rpcapi, 'live_migration'),
            mock.patch('nova.conductor.tasks.migrate.'
                       'replace_allocation_with_migration'),
        ) as (mock_check_up, mock_check_dest, mock_claim, mock_save, mock_mig,
              m_alloc):
            mock_mig.return_value = "bob"
            m_alloc.return_value = (mock.MagicMock(), mock.sentinel.allocs)

            self.assertEqual("bob", self.task.execute())
            mock_check_up.assert_called_once_with(self.instance_host)
            mock_check_dest.assert_called_once_with()
            allocs = mock.sentinel.allocs
            mock_claim.assert_called_once_with(
                self.context, self.task.report_client,
                self.instance, mock.sentinel.source_node, dest_node,
                source_allocations=allocs, consumer_generation=None)
            mock_mig.assert_called_once_with(
                self.context,
                host=self.instance_host,
                instance=self.instance,
                dest=self.destination,
                block_migration=self.block_migration,
                migration=self.migration,
                migrate_data=None)
            self.assertTrue(mock_save.called)
            mock_get_az.assert_called_once_with(self.context, self.destination)
            self.assertEqual('fake-az', self.instance.availability_zone)
            # make sure the source/dest fields were set on the migration object
            self.assertEqual(self.instance.node, self.migration.source_node)
            self.assertEqual(dest_node.hypervisor_hostname,
                             self.migration.dest_node)
            self.assertEqual(self.task.destination,
                             self.migration.dest_compute)
            m_alloc.assert_called_once_with(self.context,
                                            self.instance,
                                            self.migration)
        # When the task is executed with a destination it means the host is
        # being forced and we don't call the scheduler, so we don't need to
        # heal the request spec.
        self.heal_reqspec_is_bfv_mock.assert_not_called()

        # When the task is executed with a destination it means the host is
        # being forced and we don't call the scheduler, so we don't need to
        # modify the request spec
        self.ensure_network_metadata_mock.assert_not_called()

    @mock.patch('nova.availability_zones.get_host_availability_zone',
                return_value='nova')
    def test_execute_without_destination(self, mock_get_az):
        self.destination = None
        self._generate_task()
        self.assertIsNone(self.task.destination)

        with test.nested(
            mock.patch.object(self.task, '_check_host_is_up'),
            mock.patch.object(self.task, '_find_destination'),
            mock.patch.object(self.task.compute_rpcapi, 'live_migration'),
            mock.patch.object(self.migration, 'save'),
            mock.patch('nova.conductor.tasks.migrate.'
                       'replace_allocation_with_migration'),
        ) as (mock_check, mock_find, mock_mig, mock_save, mock_alloc):
            mock_find.return_value = ("found_host", "found_node", None)
            mock_mig.return_value = "bob"
            mock_alloc.return_value = (mock.MagicMock(), mock.MagicMock())

            self.assertEqual("bob", self.task.execute())
            mock_check.assert_called_once_with(self.instance_host)
            mock_find.assert_called_once_with()
            mock_mig.assert_called_once_with(self.context,
                host=self.instance_host,
                instance=self.instance,
                dest="found_host",
                block_migration=self.block_migration,
                migration=self.migration,
                migrate_data=None)
            self.assertTrue(mock_save.called)
            mock_get_az.assert_called_once_with(self.context, 'found_host')
            self.assertEqual('found_host', self.migration.dest_compute)
            self.assertEqual('found_node', self.migration.dest_node)
            self.assertEqual(self.instance.node, self.migration.source_node)
            self.assertTrue(mock_alloc.called)

    def test_check_instance_is_active_passes_when_paused(self):
        self.task.instance['power_state'] = power_state.PAUSED
        self.task._check_instance_is_active()

    def test_check_instance_is_active_fails_when_shutdown(self):
        self.task.instance['power_state'] = power_state.SHUTDOWN
        self.assertRaises(exception.InstanceInvalidState,
                          self.task._check_instance_is_active)

    @mock.patch.object(objects.ComputeNode, 'get_by_host_and_nodename')
    def test_check_instance_has_no_numa_passes_no_numa(self, mock_get):
        self.flags(enable_numa_live_migration=False, group='workarounds')
        self.task.instance.numa_topology = None
        mock_get.return_value = objects.ComputeNode(
            uuid=uuids.cn1, hypervisor_type='qemu')
        self.task._check_instance_has_no_numa()

    @mock.patch.object(objects.ComputeNode, 'get_by_host_and_nodename')
    def test_check_instance_has_no_numa_passes_non_kvm(self, mock_get):
        self.flags(enable_numa_live_migration=False, group='workarounds')
        self.task.instance.numa_topology = objects.InstanceNUMATopology(
            cells=[objects.InstanceNUMACell(id=0, cpuset=set([0]),
                                            memory=1024)])
        mock_get.return_value = objects.ComputeNode(
            uuid=uuids.cn1, hypervisor_type='xen')
        self.task._check_instance_has_no_numa()

    @mock.patch.object(objects.ComputeNode, 'get_by_host_and_nodename')
    @mock.patch.object(objects.Service, 'get_minimum_version',
                       return_value=39)
    def test_check_instance_has_no_numa_passes_workaround(
            self, mock_get_min_ver, mock_get):
        self.flags(enable_numa_live_migration=True, group='workarounds')
        self.task.instance.numa_topology = objects.InstanceNUMATopology(
            cells=[objects.InstanceNUMACell(id=0, cpuset=set([0]),
                                            memory=1024)])
        mock_get.return_value = objects.ComputeNode(
            uuid=uuids.cn1, hypervisor_type='qemu')
        self.task._check_instance_has_no_numa()
        mock_get_min_ver.assert_called_once_with(self.context, 'nova-compute')

    @mock.patch.object(objects.ComputeNode, 'get_by_host_and_nodename')
    @mock.patch.object(objects.Service, 'get_minimum_version',
                       return_value=39)
    def test_check_instance_has_no_numa_fails(self, mock_get_min_ver,
                                              mock_get):
        self.flags(enable_numa_live_migration=False, group='workarounds')
        mock_get.return_value = objects.ComputeNode(
            uuid=uuids.cn1, hypervisor_type='qemu')
        self.task.instance.numa_topology = objects.InstanceNUMATopology(
            cells=[objects.InstanceNUMACell(id=0, cpuset=set([0]),
                                            memory=1024)])
        self.assertRaises(exception.MigrationPreCheckError,
                          self.task._check_instance_has_no_numa)
        mock_get_min_ver.assert_called_once_with(self.context, 'nova-compute')

    @mock.patch.object(objects.ComputeNode, 'get_by_host_and_nodename')
    @mock.patch.object(objects.Service, 'get_minimum_version',
                       return_value=40)
    def test_check_instance_has_no_numa_new_svc_passes(self, mock_get_min_ver,
                                                       mock_get):
        self.flags(enable_numa_live_migration=False, group='workarounds')
        mock_get.return_value = objects.ComputeNode(
            uuid=uuids.cn1, hypervisor_type='qemu')
        self.task.instance.numa_topology = objects.InstanceNUMATopology(
            cells=[objects.InstanceNUMACell(id=0, cpuset=set([0]),
                                            memory=1024)])
        self.task._check_instance_has_no_numa()
        mock_get_min_ver.assert_called_once_with(self.context, 'nova-compute')

    @mock.patch.object(objects.Service, 'get_by_compute_host')
    @mock.patch.object(servicegroup.API, 'service_is_up')
    def test_check_instance_host_is_up(self, mock_is_up, mock_get):
        mock_get.return_value = "service"
        mock_is_up.return_value = True

        self.task._check_host_is_up("host")
        mock_get.assert_called_once_with(self.context, "host")
        mock_is_up.assert_called_once_with("service")

    @mock.patch.object(objects.Service, 'get_by_compute_host')
    @mock.patch.object(servicegroup.API, 'service_is_up')
    def test_check_instance_host_is_up_fails_if_not_up(self, mock_is_up,
                                                       mock_get):
        mock_get.return_value = "service"
        mock_is_up.return_value = False

        self.assertRaises(exception.ComputeServiceUnavailable,
                self.task._check_host_is_up, "host")
        mock_get.assert_called_once_with(self.context, "host")
        mock_is_up.assert_called_once_with("service")

    @mock.patch.object(objects.Service, 'get_by_compute_host',
                       side_effect=exception.ComputeHostNotFound(host='host'))
    def test_check_instance_host_is_up_fails_if_not_found(self, mock):
        self.assertRaises(exception.ComputeHostNotFound,
            self.task._check_host_is_up, "host")

    @mock.patch.object(objects.Service, 'get_by_compute_host')
    @mock.patch.object(live_migrate.LiveMigrationTask, '_get_compute_info')
    @mock.patch.object(servicegroup.API, 'service_is_up')
    @mock.patch.object(compute_rpcapi.ComputeAPI,
                       'check_can_live_migrate_destination')
    def test_check_requested_destination(self, mock_check, mock_is_up,
                                         mock_get_info, mock_get_host):
        mock_get_host.return_value = "service"
        mock_is_up.return_value = True
        hypervisor_details = objects.ComputeNode(
            hypervisor_type="a",
            hypervisor_version=6.1,
            free_ram_mb=513,
            memory_mb=512,
            ram_allocation_ratio=1.0)
        mock_get_info.return_value = hypervisor_details
        mock_check.return_value = "migrate_data"
        self.task.limits = fake_limits1

        with test.nested(
            mock.patch.object(self.task.network_api,
                              'supports_port_binding_extension',
                              return_value=False),
            mock.patch.object(self.task, '_check_can_migrate_pci')):
            self.assertEqual((hypervisor_details, hypervisor_details),
                             self.task._check_requested_destination())
        self.assertEqual("migrate_data", self.task.migrate_data)
        mock_get_host.assert_called_once_with(self.context, self.destination)
        mock_is_up.assert_called_once_with("service")
        self.assertEqual([mock.call(self.destination),
                          mock.call(self.instance_host),
                          mock.call(self.destination)],
                         mock_get_info.call_args_list)
        mock_check.assert_called_once_with(self.context, self.instance,
            self.destination, self.block_migration, self.disk_over_commit,
            self.task.migration, fake_limits1)

    def test_check_requested_destination_fails_with_same_dest(self):
        self.task.destination = "same"
        self.task.source = "same"
        self.assertRaises(exception.UnableToMigrateToSelf,
                          self.task._check_requested_destination)

    @mock.patch.object(objects.Service, 'get_by_compute_host',
                       side_effect=exception.ComputeHostNotFound(host='host'))
    def test_check_requested_destination_fails_when_destination_is_up(self,
                                                                      mock):
        self.assertRaises(exception.ComputeHostNotFound,
                          self.task._check_requested_destination)

    @mock.patch.object(live_migrate.LiveMigrationTask, '_check_host_is_up')
    @mock.patch.object(objects.ComputeNode,
                       'get_first_node_by_host_for_old_compat')
    def test_check_requested_destination_fails_with_not_enough_memory(
        self, mock_get_first, mock_is_up):
        mock_get_first.return_value = (
            objects.ComputeNode(free_ram_mb=513,
                                memory_mb=1024,
                                ram_allocation_ratio=0.9,))

        # free_ram is bigger than instance.ram (512) but the allocation
        # ratio reduces the total available RAM to 410MB
        # (1024 * 0.9 - (1024 - 513))
        self.assertRaises(exception.MigrationPreCheckError,
                          self.task._check_requested_destination)
        mock_is_up.assert_called_once_with(self.destination)
        mock_get_first.assert_called_once_with(self.context, self.destination)

    @mock.patch.object(live_migrate.LiveMigrationTask, '_check_host_is_up')
    @mock.patch.object(live_migrate.LiveMigrationTask,
                       '_check_destination_has_enough_memory')
    @mock.patch.object(live_migrate.LiveMigrationTask, '_get_compute_info')
    def test_check_requested_destination_fails_with_hypervisor_diff(
        self, mock_get_info, mock_check, mock_is_up):
        mock_get_info.side_effect = [
            objects.ComputeNode(hypervisor_type='b'),
            objects.ComputeNode(hypervisor_type='a')]

        self.assertRaises(exception.InvalidHypervisorType,
                          self.task._check_requested_destination)
        mock_is_up.assert_called_once_with(self.destination)
        mock_check.assert_called_once_with()
        self.assertEqual([mock.call(self.instance_host),
                          mock.call(self.destination)],
                         mock_get_info.call_args_list)

    @mock.patch.object(live_migrate.LiveMigrationTask, '_check_host_is_up')
    @mock.patch.object(live_migrate.LiveMigrationTask,
                       '_check_destination_has_enough_memory')
    @mock.patch.object(live_migrate.LiveMigrationTask, '_get_compute_info')
    def test_check_requested_destination_fails_with_hypervisor_too_old(
        self, mock_get_info, mock_check, mock_is_up):
        host1 = {'hypervisor_type': 'a', 'hypervisor_version': 7}
        host2 = {'hypervisor_type': 'a', 'hypervisor_version': 6}
        mock_get_info.side_effect = [objects.ComputeNode(**host1),
                                     objects.ComputeNode(**host2)]

        self.assertRaises(exception.DestinationHypervisorTooOld,
                          self.task._check_requested_destination)
        mock_is_up.assert_called_once_with(self.destination)
        mock_check.assert_called_once_with()
        self.assertEqual([mock.call(self.instance_host),
                          mock.call(self.destination)],
                         mock_get_info.call_args_list)

    @mock.patch.object(objects.Service, 'get_by_compute_host')
    @mock.patch.object(live_migrate.LiveMigrationTask, '_get_compute_info')
    @mock.patch.object(servicegroup.API, 'service_is_up')
    @mock.patch.object(compute_rpcapi.ComputeAPI,
                       'check_can_live_migrate_destination')
    @mock.patch.object(objects.HostMapping, 'get_by_host',
                       return_value=objects.HostMapping(
                           cell_mapping=objects.CellMapping(
                               uuid=uuids.different)))
    def test_check_requested_destination_fails_different_cells(
            self, mock_get_host_mapping, mock_check, mock_is_up,
            mock_get_info, mock_get_host):
        mock_get_host.return_value = "service"
        mock_is_up.return_value = True
        hypervisor_details = objects.ComputeNode(
            hypervisor_type="a",
            hypervisor_version=6.1,
            free_ram_mb=513,
            memory_mb=512,
            ram_allocation_ratio=1.0)
        mock_get_info.return_value = hypervisor_details
        mock_check.return_value = "migrate_data"

        with test.nested(
            mock.patch.object(self.task.network_api,
                              'supports_port_binding_extension',
                              return_value=False),
            mock.patch.object(self.task, '_check_can_migrate_pci')):
            ex = self.assertRaises(exception.MigrationPreCheckError,
                                   self.task._check_requested_destination)
        self.assertIn('across cells', six.text_type(ex))

    @mock.patch.object(live_migrate.LiveMigrationTask,
                       '_call_livem_checks_on_host')
    @mock.patch.object(live_migrate.LiveMigrationTask,
                       '_check_compatible_with_source_hypervisor')
    @mock.patch.object(query.SchedulerQueryClient, 'select_destinations',
                       return_value=[[fake_selection1]])
    @mock.patch.object(objects.RequestSpec, 'reset_forced_destinations')
    @mock.patch.object(scheduler_utils, 'setup_instance_group')
    def test_find_destination_works(self, mock_setup, mock_reset, mock_select,
                                    mock_check, mock_call):
        self.assertEqual(("host1", "node1", fake_limits1),
                         self.task._find_destination())

        # Make sure the request_spec was updated to include the cell
        # mapping.
        self.assertIsNotNone(self.fake_spec.requested_destination.cell)
        # Make sure the spec was updated to include the project_id.
        self.assertEqual(self.fake_spec.project_id, self.instance.project_id)

        mock_setup.assert_called_once_with(self.context, self.fake_spec)
        mock_reset.assert_called_once_with()
        self.ensure_network_metadata_mock.assert_called_once_with(
            self.instance)
        self.heal_reqspec_is_bfv_mock.assert_called_once_with(
            self.context, self.fake_spec, self.instance)
        mock_select.assert_called_once_with(self.context, self.fake_spec,
            [self.instance.uuid], return_objects=True, return_alternates=False)
        mock_check.assert_called_once_with('host1')
        mock_call.assert_called_once_with('host1', {})

    @mock.patch.object(live_migrate.LiveMigrationTask,
                       '_call_livem_checks_on_host')
    @mock.patch.object(live_migrate.LiveMigrationTask,
                       '_check_compatible_with_source_hypervisor')
    @mock.patch.object(query.SchedulerQueryClient, 'select_destinations',
                       return_value=[[fake_selection1]])
    @mock.patch.object(scheduler_utils, 'setup_instance_group')
    def test_find_destination_no_image_works(self, mock_setup, mock_select,
                                             mock_check, mock_call):
        self.instance['image_ref'] = ''

        self.assertEqual(("host1", "node1", fake_limits1),
                         self.task._find_destination())

        mock_setup.assert_called_once_with(self.context, self.fake_spec)
        mock_select.assert_called_once_with(
            self.context, self.fake_spec, [self.instance.uuid],
            return_objects=True, return_alternates=False)
        mock_check.assert_called_once_with('host1')
        mock_call.assert_called_once_with('host1', {})

    @mock.patch.object(live_migrate.LiveMigrationTask,
                       '_remove_host_allocations')
    @mock.patch.object(live_migrate.LiveMigrationTask,
                       '_call_livem_checks_on_host')
    @mock.patch.object(live_migrate.LiveMigrationTask,
                       '_check_compatible_with_source_hypervisor')
    @mock.patch.object(query.SchedulerQueryClient, 'select_destinations',
                       side_effect=[[[fake_selection1]], [[fake_selection2]]])
    @mock.patch.object(scheduler_utils, 'setup_instance_group')
    def _test_find_destination_retry_hypervisor_raises(
            self, error, mock_setup, mock_select, mock_check, mock_call,
            mock_remove):
        mock_check.side_effect = [error, None]

        self.assertEqual(("host2", "node2", fake_limits2),
                         self.task._find_destination())
        # Should have removed allocations for the first host.
        mock_remove.assert_called_once_with(fake_selection1.compute_node_uuid)
        mock_setup.assert_called_once_with(self.context, self.fake_spec)
        mock_select.assert_has_calls([
            mock.call(self.context, self.fake_spec, [self.instance.uuid],
                      return_objects=True, return_alternates=False),
            mock.call(self.context, self.fake_spec, [self.instance.uuid],
                      return_objects=True, return_alternates=False)])
        mock_check.assert_has_calls([mock.call('host1'), mock.call('host2')])
        mock_call.assert_called_once_with('host2', {})

    def test_find_destination_retry_with_old_hypervisor(self):
        self._test_find_destination_retry_hypervisor_raises(
                exception.DestinationHypervisorTooOld)

    def test_find_destination_retry_with_invalid_hypervisor_type(self):
        self._test_find_destination_retry_hypervisor_raises(
                exception.InvalidHypervisorType)

    @mock.patch.object(live_migrate.LiveMigrationTask,
                       '_remove_host_allocations')
    @mock.patch.object(live_migrate.LiveMigrationTask,
                       '_call_livem_checks_on_host')
    @mock.patch.object(live_migrate.LiveMigrationTask,
                       '_check_compatible_with_source_hypervisor')
    @mock.patch.object(query.SchedulerQueryClient, 'select_destinations',
                       side_effect=[[[fake_selection1]], [[fake_selection2]]])
    @mock.patch.object(scheduler_utils, 'setup_instance_group')
    def test_find_destination_retry_with_invalid_livem_checks(
            self, mock_setup, mock_select, mock_check, mock_call, mock_remove):
        self.flags(migrate_max_retries=1)
        mock_call.side_effect = [exception.Invalid(), None]

        self.assertEqual(("host2", "node2", fake_limits2),
                         self.task._find_destination())
        # Should have removed allocations for the first host.
        mock_remove.assert_called_once_with(fake_selection1.compute_node_uuid)
        mock_setup.assert_called_once_with(self.context, self.fake_spec)
        mock_select.assert_has_calls([
            mock.call(self.context, self.fake_spec, [self.instance.uuid],
                      return_objects=True, return_alternates=False),
            mock.call(self.context, self.fake_spec, [self.instance.uuid],
                      return_objects=True, return_alternates=False)])
        mock_check.assert_has_calls([mock.call('host1'), mock.call('host2')])
        mock_call.assert_has_calls(
            [mock.call('host1', {}), mock.call('host2', {})])

    @mock.patch.object(live_migrate.LiveMigrationTask,
                       '_remove_host_allocations')
    @mock.patch.object(live_migrate.LiveMigrationTask,
                       '_call_livem_checks_on_host')
    @mock.patch.object(live_migrate.LiveMigrationTask,
                       '_check_compatible_with_source_hypervisor')
    @mock.patch.object(query.SchedulerQueryClient, 'select_destinations',
                       side_effect=[[[fake_selection1]], [[fake_selection2]]])
    @mock.patch.object(scheduler_utils, 'setup_instance_group')
    def test_find_destination_retry_with_failed_migration_pre_checks(
            self, mock_setup, mock_select, mock_check, mock_call, mock_remove):
        self.flags(migrate_max_retries=1)
        mock_call.side_effect = [exception.MigrationPreCheckError('reason'),
                                 None]

        self.assertEqual(("host2", "node2", fake_limits2),
                         self.task._find_destination())
        # Should have removed allocations for the first host.
        mock_remove.assert_called_once_with(fake_selection1.compute_node_uuid)
        mock_setup.assert_called_once_with(self.context, self.fake_spec)
        mock_select.assert_has_calls([
            mock.call(self.context, self.fake_spec, [self.instance.uuid],
                      return_objects=True, return_alternates=False),
            mock.call(self.context, self.fake_spec, [self.instance.uuid],
                      return_objects=True, return_alternates=False)])
        mock_check.assert_has_calls([mock.call('host1'), mock.call('host2')])
        mock_call.assert_has_calls(
            [mock.call('host1', {}), mock.call('host2', {})])

    @mock.patch.object(objects.Migration, 'save')
    @mock.patch.object(live_migrate.LiveMigrationTask,
                       '_remove_host_allocations')
    @mock.patch.object(live_migrate.LiveMigrationTask,
                       '_check_compatible_with_source_hypervisor',
                       side_effect=exception.DestinationHypervisorTooOld())
    @mock.patch.object(query.SchedulerQueryClient, 'select_destinations',
                       return_value=[[fake_selection1]])
    @mock.patch.object(scheduler_utils, 'setup_instance_group')
    def test_find_destination_retry_exceeds_max(
            self, mock_setup, mock_select, mock_check, mock_remove, mock_save):
        self.flags(migrate_max_retries=0)

        self.assertRaises(exception.MaxRetriesExceeded,
                          self.task._find_destination)
        self.assertEqual('failed', self.task.migration.status)
        mock_save.assert_called_once_with()
        # Should have removed allocations for the first host.
        mock_remove.assert_called_once_with(fake_selection1.compute_node_uuid)
        mock_setup.assert_called_once_with(self.context, self.fake_spec)
        mock_select.assert_called_once_with(
            self.context, self.fake_spec, [self.instance.uuid],
            return_objects=True, return_alternates=False)
        mock_check.assert_called_once_with('host1')

    @mock.patch.object(query.SchedulerQueryClient, 'select_destinations',
                       side_effect=exception.NoValidHost(reason=""))
    @mock.patch.object(scheduler_utils, 'setup_instance_group')
    def test_find_destination_when_runs_out_of_hosts(self, mock_setup,
                                                     mock_select):
        self.assertRaises(exception.NoValidHost, self.task._find_destination)
        mock_setup.assert_called_once_with(self.context, self.fake_spec)
        mock_select.assert_called_once_with(
            self.context, self.fake_spec, [self.instance.uuid],
            return_objects=True, return_alternates=False)

    @mock.patch("nova.utils.get_image_from_system_metadata")
    @mock.patch("nova.scheduler.utils.build_request_spec")
    @mock.patch("nova.scheduler.utils.setup_instance_group")
    @mock.patch("nova.objects.RequestSpec.from_primitives")
    def test_find_destination_with_remoteError(self,
        m_from_primitives, m_setup_instance_group,
        m_build_request_spec, m_get_image_from_system_metadata):
        m_get_image_from_system_metadata.return_value = {'properties': {}}
        m_build_request_spec.return_value = {}
        fake_spec = objects.RequestSpec()
        m_from_primitives.return_value = fake_spec
        with mock.patch.object(self.task.query_client,
            'select_destinations') as m_select_destinations:
            error = messaging.RemoteError()
            m_select_destinations.side_effect = error
            self.assertRaises(exception.MigrationSchedulerRPCError,
                              self.task._find_destination)

    def test_call_livem_checks_on_host(self):
        with test.nested(
                mock.patch.object(self.task.compute_rpcapi,
                                  'check_can_live_migrate_destination',
                                  side_effect=messaging.MessagingTimeout),
                mock.patch.object(self.task, '_check_can_migrate_pci')):
            self.assertRaises(exception.MigrationPreCheckError,
                              self.task._call_livem_checks_on_host, {}, {})

    def test_call_livem_checks_on_host_bind_ports(self):
        data = objects.LibvirtLiveMigrateData()
        bindings = {
            uuids.port1: {'host': 'dest-host'},
            uuids.port2: {'host': 'dest-host'}
        }

        @mock.patch.object(self.task, '_check_can_migrate_pci')
        @mock.patch.object(self.task.compute_rpcapi,
                           'check_can_live_migrate_destination',
                           return_value=data)
        @mock.patch.object(self.task.network_api,
                           'supports_port_binding_extension',
                           return_value=True)
        @mock.patch.object(self.task.network_api,
                           'bind_ports_to_host', return_value=bindings)
        def _test(mock_bind_ports_to_host,
                  mock_supports_port_binding,
                  mock_check_can_live_migrate_dest,
                  mock_check_can_migrate_pci):
            nwinfo = network_model.NetworkInfo([
                network_model.VIF(uuids.port1),
                network_model.VIF(uuids.port2)])
            self.instance.info_cache = objects.InstanceInfoCache(
                network_info=nwinfo)
            self.task._call_livem_checks_on_host('dest-host', {})
            # Assert the migrate_data set on the task based on the port
            # bindings created.
            self.assertIn('vifs', data)
            self.assertEqual(2, len(data.vifs))
            for vif in data.vifs:
                self.assertIn('source_vif', vif)
                self.assertEqual('dest-host', vif.host)
                self.assertEqual(vif.port_id, vif.source_vif['id'])

        _test()

    @mock.patch('nova.network.neutron.API.bind_ports_to_host')
    def test_bind_ports_on_destination_merges_profiles(self, mock_bind_ports):
        """Assert that if both the migration_data and the provider mapping
        contains binding profile related information then such information is
        merged in the resulting profile.
        """

        self.task.migrate_data = objects.LibvirtLiveMigrateData(
            vifs=[
                objects.VIFMigrateData(
                    port_id=uuids.port1,
                    profile_json=jsonutils.dumps(
                        {'some-key': 'value'}))
            ])
        provider_mappings = {uuids.port1: [uuids.dest_bw_rp]}

        self.task._bind_ports_on_destination('dest-host', provider_mappings)

        mock_bind_ports.assert_called_once_with(
            context=self.context, instance=self.instance, host='dest-host',
            vnic_types=None,
            port_profiles={uuids.port1: {'allocation': uuids.dest_bw_rp,
                                         'some-key': 'value'}})

    @mock.patch('nova.network.neutron.API.bind_ports_to_host')
    def test_bind_ports_on_destination_migration_data(self, mock_bind_ports):
        """Assert that if only the migration_data contains binding profile
        related information then that is sent to neutron.
        """

        self.task.migrate_data = objects.LibvirtLiveMigrateData(
            vifs=[
                objects.VIFMigrateData(
                    port_id=uuids.port1,
                    profile_json=jsonutils.dumps(
                        {'some-key': 'value'}))
            ])
        provider_mappings = {}

        self.task._bind_ports_on_destination('dest-host', provider_mappings)

        mock_bind_ports.assert_called_once_with(
            context=self.context, instance=self.instance, host='dest-host',
            vnic_types=None,
            port_profiles={uuids.port1: {'some-key': 'value'}})

    @mock.patch('nova.network.neutron.API.bind_ports_to_host')
    def test_bind_ports_on_destination_provider_mapping(self, mock_bind_ports):
        """Assert that if only the provider mapping contains binding
        profile related information then that is sent to neutron.
        """

        self.task.migrate_data = objects.LibvirtLiveMigrateData(
            vifs=[
                objects.VIFMigrateData(
                    port_id=uuids.port1)
            ])
        provider_mappings = {uuids.port1: [uuids.dest_bw_rp]}

        self.task._bind_ports_on_destination('dest-host', provider_mappings)

        mock_bind_ports.assert_called_once_with(
            context=self.context, instance=self.instance, host='dest-host',
            vnic_types=None,
            port_profiles={uuids.port1: {'allocation': uuids.dest_bw_rp}})

    @mock.patch(
        'nova.compute.utils.'
        'update_pci_request_spec_with_allocated_interface_name')
    @mock.patch('nova.scheduler.utils.fill_provider_mapping')
    @mock.patch.object(live_migrate.LiveMigrationTask,
                       '_call_livem_checks_on_host')
    @mock.patch.object(live_migrate.LiveMigrationTask,
                       '_check_compatible_with_source_hypervisor')
    @mock.patch.object(query.SchedulerQueryClient, 'select_destinations',
                       return_value=[[fake_selection1]])
    @mock.patch.object(objects.RequestSpec, 'reset_forced_destinations')
    @mock.patch.object(scheduler_utils, 'setup_instance_group')
    def test_find_destination_with_resource_request(
            self, mock_setup, mock_reset, mock_select, mock_check, mock_call,
            mock_fill_provider_mapping, mock_update_pci_req):
        resource_req = [objects.RequestGroup(requester_id=uuids.port_id)]
        self.mock_get_res_req.return_value = resource_req

        self.assertEqual(("host1", "node1", fake_limits1),
                         self.task._find_destination())

        # Make sure the request_spec was updated to include the cell
        # mapping.
        self.assertIsNotNone(self.fake_spec.requested_destination.cell)
        # Make sure the spec was updated to include the project_id.
        self.assertEqual(self.fake_spec.project_id, self.instance.project_id)
        # Make sure that requested_resources are added to the request spec
        self.assertEqual(
            resource_req, self.task.request_spec.requested_resources)

        mock_setup.assert_called_once_with(self.context, self.fake_spec)
        mock_reset.assert_called_once_with()
        self.ensure_network_metadata_mock.assert_called_once_with(
            self.instance)
        self.heal_reqspec_is_bfv_mock.assert_called_once_with(
            self.context, self.fake_spec, self.instance)
        mock_select.assert_called_once_with(self.context, self.fake_spec,
            [self.instance.uuid], return_objects=True, return_alternates=False)
        mock_check.assert_called_once_with('host1')
        mock_call.assert_called_once_with('host1', {uuids.port_id: []})
        mock_fill_provider_mapping.assert_called_once_with(
            self.task.request_spec, fake_selection1)
        mock_update_pci_req.assert_called_once_with(
            self.context, self.task.report_client, self.instance,
            {uuids.port_id: []})

    @mock.patch.object(objects.InstanceMapping, 'get_by_instance_uuid',
                       side_effect=exception.InstanceMappingNotFound(
                           uuid=uuids.instance))
    def test_get_source_cell_mapping_not_found(self, mock_get):
        """Negative test where InstanceMappingNotFound is raised and converted
        to MigrationPreCheckError.
        """
        self.assertRaises(exception.MigrationPreCheckError,
                          self.task._get_source_cell_mapping)
        mock_get.assert_called_once_with(
            self.task.context, self.task.instance.uuid)

    @mock.patch.object(objects.HostMapping, 'get_by_host',
                       side_effect=exception.HostMappingNotFound(
                           name='destination'))
    def test_get_destination_cell_mapping_not_found(self, mock_get):
        """Negative test where HostMappingNotFound is raised and converted
        to MigrationPreCheckError.
        """
        self.assertRaises(exception.MigrationPreCheckError,
                          self.task._get_destination_cell_mapping)
        mock_get.assert_called_once_with(
            self.task.context, self.task.destination)

    @mock.patch('nova.scheduler.client.report.SchedulerReportClient.'
                'remove_provider_tree_from_instance_allocation')
    def test_remove_host_allocations(self, remove_provider):
        self.task._remove_host_allocations(uuids.cn)
        remove_provider.assert_called_once_with(
            self.task.context, self.task.instance.uuid, uuids.cn)

    def test_check_can_migrate_pci(self):
        """Tests that _check_can_migrate_pci() allows live-migration if
        instance does not contain non-network related PCI requests and
        raises MigrationPreCheckError otherwise
        """

        @mock.patch.object(self.task.network_api,
                           'supports_port_binding_extension')
        @mock.patch.object(live_migrate,
                           'supports_vif_related_pci_allocations')
        def _test(instance_pci_reqs,
                  supp_binding_ext_retval,
                  supp_vif_related_pci_alloc_retval,
                  mock_supp_vif_related_pci_alloc,
                  mock_supp_port_binding_ext):
            mock_supp_vif_related_pci_alloc.return_value = \
                supp_vif_related_pci_alloc_retval
            mock_supp_port_binding_ext.return_value = \
                supp_binding_ext_retval
            self.task.instance.pci_requests = instance_pci_reqs
            self.task._check_can_migrate_pci("Src", "Dst")
            # in case we managed to get away without rasing, check mocks
            if instance_pci_reqs:
                mock_supp_port_binding_ext.assert_called_once_with(
                    self.context)
                self.assertTrue(mock_supp_vif_related_pci_alloc.called)

        # instance has no PCI requests
        _test(None, False, False)  # No support in Neutron and Computes
        _test(None, True, False)  # No support in Computes
        _test(None, False, True)  # No support in Neutron
        _test(None, True, True)  # Support in both Neutron and Computes
        # instance contains network related PCI requests (alias_name=None)
        pci_requests = objects.InstancePCIRequests(
            requests=[objects.InstancePCIRequest(alias_name=None)])
        self.assertRaises(exception.MigrationPreCheckError,
                          _test, pci_requests, False, False)
        self.assertRaises(exception.MigrationPreCheckError,
                          _test, pci_requests, True, False)
        self.assertRaises(exception.MigrationPreCheckError,
                          _test, pci_requests, False, True)
        _test(pci_requests, True, True)
        # instance contains Non network related PCI requests (alias_name!=None)
        pci_requests.requests.append(
            objects.InstancePCIRequest(alias_name="non-network-related-pci"))
        self.assertRaises(exception.MigrationPreCheckError,
                          _test, pci_requests, False, False)
        self.assertRaises(exception.MigrationPreCheckError,
                          _test, pci_requests, True, False)
        self.assertRaises(exception.MigrationPreCheckError,
                          _test, pci_requests, False, True)
        self.assertRaises(exception.MigrationPreCheckError,
                          _test, pci_requests, True, True)


text:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ITStudySearch.Models
{
    public class AtndJson
    {
        public class Event
        {
            public class Event2
            {
                public int event_id { get; set; }
                public string title { get; set; }
                public object _catch { get; set; }
                public string description { get; set; }
                public string event_url { get; set; }
                public DateTime started_at { get; set; }
                public DateTime? ended_at { get; set; }
                public string url { get; set; }
                public int limit { get; set; }
                public string address { get; set; }
                public string place { get; set; }
                public string lat { get; set; }
                public string lon { get; set; }
                public int owner_id { get; set; }
                public string owner_nickname { get; set; }
                public object owner_twitter_id { get; set; }
                public int accepted { get; set; }
                public int waiting { get; set; }
                public DateTime updated_at { get; set; }
            }

            public Event2 _event { get; set; }
        }

        public int results_returned { get; set; }
        public int results_start { get; set; }
        public List<Event> events { get; set; }
    }
}

redpajama_set_name: RedPajamaGithub


text:
<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Application\Utils;

/**
 * Description of Aggregate
 *
 * @author Alexander
 */
class Aggregate implements QueryAggregateInterface
{
    const NONE = 0;
    const COUNT = 1;
    const SUM = 2;
    const AVERAGE = 3;
    const MIN = 4;
    const MAX = 5;

    const SQL_NAMES = array(
        self::NONE => '',
        self::COUNT => 'COUNT',
        self::SUM => 'SUM',
        self::AVERAGE => 'AVG',
        self::MIN => 'MIN',
        self::MAX => 'MAX'
    );

    protected $propertyName;
    protected $aggregateType;
    protected $aggregateName;
    protected $group;

    /**
     *
     * @param string $propertyName
     * @param int $aggregateType \Application\Utils\AggregateType
     * @param string $aggregateName
     * @param bool $group
     */
    public function __construct($propertyName, $aggregateType, $aggregateName = null, $group = false)
    {
        $this->propertyName = $propertyName;
        $this->aggregateType = $aggregateType;
        $this->aggregateName = $aggregateName;
        $this->group = $group;
    }

    public function getPropertyName($prefix = '')
    {
        if ($this->getAggregateType() === self::COUNT) {
            $result = '*';
        } else {
            $result = $this->propertyName;
            if ($prefix) {
                $result = $prefix.'.'.$result;
            }
        }
        return $result;
    }

    public function getAggregateType()
    {
        return $this->aggregateType;
    }

    public function getAggregateName()
    {
        return $this->aggregateName;
    }

    public function getGroup()
    {
        return $this->group;
    }

    public function getAggregateExpression($prefix = '')
    {
        $sqlAggregate = self::SQL_NAMES[$this->getAggregateType()];
        if ($sqlAggregate) {
            return new \Zend\Db\Sql\Expression("$sqlAggregate({$this->getPropertyName($prefix)})");
        } else {
            return $this->getPropertyName($prefix);
        }
    }
}


text:
/*
 *
 */
package org.test.shop.util;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.StringUtils;
import org.test.shop.controller.data.types.Parametr;
import org.test.shop.controller.data.types.ScrollableSettings;
import org.test.shop.model.domain.SObject;
import org.test.shop.model.domain.SView;
import org.test.shop.model.domain.entity.secure.SasUser;
import org.test.shop.util.data.types.DateCompare;
import org.test.shop.util.data.types.DateQuery;
import org.test.shop.util.data.types.FindByDateExample;
import org.test.shop.util.data.types.FindByExample;
import org.test.shop.util.data.types.RangeDateQuery;
import org.test.shop.util.data.types.SingleDateQuery;
import org.test.shop.util.data.types.TotalQuery;

// TODO: Auto-generated Javadoc
/**
 * The Class DaoUtil.
 */
public class DaoUtil {

	private static String[] exceptEntity = {
			"ShopServHotel2View"
	};

	/**
	 * Read all fields.
	 *
	 * @param fields the fields
	 * @param type the type
	 * @return the list
	 */
	public static List<Field> readAllFields(List<Field> fields, Class<?> type) {
	    fields.addAll(Arrays.asList(type.getDeclaredFields()));

	    if (type.getSuperclass() != null) {
	        fields = readAllFields(fields, type.getSuperclass());
	    }

	    return fields;
	}

	/**
	 * Read type.
	 *
	 * @param value the value
	 * @param name the name
	 * @param owner the owner
	 * @return the string
	 */

	public static String readType(Object value, String name, String owner, DateCompare type) {
		String stringStatement = "lower(%s.%s) like lower(:%s)";
		String otherStatement = "%s.%s = :%s";

		if (value instanceof String) {
			return String.format(stringStatement, owner, name, name.toLowerCase());
		}
		else if (value instanceof Date) {
			return String.format(type.value(), owner, name, name);
		}

		return String.format(otherStatement, owner, name, name);
	}

	@SuppressWarnings("unchecked")
	public static <T> T readValue(T value) {
		String stringStatement = "#PERCENT%s#PERCENT";
		if (value instanceof String) {
			return (T) String.format(stringStatement, value).replace("#PERCENT", "%");
		}

		return value;
	}

	/**
	 * Read type.
	 *
	 * @param value the value
	 * @param name the name
	 * @param owner the owner
	 * @param parameterName the parameter name
	 * @return the string
	 */
	public static String readType(Object value, String name, String owner, String parameterName, DateCompare type) {
		String stringStatement = "%s.%s like :%s";
		String otherStatement = "%s.%s = :%s";

		if (value instanceof String) {
			return String.format(stringStatement, owner, name, parameterName);
		}
		else if (value instanceof Date) {
			return String.format(type.value(), owner, name, parameterName);
		}

		return String.format(otherStatement, owner, name, name);
	}

	/**
	 * Creates the statement.
	 *
	 * @param statement the statement
	 * @param values the values
	 * @param clazz the clazz
	 * @param em the em
	 * @return the query
	 */
	public static Query createStatement(String statement, Map<String, Object> values, Class<?> clazz, EntityManager em) {
		Query query = em.createQuery(statement, clazz);

		for (String key : values.keySet()) {
			Object value = values.get(key);
			if (value instanceof String) {
				query.setParameter(key, "%" + value + "%");
			}
			else {
				query.setParameter(key, value);
			}
		}

		return query;
	}

	/**
	 * Find by example.
	 *
	 * @param <T> the generic type
	 * @param example the example
	 * @param clazz the clazz
	 * @param em the em
	 * @return the list
	 */
	@SuppressWarnings("unchecked")
	public static <T> List<T> findByExample(T example, Class<?> clazz, EntityManager em) {
		return (List<T>) findByExampleQuery(example, clazz, em);
	}

	/**
	 * Find by example query.
	 *
	 * @param example the example
	 * @param clazz the clazz
	 * @param em the em
	 * @return the query
	 */
	public static Query findByExampleQuery(Object example, Class<?> clazz, EntityManager em) {
		FindByExample selectClause = findByExampleQueryRaw(example, clazz, em);
		return createStatement(selectClause.getSelectClause(), selectClause.getValues(), clazz, em);
	}

	/**
	 * Read s object.
	 *
	 * @param example the example
	 * @param clazz the clazz
	 * @param shortName the short name
	 * @param owner the owner
	 * @return the map
	 */
	private static Map<String, Object> readSObject(Object example, Class<?> clazz, String shortName, String owner) {
		String[] except = {
				  "serialVersionUID"
				, "createon"
				, "modifyon"
		};

		Map<String, Object> output = new HashMap<String, Object>();
		Map<String, Object> values = new HashMap<String, Object>();
		List<String> statement = new ArrayList<String>();
		List<Field> fields = readAllFields(new LinkedList<Field>(), clazz);

		for (Field field : fields) {
			field.setAccessible(true);
			try {
				Object value = field.get(example);
				Object fieldType = field.getType();

				if (value != null) {
					if (value instanceof LinkedList || field.getType() == List.class || fieldType instanceof List) {
						System.err.println("List: " + field.getName());
					}
					else if (value instanceof SObject) {
						System.err.println("SObject");
					}
					else {
						System.out.println(
								"\t DaoUtil.readSObject()" +
								"name: " + field.getName() +
								"\n\t " +
								"type: " + field.getType() +
								"\n\t" +
								" value: " + value.getClass() +
								"\n\t" +
								" fieldType: " + fieldType +
								"\n\t----------"
						);

						if (!Arrays.asList(except).contains(field.getName())) {
							values.put(owner + field.getName().toLowerCase(), value);
							statement.add(readType(value, field.getName().toLowerCase(), shortName + "." + owner, owner + field.getName().toLowerCase(), DateCompare.EQUALS));
						}
					}
				}
			} catch (IllegalArgumentException | IllegalAccessException e) {
				e.printStackTrace();
			}
		}

		output.put("values", values);
		output.put("statement", statement);

		return output;
	}

	public static FindByDateExample findByExample2(Object example, Class<?> clazz, EntityManager em, DateCompare type) {
		String[] except = {
				  "serialVersionUID"
				, "checked"
				, "sync"
		};

		Map<String, Object> values = new HashMap<String, Object>();
		List<String> statement = new ArrayList<String>();
		String shortName = clazz.getSimpleName().toLowerCase().substring(0, 3) + "0";
		String name = clazz.getSimpleName();

		String selectClause = "SELECT " + shortName + " FROM " + name + " " + shortName;
		readValues(example, clazz, except, values, statement, shortName, type);

		System.out.println("DaoUtil.findByExample2()#values: " + values.size() + ", statement: " + statement.size());

		if(statement.size() > 0){
			selectClause += " WHERE " + StringUtils.join(statement, null);
		}

		System.out.println("DaoUtil.findByExample2()#selectClause: " + selectClause);

		return new FindByDateExample(selectClause, values, em.createQuery(selectClause, clazz));
	}

	public static FindByExample findByExampleQueryRaw(Object example, Class<?> clazz, EntityManager em, String delimitter) {
		String[] except = {
				  "serialVersionUID"
				, "createon"
				, "modifyon"
				, "checked"
				, "sync"
		};
		if (delimitter == null || delimitter.replaceAll("\\s+", "").length() == 0) {
			delimitter = " AND ";
		}

		Map<String, Object> values = new HashMap<String, Object>();
		List<String> statement = new ArrayList<String>();
		String shortName = clazz.getSimpleName().toLowerCase().substring(0, 3) + "0";
		String name = clazz.getSimpleName();

		String selectClause = String.format("SELECT %s FROM %s %s ", shortName, name, shortName);
		readValues(example, clazz, except, values, statement, shortName, DateCompare.EQUALS);

		if(statement.size() > 0){
			selectClause += " WHERE " + StringUtils.join(statement, delimitter);
		}

		System.out.println("DaoUtil.findByExampleQueryRaw()\n" + selectClause + "\n");

		return new FindByExample(selectClause, values, em.createQuery(selectClause, clazz));
	}

	@SuppressWarnings("unchecked")
	private static void readValues(Object example, Class<?> clazz, String[] except, Map<String, Object> values
			, List<String> statement, String shortName, DateCompare type) {

		List<Field> fields = readAllFields(new LinkedList<Field>(), clazz);
		for (Field field : fields) {
			field.setAccessible(true);
			try {
				Object value = field.get(example);

				if (value != null && value.toString().replaceAll("\\s+", "").length() > 0) {
					if (value instanceof List) {

					}
					else if (value instanceof SObject) {
						Map<String, Object> result = readSObject(value, value.getClass(), shortName, field.getName());
						values.putAll((Map<? extends String, ? extends Object>) result.get("values"));
						statement.addAll((Collection<? extends String>) result.get("statement"));
					}
					else {
						if (!Arrays.asList(except).contains(field.getName())) {
							values.put(field.getName().toLowerCase(), readValue(value));
							statement.add(readType(value, field.getName(), shortName, type));
//							readType(value, field, owner, parameterName, type)
						}
					}
				}
			} catch (IllegalArgumentException | IllegalAccessException e) {
				e.printStackTrace();
			}
		}

	}

	public static FindByExample findByExampleQueryRaw(Object example, Class<?> clazz, EntityManager em) {
		return findByExampleQueryRaw(example, clazz, em, " AND ");
	}

	/**
	 * Sas query.
	 *
	 * @param query the query
	 * @param settings the settings
	 * @param clazz the clazz
	 * @param em the em
	 * @return the query
	 */
	public static Query sasQuery(String query, ScrollableSettings settings, Class<?> clazz, EntityManager em) {
		String innerQuery = query;
		Query jpaQuery = em.createQuery(query, clazz);
		Boolean whereIncluded = query.toLowerCase().contains("where");
		String name = clazz.getSimpleName();
		String shortName = clazz.getSimpleName().toLowerCase().substring(0, 3) + "0";
		Boolean isView = name.toLowerCase().endsWith("view");
		Boolean isDict = name.toLowerCase().startsWith("dict");
		Boolean isRel = name.toLowerCase().startsWith("rel");
		Boolean isUsual = !(isView || isDict || isRel);
		String delimiter = " WHERE ";

		if (settings.getSasUser() != null) {
			if (whereIncluded) {
				delimiter = " AND ";
			}

			SasUser user = settings.getSasUser();
			String createdby = null;
			Integer firmid = -1;

			if (settings.getOnlyUserData()) {
				createdby = user.getUser().getUsername();
				innerQuery += delimiter + shortName + ".createdby = :createdby";
			}
			else if (Arrays.asList(exceptEntity).contains(name)) {
				System.err.println("DaoUtil.sasQuery()#Found exception: " +  name);
			}
			else {
				firmid = user.getUser().getFirmid();
				if (isView) {
					innerQuery += delimiter + shortName + ".firm = :firm";
				}

				if(isUsual) {
					innerQuery += delimiter + shortName + ".firmid = :firmid";
				}
			}

			if (Arrays.asList(exceptEntity).contains(name)) {

			}
			else if (!isDict) {
				innerQuery += " ORDER BY " + shortName + ".modifyon DESC NULLS LAST";
			}
			else if(isDict){
				if(settings.getType() != null && settings.getType().length() > 0){
					whereIncluded = innerQuery.toLowerCase().contains("where");

					if(whereIncluded){
						innerQuery += " AND " + shortName + ".dictType = '" + settings.getType() + "'";
					}
					else {
						innerQuery += " WHERE " + shortName + ".dictType = '" + settings.getType() + "'";
					}
				}
			}

			jpaQuery = em.createQuery(innerQuery, clazz);
			if (settings.getOnlyUserData()) {
				jpaQuery.setParameter("createdby", createdby);
			}
			else {
				if (isView || isUsual) {
					if (innerQuery.contains(":firm")) {
						try {
							jpaQuery.setParameter("firm", firmid);
						} catch (Exception e) {

						}
					}

					if (innerQuery.contains(":firmid")) {
						try {
							jpaQuery.setParameter("firmid", firmid);
						} catch (Exception e) {

						}
					}
				}
			}
		}

		return jpaQuery;
	}

//	#-----------------
	/*
	@SuppressWarnings("unchecked")
	public static <T> List<T> findByExample(T example, Class<?> clazz, EntityManager em) {
		return (List<T>) findByExampleQuery(example, clazz, em);
	}

	public static Query findByExampleQuery(Object example, Class<?> clazz, EntityManager em) {
		FindByExample selectClause = findByExampleQueryRaw(example, clazz, em);
		return createStatement(selectClause.getSelectClause(), selectClause.getValues(), clazz, em);
	}

	public static FindByExample findByExampleQueryRaw(Object example, Class<?> clazz, EntityManager em) {
		String[] except = {
				  "serialVersionUID"
				, "createon"
				, "modifyon"
		};

		Map<String, Object> values = new HashMap<String, Object>();
		List<String> statement = new ArrayList<String>();
		String shortName = clazz.getSimpleName().toLowerCase().substring(0, 3) + "0";
		String name = clazz.getSimpleName();

		String selectClause = "SELECT " + shortName + " FROM " + name + " " + shortName;
		List<Field> fields = readAllFields(new LinkedList<Field>(), clazz);
		for (Field field : fields) {
			field.setAccessible(true);
			try {
				Object value = field.get(example);

				if (value != null) {
					if (value instanceof List) {
						if (((List) value).size() > 0) {
							values.put(field.getName().toLowerCase(), (List) value);
							statement.add(readType(value, field.getName().toLowerCase(), shortName));
						}
					}
					else {
						if (!Arrays.asList(except).contains(field.getName())) {
							values.put(field.getName().toLowerCase(), value);
							statement.add(readType(value, field.getName().toLowerCase(), shortName));
						}
					}
				}
			} catch (IllegalArgumentException | IllegalAccessException e) {
				e.printStackTrace();
			}
		}

		if(statement.size() > 0){
			selectClause += " WHERE " + StringUtils.join(statement, " AND ");
		}

		System.out.println(
				"\n\t  selectClause: " +
				selectClause +
				"\n\t, values: " +
				values
		);

		return new FindByExample(selectClause, values);
	}
	*/
	/*
	public static Query sasQuery(String query, ScrollableSettings settings, Class<?> clazz, EntityManager em) {
		String innerQuery = query;
		Query jpaQuery = em.createQuery(query, clazz);
		String name = clazz.getSimpleName();
		String shortName = clazz.getSimpleName().toLowerCase().substring(0, 3) + "0";
		String delimiter = " WHERE ";
		Boolean whereIncluded = query.toLowerCase().contains("where");
		Boolean isView = name.toLowerCase().endsWith("view");
		Boolean isDict = name.toLowerCase().startsWith("dict");
		Boolean isRel = name.toLowerCase().startsWith("rel");
		Boolean isUsual = !(isView || isDict || isRel);

		if (settings.getSasUser() != null) {
			if (whereIncluded) {
				delimiter = " AND ";
			}

			SasUser user = settings.getSasUser();
			String createdby = null;
			Integer firmid = -1;

			if (settings.getOnlyUserData()) {
				createdby = user.getUser().getUsername();
				innerQuery += delimiter + shortName + ".createdby = :createdby";
			}
			else {
				firmid = user.getUser().getFirmid();
				if (isView) {
					innerQuery += delimiter + shortName + ".firm = :firm";
				}

				if(isUsual) {
					innerQuery += delimiter + shortName + ".firmid = :firmid";
				}
			}

			jpaQuery = em.createQuery(innerQuery, clazz);
			if (settings.getOnlyUserData()) {
				jpaQuery.setParameter("createdby", createdby);
			}
			else {
				if (isView) {
					jpaQuery.setParameter("firm", firmid);
				}

				if(isUsual) {
					jpaQuery.setParameter("firmid", firmid);
				}

			}
		}

		return jpaQuery;
	}
	*/
	/**
	 * Sas query raw.
	 *
	 * @param query the query
	 * @param settings the settings
	 * @param clazz the clazz
	 * @param em the em
	 * @return the string
	 */
	public static String sasQueryRaw(String query, ScrollableSettings settings, Class<?> clazz, EntityManager em) {
		String innerQuery = query;
		Query jpaQuery = em.createQuery(query, clazz);
		Boolean whereIncluded = query.toLowerCase().contains("where");
		String name = clazz.getSimpleName();
		String shortName = clazz.getSimpleName().toLowerCase().substring(0, 3) + "0";
		Boolean isView = name.toLowerCase().endsWith("view");
		Boolean isDict = name.toLowerCase().startsWith("dict");
		Boolean isRel = name.toLowerCase().startsWith("rel");
		Boolean isUsual = !(isView || isDict || isRel);
		String delimiter = " WHERE ";

		if (settings.getSasUser() != null) {
			if (whereIncluded) {
				delimiter = " AND ";
			}

			SasUser user = settings.getSasUser();
			String createdby = null;
			Integer firmid = -1;

			if (settings.getOnlyUserData()) {
				createdby = user.getUser().getUsername();
				innerQuery += delimiter + shortName + ".createdby = :createdby";
			}
			else {
				firmid = user.getUser().getFirmid();
				if (isView) {
					innerQuery += delimiter + shortName + ".firm = :firm";
				}

				if(isUsual) {
					innerQuery += delimiter + shortName + ".firmid = :firmid";
				}
			}
		}

		return innerQuery;
	}

	/**
	 * Read short name.
	 *
	 * @param clazz the clazz
	 * @return the string
	 */
	public static String readShortName(Class<?> clazz){
		return clazz.getSimpleName().toLowerCase().substring(0, 3) + "0";
	}

	public static String readName(Class<?> clazz){
		return clazz.getSimpleName();
	}

	public static TotalQuery total(ScrollableSettings settings, Class<?> clazz, EntityManager em) throws Exception {
//		Long count = (Long) em.createQuery("SELECT count(*) from Goods goo0 WHERE goo0.firmid = :firmid")
		String queryTemplate = "SELECT count(*) from %s %s";
		String shortName = readShortName(clazz);
		String query = String.format(queryTemplate, readTableName(clazz), shortName);

		query += " WHERE " + shortName + ".firmid = :firmid";
		/*
		if (clazz == Photo.class) {
			DateQuery dateQuery = readDateParams(settings, shortName + ".createon");
			query += " AND " + dateQuery.getQuery();
		}
		*/
		TotalQuery totalQuery = new TotalQuery(query, em.createNativeQuery(query));
		totalQuery.setQuery(totalQuery.getQuery().setParameter("firmid", settings.getSasUser().getUser().getFirmid()));

		return totalQuery;
	}

	private static DateQuery readDateParams(ScrollableSettings settings, String name) throws Exception{
		Boolean isSingleDate = false;
		Boolean isRangeDate = false;
		String dateStart = null;
		String dateEnd = null;

		for (Parametr parametr : settings.getParametrs()) {
			if(parametr.getName().toLowerCase().equals("single_date")){
				isSingleDate = true;
			}
			else if(parametr.getName().toLowerCase().equals("range_date")){
				isRangeDate = true;
			}
			else if(parametr.getName().toLowerCase().equals("date_start")){
				dateStart = parametr.getValue();
			}
			else if(parametr.getName().toLowerCase().equals("date_end")){
				dateEnd = parametr.getValue();
			}
		}

		if(dateStart == null){
			throw new Exception("Cannot work with NULL parametr, set dateStart parametr");
		}
		else if(!isSingleDate && !isRangeDate){
			isSingleDate = true;
		}
		else if(dateEnd == null && isRangeDate){
			dateEnd = dateStart;
		}

		if (isSingleDate) {
			return new SingleDateQuery(name, dateStart);
		}
		else if (isRangeDate) {
			return new RangeDateQuery(name, dateStart, dateEnd);
		}

		return null;
	}

//	===================================================================

	public static Boolean isBoolean(String input) {
		Boolean intResult = input.matches("[0-1]{1}");
		Boolean stringResult = input.matches("true|false");

		if (intResult || stringResult) {
			return true;
		}

		return false;
	}

	public static Boolean isDate(String input) {
		return input.matches("[0-9]{4}-[0-9]{2}-[0-9]{2}");
	}

	public static Boolean isTimestamp(String input) {
		return input.matches("[0-9]{4}-[0-9]{2}-[0-9]{2}\\s[0-9]{2}:[0-9]{2}:[0-9]{2,}");
	}

	public static Boolean parseBoolean(String input) {
		Integer result = parseInteger(input);
		if (result == 1) {
			return true;
		}

		return false;
	}

	public static String parseDate(String input) {
		return input;
	}

	public static String parseTimestamp(String input) {
		return input;
	}

	public static Integer parseInteger(String input) {
		Integer result = null;

		try {
			result = Integer.parseInt(input);
		} catch (NumberFormatException e) {
			return null;
		}

		return result;
	}

	public static String parseColumnName(String input) {
		String result = "";
		for (char c : input.toCharArray()) {
			if (Character.isUpperCase(c)) {
				result += "_" + Character.toLowerCase(c);
			}
			else {
				result += c;
			}
		}

		return result;
	}
	/*
	public static String parseColumnName(String input) {
		return input;
	}
	*/
	public static String readType2(Class<?> type, String name, String owner, String filter, Boolean lower) {
		String stringStatement = "lower(%s.%s) like :filter";
		String integerStatement = "%s.%s = :filterForInteger";
		String booleanStatement = "%s.%s = :filterForBoolean";
		String dateStatement = "CAST(%s.%s as DATE) = CAST('%s' as DATE)";
		String timestampStatement = "%s.%s = CAST('%s' as TIMESTAMP)";
//		String otherStatement = "%s.%s = :filterForOther";

		if(lower){
			name = parseColumnName(name);
		}

		if (type == String.class) {
			return String.format(stringStatement, owner, name);
		}
		else if (type == Integer.class || type == BigDecimal.class) {
			if(parseInteger(filter) != null){
				return String.format(integerStatement, owner, name);
			}
		}
		else if (type == Boolean.class) {
			if (isBoolean(filter)) {
				return String.format(booleanStatement, owner, name, parseBoolean(filter));
			}
		}
		else if (type == Date.class) {
			if(isTimestamp(filter)){
				return String.format(timestampStatement, owner, name, parseTimestamp(filter));
			}
			if(isDate(filter)){
				return String.format(dateStatement, owner, name, parseDate(filter));
			}
		}

		return null;
	}

	public static FindByExample search(Object example, String filter, Class<?> clazz, EntityManager em) {
		String[] except = {
				  "serialVersionUID"
				, "createon"
				, "modifyon"
				, "details"
				, "sync"
		};

		List<String> statement = new ArrayList<String>();
		String shortName = clazz.getSimpleName().toLowerCase().substring(0, 3) + "0";
		String name = clazz.getSimpleName();
		Boolean needNativeQuery = false;

		if (isDate(filter) || isTimestamp(filter)) {
			needNativeQuery = true;
		}

		String selectClause = "SELECT " + shortName + " FROM " + name + " " + shortName;
		List<Field> fields = readAllFields(new LinkedList<Field>(), clazz);
		for (Field field : fields) {
			field.setAccessible(true);
			try {
				Object value = field.get(example);
				String fieldName = field.getName();
				Class<?> fieldClazz = field.getType();

				if (!(value instanceof List)) {
					if (!Arrays.asList(except).contains(fieldName)) {
						if(!hasTransientAnnotation(clazz, fieldName)){
							String readResult = readType2(fieldClazz, fieldName, shortName, filter, needNativeQuery);
							if (readResult != null) {
								statement.add(readResult);
							}
						}
					}
				}
			} catch (IllegalArgumentException | IllegalAccessException e) {
				e.printStackTrace();
			}
		}

		if(statement.size() > 0){
			selectClause += " WHERE " + StringUtils.join(statement, " OR ");
		}

		return new FindByExample(selectClause, "%" + filter.toLowerCase() + "%", needNativeQuery);
	}

	public static Boolean hasTransientAnnotation(Class<?> clazz, String fieldName) {
		for (Method method : clazz.getDeclaredMethods()) {
			Transient transient1 = method.getAnnotation(Transient.class);
			if (transient1 != null) {
				if (method.getName().toLowerCase().contains(fieldName.toLowerCase())) {
					return true;
				}
			}
		}

		return false;
	}

	private static Boolean containsField(Class clazz, String fieldName) {
		Boolean result = false;
		Field field = null;
		try {
			field = clazz.getDeclaredField(fieldName);
		} catch (NoSuchFieldException e1) {
			Class superClass = clazz.getSuperclass();
		      if (superClass == null) {
		    	  return false;
		      } else {
		        return containsField(superClass, fieldName);
		      }
		} catch (SecurityException e1) {

		}

    	if (field != null) {
			return true;
		}

	    return false;
	  }

	public static String readTableName(Class<?> clazz){
		Annotation[] annotations = clazz.getAnnotations();

		for(Annotation annotation : annotations){
		    if(annotation instanceof Table){
		    	Table myAnnotation = (Table) annotation;
		    	return myAnnotation.name();
		    }
		}

		return readName(clazz);
	}

	public static Query sasQueryByNamedFilter(String rawQuery, ScrollableSettings settings, Class<?> clazz, EntityManager em) {
		String query = rawQuery.replace("#TABLENAME", clazz.getSimpleName())
							   .replace("#SHORTNAME", readShortName(clazz))
							   ;

		return sasQuery(query, settings, clazz, em);
	}

}


text:
/**
 * (created at 2011-1-23)
 */
package com.alibaba.cobar.parser.ast.expression.primary.function.groupby;

import java.util.List;

import com.alibaba.cobar.parser.ast.expression.Expression;
import com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression;

/**
 * @author QIU Shuo
 */
public class Variance extends FunctionExpression {
    public Variance(List<Expression> arguments) {
        super("VARIANCE", arguments);
    }

    @Override
    public FunctionExpression constructFunction(List<Expression> arguments) {
        return new Variance(arguments);
    }

}


text:
{-# LANGUAGE DeriveGeneric #-}
{- |
The 'Image' data type. This should be imported qualified so as not
to conflict with the accessors from the 'Response' and 'Item' data types:

> import qualified IsoHunt.Response as Response
> import qualified IsoHunt.Item as Item
> import qualified IsoHunt.Image as Image
>
> ... Response.title r ... Item.title i ... Image.title im ...

These fields are mostly undocumented; see
<http://ca.isohunt.com/js/json.php?ihq=ubuntu&start=1&rows=4> for
an example response.

-}
module IsoHunt.Image(
  Image(..),
  ) where

import GHC.Generics
import Data.Text
import Data.Aeson
import Data.Typeable

data Image =
  Image {
    title :: Text,
    url :: Text,
    link :: Text,
    width :: Integer,
    height :: Integer
    } deriving(Eq, Ord, Show, Typeable, Generic)

instance FromJSON Image


text:
class linked_list
	def clone
		result = linked_list.new
		ptr = @head
		while not ptr.nil?
			result.append(ptr.datum)
			ptr = ptr.succ
		end
		result
	end
end


text:
package com.lachesis.mnis.core.doctree;

import java.util.List;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.lachesis.mnis.core.SpringTest;
import com.lachesis.mnis.core.doctree.entity.ComNavTree;
import com.lachesis.mnis.core.doctree.repository.DocTreeRepository;

public class DocTreeReositoryTest extends SpringTest{

	@Autowired
	DocTreeRepository docTreeRepository;

	@Test
	public void testGetComNavTrees(){
		List<ComNavTree> trees = docTreeRepository.getComNavTrees();
		Assert.assertNotNull(trees);
	}
}


text:
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license.
//
// Microsoft Bot Framework: http://botframework.com
//
// Bot Builder SDK GitHub:
// https://github.com/Microsoft/BotBuilder
//
// Copyright (c) Microsoft Corporation
// All rights reserved.
//
// MIT License:
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using Microsoft.Bot.Builder.Internals.Fibers;
using Microsoft.Bot.Builder.Scorables;
using Microsoft.Bot.Builder.Scorables.Internals;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.Bot.Builder.Tests
{
    public abstract class ScorableTestBase
    {
        public static Mock<IScorable<Item, Score>> Mock<Item, Score>(Item item, Score score, CancellationToken token, bool hasScore)
        {
            object state = new object();

            var mock = new Mock<IScorable<Item, Score>>(MockBehavior.Strict);
            mock.Setup(s => s.PrepareAsync(item, token)).Returns(Task.FromResult(state));
            mock.Setup(s => s.HasScore(item, state)).Returns(hasScore);
            mock.Setup(s => s.GetScore(item, state)).Returns(score);
            mock.Setup(s => s.PostAsync(item, state, token)).Returns(Task.CompletedTask);
            mock.Setup(s => s.DoneAsync(item, state, token)).Returns(Task.CompletedTask);

            return mock;
        }

        public static void Verify<Item, Score>(Mock<IScorable<Item, Score>> mock, Item item, CancellationToken token, Times prepare, Times hasScore, Times getScore, Times post, Times? done = null)
        {
            done = done ?? prepare;

            mock.Verify(s => s.PrepareAsync(item, token), prepare);
            mock.Verify(s => s.HasScore(item, It.IsAny<object>()), hasScore);
            mock.Verify(s => s.GetScore(item, It.IsAny<object>()), getScore);
            mock.Verify(s => s.PostAsync(item, It.IsAny<object>(), token), post);
            mock.Verify(s => s.DoneAsync(item, It.IsAny<object>(), token), done.Value);
        }

        public static Times Once(bool proposition)
        {
            return proposition ? Times.Once() : Times.Never();
        }

        public static Times Many(bool proposition)
        {
            return proposition ? Times.AtLeastOnce() : Times.Never();
        }
    }

    [TestClass]
    public sealed class ScorableTests : ScorableTestBase
    {
        public static readonly string Item = "hello";
        public static readonly double Score = 1.0;
        public static readonly CancellationToken Token = new CancellationTokenSource().Token;

        [TestMethod]
        public async Task Scorable_Where()
        {
            foreach (var hasScore in new[] { false, true })
            {
                foreach (var whereScore in new[] { false, true })
                {
                    // arrange
                    var mock = Mock(Item, Score, Token, hasScore);
                    var test = mock.Object.WhereScore((i, s) => whereScore);
                    // act
                    bool actualPost = await test.TryPostAsync(Item, Token);
                    // assert
                    bool expectedPost = hasScore && whereScore;
                    Assert.AreEqual(expectedPost, actualPost);
                    Verify(mock, Item, Token, Once(true), Once(true), Many(hasScore), Once(expectedPost));
                }
            }
        }

        [TestMethod]
        public async Task Scorable_First()
        {
            foreach (var hasScoreOne in new[] { false, true })
            {
                foreach (var hasScoreTwo in new[] { false, true })
                {
                    // arrange
                    var mockOne = Mock(Item, Score, Token, hasScoreOne);
                    var mockTwo = Mock(Item, Score, Token, hasScoreTwo);
                    var scorables = new[] { mockOne.Object, mockTwo.Object };
                    var test = scorables.First();
                    // act
                    bool actualPost = await test.TryPostAsync(Item, Token);
                    // assert
                    bool expectedPost = hasScoreOne || hasScoreTwo;
                    Assert.AreEqual(expectedPost, actualPost);
                    Verify(mockOne, Item, Token, Once(true), Many(true), Many(hasScoreOne), Once(hasScoreOne));
                    Verify(mockTwo, Item, Token, Once(!hasScoreOne), Many(!hasScoreOne), Many(!hasScoreOne && hasScoreTwo), Once(!hasScoreOne && hasScoreTwo));
                }
            }
        }

        public static readonly ITraits<double> Traits = NormalizedTraits.Instance;
        public static readonly IComparer<double> Comparer = Comparer<double>.Default;

        [TestMethod]
        public async Task Scorable_Fold_StableSort()
        {
            var scores = new[] { 0.1, 0.5, 0.5, 0.3, 0.3, 0.3, 0.4, 0.5 };
            var mocks = scores.Select(score => Mock(Item, score, Token, true)).ToArray();
            var sortedStartOrder = mocks.Select(m => m.Object).ToArray();
            var sortedScoreOrder = sortedStartOrder.OrderByDescending(s => scores[Array.IndexOf(sortedStartOrder, s)]).ToArray();

            var fold = sortedStartOrder.Fold(Comparer, (stage, scorable, item, state, score) =>
            {
                for (int indexStartOrder = 0; indexStartOrder < mocks.Length; ++indexStartOrder)
                {
                    int indexScoreOrder = Array.IndexOf(sortedScoreOrder, sortedStartOrder[indexStartOrder]);

                    var mock = mocks[indexStartOrder];
                    switch (stage)
                    {
                        case FoldStage.AfterFold:
                            bool afterFold = indexStartOrder <= Array.IndexOf(sortedStartOrder, scorable);
                            Verify(mock, Item, Token, Once(afterFold), Once(afterFold), Once(afterFold), Once(false), Once(false));
                            break;
                        case FoldStage.StartPost:
                            bool startPost = indexScoreOrder < Array.IndexOf(sortedScoreOrder, scorable);
                            Verify(mock, Item, Token, Once(true), Many(true), Many(true), Once(startPost), Once(false));
                            break;
                        case FoldStage.AfterPost:
                            bool afterPost = indexScoreOrder <= Array.IndexOf(sortedScoreOrder, scorable);
                            Verify(mock, Item, Token, Once(true), Many(true), Many(true), Once(afterPost), Once(false));
                            break;
                        default:
                            throw new NotImplementedException();
                    }
                }

                return true;
            });

            var success = await fold.TryPostAsync(Item, Token);
            Assert.IsTrue(success);

            foreach (var mock in mocks)
            {
                Verify(mock, Item, Token, Once(true), Many(true), Many(true), Once(true), Once(true));
            }
        }

        [TestMethod]
        public async Task Scorable_Traits()
        {
            var tests = new[]
            {
                new { hasScore = false, score = Traits.Minimum },
                new { hasScore = true, score = Traits.Minimum },
                new { hasScore = true, score = Traits.Maximum },
                new { hasScore = true, score = Traits.Minimum + (Traits.Maximum - Traits.Minimum) / 2.0 },
            };
            foreach (var testOne in tests)
            {
                foreach (var testTwo in tests)
                {
                    // arrange
                    var mockOne = Mock(Item, testOne.score, Token, testOne.hasScore);
                    var mockTwo = Mock(Item, testTwo.score, Token, testTwo.hasScore);
                    var scorables = new[] { mockOne.Object, mockTwo.Object };
                    var test = new TraitsScorable<string, double>(Traits, Comparer, scorables);
                    // act
                    bool actualPost = await test.TryPostAsync(Item, Token);
                    // assert
                    var expectedPost = testOne.hasScore || testTwo.hasScore;
                    Assert.AreEqual(expectedPost, actualPost);

                    bool earlyOut = testOne.hasScore && Comparer.Compare(testOne.score, Traits.Maximum) == 0;

                    var compare = Comparer.Compare(testOne.score, testTwo.score);
                    bool largeOne = (testOne.hasScore && !testTwo.hasScore) || (testOne.hasScore && testTwo.hasScore && compare >= 0);
                    bool largeTwo = (!testOne.hasScore && testTwo.hasScore) || (testOne.hasScore && testTwo.hasScore && compare < 0);

                    Verify(mockOne, Item, Token, Once(true), Many(true), Many(testOne.hasScore), Once(testOne.hasScore && largeOne));
                    Verify(mockTwo, Item, Token, Once(!earlyOut), Many(!earlyOut), Many(!earlyOut && testTwo.hasScore), Once(!earlyOut && testTwo.hasScore && largeTwo));
                }
            }
        }

        public static void TestReduceHelper<Item, Score>(params IScorable<Item, Score>[] test)
        {
            IEnumerable<IScorable<Item, Score>> scorables = test;
            IScorable<Item, Score> scorable;
            var success = Scorable.TryReduce(ref scorables, out scorable);

            var filtered = test.Where(Scorable.Keep).ToArray();
            switch (filtered.Length)
            {
                case 0:
                    Assert.IsTrue(success);
                    Assert.IsInstanceOfType(scorable, typeof(NullScorable<Item, Score>));
                    break;
                case 1:
                    Assert.IsTrue(success);
                    Assert.AreEqual(1, filtered.Length);
                    Assert.AreEqual(filtered[0], scorable);
                    break;
                default:
                    Assert.IsFalse(success);
                    Assert.IsNull(scorable);
                    CollectionAssert.AreEqual(filtered, (ICollection)scorables);
                    break;
            }
        }

        public static void TestReduce<Item, Score>()
        {
            Func<IScorable<Item, Score>> Null = () => NullScorable<Item, Score>.Instance;
            Func<IScorable<Item, Score>> Some = () => new Mock<IScorable<Item, Score>>().Object;

            for (int count = 0; count < 4; ++count)
            {
                for (int pattern = 0; pattern < (1 << count); ++pattern)
                {
                    var test = Enumerable.Range(0, count)
                        .Select((_, index) => (pattern & (1 << index)) != 0 ? Some() : Null())
                        .ToArray();

                    TestReduceHelper(test);
                }
            }
        }

        [TestMethod]
        public void Scorable_TryReduce()
        {
            TestReduce<string, Guid>();
        }

        public static void AssertComparer<T>(IComparer<T> comparer, params T[] items)
        {
            for (int one = 0; one < items.Length; ++one)
            {
                for (int two = 0; two < items.Length; ++two)
                {
                    var expected = one.CompareTo(two);
                    var itemOne = items[one];
                    var itemTwo = items[two];
                    var actual = comparer.Compare(itemOne, itemTwo);
                    Assert.AreEqual(expected, actual);
                }
            }
        }

        [TestMethod]
        public void Scorable_MatchComparer()
        {
            var regex = new Regex("hello");
            var matchFail = regex.Match("nothing");
            var matchSmall = regex.Match("extra garbage hello extra garbage");
            var matchLarge = regex.Match("hello");

            AssertComparer(MatchComparer.Instance, matchFail, matchSmall, matchLarge);
        }
    }
}