text:
@import CommonCrypto;

#import "MainViewController+OAuth.h"

#import <AuthenticationServices/AuthenticationServices.h>

#import "AppManager.h"
#import <FirebaseAuth/FIROAuthProvider.h>
#import "MainViewController+Internal.h"

NS_ASSUME_NONNULL_BEGIN

@interface MainViewController () <ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding>

@end

@implementation MainViewController (OAuth)

- (StaticContentTableViewSection *)oAuthSection {
  __weak typeof(self) weakSelf = self;
  return [StaticContentTableViewSection sectionWithTitle:@"OAuth" cells:@[
    [StaticContentTableViewCell cellWithTitle:@"Sign in with Google"
                                       action:^{ [weakSelf signInGoogleHeadfulLite]; }],
    [StaticContentTableViewCell cellWithTitle:@"Link with Google"
                                       action:^{ [weakSelf linkWithGoogleHeadfulLite]; }],
    [StaticContentTableViewCell cellWithTitle:@"Reauthenticate with Google"
                                       action:^{ [weakSelf reauthenticateWithGoogleHeadfulLite]; }],
    [StaticContentTableViewCell cellWithTitle:@"Sign in with Apple"
                                       action:^{ [weakSelf signInWithApple]; }],
    [StaticContentTableViewCell cellWithTitle:@"Link with Apple"
                                       action:^{ [weakSelf linkWithApple]; }],
    [StaticContentTableViewCell cellWithTitle:@"Unlink with Apple"
                                       action:^{ [weakSelf unlinkFromProvider:@"apple.com" completion:nil]; }],
    [StaticContentTableViewCell cellWithTitle:@"Reauthenticate with Apple"
                                       action:^{ [weakSelf reauthenticateWithApple]; }],
    [StaticContentTableViewCell cellWithTitle:@"Sign in with Twitter"
                                       action:^{ [weakSelf signInTwitterHeadfulLite]; }],
    [StaticContentTableViewCell cellWithTitle:@"Sign in with GitHub"
                                       action:^{ [weakSelf signInGitHubHeadfulLite]; }],
    [StaticContentTableViewCell cellWithTitle:@"Sign in with GitHub (Access token)"
                                       action:^{ [weakSelf signInWithGitHub]; }],
    [StaticContentTableViewCell cellWithTitle:@"Sign in with Microsoft"
                                       action:^{ [weakSelf signInMicrosoftHeadfulLite]; }],
    [StaticContentTableViewCell cellWithTitle:@"Sign in with Yahoo"
                                       action:^{ [weakSelf signInYahooHeadfulLite]; }],
    [StaticContentTableViewCell cellWithTitle:@"Sign in with Linkedin"
                                       action:^{ [weakSelf signInLinkedinHeadfulLite]; }],
    ]];
}

- (void)signInGoogleHeadfulLite {
  FIROAuthProvider *provider = self.googleOAuthProvider;
  provider.customParameters = @{
                                @"prompt" : @"consent",
                                };
  provider.scopes = @[ @"profile", @"email", @"https://www.googleapis.com/auth/plus.me" ];
  [self showSpinner:^{
    [[AppManager auth] signInWithProvider:provider
                               UIDelegate:nil
                               completion:^(FIRAuthDataResult *_Nullable authResult,
                                            NSError *_Nullable error) {
       [self hideSpinner:^{
         if (error) {
           [self logFailure:@"sign-in with provider (Google) failed" error:error];
         } else if (authResult.additionalUserInfo) {
           [self logSuccess:[self stringWithAdditionalUserInfo:authResult.additionalUserInfo]];
           if (self.isNewUserToggleOn) {
             NSString *newUserString = authResult.additionalUserInfo.newUser ?
             @"New user" : @"Existing user";
             [self showMessagePromptWithTitle:@"New or Existing"
                                      message:newUserString
                             showCancelButton:NO
                                   completion:nil];
           }
         }
         [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error];
       }];
     }];
  }];
}

- (void)linkWithGoogleHeadfulLite {
  FIROAuthProvider *provider = self.googleOAuthProvider;
  provider.customParameters = @{
                                @"prompt" : @"consent",
                                };
  provider.scopes = @[ @"profile", @"email", @"https://www.googleapis.com/auth/plus.me" ];
  [self showSpinner:^{
    [[AppManager auth].currentUser linkWithProvider:provider
                                         UIDelegate:nil
                                         completion:^(FIRAuthDataResult *_Nullable authResult,
                                                      NSError *_Nullable error) {
     [self hideSpinner:^{
       if (error) {
         [self logFailure:@"Reauthenticate with provider (Google) failed" error:error];
       } else if (authResult.additionalUserInfo) {
         [self logSuccess:[self stringWithAdditionalUserInfo:authResult.additionalUserInfo]];
         if (self.isNewUserToggleOn) {
           NSString *newUserString = authResult.additionalUserInfo.newUser ?
           @"New user" : @"Existing user";
           [self showMessagePromptWithTitle:@"New or Existing"
                                    message:newUserString
                           showCancelButton:NO
                                 completion:nil];
         }
       }
       [self showTypicalUIForUserUpdateResultsWithTitle:@"Link Error" error:error];
     }];
   }];
  }];
}

- (void)reauthenticateWithGoogleHeadfulLite {
  FIROAuthProvider *provider = self.googleOAuthProvider;
  provider.customParameters = @{
                                @"prompt" : @"consent",
                                };
  provider.scopes = @[ @"profile", @"email", @"https://www.googleapis.com/auth/plus.me" ];
  [self showSpinner:^{
    [[AppManager auth].currentUser reauthenticateWithProvider:provider
                                                   UIDelegate:nil
                                                   completion:^(FIRAuthDataResult *_Nullable authResult,
                                                                NSError *_Nullable error) {
     [self hideSpinner:^{
       if (error) {
         [self logFailure:@"Link with provider (Google) failed" error:error];
       } else if (authResult.additionalUserInfo) {
         [self logSuccess:[self stringWithAdditionalUserInfo:authResult.additionalUserInfo]];
         if (self.isNewUserToggleOn) {
           NSString *newUserString = authResult.additionalUserInfo.newUser ?
           @"New user" : @"Existing user";
           [self showMessagePromptWithTitle:@"New or Existing"
                                    message:newUserString
                           showCancelButton:NO
                                 completion:nil];
         }
       }
       [self showTypicalUIForUserUpdateResultsWithTitle:@"Reauthenticate Error" error:error];
     }];
   }];
  }];
}

- (void)signInTwitterHeadfulLite {
  FIROAuthProvider *provider = self.twitterOAuthProvider;
  [self showSpinner:^{
    [provider getCredentialWithUIDelegate:nil completion:^(FIRAuthCredential *_Nullable credential,
                                                           NSError *_Nullable error) {
      if (error) {
        [self logFailure:@"sign-in with Twitter failed" error:error];
        return;
      }
      [[AppManager auth] signInWithCredential:credential
                                   completion:^(FIRAuthDataResult *_Nullable
                                                authResult,
                                                NSError *_Nullable error) {
         [self hideSpinner:^{
           if (error) {
             [self logFailure:@"sign-in with Twitter (headful-lite) failed" error:error];
             return;
           } else {
             [self logSuccess:@"sign-in with Twitter (headful-lite) succeeded."];
           }
           [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error];
         }];
       }];
    }];
  }];
}

- (void)signInWithGitHub {
  [self showTextInputPromptWithMessage:@"GitHub Access Token:"
                       completionBlock:^(BOOL userPressedOK, NSString *_Nullable accessToken) {
   if (!userPressedOK || !accessToken.length) {
     return;
   }
   FIROAuthCredential *credential =
   [FIROAuthProvider credentialWithProviderID:FIRGitHubAuthProviderID accessToken:accessToken];
   if (credential) {
     [[AppManager auth] signInWithCredential:credential
                                  completion:^(FIRAuthDataResult *_Nullable result,
                                               NSError *_Nullable error) {
      if (error) {
        [self logFailure:@"sign-in with provider failed" error:error];
      } else {
        [self logSuccess:@"sign-in with provider succeeded."];
      }
      [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In" error:error];
    }];
   }
 }];
}

- (void)signInGitHubHeadfulLite {
  FIROAuthProvider *provider = self.gitHubOAuthProvider;
  [self showSpinner:^{
    [provider getCredentialWithUIDelegate:nil completion:^(FIRAuthCredential *_Nullable credential,
                                                           NSError *_Nullable error) {
      if (error) {
        [self logFailure:@"sign-in with GitHub failed" error:error];
        return;
      }
      [[AppManager auth] signInWithCredential:credential
                                   completion:^(FIRAuthDataResult *_Nullable
                                                authResult,
                                                NSError *_Nullable error) {
         [self hideSpinner:^{
           if (error) {
             [self logFailure:@"sign-in with GitHub (headful-lite) failed" error:error];
             return;
           } else {
             [self logSuccess:@"sign-in with GitHub (headful-lite) succeeded."];
           }
           [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error];
         }];
       }];
    }];
  }];
}

- (void)signInLinkedinHeadfulLite {
  FIROAuthProvider *provider = self.linkedinOAuthProvider;
  [self showSpinner:^{
    [provider getCredentialWithUIDelegate:nil completion:^(FIRAuthCredential *_Nullable credential,
                                                           NSError *_Nullable error) {
      if (error) {
        [self logFailure:@"sign-in with Linkedin failed" error:error];
        return;
      }
      [[AppManager auth] signInWithCredential:credential
                                   completion:^(FIRAuthDataResult *_Nullable
                                                authResult,
                                                NSError *_Nullable error) {
         [self hideSpinner:^{
           if (error) {
             [self logFailure:@"sign-in with Linkedin (headful-lite) failed" error:error];
             return;
           } else {
             [self logSuccess:@"sign-in with Linkedin (headful-lite) succeeded."];
           }
           [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error];
         }];
       }];
    }];
  }];
}

- (void)signInMicrosoftHeadfulLite {
  FIROAuthProvider *provider = self.microsoftOAuthProvider;
  provider.customParameters = @{
                                @"prompt" : @"consent",
                                @"login_hint" : @"tu8731@gmail.com",
                                };
  provider.scopes = @[ @"user.readwrite,calendars.read" ];
  [self showSpinner:^{
    [provider getCredentialWithUIDelegate:nil completion:^(FIRAuthCredential *_Nullable credential,
                                                           NSError *_Nullable error) {
      if (error) {
        [self logFailure:@"sign-in with Microsoft failed" error:error];
        return;
      }
      [[AppManager auth] signInWithCredential:credential
                                   completion:^(FIRAuthDataResult *_Nullable
                                                authResult,
                                                NSError *_Nullable error) {
         [self hideSpinner:^{
           if (error) {
             [self logFailure:@"sign-in with Microsoft failed" error:error];
             return;
           } else {
             [self logSuccess:@"sign-in with Microsoft (headful-lite) succeeded."];
           }
           [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error];
         }];
       }];
    }];
  }];
}

- (void)signInYahooHeadfulLite {
  FIROAuthProvider *provider = self.yahooOAuthProvider;
  [self showSpinner:^{
    [provider getCredentialWithUIDelegate:nil completion:^(FIRAuthCredential *_Nullable credential,
                                                           NSError *_Nullable error) {
      if (error) {
        [self logFailure:@"sign-in with Yahoo failed" error:error];
        return;
      }
      [[AppManager auth] signInWithCredential:credential
                                   completion:^(FIRAuthDataResult *_Nullable
                                                authResult,
                                                NSError *_Nullable error) {
         [self hideSpinner:^{
           if (error) {
             [self logFailure:@"sign-in with Yahoo (headful-lite) failed" error:error];
             return;
           } else {
             [self logSuccess:@"sign-in with Yahoo (headful-lite) succeeded."];
           }
           [self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error];
         }];
       }];
    }];
  }];
}

- (ASAuthorizationAppleIDRequest *)appleIDRequestWithState:(NSString *)state API_AVAILABLE(ios(13.0)) {
  ASAuthorizationAppleIDRequest *request = [[[ASAuthorizationAppleIDProvider alloc] init] createRequest];
  request.requestedScopes = @[ASAuthorizationScopeEmail, ASAuthorizationScopeFullName];
  NSString *rawNonce = [self randomNonce:32];
  self.appleRawNonce = rawNonce;
  request.nonce = [self stringBySha256HashingString:rawNonce];
  request.state = state;
  return request;
}

- (NSString *)randomNonce:(NSInteger)length {
  NSAssert(length > 0, @"Expected nonce to have positive length");
  NSString *characterSet = @"0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._";
  NSMutableString *result = [NSMutableString string];
  NSInteger remainingLength = length;

  while (remainingLength > 0) {
    NSMutableArray *randoms = [NSMutableArray arrayWithCapacity:16];
    for (NSInteger i = 0; i < 16; i++) {
      uint8_t random = 0;
      int errorCode = SecRandomCopyBytes(kSecRandomDefault, 1, &random);
      NSAssert(errorCode == errSecSuccess, @"Unable to generate nonce: OSStatus %i", errorCode);

      [randoms addObject:@(random)];
    }

    for (NSNumber *random in randoms) {
      if (remainingLength == 0) {
        break;
      }

      if (random.unsignedIntValue < characterSet.length) {
        unichar character = [characterSet characterAtIndex:random.unsignedIntValue];
        [result appendFormat:@"%C", character];
        remainingLength--;
      }
    }
  }

  return result;
}

- (NSString *)stringBySha256HashingString:(NSString *)input {
  const char *string = [input UTF8String];
  unsigned char result[CC_SHA256_DIGEST_LENGTH];
  CC_SHA256(string, (CC_LONG)strlen(string), result);

  NSMutableString *hashed = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH * 2];
  for (NSInteger i = 0; i < CC_SHA256_DIGEST_LENGTH; i++) {
    [hashed appendFormat:@"%02x", result[i]];
  }
  return hashed;
}

