Search in sources :

Example 1 with ApiKey

use of org.ff4j.web.api.filter.ApiKey in project ff4j by ff4j.

the class ApiKeyBeanTest method ApiKeyinit.

@Test
public void ApiKeyinit() {
    ApiKey ak = new ApiKey();
    ak.setUserId("uid");
    Assert.assertEquals("uid", ak.getUserId());
    ak.setValue("val");
    Assert.assertEquals("val", ak.getValue());
    Date d = new Date();
    ak.setExpirationTime(d);
    Assert.assertEquals(d, ak.getExpirationTime());
}
Also used : ApiKey(org.ff4j.web.api.filter.ApiKey) Date(java.util.Date) Test(org.junit.Test)

Example 2 with ApiKey

use of org.ff4j.web.api.filter.ApiKey in project ff4j by ff4j.

the class ApiKeyValidatorFilterTest method testFilterApiKeyNotFound.

@Test(expected = WebApplicationException.class)
public void testFilterApiKeyNotFound() throws Exception {
    ContainerRequestContext mockRequest = mock(ContainerRequestContext.class);
    MultivaluedMap<String, String> mvm = new MultivaluedHashMap<>();
    mvm.putSingle(ApiKeyValidatorFilter.HEADER_APIKEY, "12");
    when(mockRequest.getHeaders()).thenReturn(mvm);
    // create valid KEY to test against
    Map<String, ApiKey> initMap = new HashMap<>();
    ApiKey ak1 = new ApiKey();
    ak1.setUserId("user1");
    ak1.setValue("13");
    ak1.setExpirationTime(new Date(System.currentTimeMillis() + 100000));
    initMap.put(ak1.getValue(), ak1);
    ApiKeyValidatorFilter.setValidApiKeysMap(initMap);
    ApiKeyValidatorFilter f1 = new ApiKeyValidatorFilter();
    f1.filter(mockRequest);
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ApiKey(org.ff4j.web.api.filter.ApiKey) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ApiKeyValidatorFilter(org.ff4j.web.api.filter.ApiKeyValidatorFilter) Date(java.util.Date) Test(org.junit.Test)

Example 3 with ApiKey

use of org.ff4j.web.api.filter.ApiKey in project ff4j by ff4j.

the class ApiKeyValidatorFilterTest method testFilterApiKeyExpired.

@Test(expected = WebApplicationException.class)
public void testFilterApiKeyExpired() throws Exception {
    ContainerRequestContext mockRequest = mock(ContainerRequestContext.class);
    MultivaluedMap<String, String> mvm = new MultivaluedHashMap<>();
    mvm.putSingle(ApiKeyValidatorFilter.HEADER_APIKEY, "12");
    when(mockRequest.getHeaders()).thenReturn(mvm);
    // create valid KEY to test against
    Map<String, ApiKey> initMap = new HashMap<>();
    ApiKey ak1 = new ApiKey();
    ak1.setUserId("user1");
    ak1.setValue("12");
    ak1.setExpirationTime(new Date(System.currentTimeMillis() - 100000));
    initMap.put(ak1.getValue(), ak1);
    ApiKeyValidatorFilter.setValidApiKeysMap(initMap);
    ApiKeyValidatorFilter f1 = new ApiKeyValidatorFilter();
    f1.filter(mockRequest);
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ApiKey(org.ff4j.web.api.filter.ApiKey) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ApiKeyValidatorFilter(org.ff4j.web.api.filter.ApiKeyValidatorFilter) Date(java.util.Date) Test(org.junit.Test)

Example 4 with ApiKey

use of org.ff4j.web.api.filter.ApiKey in project ff4j by ff4j.

the class ApiKeyValidatorFilterTest method testFilterOK.

@Test
public void testFilterOK() throws Exception {
    ContainerRequestContext mockRequest = mock(ContainerRequestContext.class);
    MultivaluedMap<String, String> mvm = new MultivaluedHashMap<>();
    mvm.putSingle(ApiKeyValidatorFilter.HEADER_APIKEY, "12");
    when(mockRequest.getHeaders()).thenReturn(mvm);
    // create valid KEY to test against
    Map<String, ApiKey> initMap = new HashMap<>();
    ApiKey ak1 = new ApiKey();
    ak1.setUserId("user1");
    ak1.setValue("12");
    ak1.setExpirationTime(new Date(System.currentTimeMillis() + 100000));
    initMap.put(ak1.getValue(), ak1);
    ApiKeyValidatorFilter.setValidApiKeysMap(initMap);
    ApiKeyValidatorFilter f1 = new ApiKeyValidatorFilter();
    f1.filter(mockRequest);
}
Also used : MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ApiKey(org.ff4j.web.api.filter.ApiKey) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ApiKeyValidatorFilter(org.ff4j.web.api.filter.ApiKeyValidatorFilter) Date(java.util.Date) Test(org.junit.Test)

Example 5 with ApiKey

use of org.ff4j.web.api.filter.ApiKey in project ff4j by ff4j.

the class ApiKeyValidatorFilterTest method testApiKeyValidaton.

@Test
public void testApiKeyValidaton() throws Exception {
    Map<String, ApiKey> initMap = new HashMap<>();
    ApiKey ak1 = new ApiKey();
    ak1.setUserId("user1");
    ak1.setValue("key1");
    ak1.setExpirationTime(new Date(System.currentTimeMillis() + 20000));
    initMap.put(ak1.getValue(), ak1);
    new ApiKeyValidatorFilter(initMap);
    Assert.assertTrue(ApiKeyValidatorFilter.getValidApiKeysMap().containsKey("key1"));
    ApiKeyValidatorFilter.setValidApiKeysMap(initMap);
}
Also used : ApiKey(org.ff4j.web.api.filter.ApiKey) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ApiKeyValidatorFilter(org.ff4j.web.api.filter.ApiKeyValidatorFilter) Date(java.util.Date) Test(org.junit.Test)

Aggregations

Date (java.util.Date)5 ApiKey (org.ff4j.web.api.filter.ApiKey)5 Test (org.junit.Test)5 HashMap (java.util.HashMap)4 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)4 ApiKeyValidatorFilter (org.ff4j.web.api.filter.ApiKeyValidatorFilter)4 ContainerRequestContext (javax.ws.rs.container.ContainerRequestContext)3