text:
package com.amazonaws.services.s3.model;
import java.io.Serializable;

import com.amazonaws.AmazonWebServiceRequest;

/**
 * Contains options for setting the notification configuration for a bucket.
 *
 * @see SetBucketNotificationConfigurationRequest#SetBucketNotificationConfigurationRequest(String, BucketNotificationConfiguration)
 */
public class SetBucketNotificationConfigurationRequest extends AmazonWebServiceRequest implements Serializable, ExpectedBucketOwnerRequest {

    private BucketNotificationConfiguration notificationConfiguration;

    private String bucketName;

    private String expectedBucketOwner;

    private Boolean skipDestinationValidation;

    /**
     * @deprecated Use SetBucketNotificationConfigurationRequest(String, BucketNotificationConfiguration) instead.
     */
    @Deprecated
    public SetBucketNotificationConfigurationRequest(BucketNotificationConfiguration bucketNotificationConfiguration,
            String bucket) {
        this.notificationConfiguration = bucketNotificationConfiguration;
        this.bucketName = bucket;
    }
    /**
     * Constructs a new {@link SetBucketNotificationConfigurationRequest}
     * to set the bucket notification configuration of
     * the specified bucket.
     *
     * @param bucketName
     *            The name of the bucket for which to set the notification
     *            configuration.
     * @param notificationConfiguration
     *            The new notification configuration for this bucket, which
     *            completely replaces any existing configuration.
     */
    public SetBucketNotificationConfigurationRequest(
            String bucketName, BucketNotificationConfiguration notificationConfiguration) {
        this.bucketName = bucketName;
        this.notificationConfiguration = notificationConfiguration;
    }

    public String getExpectedBucketOwner() {
        return expectedBucketOwner;
    }

    public SetBucketNotificationConfigurationRequest withExpectedBucketOwner(String expectedBucketOwner) {
        this.expectedBucketOwner = expectedBucketOwner;
        return this;
    }

    public void setExpectedBucketOwner(String expectedBucketOwner) {
        withExpectedBucketOwner(expectedBucketOwner);
    }

    /**
     * @deprecated Use getNotificationConfiguration() instead.
     */
    @Deprecated
    public BucketNotificationConfiguration getBucketNotificationConfiguration() {
        return notificationConfiguration;
    }

    /**
     * Gets the new notification configuration for the specified bucket.
     *
     * @return The new notification configuration for the specified bucket.
     *
     * @see SetBucketNotificationConfigurationRequest#withNotificationConfiguration(BucketNotificationConfiguration)
     */
    public BucketNotificationConfiguration getNotificationConfiguration() {
        return notificationConfiguration;
    }

    /**
     * @deprecated Use setNotificationConfiguration(BucketNotificationConfiguration) instead.
     */
    @Deprecated
    public void setBucketNotificationConfiguration(BucketNotificationConfiguration bucketNotificationConfiguration) {
        this.notificationConfiguration = bucketNotificationConfiguration;
    }

    /**
     * Sets the new notification configuration for the specified bucket.
     *
     * @param notificationConfiguration
     *            The new notification configuration for the specified bucket.
     *
     * @see SetBucketNotificationConfigurationRequest#withNotificationConfiguration(BucketNotificationConfiguration)
     */
    public void setNotificationConfiguration(BucketNotificationConfiguration notificationConfiguration) {
        this.notificationConfiguration = notificationConfiguration;
    }

    /**
     * Sets the new notification configuration for the specified bucket and
     * returns this object, enabling additional method calls to be chained
     * together.
     *
     * @param notificationConfiguration
     *            The new notification configuration for the specified bucket.
     *
     * @return This {@link SetBucketNotificationConfigurationRequest} object,
     *         enabling that additional method calls may be chained together.
     *
     * @see SetBucketNotificationConfigurationRequest#getNotificationConfiguration()
     */
    public SetBucketNotificationConfigurationRequest withNotificationConfiguration(
            BucketNotificationConfiguration notificationConfiguration) {
        setNotificationConfiguration(notificationConfiguration);
        return this;
    }

    /**
     * @deprecated Use getBucketName() instead.
     */
    @Deprecated
    public String getBucket() {
        return bucketName;
    }

    /**
     * Gets the name of the bucket whose notification configuration is being
     * set.
     *
     * @return The name of the bucket whose notification configuration is being
     *         set.
     *
     * @see SetBucketNotificationConfigurationRequest#setBucketName(String)
     */
    public String getBucketName() {
        return bucketName;
    }

    /**
     * @deprecated Use setBucketName(String) instead.
     */
    @Deprecated
    public void setBucket(String bucket) {
        this.bucketName = bucket;
    }

    /**
     * Sets the name of the bucket whose notification configuration is being
     * set.
     *
     * @param bucketName
     *            The name of the bucket whose notification configuration is
     *            being set.
     *
     * @see SetBucketNotificationConfigurationRequest#getBucketName()
     */
    public void setBucketName(String bucketName) {
        this.bucketName = bucketName;
    }

    /**
     * Sets the name of the bucket whose notification configuration is being
     * set, and returns this object so that additional method calls may be
     * chained together.
     *
     * @param bucketName
     *            The name of the bucket whose notification configuration is
     *            being set.
     *
     * @return This {@link SetBucketNotificationConfigurationRequest} object so
     *         that additional method calls may be chained together.
     *
     * @see SetBucketNotificationConfigurationRequest#setBucketName(String)
     */
    public SetBucketNotificationConfigurationRequest withBucketName(
            String bucketName) {
        setBucketName(bucketName);
        return this;
    }

    /**
     * Skips validation of SQS, SNS, and Lambda destinations
     */
    public Boolean getSkipDestinationValidation() {
        return skipDestinationValidation;
    }

    /**
     * Skips validation of SQS, SNS, and Lambda destinations
     */
    public void setSkipDestinationValidation(Boolean skipDestinationValidation) {
        this.skipDestinationValidation = skipDestinationValidation;
    }

    /**
     * Skips validation of SQS, SNS, and Lambda destinations
     */
    public SetBucketNotificationConfigurationRequest withSkipDestinationValidation(Boolean skipDestinationValidation) {
        this.skipDestinationValidation = skipDestinationValidation;
        return this;
    }
}


text:
// Copyright 2016, DELL EMC, Inc.

'use strict';
//jshint ignore: start

var di = require('di');

module.exports = DellWsmanDiscoveryJobFactory;
di.annotate(DellWsmanDiscoveryJobFactory, new di.Provide('Job.Dell.Wsman.Discovery'));
di.annotate(DellWsmanDiscoveryJobFactory, new di.Inject(
    'Job.Dell.Wsman.Base',
    'JobUtils.WsmanTool',
    'Logger',
    'Promise',
    'Assert',
    'Util',
    'Services.Waterline',
    'Services.Lookup',
    'Services.Configuration',
    '_',
    'Errors',
    'JobUtils.WorkflowTool',
    'Protocol.Events',
    'validator',
    'JobUtils.RedfishTool'
));

