package com.qmth.boot.test.core.security;

import com.qmth.boot.core.security.service.AuthorizationService;
import com.qmth.boot.core.security.service.impl.AuthorizationServiceContainer;
import com.qmth.boot.test.core.security.model.TestAuthorizationService;
import com.qmth.boot.tools.signature.SignatureType;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

public class AuthContainerTest {

    private static AuthorizationServiceContainer container;

    private static AuthorizationService s1, s2;

    @BeforeClass
    public static void init() {
        s1 = new TestAuthorizationService();
        s2 = new TestAuthorizationService();
        container = new AuthorizationServiceContainer();
    }

    @Test
    public void testPrefix() {
        container.setPrefix("/a", s1);
        container.setPrefix("/ab", s2);
        container.setPrefix("/abc", s1);
        container.setPrefix("/a/b", s1);
        container.setDefault(s2);

        Assert.assertEquals(container.find("/a/b/c", SignatureType.TOKEN), s1);
        Assert.assertEquals(container.find("/ab/c", SignatureType.TOKEN), s2);
        Assert.assertEquals(container.find("/abcd", SignatureType.TOKEN), s1);
        Assert.assertEquals(container.find("/ac", SignatureType.TOKEN), s1);
        Assert.assertEquals(container.find("/b", SignatureType.TOKEN), s2);
    }

    @Test
    public void testType() {
        container.setPrefix("/a/b", SignatureType.TOKEN, s1);
        container.setPrefix("/ab", SignatureType.SECRET, s2);

        Assert.assertNull(container.find("/a/b/c", SignatureType.SECRET));
        Assert.assertNull(container.find("/ab/c", SignatureType.TOKEN));
    }

}