- (void)signInWithApple {
  if (@available(iOS 13, *)) {
    ASAuthorizationAppleIDRequest* request = [self appleIDRequestWithState:@"signIn"];

    ASAuthorizationController* controller = [[ASAuthorizationController alloc] initWithAuthorizationRequests:@[request]];
    controller.delegate = self;
    controller.presentationContextProvider = self;
    [controller performRequests];
  }
}

- (void)linkWithApple {
  if (@available(iOS 13, *)) {
    ASAuthorizationAppleIDRequest* request = [self appleIDRequestWithState:@"link"];

    ASAuthorizationController* controller = [[ASAuthorizationController alloc] initWithAuthorizationRequests:@[request]];
    controller.delegate = self;
    controller.presentationContextProvider = self;
    [controller performRequests];
  }
}

- (void)reauthenticateWithApple {
  if (@available(iOS 13, *)) {
    ASAuthorizationAppleIDRequest* request = [self appleIDRequestWithState:@"reauth"];

    ASAuthorizationController* controller = [[ASAuthorizationController alloc] initWithAuthorizationRequests:@[request]];
    controller.delegate = self;
    controller.presentationContextProvider = self;
    [controller performRequests];
  }
}

- (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithAuthorization:(ASAuthorization *)authorization API_AVAILABLE(ios(13.0)) {
  ASAuthorizationAppleIDCredential* appleIDCredential = authorization.credential;
  NSString *IDToken = [NSString stringWithUTF8String:[appleIDCredential.identityToken bytes]];
  FIROAuthCredential *credential = [FIROAuthProvider credentialWithProviderID:@"apple.com"
                                                                      IDToken:IDToken
                                                                     rawNonce:self.appleRawNonce
                                                                  accessToken:nil];

  if ([appleIDCredential.state isEqualToString:@"signIn"]) {
    [FIRAuth.auth signInWithCredential:credential completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) {
      if (!error) {
        NSLog(@"%@", authResult.description);
      } else {
        NSLog(@"%@", error.description);
      }
    }];
  } else if ([appleIDCredential.state isEqualToString:@"link"]) {
    [FIRAuth.auth.currentUser linkWithCredential:credential completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) {
      if (!error) {
        NSLog(@"%@", authResult.description);
      } else {
        NSLog(@"%@", error.description);
      }
    }];
  } else if ([appleIDCredential.state isEqualToString:@"reauth"]) {
    [FIRAuth.auth.currentUser reauthenticateWithCredential:credential completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) {
      if (!error) {
        NSLog(@"%@", authResult.description);
      } else {
        NSLog(@"%@", error.description);
      }
    }];
  }
}