function DellWsmanDiscoveryJobFactory(
    BaseJob,
    WsmanTool,
    Logger,
    Promise,
    assert,
    util,
    waterline,
    lookup,
    configuration,
    _,
    errors,
    workflowTool,
    eventsProtocol,
    validator,
    RedfishTool
) {
    var logger = Logger.initialize(DellWsmanDiscoveryJobFactory);

    /**
     * @param {Object} options task options object
     * @param {Object} context graph context object
     * @param {String} taskId running task identifier
     * @constructor
     */
    function DellWsmanDiscoveryJob(options, context, taskId) {
        DellWsmanDiscoveryJob.super_.call(this,
                                   logger,
                                   options,
                                   context,
                                   taskId);

        assert.object(this.options);
        this.deviceTypesToDiscover = [];
        this.doInventory = options.inventory;
        this.ipRangesToProcess = 0;
        this.nodeId = this.context.target;

        this.newComputeNodes = [];
        this.newChassisNodes = [];
        this.newSwitchNodes = [];
        this.newStorageNodes = [];
        this.newIomNodes = [];
        this.generatedEnclosureNodes = [];
        this.tmpNodeArray = [];

        this.generatedRangeRequest = [];
        this.credentialMapping = [];
        this.redfish = new RedfishTool();
        this.nodesToProcess = 0;
    }

    util.inherits(DellWsmanDiscoveryJob, BaseJob);

    /**
     * @memberOf DellWsmanDiscoveryJob
     */
    DellWsmanDiscoveryJob.prototype._initJob = function () {
        var self = this;

        self.dell = configuration.get('dell');
        if (!self.dell || !self.dell.services || !self.dell.services.discovery) {
            throw new errors.NotFoundError('Dell Discovery web service is not defined in wsmanConfig.json.'); //jshint ignore:line
        }
    };

    DellWsmanDiscoveryJob.prototype.addCatalog = function(node, summary, name) {
        var self = this;

        return waterline.catalogs.findLatestCatalogOfSource(node.id, name)
        .then(function(catalog){
            if (_.isEmpty(catalog)) {
                logger.info("addCatalog: Catalog (" + name + ") not found.  Creating...");
                return waterline.catalogs.create({
                    node: node.id,
                    source: name,
                    data: summary
                });
            } else {
                logger.info("addCatalog: Catalog (" + name + ") found!  Updating...");
                return waterline.catalogs.updateByIdentifier(catalog.id, {data: summary});
            }
        })
        .catch(function(err) {
            logger.error("Job error processing catalog output.", {
                error: err,
                id: node,
                taskContext: self.context
            });
        });
    };

    DellWsmanDiscoveryJob.prototype._handleSyncRequest = function() {
        var self = this;

        var host = self.dell.gateway;
        var path = self.dell.services.discovery.range;
        var method = 'POST';

        if(self.options.deviceTypesToDiscover) {
            if(_.isArray(self.options.deviceTypesToDiscover)) {
                // this is for all ranges unless overridden at the range level
                self.deviceTypesToDiscover = self.options.deviceTypesToDiscover;
            } else {
                throw new Error('deviceTypesToDiscover must be an array. ex: ["SERVER", "CHASSIS"]');
            }
        }
        if(_.isEmpty(self.options.ranges)) {
            logger.info('Provided range list is empty. Skip discovering.');
            self._done();
            return Promise.resolve();
        }
        self.options.ranges.forEach(function(entry){
            if(!validator.isIP(entry.startIp) || !validator.isIP(entry.endIp)){
                throw new Error('Invalid IP range: (' + entry.startIp + ' - ' + entry.endIp + ')');
            }
            if(entry.deviceTypesToDiscover && !_.isArray(entry.deviceTypesToDiscover)) {
                throw new Error('deviceTypesToDiscover must be an array for range: (' + entry.startIp + ' - ' + entry.endIp + ')');
            }
        });
        if(!self.doInventory) {
            self.doInventory = 'false';
        } else {
            if(_.isBoolean(self.doInventory)) {
                if(self.doInventory === true) {
                    self.doInventory = 'true';
                } else {
                    self.doInventory = 'false';
                }
            }
        }

        var discoverIpRanges = [];
        self.options.ranges.forEach(function(range){
            if(!range.credentials || !range.credentials.userName || !range.credentials.password) {
                if(!self.options.credentials || !self.options.credentials.userName || !self.options.credentials.password) {
                    throw new Error('No credentials provided for range: (' + range.startIp + ' - ' + range.endIp + ')');
                } else {
                    range.credentials = self.options.credentials;
                }
            }
            if(!range.deviceTypesToDiscover || _.isEmpty(range.deviceTypesToDiscover)){
                if(!_.isEmpty(self.deviceTypesToDiscover)) {
                    range.deviceTypesToDiscover = self.deviceTypesToDiscover;
                }
            }
            var subRanges = self.getIpv4Ranges(range);
            self.ipRangesToProcess += subRanges.length;

            subRanges.forEach(function(entry){
                discoverIpRanges.push(entry);
                logger.debug('Discovering IP Range: ' + entry.deviceStartIp + ' - ' + entry.deviceEndIp);
            });
        });
        // At this point, we've assigned a credential to every range, so we just pass null userName/password
        // to the service as a global credential is required
        var data = {
               "credential":{
                    "userName": null,
                    "password": null
               },
               discoverIpRangeDeviceRequests: discoverIpRanges
        };
        self.generatedRangeRequest = discoverIpRanges;
        return Promise.resolve(self.generateCredentialMapping())
        .then(function(){
            logger.info('IP Range discovery submitted...');
            logger.debug('DISCOVERY SERVICE PAYLOAD: ' + JSON.stringify(data, null, 4));
            return self.clientRequest(host, path, method, data);
        });
    };

    DellWsmanDiscoveryJob.prototype.generateCredentialMapping = function() {
        var self = this;
        var ipToCredential = [];
        this.generatedRangeRequest.forEach(function(range){
            var _lastIp = range.deviceEndIp.split(".");
            var _firstIp = range.deviceStartIp.split(".");

            var first;
            var last;

            for(var i=0; i<=3; i++) {
                first |= (parseInt(_firstIp[i])) << ((3-i)*8);
                last |= (parseInt(_lastIp[i])) << ((3-i)*8);
            }
            var entry = {
                "firstIp": first,
                "lastIp": last,
                "credential": range.credential
            };
            ipToCredential.push(entry);
        });
        self.credentialMapping = ipToCredential;
        return ipToCredential;
    };

    DellWsmanDiscoveryJob.prototype.getCredential = function(ip, mapping) {
        var _ip = ip.split(".");
        var ipd, i;
        for(i=0; i<=3; i++) {
            ipd |= (parseInt(_ip[i])) << ((3-i)*8);
        }
        for(i=0; i<mapping.length; i++){
            var range = mapping[i];
            if(ipd >= range.firstIp && ipd <= range.lastIp){
                logger.debug('Found credential for Node: ' + ip);
                return range.credential;
            }
        }
        logger.error('No credential found for Node ' + ip);
        return undefined;
    };

    DellWsmanDiscoveryJob.prototype.getIpv4Ranges = function(entry) {
        var _lastIp = entry.endIp.split(".");
        var _firstIp = entry.startIp.split(".");

        var current;
        var last;

        for(var i=0; i<=3; i++) {
            current |= (parseInt(_firstIp[i])) << ((3-i)*8);
            last |= (parseInt(_lastIp[i])) << ((3-i)*8);
        }
        if((current & 0xffffff00) === (last & 0xffffff00)){ // this is a valid range
            logger.debug('GetRanges - Passed in range is valid... returning');
            return [{
                 "deviceType": entry.deviceTypesToDiscover,
                "deviceStartIp": entry.startIp,
                "deviceEndIp": entry.endIp,
                "credential": entry.credentials
            }];
        }

        var start = [];
        var end = [];
        var ranges = [];
        var newRange = {
            "deviceType": entry.deviceTypesToDiscover,
            "deviceStartIp": entry.startIp,
            "deviceEndIp": null,
            "credential": entry.credentials
        };

        current |= 0xFF;
        for(var i=0; i<=3; i++) {
            end[i] = (current >> ((3-i)*8)) & 0xff;
        }
        newRange.deviceEndIp = end.join('.');
        ranges.push(newRange);
        logger.debug('GetRanges - First sub range: ' + JSON.stringify(newRange));

        current += 2;  // increment from x.x.x.255 to x.x.x+1.1

        while((current & 0xffffff00) !== (last & 0xffffff00)){
            for(var i=0; i<=3; i++) {
                start[i] = (current >> ((3-i)*8)) & 0xff;
                if(i===3){
                    end[i] = 0xFF;
                } else {
                    end[i] = (current >> ((3-i)*8)) & 0xff;
                }
            }
            newRange = {
                "deviceType": entry.deviceTypesToDiscover,
                "deviceStartIp": null,
                "deviceEndIp": null,
                "credential": entry.credentials
            };
            newRange.deviceStartIp = start.join('.');
            newRange.deviceEndIp = end.join('.');
            ranges.push(newRange);
            logger.debug('GetRanges - Sub range: ' + JSON.stringify(newRange));

            current += 256;  // increment from x.x.x.255 to x.x.x+1.1
        }

        for(var i=0; i<=3; i++) {
            start[i] = (current >> ((3-i)*8)) & 0xff;
               end[i] = (last >> ((3-i)*8)) & 0xff;
        }

        newRange = {
                 "deviceType": entry.deviceTypesToDiscover,
                 "deviceStartIp": null,
                 "deviceEndIp": null,
                 "credential": entry.credentials
        };
        newRange.deviceStartIp = start.join('.');
        newRange.deviceEndIp = end.join('.');
        ranges.push(newRange);
        logger.debug('GetRanges - Last sub range: ' + JSON.stringify(newRange));

        return ranges;
    };

    DellWsmanDiscoveryJob.prototype.clientRequest = function(host, path, method, data) {
        var wsman = new WsmanTool(host, {
            verifySSL: false,
            recvTimeoutMs: 1800000
        });

        return wsman.clientRequest(path, method, data, 'IP is NOT valid or  httpStatusCode > 206 .')
        .then(function(response) {
            return response.body;
        });
    };

    /**
     * @memberOf DellWsmanDiscoveryJob
     */
    DellWsmanDiscoveryJob.prototype.collectInventory = function (device, node) {
        var self = this;
        if(self.doInventory.indexOf('true') !== -1  && device !== null){
            if(self.newChassisNodes.indexOf(node.id) !== -1 || self.newComputeNodes.indexOf(node.id) !== -1){
                logger.info('Starting POST DISCOVERY config for node: ' + node.name);
                workflowTool.runGraph(node.id, 'Graph.Dell.Wsman.PostDiscovery', null);
            }
        }
        return Promise.resolve();
    };

    /**
     * @memberOf DellWsmanDiscoveryJob
     */
    DellWsmanDiscoveryJob.prototype.doPublishDiscovered = function (node) {
        logger.debug('Publish node discovered event for node: ' + node.id);
        return Promise.resolve(eventsProtocol.publishNodeEvent(node, 'discovered'));
    };

    /**
     * @memberOf DellWsmanDiscoveryJob
     */
    DellWsmanDiscoveryJob.prototype.processStorage = function (deviceList) {
        var self = this;
        deviceList.forEach(function(entry){
            logger.info('Processing Storage - ' + 'TYPE: ' + entry.deviceName + ' | COUNT: ' + entry.discovered);
            entry.discoveredDeviceInfoList.forEach(function(device){
                var newNode = {
                        name: device.ipAddress,
                        type: 'storage',
                        identifiers: [device.ipAddress],
                        relations: []
                };
                if(device.macAddress){
                    newNode.identifiers.push(device.macAddress);
                }
                return waterline.nodes.findByIdentifier(newNode.name)
                .then(function(result){
                    if(!result){
                        return waterline.nodes.create(newNode)
                        .then(function (node_) {
                            logger.info('Created Storage Node (ID: ' + node_.id + ')');
                            self.newStorageNodes.push(node_.id);
                            node_.ip = device.ipAddress;
                            return self.doPublishDiscovered(node_)
                           .then(function(){
                               self.checkAllNodesProcessed();
                               if(self.doInventory.indexOf('true') !== -1) {
                                   return self.collectInventory(device, node_);
                               }
                           });
                        });
                    } else {
                        logger.info('Node: ' + newNode.name + ' TYPE: ' + entry.deviceName + ' already exists... Skipping.');
                        self.checkAllNodesProcessed();
                    }
                })
                .catch(function(err){
                    logger.error('Error occurred.', {error: err});
                    self.checkAllNodesProcessed();
                });
            });
        });
        return Promise.resolve();
    };

    /**
     * @memberOf DellWsmanDiscoveryJob
     */
    DellWsmanDiscoveryJob.prototype.processIom = function (deviceList) {
        var self = this;
        deviceList.forEach(function(entry){
            logger.info('Processing IOM - ' + 'TYPE: ' + entry.deviceName + ' | COUNT: ' + entry.discovered);
            entry.discoveredDeviceInfoList.forEach(function(device){
                var newNode = {
                        name: device.ipAddress,
                        type: 'iom',
                        identifiers: [device.ipAddress],
                        relations: []
                }
                if(device.macAddress){
                    newNode.identifiers.push(device.macAddress);
                }
                return waterline.nodes.findByIdentifier(newNode.name)
                .then(function(result){
                    if(!result){
                        return waterline.nodes.create(newNode)
                        .then(function (node_) {
                            logger.info('Created IOM Node (ID: ' + node_.id + ')');
                            self.newIomNodes.push(node_.id);
                            node_.ip = device.ipAddress;
                            return self.doPublishDiscovered(node_)
                           .then(function(){
                               self.checkAllNodesProcessed();
                               if(self.doInventory.indexOf('true') !== -1) {
                                   return self.collectInventory(device, node_);
                               }
                           })
                        })
                    } else {
                        logger.info('Node: ' + newNode.name + ' TYPE: ' + entry.deviceName + ' already exists... Skipping.');
                        self.checkAllNodesProcessed();
                    }
                })
                .catch(function(err){
                    logger.error('Error occurred.', {error: err});
                    self.checkAllNodesProcessed();
                });
            });
        });
        return Promise.resolve(deviceList);
    };

    /**
     * @memberOf DellWsmanDiscoveryJob
     */
    DellWsmanDiscoveryJob.prototype.processSwitch = function (deviceList) {
        var self = this;
        deviceList.forEach(function(entry){
            logger.info('Processing Switch - ' + 'TYPE: ' + entry.deviceName + ' | COUNT: ' + entry.discovered);
            entry.discoveredDeviceInfoList.forEach(function(device){
                var newNode = {
                        name: device.ipAddress,
                        type: 'switch',
                        identifiers: [device.ipAddress],
                        relations: []
                }
                if(device.macAddress){
                    newNode.identifiers.push(device.macAddress);
                }
                return waterline.nodes.findByIdentifier(newNode.name)
                .then(function(result){
                    if(!result){
                        return waterline.nodes.create(newNode)
                        .then(function (node_) {
                            logger.info('Created Switch Node (ID: ' + node_.id + ')');
                            self.newSwitchNodes.push(node_.id);
                            node_.ip = device.ipAddress;
                            return Promise.all([self.createDefaultObm(node_, device.ipAddress),
                                                self.doPublishDiscovered(node_)])
                            .then(function(){
                                self.checkAllNodesProcessed();
                                if(self.doInventory.indexOf('true') !== -1) {
                                    return self.collectInventory(device, node_);
                                }
                            })
                        })
                    } else {
                        logger.info('Node: ' + newNode.name + ' TYPE: ' + entry.deviceName + ' already exists... Skipping.');
                        self.checkAllNodesProcessed();
                    }
                })
                .catch(function(err){
                    logger.error('Error occurred.', {error: err});
                    self.checkAllNodesProcessed();
                });
            });
        })
        return Promise.resolve();
    }

    /**
     * @memberOf DellWsmanDiscoveryJob
     */
    DellWsmanDiscoveryJob.prototype.processChassis = function (deviceList) {
        var self = this;
        if(!deviceList){
            logger.info('No physical chassis found in discovery range.');
            return Promise.resolve();
        }
        deviceList.forEach(function(entry){
            logger.info('Processing Chassis - ' + 'TYPE: ' + entry.deviceName + ' | COUNT: ' + entry.discovered);
            entry.discoveredDeviceInfoList.forEach(function(device){
                if(device.summary === null || device.summary.serviceTag === null){
                    logger.error('No service tag returned for NODE: ' + device.ipAddress + '  Skipping...');
                    self.checkAllNodesProcessed();
                    return;
                }
                var newNode = {
                        name: device.ipAddress,
                        type: 'enclosure',
                        identifiers: [device.summary.serviceTag, device.ipAddress],
                        relations: []
                }
                if(device.macAddress){
                    newNode.identifiers.push(device.macAddress);
                }
                return waterline.nodes.findByIdentifier(newNode.name)
                .then(function(result){
                    if(!result){
                        return waterline.nodes.create(newNode)
                        .then(function (node_) {
                            logger.info('Created Chassis Node (ID: ' + node_.id + ')');
                            self.newChassisNodes.push(node_.id);
                            node_.ip = device.ipAddress;
                            var dmiCatalog = {
                                    'System Information': {
                                        'Manufacturer': device.summary.manufacturer || 'Dell Inc.',
                                        'Product Name': device.summary.model
                                    }
                            }
                            return Promise.all([self.addCatalog(node_, device.summary, 'DeviceSummary'),
                                                self.addCatalog(node_, dmiCatalog, 'dmi'),
                                                self.createWsmanObm(node_, device.ipAddress),
                                                self.doPublishDiscovered(node_)])
                            .then(function(){
                                self.checkAllNodesProcessed();
                                if(self.doInventory.indexOf('true') !== -1) {
                                    return self.collectInventory(device, node_);
                                }
                            });
                        });
                    } else {
                        logger.info('Node: ' + newNode.name + ' TYPE: ' + entry.deviceName + ' already exists... Skipping.');
                        self.checkAllNodesProcessed();
                    }
                })
                .catch(function(err){
                    logger.error('Error occurred.', {error: err});
                    self.checkAllNodesProcessed();
                });
            });
        });
        return Promise.resolve();
    };

    /**
     * @memberOf DellWsmanDiscoveryJob
     */
    DellWsmanDiscoveryJob.prototype.processCompute = function (deviceList) {
        var self = this;
        deviceList.forEach(function(entry){
            logger.info('Processing Servers - ' + 'TYPE: ' + entry.deviceName + ' | COUNT: ' + entry.discovered);
            entry.discoveredDeviceInfoList.forEach(function(device){
                if(device.summary === null || device.summary.serviceTag === null){
                    logger.error('No service tag returned for NODE: ' + device.ipAddress + '  Skipping...');
                    self.checkAllNodesProcessed();
                    return;
                }
                var newNode = {
                        name: device.ipAddress,
                        type: 'compute',
                        identifiers: [device.summary.serviceTag, device.ipAddress],
                        relations: []
                }
                if(device.macAddress){
                    newNode.identifiers.push(device.macAddress);
                }
                return waterline.nodes.findByIdentifier(newNode.name)
                .then(function(result){
                    if(!result){
                        return waterline.nodes.create(newNode)
                        .then(function (node_) {
                            logger.info('Created Server Node (ID: ' + node_.id + ')');
                            self.newComputeNodes.push(node_.id);
                            node_.ip = device.ipAddress;
                            var dmiCatalog = {
                                    'System Information': {
                                        'Manufacturer': device.summary.manufacturer || 'Dell Inc.',
                                        'Product Name': device.summary.model
                                    }
                            }
                            return Promise.all([self.createWsmanObm(node_, device.ipAddress),
                                                self.addCatalog(node_, device.summary, 'DeviceSummary'),
                                                self.addCatalog(node_, dmiCatalog, 'dmi'),
                                                self.doPublishDiscovered(node_)])
                            .then(function(){
                                self.createEnclosure(node_, device.ipAddress)
                                .then(function(encNode){
                                    self.checkAllNodesProcessed();
                                    if(self.doInventory.indexOf('true') !== -1) {
                                        return self.collectInventory(device, node_);
                                    }
                                });
                            });
                        });
                    } else {
                        logger.info('Node: ' + newNode.name + ' TYPE: ' + entry.deviceName + ' already exists... Skipping.');
                        self.checkAllNodesProcessed();
                    }
                })
                .catch(function(err){
                    logger.error('Error occurred.', {error: err});
                    self.checkAllNodesProcessed();
                });
            });
        });
        return Promise.resolve();
    };

    /**
     * @memberOf DellWsmanDiscoveryJob
     */
    DellWsmanDiscoveryJob.prototype._handleSyncResponse = function(result) {
        var self = this;
        var discoveredDeviceGroups = new Map();
        result.forEach(function(group){
            group.discoveredDeviceList.forEach(function(devices) {
                if(devices.discovered > 0) {
                    if(group.deviceGroup.indexOf('SERVER') !== -1
                    || group.deviceGroup.indexOf('CHASSIS') !== -1
                    || group.deviceGroup.indexOf('SWITCH') !== -1){
                        self.nodesToProcess += devices.discovered;
                    }
                    discoveredDeviceGroups.set(group.deviceGroup, group.discoveredDeviceList);
                    return;
                }
            });
        });
        logger.debug('NODE COUNT: ' + self.nodesToProcess);
        return Promise.resolve(discoveredDeviceGroups)
        .then(function(deviceGroups){
            return self.processChassis(deviceGroups.get('CHASSIS')) // always process chassis first
            .then(function(){
                deviceGroups.forEach(function(data, group){
                    switch(group){
                        case 'SERVER':
                            self.processCompute(data);
                        break;
                        case "CHASSIS":
                        break;
                        case "SWITCH":
                            self.processSwitch(data);
                        break;
                        case "IOM":
                            //self.processIom(data);
                        break;
                        case "STORAGE":
                            //self.processStorage(data);
                        break;
                    }
                })
            })
        })
    };

    DellWsmanDiscoveryJob.prototype.createEnclosure = function(node, ipAddr) {

        var self = this;

        var newNode = {
                name: node.name + '_Enclosure',
                type: 'enclosure',
                identifiers: [node.name + '_Enclosure'],
                relations: []
        }
        logger.info('Generating Enclosure Node for node: ' + node.name, + ' (Type: ' + node.type + ')');
        return waterline.nodes.create(newNode)
        .then(function (node_) {
            self.generatedEnclosureNodes.push(node_.id);
            return self.setRelationships(node, node_)
            .then(function(){
                return Promise.resolve(node_);
            })
        });
    }

    DellWsmanDiscoveryJob.prototype.createDefaultObm = function(node, ipAddr){

        var self = this;
        var credential = self.getCredential(ipAddr, self.credentialMapping);

        var settings = {
            service: "noop-obm-service",
            config: {"userName": credential.userName || self.dell.credentials.userName,
                        "password": credential.password || self.dell.credentials.password,
                        "host": ipAddr
            }
        };
        logger.debug('Creating NOOP OBM for node: ' + node.name);
        return waterline.obms.upsertByNode(node.id, settings)
    }

    DellWsmanDiscoveryJob.prototype.createWsmanObm = function(node, ipAddr){

        var self = this;
        var credential = self.getCredential(ipAddr, self.credentialMapping);

        var settings = {
            service: "dell-wsman-obm-service",
            config: {"user": credential.userName || self.dell.credentials.userName,
                        "password": credential.password || self.dell.credentials.password,
                        "host": ipAddr
            }
        }
        logger.info('Creating WSMan OBM for node: ' + node.name);
        return waterline.obms.upsertByNode(node.id, settings)
    }

    /**
     * @function createRedfishObm
     */
    DellWsmanDiscoveryJob.prototype.createRedfishObm = function (node, ipAddr, retries) {
        var self = this;
        var redfishType = 'Systems';
        if(node.type === 'enclosure'){
            redfishType = 'Chassis';
        }
        var credential = self.getCredential(ipAddr, self.credentialMapping);

        logger.info('Creating redfish OBM for node: ' + node.name);

        var uri = 'https://' + ipAddr + '/redfish/v1';

        var settings = {
            uri: uri,
            host: ipAddr,
            root: '/redfish/v1/',
            port: '',
            protocol: 'https',
            username: credential.userName || this.dell.credentials.userName,
            password: credential.password || this.dell.credentials.password,
            verifySSL: true,
            recvTimeoutMs: 15000
        };
        this.redfish.settings = settings;

        var rootPath = settings.root;
        return this.redfish.clientRequest(rootPath)
        .then(function(root) {
            if (!_.has(root.body, redfishType)) {
                logger.warning('No ' + redfishType + ' Members Found');
                return Promise.resolve();
            }
            var path = redfishType === 'Systems' ? root.body.Systems['@odata.id'] : root.body.Chassis['@odata.id'];
            return self.redfish.clientRequest(path)
            .then(function(res) {
                assert.object(res);
                settings.root = res.body.Members[0]['@odata.id'];
                return Promise.resolve({
                    config: settings,
                    service: 'redfish-obm-service'
                })
            })
        })
        .then(function(redfishObm){
            if(redfishObm) {
                logger.info('Persisting redfish OBM to db for node: ' + node.name);
                return waterline.obms.upsertByNode(node.id, redfishObm);
            } else {
                return Promise.resolve();
            }
        })
        .catch(function(err) {
            if(retries){
                logger.info('Failed to create Redfish OBMs. Retrying... ' + node.name);
                return self.createRedfishObm(node, ipAddr, --retries);
            }
            logger.error("Redfish call failed. No OBM settings created for " + node.name);
            return Promise.resolve();
        });
    };

    /**
     * @memberOf DellWsmanPostDiscoveryJob
     */
    DellWsmanDiscoveryJob.prototype.setRelationships = function (n1, n2) {
        var self = this;
        n1.relations.push({
            relationType: 'enclosedBy',
            targets: [n2.id]
        });
        return waterline.nodes.updateByIdentifier(
            n1.id,
            {relations: n1.relations}
        )
        .then(function(){
            if(_.isEmpty(n2.relations)){
                n2.relations.push({
                    relationType: 'encloses',
                    targets: [n1.id]
                });
            } else {
                var encloses = _.find(n2.relations, { 'relationType': 'encloses' } );
                encloses.targets.push(n1.id);
            }
        })
        .then(function(){
            return waterline.nodes.updateByIdentifier(
                n2.id,
                {relations: n2.relations}
            )
        });
    }

    DellWsmanDiscoveryJob.prototype.setComputeEnclosureToPhysicalChassisRelations = function (nodeId) {
        var self = this;
        var count = 1;

        return waterline.nodes.findByIdentifier(nodeId)
        .then(function(node){
            var target = _.find(node.relations, {'relationType': 'encloses'});
            if(target !== undefined){
                return waterline.nodes.findByIdentifier(target.targets[0])
                .then(function(enclosedNode){
                    return waterline.catalogs.findLatestCatalogOfSource(enclosedNode.id, 'devicesummary')
                    .then(function(catalog){
                        if(catalog.data.systemGeneration !== null){
                            if(catalog.data.systemGeneration.indexOf('Modular') !== -1){
                                return waterline.nodes.findByIdentifier(catalog.data.cmcip)
                                .then(function(chassis){
                                    if(_.isEmpty(chassis) === false){
                                        logger.info('Relating Compute Enclosure ' + node.name + ' to Physical Chassis ' + chassis.name);
                                        // Set up new chassis relations
                                        if(_.isEmpty(chassis.relations)){
                                            chassis.relations.push({
                                                relationType: 'encloses',
                                                targets: [node.id]
                                            });
                                        } else {
                                            var relation = _.find(chassis.relations, {'relationType': 'encloses'});
                                            relation.targets.push(node.id);
                                        }
                                        // Set up new node relations
                                        node.relations.push({
                                            relationType: 'enclosedBy',
                                            targets: [chassis.id]
                                        });
                                        return Promise.all([waterline.nodes.updateOne({id: chassis.id}, {relations: chassis.relations}),
                                                            waterline.nodes.updateOne({id: node.id}, {relations: node.relations})])
                                        .then(function(result){
                                            self.setEnclosures();
                                        })
                                    } else {
                                        logger.warning('Physical chassis for node ' + enclosedNode.name + ' was not found in discovered range.');
                                        self.setEnclosures();
                                    }
                                })
                            } else {
                                self.setEnclosures();
                            }
                        } else {
                            logger.error('System type unknown for node: ' + enclosedNode.name + ' Cannot determine physical chassis relationship.');
                            self.setEnclosures();
                        }
                    })
                })
                .catch(function(err){
                    logger.error('Error occurred.', {error: err});
                    self.setEnclosures();
                })
            } else {
                self.setEnclosures();
            }
        })
    }

        DellWsmanDiscoveryJob.prototype.checkAllNodesProcessed = function () {
            var self = this;
            logger.info('NODES TO PROCESS: ' + self.nodesToProcess);
            self.nodesToProcess--;
            if(self.nodesToProcess === 0) {
                self.tmpNodeArray = self.generatedEnclosureNodes.slice();
                if(self.tmpNodeArray.length > 0){
                    self.setEnclosures();
                } else {
                    logger.info('DISCOVERY COMPLETE.');
                    self._done();
                    return;
                }
            }
        }

        DellWsmanDiscoveryJob.prototype.setEnclosures = function () {
            var self = this;
            if(self.tmpNodeArray.length === 0){
                   logger.info('DISCOVERY COMPLETE.');
                self._done();
                return;
            }
            var nodeId = self.tmpNodeArray.shift();
            self.setComputeEnclosureToPhysicalChassisRelations(nodeId);
        }

    return DellWsmanDiscoveryJob;
}
//jshint ignore: end

