package net.ranides.assira.credentials;

import net.ranides.assira.collection.maps.GenericHashMap;
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.*;

/**
 * @author msieron
 */
public class BasicUserStoreTest {

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

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


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

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

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

}