- (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithError:(NSError *)error API_AVAILABLE(ios(13.0)) {
  NSLog(@"%@", error.description);
}

@end

NS_ASSUME_NONNULL_END


text:
package org.onosproject.openstacktelemetry.api;

/**
 * A record which contains influx topic and network metric data.
 */
public interface InfluxRecord<K, V> {

    /**
     * Gets measurement name in InfluxDB.
     *
     * @return measurement name
     */
    K measurement();

    /**
     * Gets flow information and its statistics data.
     *
     * @return flow information and its statistics data
     */
    V flowInfos();

}

redpajama_set_name: RedPajamaGithub


text:
define([
    'require',
    'angular',
    'angularMocks',
    'app/app.module',
], function (require, angular) {
    var scope;
    var rootScope;
/**
 * Tests sit right alongside the file they are testing, which is more intuitive
 * and portable than separating `src` and `test` directories. Additionally, the
 * build process will exclude all `.spec.js` files from the build
 * automatically.
 */
    describe( 'home section', function() {
        beforeEach( angular.mock.module( require('app/app.module') ) );

        beforeEach(angular.mock.inject(function($rootScope){
            scope = $rootScope.$new();
            rootScope = $rootScope;
        }));

        it( 'should have a dummy test', inject( function() {
          expect( true ).toBeTruthy();
        }));
    });
});


text:
import React from 'react'
import ACTIONS from '../actions.js'
import STORE from '../store.js'
import NavBar from './components/navBar'
import Header from './components/header'

var ProfilePage = React.createClass({

	componentWillMount: function(){

		ACTIONS.fetchGames()
		STORE.on('dataUpdated', () => {
			this.setState(STORE.data)
		})

	},

	getInitialState: function(){

		return STORE.data

	},

	createRecentGames: function(singleGame) {
		var attributes = singleGame.attributes
		return(

{attributes.createdAt}

winning score:   {attributes.winner}   {(attributes.playerOneScore>attributes.playerTwoScore)?attributes.playerOneScore:attributes.playerTwoScore}

losing score:   {attributes.loser}   {(attributes.playerOneScore>attributes.playerTwoScore)?attributes.playerTwoScore:attributes.playerOneScore}

spread:   {(attributes.playerOneScore>attributes.playerTwoScore)?(attributes.playerOneScore-attributes.playerTwoScore):(attributes.playerTwoScore-attributes.playerOneScore)}

		)
	},

	render: function(){

		var totalGamesArray = this.state.items.models
		console.log(totalGamesArray)

		var currentUserId = this.props.currentUserId

		var recentGamesArray = []

		for (var i = 0; i < totalGamesArray.length; i ++) {
			if (totalGamesArray[i].attributes.playerOne === currentUserId ||
				totalGamesArray[i].attributes.playerTwo === currentUserId) {
				recentGamesArray.push(totalGamesArray[i])
			}
		}

		return(

				<NavBar />

				<StatsComponent stats = {this.state}/>

				<RecentGamesComponent recentGames={recentGamesArray} makeItem={this.createRecentGames}/>

		)

	}

})

var AvatarComponent = React.createClass({
	render: function(){

		return(

		)
	}
})

var RecentGamesComponent = React.createClass({

	render: function(){

		return (

Recent Games

				{this.props.recentGames.map(this.props.makeItem)}

		)

	}

})

var StatsComponent = React.createClass({

	render: function(){

		return(

spread: {this.props.stats.spread}

wins: {this.props.stats.wins}

losses: {this.props.stats.losses}

		)

	}

})

export default ProfilePage


text:
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Blockly Code</title>
  <link href="style.css" rel="stylesheet" type="text/css">
  <script type="text/javascript" src="/storage.js"></script>
  <script type="text/javascript" src="../_soy/soyutils.js"></script>
  <script type="text/javascript" src="template.js"></script>
  <script>
    var MSG = {
      // Tooltips.
      trashTooltip: 'Discard all blocks.',
      linkTooltip: 'Save and link to blocks.',
      runTooltip: 'Run program.',
      // Toolbox categories.
      catControl: 'Control',
      catLogic: 'Logic',
      catMath: 'Math',
      catText: 'Text',
      catLists: 'Lists',
      catColour: 'Colour',
      catVariables: 'Variables',
      catProcedures: 'Procedures',
      // Misc text.
      blocks: 'Blocks',
      badXml: 'Error parsing XML:\n%1\n\nAbandon changes?',
      badCode: 'Program error:\n%1',
      timeout: 'Maximum execution iterations exceeded.',
      discard: 'Delete all "%1" blocks?',
      title: 'Code'
    };
    if ('BlocklyStorage' in window) {
      BlocklyStorage.HTTPREQUEST_ERROR = 'There was a problem with the request.\n';
      BlocklyStorage.LINK_ALERT = 'Share your blocks with this link:\n\n';
      BlocklyStorage.HASH_ERROR = 'Sorry, "%1" doesn\'t correspond with any saved Blockly file.';
      BlocklyStorage.XML_ERROR = 'Could not load your saved file.\n'+
          'Perhaps it was created with a different version of Blockly?\nXML: ';
    }
    var frameSrc = ['en/_messages.js',
        'common/control.js',
        'common/logic.js',
        'common/math.js',
        'common/text.js',
        'common/lists.js',
        'common/colour.js',
        'common/variables.js',
        'common/procedures.js'];
  </script>
</head>
<body>
  <script type="text/javascript" src="code.js"></script>
</body>
</html>


text:
package org.apache.camel.component.jsonb;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.dataformat.JsonLibrary;

public class JsonbJsonDataFormatTest extends JsonbMarshalTest {

    @Override
    protected RouteBuilder createRouteBuilder() {
        return new RouteBuilder() {
            @Override
            public void configure() {
                from("direct:in").marshal().json(JsonLibrary.Jsonb);
                from("direct:back").unmarshal().json(JsonLibrary.Jsonb).to("mock:reverse");

                from("direct:inPojo").marshal().json(JsonLibrary.Jsonb);
                from("direct:backPojo").unmarshal().json(JsonLibrary.Jsonb, TestPojo.class).to("mock:reversePojo");
            }
        };
    }

}


text:
class ImageUploader < FileUploader
  include CarrierWave::MiniMagick

  version :thumb do
    process resize_to_fit: [nil, 200]
  end

  version :large do
    process resize_to_fit: [nil, 720]
  end
end


text: Describes the network library


text:
package org.apache.hyracks.storage.am.lsm.common.api;

import java.io.Serializable;
import java.util.List;

import org.apache.hyracks.api.context.IHyracksTaskContext;
import org.apache.hyracks.api.exceptions.HyracksDataException;
import org.apache.hyracks.dataflow.std.file.IFileSplitProvider;

@FunctionalInterface
public interface IVirtualBufferCacheProvider extends Serializable {
    List<IVirtualBufferCache> getVirtualBufferCaches(IHyracksTaskContext ctx, IFileSplitProvider fileSplitProvider)
            throws HyracksDataException;
}


text:
"""
pynamodb attributes tests
"""
import json

from base64 import b64encode
from datetime import datetime
from datetime import timedelta
from datetime import timezone

from unittest.mock import patch, call
import pytest

from pynamodb.attributes import (
    BinarySetAttribute, BinaryAttribute, DynamicMapAttribute, NumberSetAttribute, NumberAttribute,
    UnicodeAttribute, UnicodeSetAttribute, UTCDateTimeAttribute, BooleanAttribute, MapAttribute,
    ListAttribute, JSONAttribute, TTLAttribute, VersionAttribute)
from pynamodb.constants import (
    DEFAULT_ENCODING, NUMBER, STRING, STRING_SET, NUMBER_SET, BINARY_SET,
    BINARY, BOOLEAN,
)
from pynamodb.models import Model

class AttributeTestModel(Model):

    class Meta:
        host = 'http://localhost:8000'
        table_name = 'test'

    binary_attr = BinaryAttribute(hash_key=True)
    binary_set_attr = BinarySetAttribute()
    number_attr = NumberAttribute()
    number_set_attr = NumberSetAttribute()
    unicode_attr = UnicodeAttribute()
    unicode_set_attr = UnicodeSetAttribute()
    datetime_attr = UTCDateTimeAttribute()
    bool_attr = BooleanAttribute()
    json_attr = JSONAttribute()
    map_attr = MapAttribute()
    ttl_attr = TTLAttribute()

class CustomAttrMap(MapAttribute):
    overridden_number_attr = NumberAttribute(attr_name="number_attr")
    overridden_unicode_attr = UnicodeAttribute(attr_name="unicode_attr")

class DefaultsMap(MapAttribute):
    map_field = MapAttribute(default={})
    string_set_field = UnicodeSetAttribute(null=True)

class TestAttributeDescriptor:
    """
    Test Attribute Descriptors
    """
    def setup(self):
        self.instance = AttributeTestModel()

    def test_binary_attr(self):
        """
        Binary attribute descriptor
        """
        self.instance.binary_attr = b'test'
        assert self.instance.binary_attr == b'test'

    def test_binary_set_attr(self):
        """
        Binary set attribute descriptor
        """
        self.instance.binary_set_attr = {b'test', b'test2'}
        assert self.instance.binary_set_attr == {b'test', b'test2'}

    def test_number_attr(self):
        """
        Number attribute descriptor
        """
        self.instance.number_attr = 42
        assert self.instance.number_attr == 42

    def test_number_set_attr(self):
        """
        Number set attribute descriptor
        """
        self.instance.number_set_attr = {1, 2}
        assert self.instance.number_set_attr == {1, 2}

    def test_unicode_attr(self):
        """
        Unicode attribute descriptor
        """
        self.instance.unicode_attr = u"test"
        assert self.instance.unicode_attr == u"test"

    def test_unicode_set_attr(self):
        """
        Unicode set attribute descriptor
        """
        self.instance.unicode_set_attr = {u"test", u"test2"}
        assert self.instance.unicode_set_attr == {u"test", u"test2"}

    def test_datetime_attr(self):
        """
        Datetime attribute descriptor
        """
        now = datetime.now()
        self.instance.datetime_attr = now
        assert self.instance.datetime_attr == now

    def test_bool_attr(self):
        """
        Boolean attribute descriptor
        """
        self.instance.bool_attr = True
        assert self.instance.bool_attr is True

    def test_json_attr(self):
        """
        JSON attribute descriptor
        """
        self.instance.json_attr = {'foo': 'bar', 'bar': 42}
        assert self.instance.json_attr == {'foo': 'bar', 'bar': 42}

class TestUTCDateTimeAttribute:
    """
    Tests UTCDateTime attributes
    """

    def setup(self):
        self.attr = UTCDateTimeAttribute()
        self.dt = datetime(2047, 1, 6, 8, 21, 30, 2000, tzinfo=timezone.utc)

    def test_utc_datetime_attribute(self):
        """
        UTCDateTimeAttribute.default
        """
        attr = UTCDateTimeAttribute(default=self.dt)
        assert attr.attr_type == STRING
        assert attr.default == self.dt

    def test_utc_date_time_serialize(self):
        """
        UTCDateTimeAttribute.serialize
        """
        assert self.attr.serialize(self.dt) == '2047-01-06T08:21:30.002000+0000'

    def test_utc_date_time_serialize_pre_1000(self):
        dt = self.dt.replace(year=1)
        assert self.attr.serialize(dt) == '0001-01-06T08:21:30.002000+0000'

    def test_utc_date_time_deserialize(self):
        """
        UTCDateTimeAttribute.deserialize
        """
        assert self.attr.deserialize('2047-01-06T08:21:30.002000+0000') == self.dt

    def test_utc_date_time_deserialize_pre_1000_not_padded(self):
        assert self.attr.deserialize('1-01-06T08:21:30.002000+0000') == self.dt.replace(year=1)

    @pytest.mark.parametrize(
        "invalid_string",
        [
            '2047-01-06T08:21:30.002000',       # naive datetime
            '2047-01-06T08:21:30+0000',         # missing microseconds
            '2047-01-06T08:21:30.001+0000',     # shortened microseconds
            '2047-01-06T08:21:30.002000-0000'   # "negative" utc
            '2047-01-06T08:21:30.002000+0030'   # not utc
            '2047-01-06 08:21:30.002000+0000',  # missing separator
            '2.47-01-06T08:21:30.002000+0000',
            'abcd-01-06T08:21:30.002000+0000',
            '2047-ab-06T08:21:30.002000+0000',
            '2047-01-abT08:21:30.002000+0000',
            '2047-01-06Tab:21:30.002000+0000',
            '2047-01-06T08:ab:30.002000+0000',
            '2047-01-06T08:21:ab.002000+0000',
            '2047-01-06T08:21:30.a00000+0000',
        ]
    )
    def test_utc_date_time_invalid(self, invalid_string):
        with pytest.raises(ValueError, match="does not match format"):
            self.attr.deserialize(invalid_string)

class TestBinaryAttribute:
    """
    Tests binary attributes
    """
    def test_binary_attribute(self):
        """
        BinaryAttribute.default
        """
        attr = BinaryAttribute()
        assert attr is not None
        assert attr.attr_type == BINARY

        attr = BinaryAttribute(default=b'foo')
        assert attr.default == b'foo'

    def test_binary_round_trip(self):
        """
        BinaryAttribute round trip
        """
        attr = BinaryAttribute()
        value = b'foo'
        serial = attr.serialize(value)
        assert attr.deserialize(serial) == value

    def test_binary_serialize(self):
        """
        BinaryAttribute.serialize
        """
        attr = BinaryAttribute()
        serial = b64encode(b'foo').decode(DEFAULT_ENCODING)
        assert attr.serialize(b'foo') == serial

    def test_binary_deserialize(self):
        """
        BinaryAttribute.deserialize
        """
        attr = BinaryAttribute()
        serial = b64encode(b'foo').decode(DEFAULT_ENCODING)
        assert attr.deserialize(serial) == b'foo'

    def test_binary_set_serialize(self):
        """
        BinarySetAttribute.serialize
        """
        attr = BinarySetAttribute()
        assert attr.attr_type == BINARY_SET
        assert sorted(attr.serialize({b'foo', b'bar'})) == ['YmFy', 'Zm9v']
        assert attr.serialize({}) is None

    def test_binary_set_round_trip(self):
        """
        BinarySetAttribute round trip
        """
        attr = BinarySetAttribute()
        value = {b'foo', b'bar'}
        serial = attr.serialize(value)
        assert attr.deserialize(serial) == value

    def test_binary_set_deserialize(self):
        """
        BinarySetAttribute.deserialize
        """
        attr = BinarySetAttribute()
        value = {b'foo', b'bar'}
        assert attr.deserialize(
            [b64encode(val).decode(DEFAULT_ENCODING) for val in sorted(value)]
        ) == value

    def test_binary_set_attribute(self):
        """
        BinarySetAttribute.serialize
        """
        attr = BinarySetAttribute()
        assert attr is not None

        attr = BinarySetAttribute(default={b'foo', b'bar'})
        assert attr.default == {b'foo', b'bar'}

class TestNumberAttribute:
    """
    Tests number attributes
    """
    def test_number_attribute(self):
        """
        NumberAttribute.default
        """
        attr = NumberAttribute()
        assert attr is not None
        assert attr.attr_type == NUMBER

        attr = NumberAttribute(default=1)
        assert attr.default == 1

    def test_number_serialize(self):
        """
        NumberAttribute.serialize
        """
        attr = NumberAttribute()
        assert attr.serialize(3.141) == '3.141'
        assert attr.serialize(1) == '1'
        assert attr.serialize(12345678909876543211234234324234) == '12345678909876543211234234324234'

    def test_number_deserialize(self):
        """
        NumberAttribute.deserialize
        """
        attr = NumberAttribute()
        assert attr.deserialize('1') == 1
        assert attr.deserialize('3.141') == 3.141
        assert attr.deserialize('12345678909876543211234234324234') == 12345678909876543211234234324234

    def test_number_set_deserialize(self):
        """
        NumberSetAttribute.deserialize
        """
        attr = NumberSetAttribute()
        assert attr.attr_type == NUMBER_SET
        assert attr.deserialize([json.dumps(val) for val in sorted({1, 2})]) == {1, 2}

    def test_number_set_serialize(self):
        """
        NumberSetAttribute.serialize
        """
        attr = NumberSetAttribute()
        assert attr.serialize({1, 2}) == [json.dumps(val) for val in sorted({1, 2})]
        assert attr.serialize({}) is None

    def test_number_set_attribute(self):
        """
        NumberSetAttribute.default
        """
        attr = NumberSetAttribute()
        assert attr is not None

        attr = NumberSetAttribute(default={1, 2})
        assert attr.default == {1, 2}

class TestUnicodeAttribute:
    """
    Tests unicode attributes
    """
    def test_unicode_attribute(self):
        """
        UnicodeAttribute.default
        """
        attr = UnicodeAttribute()
        assert attr is not None
        assert attr.attr_type == STRING

        attr = UnicodeAttribute(default='foo')
        assert attr.default == 'foo'

    def test_unicode_serialize(self):
        """
        UnicodeAttribute.serialize
        """
        attr = UnicodeAttribute()
        assert attr.serialize('foo') == 'foo'
        assert attr.serialize('') == ''
        assert attr.serialize(None) is None

    def test_unicode_deserialize(self):
        """
        UnicodeAttribute.deserialize
        """
        attr = UnicodeAttribute()
        assert attr.deserialize('foo') == 'foo'
        assert attr.deserialize(u'foo') == 'foo'
        assert attr.deserialize('') == ''
        assert attr.deserialize(None) is None

    def test_unicode_set_serialize(self):
        """
        UnicodeSetAttribute.serialize
        """
        attr = UnicodeSetAttribute()
        assert attr.attr_type == STRING_SET
        assert attr.serialize({}) is None

        expected = sorted(['foo', 'bar'])
        assert sorted(attr.serialize({'foo', 'bar'})) == expected

        expected = sorted(['True', 'False'])
        assert sorted(attr.serialize({'True', 'False'})) == expected

        expected = sorted(['true', 'false'])
        assert sorted(attr.serialize({'true', 'false'})) == expected

    def test_round_trip_unicode_set(self):
        """
        Round trip a unicode set
        """
        attr = UnicodeSetAttribute()
        orig = {'foo', 'bar'}
        assert orig == attr.deserialize(attr.serialize(orig))

        orig = {'true', 'false'}
        assert orig == attr.deserialize(attr.serialize(orig))

        orig = {'1', '2.8'}
        assert orig == attr.deserialize(attr.serialize(orig))

        orig = {'[1,2,3]', '2.8'}
        assert orig == attr.deserialize(attr.serialize(orig))

    def test_unicode_set_deserialize(self):
        """
        UnicodeSetAttribute.deserialize
        """
        attr = UnicodeSetAttribute()
        value = {'foo', 'bar'}
        assert attr.deserialize(value) == value

        value = {'True', 'False'}
        assert attr.deserialize(value) == value

        value = {'true', 'false'}
        assert attr.deserialize(value) == value

        value = {'1', '2.8'}
        assert attr.deserialize(value) == value

    def test_unicode_set_attribute(self):
        """
        UnicodeSetAttribute.default
        """
        attr = UnicodeSetAttribute()
        assert attr is not None
        assert attr.attr_type == STRING_SET
        attr = UnicodeSetAttribute(default={'foo', 'bar'})
        assert attr.default == {'foo', 'bar'}

class TestBooleanAttribute:
    """
    Tests boolean attributes
    """
    def test_boolean_attribute(self):
        """
        BooleanAttribute.default
        """
        attr = BooleanAttribute()
        assert attr is not None

        assert attr.attr_type == BOOLEAN
        attr = BooleanAttribute(default=True)
        assert attr.default is True

    def test_boolean_serialize(self):
        """
        BooleanAttribute.serialize
        """
        attr = BooleanAttribute()
        assert attr.serialize(True) is True
        assert attr.serialize(False) is False
        assert attr.serialize(None) is None

    def test_boolean_deserialize(self):
        """
        BooleanAttribute.deserialize
        """
        attr = BooleanAttribute()
        assert attr.deserialize(True) is True
        assert attr.deserialize(False) is False

class TestTTLAttribute:
    """
    Test TTLAttribute.
    """
    def test_default_and_default_for_new(self):
        with pytest.raises(ValueError, match='An attribute cannot have both default and default_for_new parameters'):
            TTLAttribute(default=timedelta(seconds=1), default_for_new=timedelta(seconds=2))

    @patch('time.time')
    def test_timedelta_ttl(self, mock_time):
        mock_time.side_effect = [1559692800]  # 2019-06-05 00:00:00 UTC
        model = AttributeTestModel()
        model.ttl_attr = timedelta(seconds=60)
        assert model.ttl_attr == datetime(2019, 6, 5, 0, 1, tzinfo=timezone.utc)

    def test_datetime_naive_ttl(self):
        model = AttributeTestModel()
        with pytest.raises(ValueError, match='timezone-aware'):
            model.ttl_attr = datetime(2019, 6, 5, 0, 1)
        assert model.ttl_attr is None

    def test_datetime_with_tz_ttl(self):
        model = AttributeTestModel()
        model.ttl_attr = datetime(2019, 6, 5, 0, 1, tzinfo=timezone.utc)
        assert model.ttl_attr == datetime(2019, 6, 5, 0, 1, tzinfo=timezone.utc)

    def test_ttl_attribute_wrong_type(self):
        with pytest.raises(ValueError, match='TTLAttribute value must be a timedelta or datetime'):
            model = AttributeTestModel()
            model.ttl_attr = 'wrong type'

    @patch('time.time')
    def test_serialize_timedelta(self, mock_time):
        mock_time.side_effect = [1559692800]  # 2019-06-05 00:00:00 UTC
        assert TTLAttribute().serialize(timedelta(seconds=60)) == str(1559692800 + 60)

    def test_serialize_none(self):
        model = AttributeTestModel()
        model.ttl_attr = None
        assert model.ttl_attr == None
        assert TTLAttribute().serialize(model.ttl_attr) == None

    @patch('time.time')
    def test_serialize_deserialize(self, mock_time):
        mock_time.side_effect = [1559692800, 1559692800]  # 2019-06-05 00:00:00 UTC
        model = AttributeTestModel()
        model.ttl_attr = timedelta(minutes=1)
        assert model.ttl_attr == datetime(2019, 6, 5, 0, 1, tzinfo=timezone.utc)
        s = TTLAttribute().serialize(model.ttl_attr)
        assert s == '1559692860'
        assert TTLAttribute().deserialize(s) == datetime(2019, 6, 5, 0, 1, 0, tzinfo=timezone.utc)

class TestJSONAttribute:
    """
    Tests json attributes
    """
    def test_quoted_json(self):
        attr = JSONAttribute()
        serialized = attr.serialize('\\t')
        assert attr.deserialize(serialized) == '\\t'

        serialized = attr.serialize('"')
        assert attr.deserialize(serialized) == '"'

    def test_json_attribute(self):
        """
        JSONAttribute.default
        """
        attr = JSONAttribute()
        assert attr is not None

        assert attr.attr_type == STRING
        attr = JSONAttribute(default={})
        assert attr.default == {}

    def test_json_serialize(self):
        """
        JSONAttribute.serialize
        """
        attr = JSONAttribute()
        item = {'foo': 'bar', 'bool': True, 'number': 3.141}
        assert attr.serialize(item) == json.dumps(item)
        assert attr.serialize({}) == '{}'
        assert attr.serialize(None) is None

    def test_json_deserialize(self):
        """
        JSONAttribute.deserialize
        """
        attr = JSONAttribute()
        item = {'foo': 'bar', 'bool': True, 'number': 3.141}
        encoded = json.dumps(item)
        assert attr.deserialize(encoded) == item

    def test_control_chars(self):
        """
        JSONAttribute with control chars
        """
        attr = JSONAttribute()
        item = {'foo\t': 'bar\n', 'bool': True, 'number': 3.141}
        encoded = json.dumps(item)
        assert attr.deserialize(encoded) == item

class TestMapAttribute:
    """
    Tests map with str, int, float
    """
    def test_attribute_children(self):
        person_attribute = {
            'name': 'Justin',
            'age': 12345678909876543211234234324234,
            'height': 187.96
        }
        attr = MapAttribute()
        serialized = attr.serialize(person_attribute)
        assert attr.deserialize(serialized) == person_attribute

    # Special case for raw map attributes
    def test_null_attribute_raw_map(self):
        null_attribute = {
            'skip': None
        }
        attr = MapAttribute()
        serialized = attr.serialize(null_attribute)
        assert serialized == {'skip': {'NULL': True}}

    def test_null_attribute_subclassed_map(self):
        null_attribute = {
            'map_field': {},
            'string_set_field': None
        }
        attr = DefaultsMap()
        serialized = attr.serialize(null_attribute)
        assert serialized == {'map_field': {'M': {}}}

    def test_null_attribute_map_after_serialization(self):
        null_attribute = {
            'map_field': {},
            'string_set_field': {},
        }
        attr = DefaultsMap()
        serialized = attr.serialize(null_attribute)
        assert serialized == {'map_field': {'M': {}}}

    def test_map_of_map(self):
        attribute = {
            'name': 'Justin',
            'metrics': {
                'age': 31,
                'height': 187.96
            }
        }
        attr = MapAttribute()
        serialized = attr.serialize(attribute)
        assert attr.deserialize(serialized) == attribute

    def test_map_overridden_attrs_accessors(self):
        attr = CustomAttrMap(**{
            'overridden_number_attr': 10,
            'overridden_unicode_attr': "Hello"
        })

        assert attr.overridden_number_attr == 10
        assert attr.overridden_unicode_attr == "Hello"

    def test_map_overridden_attrs_serialize(self):
        attribute = {
            'overridden_number_attr': 10,
            'overridden_unicode_attr': "Hello"
        }
        expected = {'number_attr': {'N': '10'}, 'unicode_attr': {'S': 'Hello'}}
        assert CustomAttrMap().serialize(attribute) == expected

    def test_additional_attrs_deserialize(self):
        raw_data = {
            'number_attr': {
                'N': '10'},
            'unicode_attr': {
                'S': 'Hello'
            },
            'undeclared_attr': {
                'S': 'Goodbye'
            }
        }
        expected = {
            'overridden_number_attr': 10,
            'overridden_unicode_attr': "Hello"
        }
        assert CustomAttrMap().deserialize(raw_data).attribute_values == expected

    def test_defaults(self):
        item = DefaultsMap()
        assert item.validate()
        assert DefaultsMap().serialize(item) == {
            'map_field': {
                'M': {}
            }
        }

    def test_raw_set_attr(self):
        item = AttributeTestModel()
        item.map_attr = {}
        item.map_attr.foo = 'bar'
        item.map_attr.num = 3
        item.map_attr.nested = {'nestedfoo': 'nestedbar'}

        assert item.map_attr['foo'] == 'bar'
        assert item.map_attr['num'] == 3
        assert item.map_attr['nested']['nestedfoo'] == 'nestedbar'

    def test_raw_set_item(self):
        item = AttributeTestModel()
        item.map_attr = {}
        item.map_attr['foo'] = 'bar'
        item.map_attr['num'] = 3
        item.map_attr['nested'] = {'nestedfoo': 'nestedbar'}

        assert item.map_attr['foo'] == 'bar'
        assert item.map_attr['num'] == 3
        assert item.map_attr['nested']['nestedfoo'] == 'nestedbar'

    def test_raw_map_from_dict(self):
        item = AttributeTestModel(
            map_attr={
                "foo": "bar",
                "num": 3,
                "nested": {
                    "nestedfoo": "nestedbar"
                }
            }
        )

        assert item.map_attr['foo'] == 'bar'
        assert item.map_attr['num'] == 3

    def test_raw_map_access(self):
        raw = {
            "foo": "bar",
            "num": 3,
            "nested": {
                "nestedfoo": "nestedbar"
            }
        }
        attr = MapAttribute(**raw)

        for k, v in raw.items():
            assert attr[k] == v

    def test_raw_map_iter(self):
        raw = {
            "foo": "bar",
            "num": 3,
            "nested": {
                "nestedfoo": "nestedbar"
            }
        }
        attr = MapAttribute(**raw)

        assert sorted(iter(raw)) == sorted(iter(attr))

    def test_raw_map_json_serialize(self):
        raw = {
            "foo": "bar",
            "num": 3,
            "nested": {
                "nestedfoo": "nestedbar"
            }
        }

        serialized_raw = json.dumps(raw, sort_keys=True)
        serialized_attr_from_raw = json.dumps(
            AttributeTestModel(map_attr=raw).map_attr.as_dict(),
            sort_keys=True)
        serialized_attr_from_map = json.dumps(
            AttributeTestModel(map_attr=MapAttribute(**raw)).map_attr.as_dict(),
            sort_keys=True)

        assert serialized_attr_from_raw == serialized_raw
        assert serialized_attr_from_map == serialized_raw

    def test_typed_and_raw_map_json_serialize(self):
        class TypedMap(MapAttribute):
            map_attr = MapAttribute()

        class SomeModel(Model):
            key = NumberAttribute(hash_key=True)
            typed_map = TypedMap()

        item = SomeModel(
            typed_map=TypedMap(map_attr={'foo': 'bar'})
        )

        assert json.dumps({'map_attr': {'foo': 'bar'}}) == json.dumps(item.typed_map.as_dict())

    def test_json_serialize(self):
        class JSONMapAttribute(MapAttribute):
            arbitrary_data = JSONAttribute()

            def __eq__(self, other):
                return self.arbitrary_data == other.arbitrary_data

        item = {'foo': 'bar', 'bool': True, 'number': 3.141}
        json_map = JSONMapAttribute(arbitrary_data=item)
        serialized = json_map.serialize(json_map)
        deserialized = json_map.deserialize(serialized)
        assert isinstance(deserialized, JSONMapAttribute)
        assert deserialized == json_map
        assert deserialized.arbitrary_data == item

    def test_serialize_datetime(self):
        class CustomMapAttribute(MapAttribute):
            date_attr = UTCDateTimeAttribute()

        cm = CustomMapAttribute(date_attr=datetime(2017, 1, 1))
        serialized_datetime = cm.serialize(cm)
        expected_serialized_value = {
            'date_attr': {
                'S': u'2017-01-01T00:00:00.000000+0000'
            }
        }
        assert serialized_datetime == expected_serialized_value

    def test_complex_map_accessors(self):
        class NestedThing(MapAttribute):
            double_nested = MapAttribute()
            double_nested_renamed = MapAttribute(attr_name='something_else')

        class ThingModel(Model):
            key = NumberAttribute(hash_key=True)
            nested = NestedThing()

        t = ThingModel(nested=NestedThing(
            double_nested={'hello': 'world'},
            double_nested_renamed={'foo': 'bar'})
        )

        assert t.nested.double_nested.as_dict() == {'hello': 'world'}
        assert t.nested.double_nested_renamed.as_dict() == {'foo': 'bar'}
        assert t.nested.double_nested.hello == 'world'
        assert t.nested.double_nested_renamed.foo == 'bar'
        assert t.nested['double_nested'].as_dict() == {'hello': 'world'}
        assert t.nested['double_nested_renamed'].as_dict() == {'foo': 'bar'}
        assert t.nested['double_nested']['hello'] == 'world'
        assert t.nested['double_nested_renamed']['foo'] == 'bar'

        with pytest.raises(AttributeError):
            bad = t.nested.double_nested.bad
        with pytest.raises(AttributeError):
            bad = t.nested.bad
        with pytest.raises(AttributeError):
            bad = t.nested.something_else
        with pytest.raises(KeyError):
            bad = t.nested.double_nested['bad']
        with pytest.raises(KeyError):
            bad = t.nested['something_else']

    def test_attribute_paths_subclassing(self):
        class SubMapAttribute(MapAttribute):
            foo = UnicodeAttribute(attr_name='dyn_foo')

        class SubSubMapAttribute(SubMapAttribute):
            bar = UnicodeAttribute(attr_name='dyn_bar')

        class SubModel(Model):
            key = NumberAttribute(hash_key=True)
            sub_map = SubMapAttribute(attr_name='dyn_sub_map')

        class SubSubModel(SubModel):
            sub_sub_map = SubSubMapAttribute()

        assert SubModel.sub_map.foo.attr_name == 'dyn_foo'
        assert SubModel.sub_map.foo.attr_path == ['dyn_sub_map', 'dyn_foo']
        assert SubSubModel.sub_map.foo.attr_name == 'dyn_foo'
        assert SubSubModel.sub_map.foo.attr_path == ['dyn_sub_map', 'dyn_foo']
        assert SubSubModel.sub_sub_map.foo.attr_name == 'dyn_foo'
        assert SubSubModel.sub_sub_map.foo.attr_path == ['sub_sub_map', 'dyn_foo']
        assert SubSubModel.sub_sub_map.bar.attr_name == 'dyn_bar'
        assert SubSubModel.sub_sub_map.bar.attr_path == ['sub_sub_map', 'dyn_bar']

    def test_attribute_paths_wrapping(self):
        class InnerMapAttribute(MapAttribute):
            map_attr = MapAttribute(attr_name='dyn_map_attr')

        class MiddleMapAttributeA(MapAttribute):
            inner_map = InnerMapAttribute(attr_name='dyn_in_map_a')

        class MiddleMapAttributeB(MapAttribute):
            inner_map = InnerMapAttribute(attr_name='dyn_in_map_b')

        class OuterMapAttribute(MapAttribute):
            mid_map_a = MiddleMapAttributeA()
            mid_map_b = MiddleMapAttributeB()

        class MyModel(Model):
            key = NumberAttribute(hash_key=True)
            outer_map = OuterMapAttribute(attr_name='dyn_out_map')

        mid_map_a_map_attr = MyModel.outer_map.mid_map_a.inner_map.map_attr
        mid_map_b_map_attr = MyModel.outer_map.mid_map_b.inner_map.map_attr

        assert mid_map_a_map_attr.attr_name == 'dyn_map_attr'
        assert mid_map_a_map_attr.attr_path == ['dyn_out_map', 'mid_map_a', 'dyn_in_map_a', 'dyn_map_attr']
        assert mid_map_b_map_attr.attr_name == 'dyn_map_attr'
        assert mid_map_b_map_attr.attr_path == ['dyn_out_map', 'mid_map_b', 'dyn_in_map_b', 'dyn_map_attr']

    def test_required_elements(self):
        class InnerMapAttribute(MapAttribute):
            foo = UnicodeAttribute()

        class OuterMapAttribute(MapAttribute):
            inner_map = InnerMapAttribute()

        outer_map_attribute = OuterMapAttribute()
        with pytest.raises(ValueError):
            outer_map_attribute.serialize(outer_map_attribute)

        outer_map_attribute = OuterMapAttribute(inner_map={})
        with pytest.raises(ValueError):
            outer_map_attribute.serialize(outer_map_attribute)

        outer_map_attribute = OuterMapAttribute(inner_map=MapAttribute())
        with pytest.raises(ValueError):
            outer_map_attribute.serialize(outer_map_attribute)

        outer_map_attribute = OuterMapAttribute(inner_map={'foo': 'bar'})
        serialized = outer_map_attribute.serialize(outer_map_attribute)
        assert serialized == {'inner_map': {'M': {'foo': {'S': 'bar'}}}}

class TestDynamicMapAttribute:

    class CreatedAtTestModel(Model):
        class CreatedAtMap(DynamicMapAttribute):
            created_at = UTCDateTimeAttribute()
        test_map = CreatedAtMap(default=dict)

    def test_serialize(self):
        test_model = TestDynamicMapAttribute.CreatedAtTestModel()
        test_model.test_map.created_at = datetime(2017, 1, 1, tzinfo=timezone.utc)
        test_model.test_map.foo = 'bar'
        test_model.test_map.empty = None
        assert test_model.serialize() == {'test_map': {'M': {
            'created_at': {'S': '2017-01-01T00:00:00.000000+0000'},
            'foo': {'S': 'bar'},
            'empty': {'NULL': True},
        }}}

    def test_deserialize(self):
        serialized = {'test_map': {'M': {
            'created_at': {'S': '2017-01-01T00:00:00.000000+0000'},
            'foo': {'S': 'bar'},
            'empty': {'NULL': True},
        }}}
        test_model = TestDynamicMapAttribute.CreatedAtTestModel()
        test_model.deserialize(serialized)
        assert test_model.test_map.created_at == datetime(2017, 1, 1, tzinfo=timezone.utc)
        assert test_model.test_map.foo == 'bar'
        assert test_model.test_map.empty is None

class TestListAttribute:

    def test_untyped_list(self):
        untyped_list = [{'Hello': 'World'}, ['!'], {'foo', 'bar'}, None, "", 0, False]
        serialized = ListAttribute().serialize(untyped_list)
        # set attributes are serialized as lists
        untyped_list[2] = list(untyped_list[2])
        assert ListAttribute().deserialize(serialized) == untyped_list

    def test_list_of_strings(self):
        string_list_attribute = ListAttribute(of=UnicodeAttribute)
        string_list = ['foo', 'bar', 'baz']
        serialized = string_list_attribute.serialize(string_list)
        assert string_list_attribute.deserialize(serialized) == string_list

    def test_list_type_error(self):
        string_list_attribute = ListAttribute(of=UnicodeAttribute)

        with pytest.raises(ValueError):
            string_list_attribute.serialize([MapAttribute(foo='bar')])

        with pytest.raises(TypeError):
            string_list_attribute.deserialize([{'M': {'foo': {'S': 'bar'}}}])

    def test_serialize_null(self):
        string_set_list_attribute = ListAttribute(of=UnicodeSetAttribute)
        list_with_empty_set = [{'foo'}, {}, None]
        serialized = string_set_list_attribute.serialize(list_with_empty_set)
        assert string_set_list_attribute.deserialize(serialized) == [{'foo'}, None, None]

class TestMapAndListAttribute:

    def test_map_of_list(self):
        grocery_list = {
            'fruit': ['apple', 'pear', 32],
            'veggies': ['broccoli', 'potatoes', 5]
        }
        serialized = MapAttribute().serialize(grocery_list)
        assert MapAttribute().deserialize(serialized) == grocery_list

    def test_map_of_list_of_map(self):
        family_attributes = {
            'phillips': [
                {
                    'name': 'Justin',
                    'age': 31,
                    'height': 187.96
                },
                {
                    'name': 'Galen',
                    'age': 29,
                    'height': 193.04,
                    'male': True
                },
                {
                    'name': 'Colin',
                    'age': 32,
                    'height': 203.2,
                    'male': True,
                    'hasChild': True
                }
            ],
            'workman': [
                {
                    'name': 'Mandy',
                    'age': 29,
                    'height': 157.48,
                    'female': True
                },
                {
                    'name': 'Rodney',
                    'age': 31,
                    'height': 175.26,
                    'hasChild': False
                }
            ]
        }
        serialized = MapAttribute().serialize(family_attributes)
        assert MapAttribute().deserialize(serialized) == family_attributes

    def test_list_of_map_with_of(self):
        class Person(MapAttribute):
            name = UnicodeAttribute()
            age = NumberAttribute()

            def __lt__(self, other):
                return self.name < other.name

            def __eq__(self, other):
                return (self.name == other.name and
                        self.age == other.age)

        person1 = Person()
        person1.name = 'john'
        person1.age = 40

        person2 = Person()
        person2.name = 'Dana'
        person2.age = 41

        inp = [person1, person2]

        list_attribute = ListAttribute(default=[], of=Person)
        serialized = list_attribute.serialize(inp)
        deserialized = list_attribute.deserialize(serialized)
        assert sorted(deserialized) == sorted(inp)

    def test_list_of_map_with_of_and_custom_attribute(self, mocker):

        class CustomMapAttribute(MapAttribute):
            custom = NumberAttribute()

            def __eq__(self, other):
                return self.custom == other.custom

        serialize_mock = mocker.spy(CustomMapAttribute.custom, 'serialize',)
        deserialize_mock = mocker.spy(CustomMapAttribute.custom, 'deserialize')

        attribute1 = CustomMapAttribute()
        attribute1.custom = 1

        attribute2 = CustomMapAttribute()
        attribute2.custom = 2

        inp = [attribute1, attribute2]

        list_attribute = ListAttribute(default=[], of=CustomMapAttribute)
        serialized = list_attribute.serialize(inp)
        deserialized = list_attribute.deserialize(serialized)

        assert deserialized == inp
        assert serialize_mock.call_args_list == [call(1), call(2)]
        assert deserialize_mock.call_args_list == [call('1'), call('2')]

class TestVersionAttribute:
    def test_serialize(self):
        attr = VersionAttribute()
        assert attr.attr_type == NUMBER
        assert attr.serialize(3.141) == '3'
        assert attr.serialize(1) == '1'
        assert attr.serialize(12345678909876543211234234324234) == '12345678909876543211234234324234'

    def test_deserialize(self):
        attr = VersionAttribute()
        assert attr.deserialize('1') == 1
        assert attr.deserialize('3.141') == 3
        assert attr.deserialize('12345678909876543211234234324234') == 12345678909876543211234234324234


text:
package it.unisa.dia.gas.crypto.jpbc.signature.bls01;

import it.unisa.dia.gas.crypto.jpbc.signature.bls01.engines.BLS01Signer;
import it.unisa.dia.gas.crypto.jpbc.signature.bls01.generators.BLS01KeyPairGenerator;
import it.unisa.dia.gas.crypto.jpbc.signature.bls01.generators.BLS01ParametersGenerator;
import it.unisa.dia.gas.crypto.jpbc.signature.bls01.params.BLS01KeyGenerationParameters;
import it.unisa.dia.gas.crypto.jpbc.signature.bls01.params.BLS01Parameters;
import it.unisa.dia.gas.plaf.jpbc.pairing.PairingFactory;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.CryptoException;
import org.bouncycastle.crypto.digests.SHA256Digest;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
 * @author Angelo De Caro (jpbclib@gmail.com)
 */
public class BLS01 {

    public BLS01() {
    }

    public BLS01Parameters setup() {
        BLS01ParametersGenerator setup = new BLS01ParametersGenerator();
        setup.init(PairingFactory.getPairingParameters("params/curves/a.properties"));

        return setup.generateParameters();
    }

    public AsymmetricCipherKeyPair keyGen(BLS01Parameters parameters) {
        BLS01KeyPairGenerator keyGen = new BLS01KeyPairGenerator();
        keyGen.init(new BLS01KeyGenerationParameters(null, parameters));

        return keyGen.generateKeyPair();
    }

    public byte[] sign(String message, CipherParameters privateKey) {
        byte[] bytes = message.getBytes();

        BLS01Signer signer = new BLS01Signer(new SHA256Digest());
        signer.init(true, privateKey);
        signer.update(bytes, 0, bytes.length);

        byte[] signature = null;
        try {
            signature = signer.generateSignature();
        } catch (CryptoException e) {
            throw new RuntimeException(e);
        }
        return signature;
    }

    public boolean verify(byte[] signature, String message, CipherParameters publicKey) {
        byte[] bytes = message.getBytes();

        BLS01Signer signer = new BLS01Signer(new SHA256Digest());
        signer.init(false, publicKey);
        signer.update(bytes, 0, bytes.length);

        return signer.verifySignature(signature);
    }

    public static void main(String[] args) {
        BLS01 bls01 = new BLS01();

        // Setup
        AsymmetricCipherKeyPair keyPair = bls01.keyGen(bls01.setup());

        // Test same message
        String message = "Hello World!";
        assertTrue(bls01.verify(bls01.sign(message, keyPair.getPrivate()), message, keyPair.getPublic()));

        // Test different messages
        assertFalse(bls01.verify(bls01.sign(message, keyPair.getPrivate()), "Hello Italy!", keyPair.getPublic()));
    }

}


text:
namespace ci {

constexpr size_t CuckooKicker::kDefaultMaxKicks;

size_t CuckooKicker::GetNumSecondaryItems(const size_t bucket_idx) const {
  const Bucket& bucket = buckets_[bucket_idx];
  return absl::c_count_if(bucket.slots_,
                          [bucket_idx](const CuckooValue& value) {
                            return value.secondary_bucket == bucket_idx;
                          });
}

void CuckooKicker::FindVictim(const size_t victim_idx,
                              const size_t primary_bucket_idx,
                              const size_t secondary_bucket_idx,
                              const bool kick_secondary,
                              size_t* victim_bucket_idx,
                              size_t* idx_within_victim_bucket) const {
  // Iterate through potential victims in primary and secondary bucket until
  // we've found `victim_idx`.
  size_t curr_victim_idx = 0;

  auto search_bucket = [&](size_t bucket_idx) {
    const Bucket& bucket = buckets_[bucket_idx];
    for (size_t i = 0; i < bucket.slots_.size(); ++i) {
      const CuckooValue& curr_val = bucket.slots_[i];
      const size_t bucket_idx_to_compare =
          kick_secondary ? curr_val.secondary_bucket : curr_val.primary_bucket;
      if (bucket_idx_to_compare == bucket_idx) {
        // Slot `i` contains a potential victim.
        if (curr_victim_idx == victim_idx) {
          *victim_bucket_idx = bucket_idx;
          *idx_within_victim_bucket = i;
          return true;
        }
        ++curr_victim_idx;
      }
    }
    return false;
  };

  if (search_bucket(primary_bucket_idx)) return;
  if (search_bucket(secondary_bucket_idx)) return;

  assert(false);  // "Couldn't find victim with idx " << victim_idx;
}

CuckooValue CuckooKicker::SwapWithValue(Bucket* bucket, const size_t victim_idx,
                                        const CuckooValue& value) {
  const CuckooValue victim = bucket->slots_[victim_idx];
  bucket->slots_[victim_idx] = value;
  return victim;
}

CuckooValue CuckooKicker::SwapWithRandomValue(const CuckooValue& value,
                                              size_t* victim_bucket_idx) {
  Bucket* primary_bucket = &buckets_[value.primary_bucket];
  Bucket* secondary_bucket = &buckets_[value.secondary_bucket];

  // Method may only be called when both buckets are full.
  assert(primary_bucket->slots_.size() == slots_per_bucket_);
  assert(secondary_bucket->slots_.size() == slots_per_bucket_);

  if (!skew_kicking_) {
    // Select victim bucket.
    *victim_bucket_idx =
        GetRandomBool() ? value.primary_bucket : value.secondary_bucket;
    Bucket* victim_bucket = &buckets_[*victim_bucket_idx];
    // Choose any value as victim (irrespective of whether it resides in its
    // primary or secondary bucket).
    return SwapWithValue(victim_bucket, GetRandomVictimIndex(), value);
  }

  // Skew kicking.

  const size_t num_slots_both_buckets = 2 * slots_per_bucket_;

  // Count number of items that reside in their secondary bucket.
  const size_t num_in_secondary = GetNumSecondaryItems(value.primary_bucket) +
                                  GetNumSecondaryItems(value.secondary_bucket);

  if (num_in_secondary == 0 || num_in_secondary == num_slots_both_buckets) {
    // Can't perform skewed kick. Just kick any item.
    *victim_bucket_idx =
        GetRandomBool() ? value.primary_bucket : value.secondary_bucket;
    Bucket* victim_bucket = &buckets_[*victim_bucket_idx];
    return SwapWithValue(victim_bucket, GetRandomVictimIndex(), value);
  }
  const size_t num_in_primary = num_slots_both_buckets - num_in_secondary;

  // "Weigh" probability according to the ratio of items in secondary buckets
  // vs. items in primary buckets. For example, if the ratio is 3:1, we give
  // three times as much weight to the set of items in secondary buckets to have
  // equal probabilities for all items under a `kick_skew_factor_` of 1.0. The
  // reason for that is that we below first decide whether to kick a primary or
  // a secondary item and then choose a victim within the respective set.
  double secondary_weight_factor =
      static_cast<double>(num_in_secondary) / num_in_primary;

  // Add `kick_skew_factor_`.
  secondary_weight_factor *= kick_skew_factor_;

  // Set probability such that it is `secondary_weight_factor` times more likely
  // to kick an item from the secondary vs. one from the primary set.
  const double weighted_probability =
      secondary_weight_factor / (secondary_weight_factor + 1);
  assert(weighted_probability > 0.0 && weighted_probability < 1.0);

  // Roll a (skewed) dice to decide whether we want to kick a primary or a
  // secondary item.
  const bool kick_secondary = GetRandomBool(weighted_probability);

  // Choose victim index (within set of primary/secondary items).
  const size_t num_potential_victims =
      kick_secondary ? num_in_secondary : num_in_primary;
  const size_t victim_idx = GetRandomVictimIndex(num_potential_victims);

  // Find and kick victim.
  size_t idx_within_victim_bucket;
  FindVictim(victim_idx, value.primary_bucket, value.secondary_bucket,
             kick_secondary, victim_bucket_idx, &idx_within_victim_bucket);
  Bucket* victim_bucket = &buckets_[*victim_bucket_idx];
  return SwapWithValue(victim_bucket, idx_within_victim_bucket, value);
}

bool CuckooKicker::InsertValueWithKick(CuckooValue* value) {
  // Swap `value` with random value inside its primary or secondary bucket.
  size_t victim_bucket_idx;
  const CuckooValue victim = SwapWithRandomValue(*value, &victim_bucket_idx);

  // Try to insert `victim` into its alternative bucket.
  const size_t alternative_bucket_idx =
      victim_bucket_idx == victim.primary_bucket ? victim.secondary_bucket
                                                 : victim.primary_bucket;
  Bucket* alternative_bucket = &buckets_[alternative_bucket_idx];
  if (alternative_bucket->InsertValue(victim)) return true;

  // Alternative bucket is full. Victim becomes new in-flight value.
  *value = victim;
  return false;
}

bool CuckooKicker::InsertValueWithKicking(const CuckooValue& value) {
  Bucket* primary_bucket = &buckets_[value.primary_bucket];
  Bucket* secondary_bucket = &buckets_[value.secondary_bucket];

  if (primary_bucket->InsertValue(value)) return true;
  if (secondary_bucket->InsertValue(value)) return true;

  // Both buckets are full. Try to insert with kicking.
  CuckooValue in_flight_value = value;
  for (size_t num_kicks = 0; num_kicks <= max_kicks_; ++num_kicks) {
    if (InsertValueWithKick(&in_flight_value)) {
      if (num_kicks > max_kicks_observed_) max_kicks_observed_ = num_kicks;
      return true;
    }
  }

  // Exceeded `max_kicks_` kicks. Insertion failed.
  return false;
}

}  // namespace ci


text:
# Alert Definitions
Definitions are the templates that are used to distribute alerts to the appropriate Ambari agents. They govern the type of alert, the threshold values, and the information to be used when notifying a target. A single definition can be distributed to more than one host in order to produce multiple instances of an alert.

### Concepts
Each definition contains common information, such as a unique identifier, service, and component. Beyond this, definitions declare a type with each type having its own distinct properties.

##### Common Properties

- id
- name
- label
- cluster_name
- service_name
- component_name
- source

#### Types
###### SCRIPT
Script definitions defer all functionality to a Python script accessible to the Ambari agents from a specified relative or absolute path.

        "source" : {
          "path" : "HDFS/2.1.0.2.0/package/alerts/alert_ha_namenode_health.py",
          "type" : "SCRIPT"
        }

###### METRIC
METRIC source fields are used to define JMX endpoints that can be queried for values. The `source/reporting` and `jmx/value` fields are parameterized to match the `property_list` specified.

        "source" : {
          "jmx" : {
            "property_list" : [
              "java.lang:type=OperatingSystem/SystemCpuLoad",
              "java.lang:type=OperatingSystem/AvailableProcessors"
            ],
            "value" : "{0} * 100"
          },
          "reporting" : {
            "ok" : {
              "text" : "{1} CPU, load {0:.1%}"
            },
            "warning" : {
              "text" : "{1} CPU, load {0:.1%}",
              "value" : 200.0
            },
            "critical" : {
              "text" : "{1} CPU, load {0:.1%}",
              "value" : 250.0
            },
            "units" : "%"
          },
          "type" : "METRIC",
          "uri" : {
            "http" : "{{hdfs-site/dfs.namenode.http-address}}",
            "https" : "{{hdfs-site/dfs.namenode.https-address}}",
            "https_property" : "{{hdfs-site/dfs.http.policy}}",
            "https_property_value" : "HTTPS_ONLY",
            "default_port" : 0.0,
            "high_availability" : {
              "nameservice" : "{{hdfs-site/dfs.nameservices}}",
              "alias_key" : "{{hdfs-site/dfs.ha.namenodes.{{ha-nameservice}}}}",
              "http_pattern" : "{{hdfs-site/dfs.namenode.http-address.{{ha-nameservice}}.{{alias}}}}",
              "https_pattern" : "{{hdfs-site/dfs.namenode.https-address.{{ha-nameservice}}.{{alias}}}}"
            }
          }
        }

###### WEB
WEB definitions are similar in function to PORT definitions. However, instead of checking for TCP connectivity, they also verify that a proper HTTP response code was returned.

        "source" : {
          "reporting" : {
            "ok" : {
              "text" : "HTTP {0} response in {2:.3f} seconds"
            },
            "warning" : {
              "text" : "HTTP {0} response in {2:.3f} seconds"
            },
            "critical" : {
              "text" : "Connection failed to {1}: {3}"
            }
          },
          "type" : "WEB",
          "uri" : {
            "http" : "{{hdfs-site/dfs.namenode.http-address}}",
            "https" : "{{hdfs-site/dfs.namenode.https-address}}",
            "https_property" : "{{hdfs-site/dfs.http.policy}}",
            "https_property_value" : "HTTPS_ONLY",
            "kerberos_keytab" : "{{hdfs-site/dfs.web.authentication.kerberos.keytab}}",
            "kerberos_principal" : "{{hdfs-site/dfs.web.authentication.kerberos.principal}}",
            "default_port" : 0.0,
            "high_availability" : {
              "nameservice" : "{{hdfs-site/dfs.nameservices}}",
              "alias_key" : "{{hdfs-site/dfs.ha.namenodes.{{ha-nameservice}}}}",
              "http_pattern" : "{{hdfs-site/dfs.namenode.http-address.{{ha-nameservice}}.{{alias}}}}",
              "https_pattern" : "{{hdfs-site/dfs.namenode.https-address.{{ha-nameservice}}.{{alias}}}}"
            }
          }
        }

###### PORT
PORT definitions are used to check TCP connectivity to a remote endpoint.

       "source" : {
          "default_port" : 2181,
          "reporting" : {
            "ok" : {
              "text" : "TCP OK - {0:.3f}s response on port {1}"
            },
            "warning" : {
              "text" : "TCP OK - {0:.3f}s response on port {1}",
              "value" : 1.5
            },
            "critical" : {
              "text" : "Connection failed: {0} to {1}:{2}",
              "value" : 5.0
            }
          },
          "type" : "PORT",
          "uri" : "{{core-site/ha.zookeeper.quorum}}"
        }
      }