redpajama_set_name: RedPajamaGithub


text:
using NUnit.Framework;
using StyletIoC;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace StyletUnitTests.StyletIoC
{
    [TestFixture]
    public class StyletIoCAutobindingTests
    {
        interface I1 { }
        class C11 : I1 { }
        class C12 : I1 { }
        abstract class C13 : I1 { }

        interface I2<T> { }
        class C21<T> : I2<T> { }
        class C22<T> : I2<T> { }

        interface I3<T> { }
        class C31 : I3<int> { }
        class C32 : I3<string> { }

        [Inject("Key")]
        class C4 { }

        interface I5 { }
        class C5 { }

        [Test]
        public void NongenericInterfaceToAllImplementations()
        {
            var builder = new StyletIoCBuilder();

            builder.Bind<I1>().ToAllImplementations();
            var ioc = builder.BuildContainer();

            var result = ioc.GetAll<I1>().ToList();
            Assert.AreEqual(2, result.Count);
            Assert.IsInstanceOf<C11>(result[0]);
            Assert.IsInstanceOf<C12>(result[1]);
        }

        [Test]
        public void GenericInterfaceToAllImplementations()
        {
            var builder = new StyletIoCBuilder();
            builder.Bind(typeof(I2<>)).ToAllImplementations();
            var ioc = builder.BuildContainer();

            var result = ioc.GetAll<I2<int>>().ToList();
            Assert.AreEqual(2, result.Count);
            Assert.IsInstanceOf<C21<int>>(result[0]);
            Assert.IsInstanceOf<C22<int>>(result[1]);
        }

        [Test]
        public void IgnoresAllImplementsWhichIsNotPossible()
        {
            var builder = new StyletIoCBuilder();
            builder.Bind<I1>().ToAllImplementations();
            var ioc = builder.BuildContainer();

            var result = ioc.GetAll<I1>().ToList();
            Assert.AreEqual(2, result.Count);
            Assert.IsNotInstanceOf<C13>(result[0]);
            Assert.IsNotInstanceOf<C13>(result[1]);
        }

        [Test]
        public void AutobindingBindsConcreteTypes()
        {
            var builder = new StyletIoCBuilder();
            builder.Autobind(Enumerable.Empty<Assembly>());
            var ioc = builder.BuildContainer();

            var result = ioc.Get<C11>();
            Assert.IsInstanceOf<C11>(result);
        }

        [Test]
        public void AutobindingBindsGenericTypes()
        {
            var builder = new StyletIoCBuilder();
            builder.Autobind();
            var ioc = builder.BuildContainer();

            var result = ioc.Get<C21<int>>();
            Assert.IsInstanceOf<C21<int>>(result);
        }

        [Test]
        public void AutobindingDoesNotBindInterfaceTypes()
        {
            var builder = new StyletIoCBuilder();
            builder.Autobind();
            var ioc = builder.BuildContainer();

            Assert.Throws<StyletIoCRegistrationException>(() => ioc.Get<I1>());
        }

        [Test]
        public void AutobindingRespectsKeys()
        {
            var builder = new StyletIoCBuilder();
            builder.Autobind();
            var ioc = builder.BuildContainer();

            var result = ioc.Get<C4>("Key");
            Assert.IsInstanceOf<C4>(result);
        }

        [Test]
        public void AutobindingBindingsCanBeReplaced()
        {
            var builder = new StyletIoCBuilder();
            builder.Autobind();
            builder.Bind<C11>().ToSelf().InSingletonScope();
            var ioc = builder.BuildContainer();

            var result1 = ioc.Get<C11>();
            var result2 = ioc.Get<C11>();
            Assert.AreEqual(result2, result1);
        }

        [Test]
        public void BindsGenericInterfaceToAllNonGenericImplementations()
        {
            var builder = new StyletIoCBuilder();
            builder.Bind(typeof(I3<>)).ToAllImplementations();
            var ioc = builder.BuildContainer();

            var c31 = ioc.Get<I3<int>>();
            Assert.IsInstanceOf<C31>(c31);
        }

        [Test]
        public void ToAllImplementationsEnumerableWithNoAssembliesLooksInCallingAssembly()
        {
            var builder = new StyletIoCBuilder();
            builder.Bind<I1>().ToAllImplementations((IEnumerable<Assembly>)null);
            var ioc = builder.BuildContainer();

            var results = ioc.GetAll<I1>().ToList();
            Assert.AreEqual(2, results.Count);
        }

        [Test]
        public void AutoBindWithNoAssembliesThrows()
        {
            var builder = new StyletIoCBuilder();
            builder.Assemblies = null;
            Assert.Throws<StyletIoCRegistrationException>(() => builder.Autobind());
        }

        [Test]
        public void AutobindSearchesPassedAssemblie()
        {
            var builder = new StyletIoCBuilder();
            builder.Assemblies = null;
            builder.Autobind(typeof(StyletIoCAutobindingTests).Assembly);
            var ioc = builder.BuildContainer();

            Assert.IsInstanceOf<C11>(ioc.Get<C11>());
        }

        [Test]
        public void ToAllImplementationsThrowsIfNoImplementationsFound()
        {
            var builder = new StyletIoCBuilder();
            builder.Bind<I5>().ToAllImplementations();
            Assert.Throws<StyletIoCRegistrationException>(() => builder.BuildContainer());
        }

        [Test]
        public void ToAllImplementationsDoesNotThrowIfNoImplementationsFoundAndAllowZeroImplementationsIsTrue()
        {
            var builder = new StyletIoCBuilder();
            builder.Bind<I5>().ToAllImplementations(allowZeroImplementations: true);
            IContainer ioc = null;
            Assert.DoesNotThrow(() => ioc = builder.BuildContainer());

            Assert.DoesNotThrow(() => ioc.GetAll<I5>());
        }
    }
}


