<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.usergrid.persistence;


import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.usergrid.AbstractCoreIT;
import org.apache.usergrid.cassandra.Concurrent;
import org.apache.usergrid.persistence.entities.Application;
import org.apache.usergrid.utils.JsonUtils;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;


@Concurrent()
public class EntityDictionaryIT extends AbstractCoreIT {
    private static final Logger LOG = LoggerFactory.getLogger( EntityDictionaryIT.class );


    public EntityDictionaryIT() {
        super();
    }


    @Test
    public void testApplicationDictionaries() throws Exception {
        Application.OAuthProvider provider = new Application.OAuthProvider();
        provider.setClientId( "123456789012.apps.googleusercontent.com" );
        provider.setClientSecret( "<KEY>" );
        provider.setRedirectUris( "https://www.example.com/oauth2callback" );
        provider.setJavaScriptOrigins( "https://www.example.com" );
        provider.setAuthorizationEndpointUrl( "https://accounts.google.com/o/oauth2/auth" );
        provider.setAccessTokenEndpointUrl( "https://accounts.google.com/o/oauth2/token" );
        provider.setVersion( "2.0" );

        LOG.info( "EntityDictionaryIT.testApplicationDictionaries" );

        UUID applicationId = setup.createApplication( "testOrganization", "testApplicationDictionaries" );
        assertNotNull( applicationId );

        EntityManager em = setup.getEmf().getEntityManager( applicationId );
        assertNotNull( em );

        em.addToDictionary( em.getApplicationRef(), "oauthproviders", "google", provider );

        Object o = em.getDictionaryElementValue( em.getApplicationRef(), "oauthproviders", "google" );
        LOG.info( JsonUtils.mapToFormattedJsonString( o ) );
    }


    @Test
    public void testUserDictionaries() throws Exception {
        LOG.info( "EntityDictionaryIT.testUserDictionaries" );

        UUID applicationId = setup.createApplication( "testOrganization", "testUserDictionaries" );
        assertNotNull( applicationId );

        EntityManager em = setup.getEmf().getEntityManager( applicationId );
        assertNotNull( em );

        Map<String, Object> properties = new LinkedHashMap<String, Object>();
        properties.put( "username", "edanuff" );
        properties.put( "email", "<EMAIL>" );

        Entity user = em.create( "user", properties );
        assertNotNull( user );

        // test plaintext

        CredentialsInfo credentials = new CredentialsInfo();
        credentials.setSecret( "test" );
        credentials.setEncrypted( false );
        credentials.setRecoverable( true );
        credentials.setCryptoChain( new String[] { "plaintext" } );

        em.addToDictionary( user, "credentials", "plaintext", credentials );

        Object o = em.getDictionaryElementValue( user, "credentials", "plaintext" );
        LOG.info( JsonUtils.mapToFormattedJsonString( o ) );

        assertEquals( CredentialsInfo.class, o.getClass() );

        CredentialsInfo returned = ( CredentialsInfo ) o;

        assertEquals( credentials.getSecret(), returned.getSecret() );
        assertEquals( credentials.getEncrypted(), returned.getEncrypted() );
        assertEquals( credentials.getRecoverable(), returned.getRecoverable() );
        assertArrayEquals( credentials.getCryptoChain(), returned.getCryptoChain() );

        // test encrypted but recoverable

        credentials = new CredentialsInfo();
        credentials.setEncrypted( true );
        credentials.setRecoverable( false );
        credentials.setSecret( "<PASSWORD>" );
        credentials.setCryptoChain( new String[] { "sha-1" } );

        em.addToDictionary( user, "credentials", "encrypted", credentials );

        o = em.getDictionaryElementValue( user, "credentials", "encrypted" );
        LOG.info( JsonUtils.mapToFormattedJsonString( o ) );

        assertEquals( CredentialsInfo.class, o.getClass() );
        returned = ( CredentialsInfo ) o;


        assertEquals( credentials.getSecret(), returned.getSecret() );
        assertEquals( credentials.getEncrypted(), returned.getEncrypted() );
        assertEquals( credentials.getRecoverable(), returned.getRecoverable() );
        assertArrayEquals( credentials.getCryptoChain(), returned.getCryptoChain() );
    }

