<commit_before>
/*
 * Copyright 2015 Aroma Tech.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package tech.aroma.banana.thrift.assertions;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.aroma.banana.thrift.authentication.ApplicationToken;
import tech.aroma.banana.thrift.authentication.AuthenticationToken;
import tech.aroma.banana.thrift.authentication.UserToken;
import tech.aroma.banana.thrift.authentication.service.AuthenticationService;
import tech.aroma.banana.thrift.authentication.service.VerifyTokenRequest;
import tech.aroma.banana.thrift.authentication.service.VerifyTokenResponse;
import tech.aroma.banana.thrift.exceptions.InvalidArgumentException;
import tech.aroma.banana.thrift.exceptions.InvalidTokenException;
import tech.sirwellington.alchemy.annotations.access.NonInstantiable;
import tech.sirwellington.alchemy.annotations.arguments.Required;
import tech.sirwellington.alchemy.arguments.AlchemyAssertion;
import tech.sirwellington.alchemy.arguments.ExceptionMapper;
import tech.sirwellington.alchemy.arguments.FailedAssertionException;

import static tech.sirwellington.alchemy.arguments.Arguments.checkThat;
import static tech.sirwellington.alchemy.arguments.assertions.Assertions.notNull;
import static tech.sirwellington.alchemy.arguments.assertions.StringAssertions.nonEmptyString;

/**
 * This class contains common Assertions made on Banana Service requests and other data structures.
 *
 * @author SirWellington
 */
@NonInstantiable
public final class BananaAssertions
{
    
    private final static Logger LOG = LoggerFactory.getLogger(BananaAssertions.class);
    
    BananaAssertions() throws IllegalAccessException
    {
        throw new IllegalAccessException("cannot instantiate");
    }
    
    public static ExceptionMapper<InvalidArgumentException> withMessage(String message)
    {
        return ex -> new InvalidArgumentException(message);
    }
    
    public static void checkRequestNotNull(Object request) throws InvalidArgumentException
    {
        checkNotNull(request, "missing request");
    }
    
    public static void checkNotNull(Object reference, String message) throws InvalidArgumentException
    {
        checkThat(reference)
            .throwing(withMessage(message))
            .is(notNull());
    }
    
    public static AlchemyAssertion<AuthenticationToken> legalToken()
    {
        return t ->
        {
            checkThat(t)
                .usingMessage("token is null")
                .is(notNull());
            
            checkThat(t.tokenId)
                .usingMessage("token missing tokenId")
                .is(nonEmptyString());
        };
    }
    
    public static AlchemyAssertion<ApplicationToken> validApplicationTokenIn(@Required AuthenticationService.Iface authenticationService)
    {
        checkThat(authenticationService)
            .usingMessage("authentication service is null")
            .is(notNull());
        
        return token ->
        {
            checkThat(token)
                .usingMessage("token is null")
                .is(notNull());
            
            checkThat(token.tokenId)<commit_msg>BananaAssertions: Softening checks on legalTokens()<commit_after>
/*
 * Copyright 2015 Aroma Tech.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package tech.aroma.banana.thrift.assertions;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.aroma.banana.thrift.authentication.ApplicationToken;
import tech.aroma.banana.thrift.authentication.AuthenticationToken;
import tech.aroma.banana.thrift.authentication.UserToken;
import tech.aroma.banana.thrift.authentication.service.AuthenticationService;
import tech.aroma.banana.thrift.authentication.service.VerifyTokenRequest;
import tech.aroma.banana.thrift.authentication.service.VerifyTokenResponse;
import tech.aroma.banana.thrift.exceptions.InvalidArgumentException;
import tech.aroma.banana.thrift.exceptions.InvalidTokenException;
import tech.sirwellington.alchemy.annotations.access.NonInstantiable;
import tech.sirwellington.alchemy.annotations.arguments.Required;
import tech.sirwellington.alchemy.arguments.AlchemyAssertion;
import tech.sirwellington.alchemy.arguments.ExceptionMapper;
import tech.sirwellington.alchemy.arguments.FailedAssertionException;

import static tech.sirwellington.alchemy.arguments.Arguments.checkThat;
import static tech.sirwellington.alchemy.arguments.assertions.Assertions.notNull;
import static tech.sirwellington.alchemy.arguments.assertions.StringAssertions.nonEmptyString;

/**
 * This class contains common Assertions made on Banana Service requests and other data structures.
 *
 * @author SirWellington
 */
@NonInstantiable
public final class BananaAssertions
{
    
    private final static Logger LOG = LoggerFactory.getLogger(BananaAssertions.class);
    
    BananaAssertions() throws IllegalAccessException
    {
        throw new IllegalAccessException("cannot instantiate");
    }
    
    public static ExceptionMapper<InvalidArgumentException> withMessage(String message)
    {
        return ex -> new InvalidArgumentException(message);
    }
    
    public static void checkRequestNotNull(Object request) throws InvalidArgumentException
    {
        checkNotNull(request, "missing request");
    }
    
    public static void checkNotNull(Object reference, String message) throws InvalidArgumentException
    {
        checkThat(reference)
            .throwing(withMessage(message))
            .is(notNull());
    }
    
    public static AlchemyAssertion<AuthenticationToken> legalToken()
    {
        return t ->
        {
            checkThat(t)
                .usingMessage("token is null")
                .is(notNull());
            
            checkThat(t.tokenId)
                .usingMessage("token missing tokenId")
                .is(nonEmptyString());
            
//            checkThat(t.ownerId)
//                .usingMessage("token missing ownerId")
//                .is(nonEmptyString())
//                .usingMessage("token ownerId must be a UUID")
//                .is(validUUID());
        };
    }
    
    public static AlchemyAssertion<ApplicationToken> validApplicationTokenIn(@Required AuthenticationService.Iface authenticationService)
    {
        checkThat(authenticationService)
            .usingMessage("authentication service is null")
            .is(notNull());
        
        return token ->
        {
            checkThat(token)
                .usingMessage("token is null")
                .is(notNull());
            
            checkThat(token.tokenId)
{'fraction_non_alphanumeric': 0.08273239805952537, 'fraction_numerical': 0.002622263012980202, 'mean_word_length': 3.282987085906794, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3524445', 'n_tokens_mistral': 2019, 'n_tokens_neox': 1956, 'n_words': 465}