text:
namespace fly::net {

//==================================================================================================
std::shared_ptr<SocketService> SocketService::create(
    std::shared_ptr<fly::task::SequencedTaskRunner> task_runner,
    std::shared_ptr<NetworkConfig> config)
{
    // SocketService has a private constructor, thus cannot be used with std::make_shared. This
    // class is used to expose the private constructor locally.
    struct SocketServiceImpl final : public SocketService
    {
        SocketServiceImpl(
            std::shared_ptr<fly::task::SequencedTaskRunner> task_runner,
            std::shared_ptr<NetworkConfig> config) noexcept :
            SocketService(std::move(task_runner), std::move(config))
        {
        }
    };

    return std::make_shared<SocketServiceImpl>(std::move(task_runner), std::move(config));
}

//==================================================================================================
SocketService::SocketService(
    std::shared_ptr<fly::task::SequencedTaskRunner> task_runner,
    std::shared_ptr<NetworkConfig> config) noexcept :
    m_task_runner(std::move(task_runner)),
    m_config(std::move(config))
{
    detail::initialize();
}

//==================================================================================================
SocketService::~SocketService() noexcept
{
    detail::deinitialize();
}

//==================================================================================================
void SocketService::remove_socket(socket_type handle)
{
    auto task = [handle](std::shared_ptr<SocketService> self) {
        auto compare_handles = [handle](Request const &request) {
            return handle == request.m_handle;
        };

        std::erase_if(self->m_write_requests, compare_handles);
        std::erase_if(self->m_read_requests, compare_handles);
    };

    std::weak_ptr<SocketService> weak_self = shared_from_this();
    m_task_runner->post_task(FROM_HERE, weak_self, std::move(task));
}

//==================================================================================================
void SocketService::notify_when_writable(socket_type handle, Notification &&callback)
{
    auto task = [handle, callback = std::move(callback)](std::shared_ptr<SocketService> self) {
        const bool should_poll = self->m_write_requests.empty() && self->m_read_requests.empty();
        self->m_write_requests.emplace_back(handle, std::move(callback));

        if (should_poll)
        {
            self->poll();
        }
    };

    std::weak_ptr<SocketService> weak_self = shared_from_this();
    m_task_runner->post_task(FROM_HERE, weak_self, std::move(task));
}

//==================================================================================================
void SocketService::notify_when_readable(socket_type handle, Notification &&callback)
{
    auto task = [handle, callback = std::move(callback)](std::shared_ptr<SocketService> self) {
        const bool should_poll = self->m_write_requests.empty() && self->m_read_requests.empty();
        self->m_read_requests.emplace_back(handle, std::move(callback));

        if (should_poll)
        {
            self->poll();
        }
    };

    std::weak_ptr<SocketService> weak_self = shared_from_this();
    m_task_runner->post_task(FROM_HERE, weak_self, std::move(task));
}

//==================================================================================================
void SocketService::poll()
{
    std::set<socket_type> writable;
    std::set<socket_type> readable;

    for (auto const &request : m_write_requests)
    {
        writable.insert(request.m_handle);
    }
    for (auto const &request : m_read_requests)
    {
        readable.insert(request.m_handle);
    }

    detail::select(m_config->socket_io_wait_time(), writable, readable);

    auto invoke = [](std::set<socket_type> const &ready, std::vector<Request> &pending) {
        for (socket_type handle : ready)
        {
            auto it =
                std::find_if(pending.begin(), pending.end(), [handle](Request const &request) {
                    return handle == request.m_handle;
                });

            std::invoke(std::move(it->m_callback));
            pending.erase(it);
        }
    };

    invoke(writable, m_write_requests);
    invoke(readable, m_read_requests);

    if (!m_write_requests.empty() || !m_read_requests.empty())
    {
        auto task = [](std::shared_ptr<SocketService> self) {
            self->poll();
        };

        std::weak_ptr<SocketService> weak_self = shared_from_this();
        m_task_runner->post_task(FROM_HERE, std::move(weak_self), std::move(task));
    }
}

//==================================================================================================
SocketService::Request::Request(socket_type handle, Notification callback) noexcept :
    m_handle(handle),
    m_callback(std::move(callback))
{
}

} // namespace fly::net