    @Test
    public void testRemoveFromDictionary() throws Exception {
        LOG.info( "EntityDictionaryIT.testRemoveFromDictionary" );

        Application.OAuthProvider provider = new Application.OAuthProvider();
        provider.setClientId( "123456789012.apps.googleusercontent.com" );
        provider.setClientSecret( "<KEY>" );
        provider.setRedirectUris( "https://www.example.com/oauth2callback" );
        provider.setJavaScriptOrigins( "https://www.example.com" );
        provider.setAuthorizationEndpointUrl( "https://accounts.google.com/o/oauth2/auth" );
        provider.setAccessTokenEndpointUrl( "https://accounts.google.com/o/oauth2/token" );
        provider.setVersion( "2.0" );

        LOG.info( "EntityDictionaryIT.testApplicationDictionaries" );

        UUID applicationId = setup.createApplication( "testOrganization", "testApplicationDictionaries" );
        assertNotNull( applicationId );

        EntityManager em = setup.getEmf().getEntityManager( applicationId );
        assertNotNull( em );

        em.addToDictionary( em.getApplicationRef(), "oauthproviders", "google", provider );

        Object o = em.getDictionaryElementValue( em.getApplicationRef(), "oauthproviders", "google" );

        assertNotNull( o );

        em.removeFromDictionary( em.getApplicationRef(),"oauthproviders","google" );

        o = em.getDictionaryElementValue( em.getApplicationRef(), "oauthproviders", "google" );
        assertNull( o );


    }
}<commit_msg>Added test for getDictionaries and getDictionaryNames<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.usergrid.persistence;


import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.usergrid.AbstractCoreIT;
import org.apache.usergrid.cassandra.Concurrent;
import org.apache.usergrid.persistence.entities.Application;
import org.apache.usergrid.utils.JsonUtils;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;


@Concurrent()
public class EntityDictionaryIT extends AbstractCoreIT {
    private static final Logger LOG = LoggerFactory.getLogger( EntityDictionaryIT.class );


    public EntityDictionaryIT() {
        super();
    }


    @Test
    public void testApplicationDictionaries() throws Exception {
        Application.OAuthProvider provider = new Application.OAuthProvider();
        provider.setClientId( "123456789012.apps.googleusercontent.<EMAIL>" );
        provider.setClientSecret( "<KEY>" );
        provider.setRedirectUris( "https://www.example.com/oauth2callback" );
        provider.setJavaScriptOrigins( "https://www.example.com" );
        provider.setAuthorizationEndpointUrl( "https://accounts.google.com/o/oauth2/auth" );
        provider.setAccessTokenEndpointUrl( "https://accounts.google.com/o/oauth2/token" );
        provider.setVersion( "2.0" );

        LOG.info( "EntityDictionaryIT.testApplicationDictionaries" );

        UUID applicationId = setup.createApplication( "testOrganization", "testApplicationDictionaries" );
        assertNotNull( applicationId );

        EntityManager em = setup.getEmf().getEntityManager( applicationId );
        assertNotNull( em );

        em.addToDictionary( em.getApplicationRef(), "oauthproviders", "google", provider );

        Object o = em.getDictionaryElementValue( em.getApplicationRef(), "oauthproviders", "google" );
        LOG.info( JsonUtils.mapToFormattedJsonString( o ) );
    }


    @Test
    public void testUserDictionaries() throws Exception {
        LOG.info( "EntityDictionaryIT.testUserDictionaries" );

        UUID applicationId = setup.createApplication( "testOrganization", "testUserDictionaries" );
        assertNotNull( applicationId );

        EntityManager em = setup.getEmf().getEntityManager( applicationId );
        assertNotNull( em );

        Map<String, Object> properties = new LinkedHashMap<String, Object>();
        properties.put( "username", "edanuff" );
        properties.put( "email", "<EMAIL>" );

        Entity user = em.create( "user", properties );
        assertNotNull( user );

        // test plaintext

        CredentialsInfo credentials = new CredentialsInfo();
        credentials.setSecret( "test" );
        credentials.setEncrypted( false );
        credentials.setRecoverable( true );
        credentials.setCryptoChain( new String[] { "plaintext" } );

        em.addToDictionary( user, "credentials", "plaintext", credentials );

        Object o = em.getDictionaryElementValue( user, "credentials", "plaintext" );
        LOG.info( JsonUtils.mapToFormattedJsonString( o ) );

        assertEquals( CredentialsInfo.class, o.getClass() );

        CredentialsInfo returned = ( CredentialsInfo ) o;

        assertEquals( credentials.getSecret(), returned.getSecret() );
        assertEquals( credentials.getEncrypted(), returned.getEncrypted() );
        assertEquals( credentials.getRecoverable(), returned.getRecoverable() );
        assertArrayEquals( credentials.getCryptoChain(), returned.getCryptoChain() );

        // test encrypted but recoverable

        credentials = new CredentialsInfo();
        credentials.setEncrypted( true );
        credentials.setRecoverable( false );
        credentials.setSecret( "salt" );
        credentials.setCryptoChain( new String[] { "sha-1" } );

        em.addToDictionary( user, "credentials", "encrypted", credentials );

        o = em.getDictionaryElementValue( user, "credentials", "encrypted" );
        LOG.info( JsonUtils.mapToFormattedJsonString( o ) );

        assertEquals( CredentialsInfo.class, o.getClass() );
        returned = ( CredentialsInfo ) o;


        assertEquals( credentials.getSecret(), returned.getSecret() );
        assertEquals( credentials.getEncrypted(), returned.getEncrypted() );
        assertEquals( credentials.getRecoverable(), returned.getRecoverable() );
        assertArrayEquals( credentials.getCryptoChain(), returned.getCryptoChain() );
    }