###### AGGREGATE
AGGREGATE definitions are used to combine the results of another alert definition from different nodes.  The `source/alert_name` field must match the `name` field of another alert definition.

        "source": {
          "type": "AGGREGATE",
          "alert_name": "datanode_process",
          "reporting": {
            "ok": {
              "text": "affected: [{1}], total: [{0}]"
            },
            "warning": {
              "text": "affected: [{1}], total: [{0}]",
              "value": 10
            },
            "critical": {
              "text": "affected: [{1}], total: [{0}]",
              "value": 30
            },
            "units" : "%",
            "type": "PERCENT"
          }
        }

#### Structures & Concepts
- `uri` Definition types that contain a URI can depend on any number of valid subproperties. In some cases, the URI may be very simple and only include a single port. In other scenarios, the URI may be more complex and include properties for plaintext, SSL, and secure endpoints protected by Kerberos.

    - http - a property that contains the plaintext endpoint to test
    - https - a property that contains the SSL endpoint to test
    - https_property - a property that contains the value which can be used to determine if the component is SSL protected
    - https_property_value - a constant value to compare with `https_property` in order to determine if the component is protected by SSL
    - kerberos_keytab - a property that contains the Kerberos keytab if security is enabled
    - kerberos_principal - a property that contains the Kerberos principal if security is enabled
    - default_port - a default port which can be used if none of the above properties can be realized
    - high_availability - a structure that contains a way to dynamically build properties which contain the endpoints to use when the components are running in HA mode