text:
=head1 LICENSE

  Copyright (c) 1999-2011 The European Bioinformatics Institute and
  Genome Research Limited.  All rights reserved.

  This software is distributed under a modified Apache license.
  For license details, please see

    http://www.ensembl.org/info/about/code_licence.html

=head1 CONTACT

  Please email comments or questions to the public Ensembl
  developers list at <ensembl-dev@ebi.ac.uk>.

  Questions may also be sent to the Ensembl help desk at
  <helpdesk@ensembl.org>.

=head1 NAME

Bio::EnsEMBL::Funcgen::DBSQL::ExperimentAdaptor - A database adaptor for fetching and
storing Funcgen Experiment objects.

=head1 SYNOPSIS

my $exp_a = $db->get_ExperimentAdaptor();
my $exp = $exp_a->fetch_by_name($name);

=head1 DESCRIPTION

The ExperimentAdaptor is a database adaptor for storing and retrieving
Funcgen Experiment objects.

=cut

use strict;
use warnings;

package Bio::EnsEMBL::Funcgen::DBSQL::ExperimentAdaptor;

use Bio::EnsEMBL::Utils::Exception qw( throw warning );
use Bio::EnsEMBL::Funcgen::Experiment;
use Bio::EnsEMBL::Funcgen::DBSQL::BaseAdaptor;

use vars qw(@ISA);

#May need to our this?
@ISA = qw(Bio::EnsEMBL::Funcgen::DBSQL::BaseAdaptor);

=head2 fetch_all_by_group

  Arg [1]    : int - dbID of array_chip
  Example    : my $array = $oaa->fetch_by_array_chip_dbID($ac_dbid);
  Description: Retrieves a named Array object from the database.
  Returntype : listref of Bio::EnsEMBL::Funcgen::Experiment objects
  Exceptions : None
  Caller     : General
  Status     : At risk

=cut

