org.apigw.authserver.svc.impl.ResourceOwnerServicesImplTest.java Source code

Java tutorial

Introduction

Here is the source code for org.apigw.authserver.svc.impl.ResourceOwnerServicesImplTest.java

Source

/**
 *   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.ResourceOwnerServices;
import org.apigw.authserver.svc.TokenServices;
import org.apigw.authserver.svc.repository.AuthorizationGrantRepository;
import org.junit.After;
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.Collection;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

@RunWith(SpringJUnit4ClassRunner.class)
@TransactionConfiguration(defaultRollback = true)
@ContextConfiguration(locations = "classpath:apigw-resources-test-config.xml")
public class ResourceOwnerServicesImplTest {

    @Autowired
    private TokenServices tokenServices;
    @Autowired
    private ResourceOwnerServices resourceOwnerServices;
    @Autowired
    private AuthorizationGrantRepository authorizationGrantRepository;

    private static final String CLIENT = "client";
    private static final String READ_SCOPE = "SCOPE_READ_SCHEDULE";

    @After
    public void removeTestData() {
        authorizationGrantRepository.deleteAll();
    }

    @Test
    public void findChildrenOfLegalGuardian() {
        String legalGuardianWithOneChild = "198112040384";
        String childOfLegalGuardianWithOneChild = "200506237528";
        tokenServices.createAccessToken(legalGuardianWithOneChild, childOfLegalGuardianWithOneChild, CLIENT,
                Arrays.asList(new String[] { READ_SCOPE }));

        Collection<String> children = resourceOwnerServices.findChildrenOfLegalGuardian(legalGuardianWithOneChild);

        assertEquals(1, children.size());
        assertTrue(children.contains(childOfLegalGuardianWithOneChild));

        String legalGuardianWithThreeChildren = "196708101891";
        String childOne = "199305017809";
        String childTwo = "199609096319";
        String childThree = "199903298710";
        tokenServices.createAccessToken(legalGuardianWithThreeChildren, childOne, CLIENT,
                Arrays.asList(new String[] { READ_SCOPE }));
        tokenServices.createAccessToken(legalGuardianWithThreeChildren, childTwo, CLIENT,
                Arrays.asList(new String[] { READ_SCOPE }));
        tokenServices.createAccessToken(legalGuardianWithThreeChildren, childThree, CLIENT,
                Arrays.asList(new String[] { READ_SCOPE }));

        children = resourceOwnerServices.findChildrenOfLegalGuardian(legalGuardianWithThreeChildren);
        assertEquals(3, children.size());
        assertTrue(children.contains(childOne));
        assertTrue(children.contains(childTwo));
        assertTrue(children.contains(childThree));
    }

}