- `reporting` A structure that defines the thresholds and text to use when determining the state for an alert. `ok` is always a required element, however only a single `warning` or `critical` element is needed. Some alerts may only have two states (such as `OK` and `CRITICAL`) and will bypass the need for a `warning` element.

- `default_port` A URI, host, or integer that represents a fallback value to use if none of the other specified properties can be realized.

### API Summary

##### Create
    POST api/v1/clusters/<cluster>/alert_definitions

    {
      "AlertDefinition" : {
        "service_name" : "YARN",
        "component_name" : "RESOURCEMANAGER",
        "enabled" : true,
        "interval" : 1,
        "label" : "Example YARN ResourceManager Alert",
        "name" : "example_yarn_rm_alert",
        "scope" : "ANY",
        "source" : {
          "default_port" : 8050,
          "reporting" : {
            "ok" : {
              "text" : "TCP OK - {0:.4f} response on port {1}"
            },
           "warning" : {
              "text" : "TCP OK - {0:.3f}s response on port {1}",
              "value" : 1.5
            },
            "critical" : {
              "text" : "Connection failed: {0} on host {1}:{2}",
              "value" : 5.0
            }
          },
          "type" : "PORT",
          "uri" : "{{yarn-site/yarn.resourcemanager.address}}"
        }
      }
    }