sub fetch_all_by_group {
    my $self = shift;

    throw("Not yet implemented");

    my $ac_dbid = shift;
    my $sth = $self->prepare("
		SELECT a.array_id
		FROM array a, array_chip ac
		WHERE a.array_id = ac.array_id
		AND ac.array_chip_id = $ac_dbid
	");

    $sth->execute();
    my ($array_id) = $sth->fetchrow();

    return $self->fetch_by_dbID($array_id);
}

=head2 fetch_by_name

  Arg [1]    : string - name of an Experiment
  Example    : my $exp = $exp_a->fetch_by_name('Exp-1');
  Description: Retrieves a named Experiment object from the database.
  Returntype : Bio::EnsEMBL::Funcgen::Experiment
  Exceptions : Throws if no name defined or if more than one returned
  Caller     : General
  Status     : At Risk -replace with fetch_all_by_name and fetch_by_name_group

=cut

sub fetch_by_name {
  my $self = shift;
  my $name = shift;

  throw("Need to specify and experiment name argument") if (! defined $name);

  my $result = $self->generic_fetch("e.name = '$name'");

  if (scalar @$result > 1) {
    throw("Experiment $name is not unique in the database, but only one result has been returned");
    #should have unique key of group_id and experiment_name
  }
  return $result->[0];
}

=head2 get_all_experiment_names

  Arg [1]    : (optional) boolean - flag to denote whether experiment is flagged for web display
  Example    : my @names = @{$exp_a->get_all_experiment_names()};
  Description: Retrieves names of all experiments.
  Returntype : ARRAYREF
  Exceptions : none
  Caller     : General
  Status     : At Risk -rename fetch?

=cut

sub get_all_experiment_names{
  my ($self, $displayable) = @_;

  my ($constraint);

  my $sql = "SELECT e.name FROM experiment e";
  my @names = map $_ = "@$_", @{$self->db->dbc->db_handle->selectall_arrayref($sql)};

  $sql .= ", status s WHERE e.experiment_id =\"s.table_id\" AND s.table_name=\"experiment\" AND s.state=\"DISPLAYABLE\"" if($displayable);

  #can we do return [map $_ = "@$_", @{$self->db->dbc->db_handle->selectall_arrayref($sql)}];
  return \@names;
}

#fetch_by_name_group

#=head2 fetch_all_by_design_type
#
#  Arg [1]    : List of strings - type(s) (e.g. AFFY or OLIGO)
#  Example    : my @arrays = @{$oaa->fetch_all_by_type('OLIGO')};
#  Description: Fetch all arrays of a particular type.
#  Returntype : Listref of Bio::EnsEMBL::Funcgen::Array objects
#  Exceptions : Throws if no type is provided
#  Caller     : General
#  Status     : Medium Risk

#=cut

#sub fetch_all_by_design_type {
#	my ($self, @types) = @_;

#	throw('Need type as parameter') if !@types;

#	my $constraint;
#	if (scalar @types == 1) {
#		$constraint = qq( oa.type = '$types[0]' );
#	} else {
#		$constraint = join q(','), @types;
#		$constraint = qq( oa.type IN ('$constraint') );
#	}

#	return $self->generic_fetch($constraint);
#}

=head2 fetch_attributes

  Arg [1]    : Bio::EnsEMBL::Funcgen::Experiment - array to fetch attributes for
  Example    : None
  Description: This function is solely intended to lazy load attributes into
               empty Experiment objects. You should not need to call this.
  Returntype : None
  Exceptions : None
  Caller     : Bio::EnsEMBL::Funcgen::Experiment getters
  Status     : Medium Risk

=cut

sub fetch_attributes {
    my $self = shift;
    my $array = shift;

    my $tmp_array = $self->fetch_by_dbID( $array->dbID() );
    %$array = %$tmp_array;
}

=head2 _tables

  Args       : None
  Example    : None
  Description: PROTECTED implementation of superclass abstract method.
               Returns the names and aliases of the tables to use for queries.
  Returntype : List of listrefs of strings
  Exceptions : None
  Caller     : Internal
  Status     : At risk

=cut

sub _tables {
	my $self = shift;

	#should we add group, target, design_type, experimental_variable?

	return ['experiment', 'e'];
}

=head2 _columns

  Args       : None
  Example    : None
  Description: PROTECTED implementation of superclass abstract method.
               Returns a list of columns to use for queries.
  Returntype : List of strings
  Exceptions : None
  Caller     : Internal
  Status     : At Risk

=cut

sub _columns {
	my $self = shift;

	return qw( e.experiment_id e.name e.experimental_group_id e.date e.primary_design_type e.description e.mage_xml_id);
}

=head2 _objs_from_sth

  Arg [1]    : DBI statement handle object
  Example    : None
  Description: PROTECTED implementation of superclass abstract method.
               Creates Array objects from an executed DBI statement
			   handle.
  Returntype : Listref of Bio::EnsEMBL::Funcgen::Experiment objects
  Exceptions : None
  Caller     : Internal
  Status     : At Risk

=cut

sub _objs_from_sth {
	my ($self, $sth) = @_;

	my (@result, $exp_id, $name, $group_id, $p_design_type, $date, $description, $xml_id);

	$sth->bind_columns(\$exp_id, \$name, \$group_id, \$date, \$p_design_type, \$description, \$xml_id);

	while ( $sth->fetch() ) {
	  my $exp = Bio::EnsEMBL::Funcgen::Experiment->new(
													   -DBID                => $exp_id,
													   -ADAPTOR             => $self,
													   -NAME                => $name,
													   -GROUP_ID            => $group_id,
													   -DATE                => $date,
													   -PRIMARY_DESIGN_TYPE => $p_design_type,
													   -DESCRIPTION         => $description,
													   -MAGE_XML_ID         => $xml_id,
													  );

	  push @result, $exp;

	}
	return \@result;
}

=head2 store

  Args       : List of Bio::EnsEMBL::Funcgen::Experiment objects
  Example    : $oaa->store($exp1, $exp2, $exp3);
  Description: Stores given Experiment objects in the database.
  Returntype : ARRAYREF of Bio::EnsEMBL::Funcgen::Experiment objects
  Exceptions : Throws is group not present in DB
               Throws if object is not a Bio::EnsEMBL::Funcgen::Experiment
               Throws if object is already present in the DB but has no dbID
  Caller     : General
  Status     : At Risk

=cut

sub store {
    my $self = shift;
    my @args = @_;

	my ($s_exp);

	my $sth = $self->prepare('INSERT INTO experiment
                                 (name, experimental_group_id, date, primary_design_type, description, mage_xml_id)
                                 VALUES (?, ?, ?, ?, ?, ?)');

    foreach my $exp (@args) {
	  throw('Can only store Experiment objects') 	if ( ! $exp->isa('Bio::EnsEMBL::Funcgen::Experiment'));

	  if (!( $exp->dbID() && $exp->adaptor() == $self )){

		my ($g_dbid) = $self->db->fetch_group_details($exp->group());
		throw("Group specified does, not exist.  Use Importer(group, location, contact)") if(! $g_dbid);

		$s_exp = $self->fetch_by_name($exp->name());#validate on group too!
		throw("Experimental already exists in the database with dbID:".$s_exp->dbID().
			  "\nTo reuse/update this Experimental you must retrieve it using the ExperimentalAdaptor".
			  "\nMaybe you want to use the -recover option?") if $s_exp;

		$exp = $self->update_mage_xml_by_Experiment($exp) if(defined $exp->mage_xml());

		$sth->bind_param(1, $exp->name(),                SQL_VARCHAR);
		$sth->bind_param(2, $g_dbid,                     SQL_INTEGER);
		$sth->bind_param(3, $exp->date(),                SQL_VARCHAR);#date?
		$sth->bind_param(4, $exp->primary_design_type(), SQL_VARCHAR);
		$sth->bind_param(5, $exp->description(),         SQL_VARCHAR);
		$sth->bind_param(6, $exp->mage_xml_id(),         SQL_INTEGER);

		$sth->execute();
		$exp->dbID($sth->{'mysql_insertid'});
		$exp->adaptor($self);

			#do we need to set egroup, target, design_type, experimentall_variable here?
			#}
			#else{
			#	warn("Experiment already exists in DB, using previously stored Experiment\n");
			#	$exp = $s_exp;
			#}
		}else{
			#assume we want to update the states
			warn('You may want to use $exp->adaptor->store_states($exp)');
			$self->store_states($exp);
		}
	}

    return \@args;
}

=head2 fetch_mage_xml_by_Experiment

  Args       : Bio::EnsEMBL::Funcgen::Experiment
  Example    : my $xml = $exp_adaptor->fetch_mage_xml_by_Experiment($exp);
  Description: Gets the MAGE XML for this experiment
  Returntype : string
  Exceptions : throws if arg is not a valid stored Experiment
  Caller     : general
  Status     : at Risk

=cut

sub fetch_mage_xml_by_Experiment{
  my ($self, $exp) = @_;

  if(!($exp and $exp->isa('Bio::EnsEMBL::Funcgen::Experiment') && $exp->dbID())){
	throw('You must provide a valid stored Bio::EnsEMBL::Funcgen::Experiment');
  }

  return if ! $exp->mage_xml_id();

  my $sql = 'SELECT xml FROM mage_xml WHERE mage_xml_id='.$exp->mage_xml_id;

  return $self->db->dbc->db_handle->selectall_arrayref($sql)->[0];
}

=head2 fetch_mage_xml_by_experiment_name

  Args       :
  Example    : my $xml = $exp_adaptor->fetch_mage_xml_by_Experiment($exp);
  Description: Gets the MAGE XML for this experiment
  Returntype : string
  Exceptions : throws if no arg passed
  Caller     : general
  Status     : at Risk

=cut

sub fetch_mage_xml_by_experiment_name{
  my ($self, $exp_name) = @_;

  if(! defined $exp_name){
	throw('You must provide an Experiment name argument');
  }

  my $sql = 'SELECT mx.xml FROM mage_xml mx, experiment e WHERE e.name="'.$exp_name.'" and e.mage_xml_id=mx.mage_xml_id';

  return $self->db->dbc->db_handle->selectall_arrayref($sql)->[0];
}

=head2 update_mage_xml_by_Experiment

  Args       : Bio::EnsEMBL::Funcgen::Experiment
  Example    : my $xml = $exp_adaptor->fetch_mage_xml_by_Experiment($exp);
  Description: Gets the MAGE XML for this experiment
  Returntype : string
  Exceptions : throws if arg is not a valid stored Experiment
  Caller     : general
  Status     : at Risk

=cut

sub update_mage_xml_by_Experiment{
  my ($self, $exp) = @_;

  if(!($exp and $exp->isa('Bio::EnsEMBL::Funcgen::Experiment'))){
	throw('You must provide a valid Bio::EnsEMBL::Funcgen::Experiment');
  }

  if($exp->mage_xml_id()){
	#potentially calling dbID on a un-stored obj, implicit that it
	warn('Overwriting mage_xml entry for Experiment:    '.$exp->name);
	my $sql = "UPDATE mage_xml set xml='".$exp->mage_xml()."'";
	$self->db->dbc->do($sql);

  }else{
	my $sql = "INSERT INTO mage_xml (xml) VALUES('".$exp->mage_xml()."')";
	#need to get a statement handle to retrieve insert id
	my $sth = $self->prepare($sql);
	$sth->execute();
	$exp->mage_xml_id($sth->{'mysql_insertid'});

	$sql = "UPDATE experiment set mage_xml_id=".$exp->mage_xml_id()." where experiment_id =".$exp->dbID();
	$sth = $self->prepare($sql);
	$sth->execute();
  }

  return $exp;
}

=head2 list_dbIDs

  Args       : None
  Example    : my @exp_ids = @{$exp_a->list_dbIDs()};
  Description: Gets an array of internal IDs for all Experiment objects in the
               current database.
  Returntype : List of ints
  Exceptions : None
  Caller     : ?
  Status     : Medium Risk

=cut

sub list_dbIDs {
    my ($self) = @_;

    return $self->_list_dbIDs('experiment');
}

#Need to add lazy load methods
#experimental_variables
#group

1;


text:
var s_fSortFunction = function(a, b){
    return a.zIndex - b.zIndex;
};

function DisplayList()
{
    this.m_aDisplayList = [];
}

DisplayList.SetSortFunction = function(fSort){
    s_fSortFunction = fSort;
};

DisplayList.prototype.Add = function(cGameObject){
    if (this.m_aDisplayList.indexOf(cGameObject) == -1)
    {
        this.m_aDisplayList.push(cGameObject);
    }
};

DisplayList.prototype.Remove = function(cGameObject){
    var nIndex = this.m_aDisplayList.indexOf(cGameObject);

    if (nIndex >= 0)
    {
        this.m_aDisplayList.splice(nIndex, 1);
    }
};

DisplayList.prototype.Draw = function(cRenderer){
    this.m_aDisplayList.sort(s_fSortFunction);
    this.m_aDisplayList.forEach(function(cGameObject){
        if (cGameObject.Active)
        {
            cGameObject.Draw(cRenderer);
        }
    });
};

EN.DisplayList = DisplayList;


text:
from django.urls import include, path

#
# On Reversing URLs
# =================
#
# Recommended Usage:
# Always use rest_framework.reverse.reverse, do not directly use django.urls.reverse.
# If a request object r is available, use reverse(name, request=r). With the name as defined
# in desecapi.urls.v1 or desecapi.urls.v2. It will return an URL maintaining the currently requested API version.
# If there is no request object available, e.g. in commands, a mock object can be constructed
# carrying all information that is necessary to construct a full URL:
#
#         from django.conf import settings
#         from django.test import RequestFactory
#         from rest_framework.versioning import NamespaceVersioning
#
#         r = RequestFactory().request(HTTP_HOST=settings.ALLOWED_HOSTS[0])
#         r.version = 'v1'
#         r.versioning_scheme = NamespaceVersioning()
#
# Also note in this context settings.REST_FRAMEWORK['ALLOWED_VERSIONS'] and
# settings.REST_FRAMEWORK['DEFAULT_VERSIONING_CLASS']. (The latter is of type string.)
#
# Advanced Usage:
# Prefix the name of any path with 'desecapi' to get the default version,
# or prefix the name of any path with the desired namespace, e.g. 'v1:root'.
# In this case, the version information of the request will be ignored and
# providing a request object is optional. However, if no request object is provided,
# only a relative URL can be generated.
#
# Examples:
# The examples refer to the version used by the client to connect as the REQUESTED version,
# the version specified by the first argument to reverse as the SPECIFIED version, and to the
# version defined as default (see below) as the DEFAULT version.
#
#         reverse('root', request) -> absolute URL, e.g. https://.../api/v1/, with the REQUESTED version
#         reverse('root') -> django.urls.exceptions.NoReverseMatch
#         reverse('desecapi:root') -> relative URL, e.g. api/v1/, with the DEFAULT version
#         reverse('v2:root') -> relative URL, e.g. api/v2/, with the SPECIFIED version
#         reverse('v2:root', request) -> absolute URL, e.g. https://.../api/v2/, with the SPECIFIED version
#         reverse('desecapi:root', request) -> absolute URL, e.g. https://.../api/v1/, with the DEFAULT version
#         reverse('v1:root', request) -> absolute URL, e.g. https://.../api/v1/, with the SPECIFIED version
#
# See Also:
# https://github.com/encode/django-rest-framework/issues/5659
# https://github.com/encode/django-rest-framework/issues/3825
#
# Note that from the client's perspective, there is no default version: each request needs to
# specify the version in the request URL.
#

# IMPORTANT: specify default version as the last element in the list
# if no other information is available, the last-specified version will be used as default for reversing URLs
urlpatterns = [
    # other available versions in no particular order
    path("api/v2/", include("desecapi.urls.version_2", namespace="v2")),
    # the DEFAULT version
    path("api/v1/", include("desecapi.urls.version_1", namespace="v1")),
    # monitoring
    path("", include("django_prometheus.urls")),
]


text:
<?xml version="1.0"?>
<emailTemplates>
<emailTemplate>
<subject>${localSakaiName} Join Notification: Someone has joined your "${worksiteName}" site!</subject>
<message>${currentUserFirstName} ${currentUserLastName} (${currentUserDisplayId}) has joined your "${worksiteName}" site on ${localSakaiName}.
You will now see ${currentUserDisplayName} listed in your site's Site Info participant list.

If you would like to turn Join Notifications off for this site, please:

    1) Log into ${localSakaiName}
    2) Navigate to ${worksiteName}
    3) Click 'Site Info' from the list of tools on the left hand side
    4) Click 'Manage Access' from the menu bar at the top
    5) Uncheck 'Notify site maintainers by email when a user joins this site'
    6) Click the 'Update' button

