Java tutorial
/** * Copyright 2013 Stockholm County Council * * This file is part of APIGW * * APIGW is free software; you can redistribute it and/or modify * it under the terms of version 2.1 of the GNU Lesser General Public * License as published by the Free Software Foundation. * * APIGW is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with APIGW; if not, write to the * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA * */ package org.apigw.authserver.svc.impl; import static org.junit.Assert.assertNotNull; import org.apigw.authserver.api.SimpleAuthentication; import org.junit.Test; import org.springframework.security.core.Authentication; import org.springframework.security.oauth2.common.exceptions.InvalidGrantException; import org.springframework.security.oauth2.provider.AuthorizationRequest; import org.springframework.security.oauth2.provider.DefaultAuthorizationRequest; import org.springframework.security.oauth2.provider.code.AuthorizationRequestHolder; /** * Implementation of authorization code services that stores the codes and authentication in memory * with an expiration time set * * @author Albert rwall */ public class ExpiringAuthorizationCodeServicesTest { @Test public void testStore() { ExpiringAuthorizationCodeServices codeService = new ExpiringAuthorizationCodeServices(100); String code = codeService.createAuthorizationCode(createAuth()); assertNotNull(codeService.consumeAuthorizationCode(code)); } @Test(expected = InvalidGrantException.class) public void testExpirationTime() { ExpiringAuthorizationCodeServices codeService = new ExpiringAuthorizationCodeServices(1); String code = codeService.createAuthorizationCode(createAuth()); try { Thread.sleep(1500L); } catch (InterruptedException e) { e.printStackTrace(); } codeService.consumeAuthorizationCode(code); } @Test public void testRemove() { ExpiringAuthorizationCodeServices codeService = new ExpiringAuthorizationCodeServices(100); String code = codeService.createAuthorizationCode(createAuth()); assertNotNull(codeService.consumeAuthorizationCode(code)); } @Test(expected = InvalidGrantException.class) public void testRemoveAgain() { ExpiringAuthorizationCodeServices codeService = new ExpiringAuthorizationCodeServices(100); String code = codeService.createAuthorizationCode(createAuth()); assertNotNull(codeService.consumeAuthorizationCode(code)); codeService.consumeAuthorizationCode(code); } protected AuthorizationRequestHolder createAuth() { AuthorizationRequest authReq = new DefaultAuthorizationRequest(null, null); Authentication auth = new SimpleAuthentication(null); return new AuthorizationRequestHolder(authReq, auth); } }