package net.ranides.assira.credentials;

import net.ranides.assira.collection.prototype.GenericMapBuilder;
import net.ranides.assira.text.Wildcard;
import org.junit.Test;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;

import static org.junit.Assert.assertEquals;

public class UserStoreIndexTest {

    @Test
    public void simple() throws URISyntaxException {
        UserStoreIndex store = new UserStoreIndex();


        store.add(GenericMapBuilder.<String>hash()
            .put(UserProperty.ACCOUNT, "home")
            .put(UserProperty.LOGIN, "ranides")
            .put(UserProperty.PASSWORD, "password")
            .put(UserProperty.HOST, "host.net")
            .map()
        );
        store.add(GenericMapBuilder.<String>hash()
            .put(UserProperty.ACCOUNT, "home")
            .put(UserProperty.LOGIN, "john")
            .put(UserProperty.PASSWORD, "444")
            .put(UserProperty.HOST, "host.org")
            .map()
        );
        store.add(GenericMapBuilder.<String>hash()
            .put(UserProperty.ACCOUNT, "hidden")
            .put(UserProperty.LOGIN, "anne")
            .put(UserProperty.PASSWORD, "555")
            .put(UserProperty.URI, u -> Wildcard.compile("*.htm").test(u.toString()))
            .map()
        );
        store.add(GenericMapBuilder.<String>hash()
            .put(UserProperty.ACCOUNT, "hidden")
            .put(UserProperty.LOGIN, "annette")
            .put(UserProperty.PASSWORD, "666")
            .put(UserProperty.URI, u -> Wildcard.compile("*.asp").test(u.toString()))
            .map()
        );
        store.add(GenericMapBuilder.<String>hash()
            .put(UserProperty.ACCOUNT, "custom")
            .put(UserProperty.LOGIN, "joanne")
            .put(UserProperty.PASSWORD, "666")
            .put(UserProperty.URI, u -> Wildcard.compile("http://*").test(u.toString()))
            .map()
        );


        assertEquals(Arrays.asList("ranides"), store.find(new URI("http://home@host.net/info.htm")).map(UserProperty.LOGIN).sort().list() );
        assertEquals(Arrays.asList("john"), store.find(new URI("http://home@host.org/info.asp")).map(UserProperty.LOGIN).sort().list() );

        assertEquals(Arrays.asList("annette"), store.find(new URI("http://hidden@host/info.asp")).map(UserProperty.LOGIN).sort().list() );
        assertEquals(Arrays.asList("annette"), store.find(new URI("ftp://host/info.asp")).map(UserProperty.LOGIN).sort().list() );
        assertEquals(Arrays.asList("annette", "joanne"), store.find(new URI("http://host/info.asp")).map(UserProperty.LOGIN).sort().list() );

        assertEquals(Arrays.asList("anne"), store.find(new URI("http://hidden@domain/info.htm")).map(UserProperty.LOGIN).sort().list() );
        assertEquals(Arrays.asList("anne", "joanne"), store.find(new URI("http://host/info.htm")).map(UserProperty.LOGIN).sort().list() );
        assertEquals(Arrays.asList("anne", "joanne"), store.find(new URI("http://domain/info.htm")).map(UserProperty.LOGIN).sort().list() );
        assertEquals(Arrays.asList("joanne"), store.find(new URI("http://custom@domain/info.htm")).map(UserProperty.LOGIN).sort().list() );
    }

}