Regards,

The ${localSakaiName} Administrators
${institution}
</message>
<locale>en</locale>
<version>1</version>
</emailTemplate>
</emailTemplates>


text:
/**
 * \file ctrl_sock.h
 * \brief Control Socket for select() wakeup
 *
 * LWIP doesn't allow an easy mechanism to on-demand wakeup a thread
 * sleeping on select. This is a common requirement for sending
 * control commands to a network server. This control socket API
 * facilitates the same.
 */
#ifndef _CTRL_SOCK_H_
#define _CTRL_SOCK_H_

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @brief Create a control socket
 *
 *      LWIP doesn't allow an easy mechanism to on-demand wakeup a thread
 *      sleeping on select. This is a common requirement for sending
 *      control commands to a network server. This control socket API
 *      facilitates the same.
 *
 *      This API will create a UDP control socket on the specified port. It
 *      will return a socket descriptor that can then be added to your
 *      fd_set in select()
 *
 * @param[in] port the local port on which the control socket will listen
 *
 * @return - the socket descriptor that can be added to the fd_set in select.
 *         - an error code if less than zero
 */
int cs_create_ctrl_sock(int port);

/**
 * @brief Free the control socket
 *
 *      This frees up the control socket that was earlier created using
 *      cs_create_ctrl_sock()
 *
 * @param[in] fd the socket descriptor associated with this control socket
 */
void cs_free_ctrl_sock(int fd);

/**
 * @brief Send data to control socket
 *
 *      This API sends data to the control socket. If a server is blocked
 *      on select() with the control socket, this call will wake up that
 *      server.
 *
 * @param[in] send_fd the socket for sending ctrl messages
 * @param[in] port the port on which the control socket was created
 * @param[in] data pointer to a buffer that contains data to send on the socket
 * @param[in] data_len the length of the data contained in the buffer pointed to be data
 *
 * @return  - the number of bytes sent to the control socket
 *          - an error code if less than zero
 */
int cs_send_to_ctrl_sock(int send_fd, int port, void *data, unsigned int data_len);

/**
 * @brief Receive data from control socket
 *
 *      This API receives any data that was sent to the control
 *      socket. This will be typically called from the server thread to
 *      process any commands on this socket.
 *
 * @param[in] fd the socket descriptor of the control socket
 * @param[in] data pointer to a buffer that will be used to store
 * received from the control socket
 * @param[in] data_len the maximum length of the data that can be
 * stored in the buffer pointed by data
 *
 * @return  - the number of bytes received from the control socket
 *          - an error code if less than zero
 */
int cs_recv_from_ctrl_sock(int fd, void *data, unsigned int data_len);

#ifdef __cplusplus
}
#endif

#endif /* ! _CTRL_SOCK_H_ */


text:
package ru.job4j.ioc.storage;

import ru.job4j.ioc.model.PersistentClass;

import java.util.Map;

/**
 * Class MemoryStorage описывает хранилище в энергозависимой памяти.
 * @author Timur Kapkaev (timur.kap@yandex.ru)
 * @version $Id$
 * @since 08.02.2018
 * @param <E> - тип хранимых элементов
 * */
public class MemoryStorage<E extends PersistentClass> implements IStorage<E> {
    /** id generator. */
    private long idGenerator = 0;
    /** хранилище. */
    private final Map<Long, E> memory;

    /**
     * Конструктор класса MemoryStore.
     * @param memory - реализация памяти.
     * */
    public MemoryStorage(Map<Long, E> memory) {
        this.memory = memory;
    }
    /**
     * Сохраняет элемент.
     * @param element - сохраняемый элемент
     * @return сохраненный элемент (null, если не удалось выполнить сохранение)
     * */
    @Override
    public E persist(E element) {
        element.setId(++idGenerator);
        return memory.put(element.getId(), element);
    }
    /**
     * Возвращает элемент из хранилища.
     * @param id - id возвращаемого элемента
     * @return элемент хранилища (null, если элемента не существует)
     * */
    @Override
    public E get(long id) {
        return memory.get(id);
    }
    /**
     * Удаляет элемент из хранилища.
     * @param element - удаляемый элемент
     * @return удаленный элемент (null, при неудачном удалении)
     * */
    @Override
    public E remove(E element) {
       return memory.remove(element.getId());
    }
    /**
     * Обновляет элемент.
     * @param element - обновленный элемент
     * @return обновленный элемент (null, при неудачном обновлении)
     * */
    @Override
    public E update(E element) {
       return memory.put(element.getId(), element);
    }
}


text:
import base64
import json

import responses
import pytest

import mapbox
import mapbox.services.uploads

username = 'testuser'
access_token = 'pk.{0}.test'.format(
    base64.b64encode(b'{"u":"testuser"}').decode('utf-8'))

upload_response_body = """
    {{"progress": 0,
    "modified": "date.test",
    "error": null,
    "tileset": "{username}.test1",
    "complete": false,
    "owner": "{username}",
    "created": "date.test",
    "id": "id.test",
    "name": null}}""".format(username=username)

def test_class_attrs():
    """Get expected class attr values"""
    serv = mapbox.Uploader()
    assert serv.api_name == 'uploads'
    assert serv.api_version == 'v1'

# TODO: remove at 1.0.
def test_resolve_username():
    """Username is resolved and deprecation warning raised"""
    serv = mapbox.Uploader()
    with pytest.warns(DeprecationWarning):
        assert serv._resolve_username('foo', 'bar') == 'bar'

@responses.activate
def test_get_credentials():
    query_body = """
       {{"key": "_pending/{username}/key.test",
         "accessKeyId": "ak.test",
         "bucket": "tilestream-tilesets-production",
         "url": "https://tilestream-tilesets-production.s3.amazonaws.com/_pending/{username}/key.test",
         "secretAccessKey": "sak.test",
         "sessionToken": "st.test"}}""".format(username=username)

    responses.add(
        responses.POST,
        'https://api.mapbox.com/uploads/v1/{0}/credentials?access_token={1}'.format(
            username, access_token),
        match_querystring=True,
        body=query_body, status=200,
        content_type='application/json')

    res = mapbox.Uploader(access_token=access_token)._get_credentials()

    assert res.status_code == 200
    creds = res.json()
    assert username in creds['url']
    for k in ['key', 'bucket', 'url', 'accessKeyId',
              'secretAccessKey', 'sessionToken']:
        assert k in creds.keys()

@responses.activate
def test_create():
    responses.add(
        responses.POST,
        'https://api.mapbox.com/uploads/v1/{0}?access_token={1}'.format(username, access_token),
        match_querystring=True,
        body=upload_response_body, status=201,
        content_type='application/json')

    res = mapbox.Uploader(access_token=access_token).create(
        'http://example.com/test.json', 'test1')  # without username prefix
    assert res.status_code == 201
    job = res.json()
    assert job['tileset'] == "{0}.test1".format(username)

    res2 = mapbox.Uploader(access_token=access_token).create(
        'http://example.com/test.json', 'testuser.test1')  # also takes full tileset
    assert res2.status_code == 201
    job = res2.json()
    assert job['tileset'] == "{0}.test1".format(username)

@responses.activate
def test_create_name():
    upload_response_body = """
        {"progress": 0,
        "modified": "date.test",
        "error": null,
        "tileset": "testuser.test1",
        "complete": false,
        "owner": "testuser",
        "created": "date.test",
        "id": "id.test",
        "name": "testname"}"""

    def request_callback(request):
        payload = json.loads(request.body.decode())
        assert payload['name'] == "testname"
        return (201, {}, upload_response_body)

    responses.add_callback(
        responses.POST,
        'https://api.mapbox.com/uploads/v1/{0}?access_token={1}'.format(username, access_token),
        match_querystring=True,
        callback=request_callback)

    res = mapbox.Uploader(access_token=access_token).create(
        'http://example.com/test.json', 'testuser.test1', name="testname")
    assert res.status_code == 201
    job = res.json()
    assert job['name'] == "testname"

@responses.activate
def test_list():
    responses.add(
        responses.GET,
        'https://api.mapbox.com/uploads/v1/{0}?access_token={1}'.format(username, access_token),
        match_querystring=True,
        body="[{0}]".format(upload_response_body), status=200,
        content_type='application/json')

    res = mapbox.Uploader(access_token=access_token).list()
    assert res.status_code == 200
    uploads = res.json()
    assert len(uploads) == 1
    assert json.loads(upload_response_body) in uploads

@responses.activate
def test_status():
    job = json.loads(upload_response_body)
    responses.add(
        responses.GET,
        'https://api.mapbox.com/uploads/v1/{0}/{1}?access_token={2}'.format(username, job['id'], access_token),
        match_querystring=True,
        body=upload_response_body, status=200,
        content_type='application/json')

    res = mapbox.Uploader(access_token=access_token).status(job)
    assert res.status_code == 200
    res = mapbox.Uploader(access_token=access_token).status(job['id'])
    assert res.status_code == 200
    status = res.json()
    assert job == status

@responses.activate
def test_delete():
    job = json.loads(upload_response_body)
    responses.add(
        responses.DELETE,
        'https://api.mapbox.com/uploads/v1/{0}/{1}?access_token={2}'.format(username, job['id'], access_token),
        match_querystring=True,
        body=None, status=204,
        content_type='application/json')

    res = mapbox.Uploader(access_token=access_token).delete(job)
    assert res.status_code == 204

    res = mapbox.Uploader(access_token=access_token).delete(job['id'])
    assert res.status_code == 204

