Commit ebdf965e authored by Alinga Yeung's avatar Alinga Yeung
Browse files

Story 1657 rework. Added test cases for a subject with no principal and a...

Story 1657 rework. Added test cases for a subject with no principal and a subject with more than one principal.
parent 4661da62
Loading
Loading
Loading
Loading
+49 −1
Original line number Diff line number Diff line
@@ -69,9 +69,20 @@

package ca.nrc.cadc.ac.client;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import javax.security.auth.Subject;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import ca.nrc.cadc.ac.client.UserClient;

import ca.nrc.cadc.ac.AC;
import ca.nrc.cadc.auth.HttpPrincipal;
import ca.nrc.cadc.auth.NumericPrincipal;
import ca.nrc.cadc.reg.client.RegistryClient;
import ca.nrc.cadc.util.Log4jInit;

import org.junit.Assert;
@@ -123,4 +134,41 @@ public class UserClientTest
        	Assert.fail("Unexpected exception: " + t.getMessage());
        }
    }
    
    @Test
    public void testSubjectWithNoPrincipal() throws URISyntaxException, MalformedURLException
    {
        // test subject augmentation given a subject with no principal
    	Subject subject = new Subject();
    	this.createUserClient().augmentSubject(subject);
    	Assert.assertEquals("Should have no principal.", 0, subject.getPrincipals().size());
    }
    
    @Test
    public void testSubjectWithMultiplePrincipal() throws URISyntaxException, MalformedURLException
    {
        try
        {
            // test subject augmentation given a subject with more than one principal
            Subject subject = new Subject();
            subject.getPrincipals().add(new NumericPrincipal(4));
            subject.getPrincipals().add(new HttpPrincipal("cadcauthtest1"));
            this.createUserClient().augmentSubject(subject);
            Assert.fail("Expecting an IllegalArgumentException.");
        }
        catch(IllegalArgumentException e)
        {
            String expected = "Subject has more than one principal.";
            Assert.assertEquals(expected, e.getMessage());
        }
    }
    
    protected UserClient createUserClient() throws URISyntaxException, MalformedURLException
    {
    	RegistryClient regClient = new RegistryClient();
    	URI serviceURI = new URI(AC.GMS_SERVICE_URI);
    	URL baseURL = regClient.getServiceURL(serviceURI, "https");
    	return new UserClient(baseURL.toString());

    }
}