##### Update
When updating an existing definition, then entire structure is not required. A partial request is allowed and will only change the properties that were supplied. Properties that are omitted are considered unchanged.

    PUT api/v1/clusters/<cluster>/alert_definitions/<definition-id>

    {
      "AlertDefinition" : {
        "interval" : 10,
          "uri" : "{{yarn-site/yarn.resourcemanager.address.foo}}"
        }
      }
    }

##### Delete
    DELETE api/v1/clusters/<cluster>/alert_definitions/<definition-id>

##### Querying
Definitions are always retrieved from the cluster endpoint since each cluster can have its own assortment of alerts defined.

###### Request
    GET api/v1/clusters/<cluster>/alert_definitions

###### Response
    {
      "href" : "http://<server>/api/v1/clusters/<cluster>/alert_definitions",
      "items" : [
        {
          "href" : "http://<server>/api/v1/clusters/<cluster>/alert_definitions/1",
          "AlertDefinition" : {
            "cluster_name" : "<cluster>",
            "id" : 1,
            "label" : "History Server Web UI",
            "name" : "mapreduce_history_server_webui"
          }
        },
        {
          "href" : "http://<server>/api/v1/clusters/<cluster>/alert_definitions/2",
          "AlertDefinition" : {
            "cluster_name" : "<cluster>",
            "id" : 2,
            "label" : "History Server Process",
            "name" : "mapreduce_history_server_process"
          }
        },
        ...
      ]
    }

