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 org.apigw.authserver.svc.AdministrationServices; import org.apigw.authserver.svc.repository.CertifiedClientPermissionRepository; import org.apigw.authserver.svc.repository.AuthorizationGrantRepository; import org.apigw.authserver.svc.repository.CertifiedClientRepository; import org.apigw.authserver.svc.repository.PermissionRepository; import org.apigw.authserver.types.domain.*; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.transaction.TransactionConfiguration; import java.util.Arrays; import java.util.EnumSet; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; /** * Tests the functionality in AdministrationServicesImpl * @author Christian Hilmersson */ @RunWith(SpringJUnit4ClassRunner.class) @TransactionConfiguration(defaultRollback = true) @ContextConfiguration(locations = "classpath:apigw-resources-test-config.xml") public class AdministrationServicesImplTest { @Autowired private AdministrationServices administrationServices; @Autowired private AuthorizationGrantRepository authorizationGrantRepository; @Autowired private CertifiedClientPermissionRepository ccPermissionRepository; @Autowired private PermissionRepository permissionRepository; @Autowired private CertifiedClientRepository certifiedClientRepository; private static final String CLIENT = "client"; private static final String READ_SCOPE = "SCOPE_READ_SCHEDULE"; private static final String WRITE_SCOPE = "SCOPE_WRITE_SCHEDULE"; private static final String CITIZEN = "200802020200"; private static final String LEGAL_GUARDIAN = "198611062384"; @After public void removeTestData() { authorizationGrantRepository.deleteAll(); ccPermissionRepository.deleteAll(); permissionRepository.deleteAll(); certifiedClientRepository.deleteAll(); } @Before public void createTestData() throws Exception { CertifiedClient client = new CertifiedClient(); client.setClientId(CLIENT); client.setName("name"); client.setDescription("app description"); client.setX509CertificateIssuerDN("issuerDN"); client.setX509CertificateSubjectDN("subjectDN"); certifiedClientRepository.save(client); Permission perm1 = new Permission(); perm1.setName(READ_SCOPE); perm1.setDescription("reading"); perm1.setAccessTokenValiditySeconds(30 * 24 * 3600); permissionRepository.save(perm1); Permission perm2 = new Permission(); perm2.setName(WRITE_SCOPE); perm2.setAccessTokenValiditySeconds(3600); permissionRepository.save(perm2); CertifiedClientPermission ccPerm1 = new CertifiedClientPermission(); ccPerm1.setCertifiedClient(client); ccPerm1.setPermission(perm1); ccPermissionRepository.save(ccPerm1); CertifiedClientPermission ccperm2 = new CertifiedClientPermission(); ccperm2.setCertifiedClient(client); ccperm2.setPermission(perm2); ccPermissionRepository.save(ccperm2); } @Test public void testRegisterApplication() { administrationServices.registerApplication("app-two", "Two-2-Tango", "The second application in the test suite.", 0L, 0L, "IssuerDN", "SubjectDN", "Two Testing", Arrays.asList(new String[] { READ_SCOPE }), "twotwo", EnumSet.allOf(CertifiedClientRole.class), "http://localhost:8080/two"); CertifiedClient appTwo = certifiedClientRepository.findByClientId("app-two"); Assert.assertNotNull(appTwo); assertEquals("Two-2-Tango", appTwo.getName()); assertEquals(new Integer(0), appTwo.getAccessTokenValiditySeconds()); assertEquals(new Integer(0), appTwo.getRefreshTokenValiditySeconds()); assertEquals("app-two", appTwo.getClientId()); assertEquals("twotwo", appTwo.getClientSecret()); assertEquals("http://localhost:8080/two", appTwo.getClientUrl()); assertEquals("The second application in the test suite.", appTwo.getDescription()); assertEquals("Two Testing", appTwo.getOrganization()); assertEquals("IssuerDN", appTwo.getX509CertificateIssuerDN()); assertEquals("SubjectDN", appTwo.getX509CertificateSubjectDN()); } @Test public void testRegisterRole() { administrationServices.registerPermission("TEST_TESTING_READ", "Read scope for testing"); Permission r = permissionRepository.findByName("TEST_TESTING_READ"); Assert.assertNotNull(r.getName()); assertEquals("TEST_TESTING_READ", r.getName()); assertEquals("Read scope for testing", r.getDescription()); } @Test public void testFindApplication() { CertifiedClient c = administrationServices.findCertifiedClientByClientId(CLIENT); Assert.assertNotNull(c); assertEquals("name", c.getName()); assertEquals("app description", c.getDescription()); } @Test public void testFindRole() { Permission r = administrationServices.findPermission(READ_SCOPE); Assert.assertNotNull(r); assertEquals(READ_SCOPE, r.getName()); assertEquals("reading", r.getDescription()); assertEquals(30 * 24 * 3600, r.getAccessTokenValiditySeconds()); } @Test public void testDeleteRole() { Assert.assertNotNull(permissionRepository.findByName(READ_SCOPE)); administrationServices.deletePermission(READ_SCOPE); assertNull(permissionRepository.findByName(READ_SCOPE)); } @Test public void testDeleteApplication() { Assert.assertNotNull(certifiedClientRepository.findByClientId(CLIENT)); administrationServices.deleteCertifiedClientByClientId(CLIENT); assertNull(certifiedClientRepository.findByClientId(CLIENT)); } }