class MockSession(object):
    """Mocks a boto3 session."""

    def __init__(self, *args, **kwargs):
        self.bucket = None
        self.key = None
        pass

    def resource(self, name):
        self.resource_name = name
        return self

    def Object(self, bucket, key):
        assert self.resource_name == 's3'
        self.bucket = bucket
        self.key = key
        return self

    def put(self, Body):
        assert self.bucket
        assert self.key
        self.body = Body
        return True

    def Bucket(self, bucket):
        self.bucket = bucket
        return self

    def upload_file(self, filename, key, Callback=None):
        self.filename = filename
        self.key = key
        self.Callback = Callback

    def upload_fileobj(self, data, key, Callback=None):
        self.data = data
        self.key = key
        self.Callback = Callback

        bytes_read = data.read(8192)
        if bytes_read and self.Callback:
            self.Callback(len(bytes_read))
        while bytes_read:
            bytes_read = data.read(8192)
            if bytes_read and self.Callback:
                self.Callback(len(bytes_read))

    def copy(self, source, key, ExtraArgs=None, Callback=None,
             SourceClient=None, Config=None):
        self.source = source
        self.key = key
        self.Callback = Callback

@responses.activate
def test_stage(monkeypatch):

    monkeypatch.setattr(mapbox.services.uploads, 'boto3_session', MockSession)

    # Credentials
    query_body = """
       {{"key": "_pending/{username}/key.test",
         "accessKeyId": "ak.test",
         "bucket": "tilestream-tilesets-production",
         "url": "https://tilestream-tilesets-production.s3.amazonaws.com/_pending/{username}/key.test",
         "secretAccessKey": "sak.test",
         "sessionToken": "st.test"}}""".format(username=username)
    responses.add(
        responses.POST,
        'https://api.mapbox.com/uploads/v1/{0}/credentials?access_token={1}'.format(
            username, access_token),
        match_querystring=True,
        body=query_body, status=200,
        content_type='application/json')

    with open('tests/moors.json', 'rb') as src:
        stage_url = mapbox.Uploader(access_token=access_token).stage(src)
    assert stage_url.startswith("https://tilestream-tilesets-production.s3.amazonaws.com/_pending")

@responses.activate
def test_stage_filename(monkeypatch):
    """A filename works too"""

    monkeypatch.setattr(mapbox.services.uploads, 'boto3_session', MockSession)

    # Credentials
    query_body = """
       {{"key": "_pending/{username}/key.test",
         "accessKeyId": "ak.test",
         "bucket": "tilestream-tilesets-production",
         "url": "https://tilestream-tilesets-production.s3.amazonaws.com/_pending/{username}/key.test",
         "secretAccessKey": "sak.test",
         "sessionToken": "st.test"}}""".format(username=username)
    responses.add(
        responses.POST,
        'https://api.mapbox.com/uploads/v1/{0}/credentials?access_token={1}'.format(username, access_token),
        match_querystring=True,
        body=query_body, status=200,
        content_type='application/json')

    stage_url = mapbox.Uploader(access_token=access_token).stage('tests/moors.json')
    assert stage_url.startswith("https://tilestream-tilesets-production.s3.amazonaws.com/_pending")

@responses.activate
def test_big_stage(tmpdir, monkeypatch):
    """Files larger than 1M are multipart uploaded."""

    monkeypatch.setattr(mapbox.services.uploads, 'boto3_session', MockSession)

    # Credentials
    query_body = """
       {{"key": "_pending/{username}/key.test",
         "accessKeyId": "ak.test",
         "bucket": "tilestream-tilesets-production",
         "url": "https://tilestream-tilesets-production.s3.amazonaws.com/_pending/{username}/key.test",
         "secretAccessKey": "sak.test",
         "sessionToken": "st.test"}}""".format(username=username)

    responses.add(
        responses.POST,
        'https://api.mapbox.com/uploads/v1/{0}/credentials?access_token={1}'.format(
            username, access_token),
        match_querystring=True,
        body=query_body, status=200,
        content_type='application/json')

    # Make a temp file larger than 1MB.
    bigfile = tmpdir.join('big.txt')
    bigfile.write(','.join(('num' for num in range(1000000))))
    assert bigfile.size() > 1000000

    with bigfile.open(mode='rb') as src:
        stage_url = mapbox.Uploader(access_token=access_token).stage(src)
    assert stage_url.startswith("https://tilestream-tilesets-production.s3.amazonaws.com/_pending")

@responses.activate
def test_upload(monkeypatch):
    """Upload a file and create a tileset"""

    monkeypatch.setattr(mapbox.services.uploads, 'boto3_session', MockSession)

    # Credentials
    query_body = """
       {{"key": "_pending/{username}/key.test",
         "accessKeyId": "ak.test",
         "bucket": "tilestream-tilesets-production",
         "url": "https://tilestream-tilesets-production.s3.amazonaws.com/_pending/{username}/key.test",
         "secretAccessKey": "sak.test",
         "sessionToken": "st.test"}}""".format(username=username)

    responses.add(
        responses.POST,
        'https://api.mapbox.com/uploads/v1/{0}/credentials?access_token={1}'.format(
            username, access_token),
        match_querystring=True,
        body=query_body, status=200,
        content_type='application/json')

    responses.add(
        responses.POST,
        'https://api.mapbox.com/uploads/v1/{0}?access_token={1}'.format(username, access_token),
        match_querystring=True,
        body=upload_response_body, status=201,
        content_type='application/json')

    def print_cb(num_bytes):
        print("{0} bytes uploaded".format(num_bytes))

    with open('tests/moors.json', 'rb') as src:
        res = mapbox.Uploader(access_token=access_token).upload(src, 'test1', callback=print_cb)

    assert res.status_code == 201
    job = res.json()
    assert job['tileset'] == "{0}.test1".format(username)

@responses.activate
def test_upload_error(monkeypatch):
    """Upload a file and create a tileset, fails with 409"""

    monkeypatch.setattr(mapbox.services.uploads, 'boto3_session', MockSession)

    # Credentials
    query_body = """
       {{"key": "_pending/{username}/key.test",
         "accessKeyId": "ak.test",
         "bucket": "tilestream-tilesets-production",
         "url": "https://tilestream-tilesets-production.s3.amazonaws.com/_pending/{username}/key.test",
         "secretAccessKey": "sak.test",
         "sessionToken": "st.test"}}""".format(username=username)

    responses.add(
        responses.POST,
        'https://api.mapbox.com/uploads/v1/{0}/credentials?access_token={1}'.format(
            username, access_token),
        match_querystring=True,
        body=query_body, status=200,
        content_type='application/json')

    responses.add(
        responses.POST,
        'https://api.mapbox.com/uploads/v1/{0}?access_token={1}'.format(username, access_token),
        match_querystring=True,
        body="", status=409,
        content_type='application/json')

    with open('tests/moors.json', 'rb') as src:
        res = mapbox.Uploader(access_token=access_token).upload(src, 'test1')

    assert res.status_code == 409

@responses.activate
def test_upload_patch(monkeypatch):
    """Upload a file and create a tileset in patch mode"""

    monkeypatch.setattr(mapbox.services.uploads, 'boto3_session', MockSession)

    def ensure_patch(request):
        payload = json.loads(request.body.decode())
        assert payload['patch']
        headers = {}
        return (201, headers, upload_response_body)

    # Credentials
    query_body = """
       {{"key": "_pending/{username}/key.test",
         "accessKeyId": "ak.test",
         "bucket": "tilestream-tilesets-production",
         "url": "https://tilestream-tilesets-production.s3.amazonaws.com/_pending/{username}/key.test",
         "secretAccessKey": "sak.test",
         "sessionToken": "st.test"}}""".format(username=username)

    responses.add(
        responses.POST,
        'https://api.mapbox.com/uploads/v1/{0}/credentials?access_token={1}'.format(
            username, access_token),
        match_querystring=True,
        body=query_body, status=200,
        content_type='application/json')

    responses.add_callback(
        responses.POST,
        'https://api.mapbox.com/uploads/v1/{0}?access_token={1}'.format(username, access_token),
        callback=ensure_patch,
        match_querystring=True,
        content_type='application/json')

    with open('tests/moors.json', 'rb') as src:
        res = mapbox.Uploader(access_token=access_token).upload(
            src, 'testuser.Test1', name='test1', patch=True)

    assert res.status_code == 201
    job = res.json()
    assert job['tileset'] == "{0}.test1".format(username)

@responses.activate
def test_upload_bypass(monkeypatch):
    """Upload a file and create a tileset bypassing mbtiles validation"""

    monkeypatch.setattr(mapbox.services.uploads, 'boto3_session', MockSession)

    def ensure_bypass(request):
        payload = json.loads(request.body.decode())
        assert payload['bypass_mbtiles_validation']
        headers = {}
        return (201, headers, upload_response_body)

    # Credentials
    query_body = """
       {{"key": "_pending/{username}/key.test",
         "accessKeyId": "ak.test",
         "bucket": "tilestream-tilesets-production",
         "url": "https://tilestream-tilesets-production.s3.amazonaws.com/_pending/{username}/key.test",
         "secretAccessKey": "sak.test",
         "sessionToken": "st.test"}}""".format(username=username)

    responses.add(
        responses.POST,
        'https://api.mapbox.com/uploads/v1/{0}/credentials?access_token={1}'.format(
            username, access_token),
        match_querystring=True,
        body=query_body, status=200,
        content_type='application/json')

    responses.add_callback(
        responses.POST,
        'https://api.mapbox.com/uploads/v1/{0}?access_token={1}'.format(username, access_token),
        callback=ensure_bypass,
        match_querystring=True,
        content_type='application/json')

    with open('tests/moors.json', 'rb') as src:
        res = mapbox.Uploader(access_token=access_token).upload(
            src, 'testuser.Test1', name='test1', bypass=True)

    assert res.status_code == 201
    job = res.json()
    assert job['tileset'] == "{0}.test1".format(username)

def test_upload_tileset_validation():
    with pytest.raises(mapbox.errors.ValidationError):
        with open('tests/moors.json', 'rb') as src:
            # limited to 32 chars, try 40
            mapbox.Uploader(access_token=access_token).upload(
                src, 'username.' + 'aA' * 20, name='test1')

def test_upload_tileset_validation_specialchar():
    with pytest.raises(mapbox.errors.ValidationError):
        with open('tests/moors.json', 'rb') as src:
            # limited to a-z0-9-_ chars
            mapbox.Uploader(access_token=access_token).upload(
                src, 'username.&#!', name='test1')

def test_upload_tileset_validation_username():
    # even with 60 chars, the addition of the
    # testuser username puts it over 64 chars
    with pytest.raises(mapbox.errors.ValidationError):
        with open('tests/moors.json', 'rb') as src:
            mapbox.Uploader(access_token=access_token).upload(
                src, 'a' * 60, name='test1', patch=True)

def test_create_tileset_validation():
    # even with 60 chars, the addition of the username puts it over 64 chars
    with pytest.raises(mapbox.errors.ValidationError):
        mapbox.Uploader(access_token=access_token).create(
            'http://example.com/test.json', 'a' * 60)