Querying for definitions accepts the same predicates and syntax as other areas of the REST API. For example, to query for all definitions for `NAMENODE` that are of type `WEB` and `METRIC` the following query can be used.

###### Request
    GET api/v1/clusters/<cluster>/alert_definitions?AlertDefinition/component_name=NAMENODE&AlertDefinition/source/type.in(WEB,METRIC)

###### Response
    {
      "href" : "http://<server>/api/v1/clusters/<cluster>/alert_definitions?AlertDefinition/component_name=NAMENODE&AlertDefinition/source/type.in(WEB,METRIC)",
      "items" : [
        {
          "href" : "http://<server>/api/v1/clusters/<cluster>/alert_definitions/14",
          "AlertDefinition" : {
            "cluster_name" : "<cluster>",
            "component_name" : "NAMENODE",
            "id" : 14,
            "label" : "NameNode Host CPU Utilization",
            "name" : "namenode_cpu",
            "source" : {
              "jmx" : {
                "property_list" : [
                  "java.lang:type=OperatingSystem/SystemCpuLoad",
                  "java.lang:type=OperatingSystem/AvailableProcessors"
                ],
                "value" : "{0} * 100"
              },
              "reporting" : {
                "ok" : {
                  "text" : "{1} CPU, load {0:.1%}"
                },
                "warning" : {
                  "text" : "{1} CPU, load {0:.1%}",
                  "value" : 200.0
                },
                "critical" : {
                  "text" : "{1} CPU, load {0:.1%}",
                  "value" : 250.0
                },
                "units" : "%"
              },
              "type" : "METRIC",
              "uri" : {
                "http" : "{{hdfs-site/dfs.namenode.http-address}}",
                "https" : "{{hdfs-site/dfs.namenode.https-address}}",
                "https_property" : "{{hdfs-site/dfs.http.policy}}",
                "https_property_value" : "HTTPS_ONLY",
                "default_port" : 0.0,
                "high_availability" : {
                  "nameservice" : "{{hdfs-site/dfs.nameservices}}",
                  "alias_key" : "{{hdfs-site/dfs.ha.namenodes.{{ha-nameservice}}}}",
                  "http_pattern" : "{{hdfs-site/dfs.namenode.http-address.{{ha-nameservice}}.{{alias}}}}",
                  "https_pattern" : "{{hdfs-site/dfs.namenode.https-address.{{ha-nameservice}}.{{alias}}}}"
                }
              }
            }
          }
        },
        ...
      ]
    }

##### Immediate Execution
Definitions can be forcible triggered to run outside of their normally scheduled timeframe. This can be useful when verifying that an alert has changed state without waiting for it to run on its own.

