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

package oap.storage;

import org.testng.annotations.Test;

import static oap.testng.Asserts.assertString;
import static org.assertj.core.api.Assertions.assertThat;

public class DefaultIdentifierTest {

    @Test
    public void idFromPath() {
        MemoryStorage<Bean> storage = new MemoryStorage<>( IdentifierBuilder.identityPath( "s" ).build() );
        storage.store( new Bean( "1", "aaaa" ) );
        storage.store( new Bean( "2", "bbbb" ) );
        assertThat( storage.get( "aaaa" ) )
            .isPresent()
            .hasValue( new Bean( "1", "aaaa" ) );
        assertThat( storage.get( "bbbb" ) )
            .isPresent()
            .hasValue( new Bean( "2", "bbbb" ) );
    }

    @Test
    public void idAndSizeGeneration() {
        Identifier<Bean> identifier = IdentifierBuilder.<Bean>identityPath( "id" )
            .suggestion( bean -> bean.s )
            .size( 7 )
            .build();
        MemoryStorage<Bean> storage = new MemoryStorage<>( identifier );
        Bean a = new Bean( null, "some text" );
        Bean b = new Bean( null, "another text" );

        storage.store( a );
        storage.store( b );

        assertString( a.id ).isEqualTo( "SMTXTXX" );
        assertString( b.id ).isEqualTo( "NTHRTXT" );

    }

    @Test
    public void conflictResolution() {
        Identifier<Bean> identifier = IdentifierBuilder.<Bean>identityPath( "id" )
            .suggestion( bean -> bean.s )
            .size( 7 )
            .build();
        MemoryStorage<Bean> storage = new MemoryStorage<>( identifier );
        Bean a = new Bean( null, "some text" );
        Bean b = new Bean( null, "some text" );
        Bean c = new Bean( null, "some text" );
        Bean d = new Bean( null, "some text" );
        Bean e = new Bean( null, "some text" );
        Bean f = new Bean( null, "some text" );
        Bean g = new Bean( null, "some text" );

        storage.store( a );
        storage.store( b );
        storage.store( c );
        storage.store( d );
        storage.store( e );
        storage.store( f );
        storage.store( g );

        assertString( a.id ).isEqualTo( "SMTXTXX" );
        assertString( b.id ).isEqualTo( "SMTXTX0" );
        assertString( c.id ).isEqualTo( "SMTXTX1" );
        assertString( d.id ).isEqualTo( "SMTXTX2" );
        assertString( e.id ).isEqualTo( "SMTXTX3" );
        assertString( f.id ).isEqualTo( "SMTXTX4" );
        assertString( g.id ).isEqualTo( "SMTXTX5" );
    }

}
@@ -2335,39 +2335,39 @@
 d ).isEqualTo( %22
-SMTXTXX
+smtxtxx
 %22 );%0A        ass
@@ -2400,15 +2400,15 @@
 o( %22
-NTHRTXT
+nthrtxt
 %22 );
@@ -3287,15 +3287,15 @@
 o( %22
-SMTXTXX
+smtxtxx
 %22 );
@@ -3336,22 +3336,22 @@
 ualTo( %22
-SMTXTX
+smtxtx
 0%22 );%0A  
@@ -3389,22 +3389,22 @@
 ualTo( %22
-SMTXTX
+smtxtx
 1%22 );%0A  
@@ -3442,22 +3442,22 @@
 ualTo( %22
-SMTXTX
+smtxtx
 2%22 );%0A  
@@ -3495,22 +3495,22 @@
 ualTo( %22
-SMTXTX
+smtxtx
 3%22 );%0A  
@@ -3548,22 +3548,22 @@
 ualTo( %22
-SMTXTX
+smtxtx
 4%22 );%0A  
@@ -3605,14 +3605,14 @@
 o( %22
-SMTXTX
+smtxtx
 5%22 )

test fix