    @Test
    public void testRemoveFromDictionary() throws Exception {
        LOG.info( "EntityDictionaryIT.testRemoveFromDictionary" );

        Application.OAuthProvider provider = new Application.OAuthProvider();
        provider.setClientId( "123456789012.apps.googleusercontent.com" );
        provider.setClientSecret( "<KEY>" );
        provider.setRedirectUris( "https://www.example.com/oauth2callback" );
        provider.setJavaScriptOrigins( "https://www.example.com" );
        provider.setAuthorizationEndpointUrl( "https://accounts.google.com/o/oauth2/auth" );
        provider.setAccessTokenEndpointUrl( "https://accounts.google.com/o/oauth2/token" );
        provider.setVersion( "2.0" );

        LOG.info( "EntityDictionaryIT.testApplicationDictionaries" );

        UUID applicationId = setup.createApplication( "testOrganization", "testApplicationDictionaries" );
        assertNotNull( applicationId );

        EntityManager em = setup.getEmf().getEntityManager( applicationId );
        assertNotNull( em );

        em.addToDictionary( em.getApplicationRef(), "oauthproviders", "google", provider );

        Object o = em.getDictionaryElementValue( em.getApplicationRef(), "oauthproviders", "google" );

        assertNotNull( o );

        em.removeFromDictionary( em.getApplicationRef(),"oauthproviders","google" );

        o = em.getDictionaryElementValue( em.getApplicationRef(), "oauthproviders", "google" );
        assertNull( o );


    }

    @Test
    public void testGetDictionaries() throws Exception {
        LOG.info( "EntityDictionaryIT.testGetDictionaries" );


        Application.OAuthProvider provider = new Application.OAuthProvider();
        provider.setClientId( "123456789012.apps.googleusercontent.com" );
        provider.setClientSecret( "<KEY>" );
        provider.setRedirectUris( "https://www.example.com/oauth2callback" );
        provider.setJavaScriptOrigins( "https://www.example.com" );
        provider.setAuthorizationEndpointUrl( "https://accounts.google.com/o/oauth2/auth" );
        provider.setAccessTokenEndpointUrl( "https://accounts.google.com/o/oauth2/token" );
        provider.setVersion( "2.0" );

        UUID applicationId = setup.createApplication( "testOrganization", "testApplicationDictionaries" );
        assertNotNull( applicationId );

        EntityManager em = setup.getEmf().getEntityManager( applicationId );
        assertNotNull( em );

        em.addToDictionary( em.getApplicationRef(), "oauthproviders", "google", provider );

        Object o = em.getDictionaryElementValue( em.getApplicationRef(), "oauthproviders", "google" );

        assertNotNull( o );
        Set<String> set = em.getDictionaryNames( em.getApplicationRef() );

        assertTrue( set.contains( "oauthproviders" ) );

    }
}
{'fraction_non_alphanumeric': 0.11221797070853674, 'fraction_numerical': 0.006531204644412192, 'mean_word_length': 3.6244661378889567, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 20, 'lorem ipsum': 0, 'www.': 12, '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': '1376881', 'n_tokens_mistral': 4089, 'n_tokens_neox': 3951, 'n_words': 901}