###### Request
Using the `run_now` directive, a definition can be scheduled to run immediately on all hosts where it is distributed.

    PUT http://<server>/api/v1/clusters/<cluster>/alert_definitions/<definition-id>?run_now=true

###### Response
    HTTP 200 OK


text:
require 'rspec/given'
require 'expectacle/thrower'

module Expectacle
  describe Thrower do
    describe '.new' do
      Given(:thrower) { Thrower.new }

      describe '#prompts_dir' do
        When(:prompts_dir) { thrower.prompts_dir }
        Then { prompts_dir == File.join(thrower.base_dir, 'prompts') }
      end

      describe '#hosts_dir' do
        When(:hosts_dir) { thrower.hosts_dir }
        Then { hosts_dir == File.join(thrower.base_dir, 'hosts') }
      end

      describe '#commands_dir' do
        When(:commands_dir) { thrower.commands_dir }
        Then { commands_dir == File.join(thrower.base_dir, 'commands') }
      end

      describe '#opts_dir' do
        When(:opts_dir) { thrower.opts_dir }
        Then { opts_dir == File.join(thrower.base_dir, 'opts') }
      end
    end
  end
end


text:
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.util.xml;

import com.intellij.util.ArrayUtil;
import com.intellij.util.ArrayUtilRt;
import com.intellij.util.xml.ui.DomUIFactory;
import junit.framework.TestCase;
import net.sf.cglib.proxy.AdvancedProxy;
import net.sf.cglib.proxy.InvocationHandler;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author peter
 */
public class ProxyTest extends TestCase {

  public void testExtendClass() throws Throwable {
    final List<String> invocations = new ArrayList<>();
    Implementation implementation = AdvancedProxy.createProxy(Implementation.class, new Class[]{Interface3.class}, new InvocationHandler() {
      @Override
      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        invocations.add(method.getName());
        if (Object.class.equals(method.getDeclaringClass())) {
          return method.invoke(this, args);
        }
        return Implementation.class.getMethod("getField").invoke(proxy);
      }
    }, "239");
    //noinspection ResultOfMethodCallIgnored
    implementation.hashCode();
    implementation.method();
    assertEquals("239", implementation.getFoo());
    implementation.setField("42");
    assertEquals("42", implementation.getBar());
    assertEquals("42", implementation.toString());
    assertEquals(Arrays.asList("hashCode", "getFoo", "getFoo", "getBar"), invocations);

    assertEquals("42", Interface1.class.getMethod("getFoo").invoke(implementation));

    assertEquals("42", Interface3.class.getMethod("bar").invoke(implementation));

    assertEquals("42", Interface1.class.getMethod("foo").invoke(implementation));
    assertEquals("42", Interface2.class.getMethod("foo").invoke(implementation));
    assertEquals("42", Interface2.class.getMethod("foo").invoke(implementation));
    assertEquals("42", Implementation.class.getMethod("foo").invoke(implementation));
  }

  public interface Interface1 {
    Object getFoo();
    Object foo();
  }

  public interface Interface2 extends Interface1 {
    @Override
    CharSequence getFoo();
    @Override
    CharSequence foo();
  }

  public interface Interface3 extends Interface1 {
    @Override
    String foo();
    String bar();
  }

  private abstract static class Implementation implements Interface2 {
    private String myField;

    protected Implementation(final String field) {
      myField = field;
    }

    public String getField() {
      return myField;
    }

    public void setField(final String field) {
      myField = field;
    }

    public void method() {
      getFoo();
    }

    public String toString() {
      return myField;
    }

    @Override
    public abstract String getFoo();

    public abstract String getBar();

    @Override
    public abstract String foo();
  }

  @SuppressWarnings("CastToIncompatibleInterface")
  public void testAddInterfaces() {
    final BaseImpl proxy = AdvancedProxy.createProxy(BaseImpl.class, BaseIEx.class);
    assertEquals("a", proxy.sayA());
    assertEquals("a", ((BaseI)proxy).sayA());
    assertEquals("a", ((BaseIEx)proxy).sayA());
  }

  public interface BaseI {
    Object sayA();
  }

  public interface BaseIEx extends BaseI {
    @Override
    CharSequence sayA();
  }

  public static abstract class BaseImpl implements BaseI {
    @Override
    public String sayA() {
      return "a";
    }
  }

  public static abstract class AbstractBase implements BaseI {
    @Override
    public abstract String sayA();

    public abstract static class AbstractBaseImpl extends AbstractBase {
    }
  }

  public void testCovariantFromInterface() {
    final AbstractBase.AbstractBaseImpl proxy = AdvancedProxy.createProxy(AbstractBase.AbstractBaseImpl.class, ArrayUtil.EMPTY_CLASS_ARRAY,
                                                                          new InvocationHandler() {
                                                                            @Override
                                                                            public Object invoke(Object proxy,
                                                                                Method method,
                                                                                Object[] args) {
                                                                              return "a";
                                                                            }
                                                                          }, false, ArrayUtilRt.EMPTY_OBJECT_ARRAY);
    assertEquals("a", proxy.sayA());
    assertEquals("a", proxy.sayA());
    assertEquals("a", ((BaseI)proxy).sayA());
  }

  public static class CovariantFromBaseClassTest {
    public interface Intf {
      String sayA();
    }

    public static class Base {
      public Object sayA() {
        return "beeee";
      }
    }

    public abstract static class Impl extends Base implements Intf {
      @Override
      public abstract String sayA();
    }
  }

  public void testCovariantFromBaseClass() {
    final CovariantFromBaseClassTest.Impl proxy = AdvancedProxy.createProxy(CovariantFromBaseClassTest.Impl.class,
                                                                            ArrayUtil.EMPTY_CLASS_ARRAY, new InvocationHandler() {
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) {
          return "a";
        }
      }, false, ArrayUtilRt.EMPTY_OBJECT_ARRAY);
    assertEquals("a", proxy.sayA());
    assertEquals("a", ((CovariantFromBaseClassTest.Base)proxy).sayA());
    assertEquals("a", ((CovariantFromBaseClassTest.Intf)proxy).sayA());
  }

  public void testGenericMethodInvocationJava8() throws Throwable {
    ConcreteInterface proxy = AdvancedProxy.createProxy(new InvocationHandler() {
      @Override
      public Object invoke(Object proxy, Method method, Object[] args) {
        return 42;
      }
    }, null, ConcreteInterface.class);
    Method foo = DomUIFactory.findMethod(GenericInterface.class, "foo");
    assert foo != null;
    assertEquals(42, proxy.foo("a"));
    assertEquals(42, foo.invoke(proxy, "a"));
  }

  interface GenericInterface<T> {
    Object foo(T t);
  }

  interface ConcreteInterface extends GenericInterface<String> {
    @Override
    Object foo(String t);
  }
}


text:
require File.dirname(__FILE__) + '/../../test_helper'

class ApplicationHelperTest < HelperTestCase

  include ApplicationHelper

  context 'sortable_table_header' do
    setup do
      self.stubs(:default_sort_column).returns(:title)
      self.stubs(:params).returns({ :controller => :jobs, :action => :index, :sort => nil, :order => nil })
    end

    [:name, :sort].each do |param|
      should "raise an error without default param #{param}" do
        opts = { :name => 'name', :sort => 'sort' }
        opts.delete param
        assert_raise(ArgumentError) do
          sortable_table_header opts
        end
      end
    end

    context 'with no params[:sort] or params[:order]' do
      setup do
        @html = sortable_table_header(:name  => 'Title',
                                      :sort  => 'title',
                                      :title => 'Sort by title')
      end

      should 'return a table header without a class attribute' do
        assert @html.include?('<th>')
      end
    end

    context 'with params[:class]' do
      setup do
        @html = sortable_table_header(:name  => 'Title',
                                      :sort  => 'title',
                                      :title => 'Sort by title',
                                      :class => "hr_class")
      end

      should 'return a table header with a class attribute equal to the passed in class' do
        assert @html.include?('<th class="hr_class"')
      end
    end

    context "without an :anchor" do
      setup do
        @html = sortable_table_header(:name  => 'Title',
                                      :sort  => 'title',
                                      :title => 'Sort by title')
      end

      should 'return a link that contains a url with no anchor' do
        assert @html.match(/href="[^#]+?"/)
      end
    end

    context "with an :anchor" do
      setup do
        @html = sortable_table_header(:name   => 'Title',
                                      :sort   => 'title',
                                      :title  => 'Sort by title',
                                      :anchor => 'search-results')
      end

      should 'return a link that contains a url with that anchor' do
        assert @html.match(/href="[^"]*?#search-results"/)
      end
    end
  end

end


text:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.github.mealingr</groupId>
    <artifactId>number-name</artifactId>
    <version>1.0</version>

    <properties>
        <source.version>1.8</source.version>
        <target.version>1.8</target.version>
        <junit.verison>4.12</junit.verison>
        <hamcrest.version>1.3</hamcrest.version>
        <powermock.verison>1.6.6</powermock.verison>
        <jacoco.version>0.7.8</jacoco.version>
        <pitest.version>1.1.11</pitest.version>
        <checkstyle.plugin.version>2.17</checkstyle.plugin.version>
        <checkstyle.version>7.4</checkstyle.version>
        <failsafe.version>2.19.1</failsafe.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.verison}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-all</artifactId>
            <version>${hamcrest.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>${powermock.verison}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito</artifactId>
            <version>${powermock.verison}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.properties</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${source.version}</source>
                    <target>${target.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>${jacoco.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.pitest</groupId>
                <artifactId>pitest-maven</artifactId>
                <version>${pitest.version}</version>
                <configuration>
                    <targetClasses>
                        <param>com.github.mealingr.*</param>
                    </targetClasses>
                    <targetTests>
                        <param>com.github.mealingr.*</param>
                    </targetTests>
                    <avoidCallsTo>
                        <avoidCallsTo>java.util.logging</avoidCallsTo>
                    </avoidCallsTo>
                    <outputFormats>
                        <outputFormat>XML</outputFormat>
                        <outputFormat>HTML</outputFormat>
                    </outputFormats>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>${checkstyle.plugin.version}</version>
                <dependencies>
                    <dependency>
                        <groupId>com.puppycrawl.tools</groupId>
                        <artifactId>checkstyle</artifactId>
                        <version>${checkstyle.version}</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>validate</id>
                        <phase>validate</phase>
                        <configuration>
                            <configLocation>src/test/resources/checkstyle.xml</configLocation>
                            <encoding>UTF-8</encoding>
                            <consoleOutput>true</consoleOutput>
                            <failsOnError>true</failsOnError>
                        </configuration>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>${failsafe.version}</version>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.github.mealingr.numbername.Main</mainClass>
                            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                            <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <finalName>${project.artifactId}-${project.version}</finalName>
                    <appendAssemblyId>false</appendAssemblyId>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>


text:
@function can.Map.validations.static.validateLengthOf validateLengthOf
@parent can.Map.validations 4

@signature `observe.validateLengthOf(attrNames, min, max, options)`

Validates that the specified attributes' lengths are in the given range.

	init : function(){
		this.validateLengthOf(["suffix"],3,5)
	}

@param {Array<String>|String} attrNames Attribute name(s) to to validate
@param {Number} min Minimum length (inclusive)
@param {Number} max Maximum length (inclusive)
@param {Object} [options] Options for the validations.  Valid options include 'message' and 'testIf'.