43939560651fce5924dcfe278176da864f02a658
[aaa.git] / aaa-it / src / test / java / org / opendaylight / aaa / AAATest.java
1 /*
2  * Copyright (c) 2014 Hewlett-Packard Development Company, L.P. and others.
3  * All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9 package org.opendaylight.aaa;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.ops4j.pax.exam.CoreOptions.maven;
15 import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
16 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.configureConsole;
17 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.features;
18 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.karafDistributionConfiguration;
19 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder;
20 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.logLevel;
21
22 import java.io.File;
23
24 import javax.inject.Inject;
25
26 import org.apache.oltu.oauth2.client.OAuthClient;
27 import org.apache.oltu.oauth2.client.URLConnectionClient;
28 import org.apache.oltu.oauth2.client.request.OAuthClientRequest;
29 import org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse;
30 import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
31 import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
32 import org.apache.oltu.oauth2.common.message.types.GrantType;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.opendaylight.aaa.api.Claim;
36 import org.opendaylight.aaa.api.CredentialAuth;
37 import org.opendaylight.aaa.api.PasswordCredentials;
38 import org.ops4j.pax.exam.Configuration;
39 import org.ops4j.pax.exam.Option;
40 import org.ops4j.pax.exam.junit.PaxExam;
41 import org.ops4j.pax.exam.karaf.options.LogLevelOption.LogLevel;
42
43 /**
44  * Karaf integration tests for AAA.
45  *
46  * @author liemmn
47  *
48  */
49 @RunWith(PaxExam.class)
50 public class AAATest {
51     private static final String TOKEN_URL = "http://localhost:8181/oauth2/token";
52
53     @Inject
54     private CredentialAuth<PasswordCredentials> ca;
55
56     @Test
57     public void testAuthN() throws OAuthSystemException, OAuthProblemException {
58         // Test create token
59         OAuthClientRequest request = OAuthClientRequest
60                 .tokenLocation(TOKEN_URL).setGrantType(GrantType.PASSWORD)
61                 .setClientId("dlux").setClientSecret("secrete")
62                 .setUsername("admin").setPassword("admin").setScope("sdn")
63                 .buildQueryMessage();
64         OAuthClient client = new OAuthClient(new URLConnectionClient());
65         OAuthJSONAccessTokenResponse resp = client.accessToken(request);
66         assertNotNull(resp.getAccessToken());
67         assertEquals(Long.valueOf(3600), resp.getExpiresIn());
68
69         // Test credential auth
70         PasswordCredentials creds = new PasswordCredentialBuilder()
71                 .setUserName("admin").setPassword("admin").build();
72         Claim claim = ca.authenticate(creds, "sdn");
73         assertEquals("admin", claim.user());
74         assertFalse(claim.roles().isEmpty());
75     }
76
77     @Configuration
78     public Option[] config() {
79         return new Option[] {
80                 // Provision and launch a container based on a distribution of
81                 // Karaf (Apache ServiceMix).
82                 karafDistributionConfiguration()
83                         .frameworkUrl(
84                                 maven().groupId("org.opendaylight.controller")
85                                         .artifactId(
86                                                 "distribution.opendaylight-karaf")
87                                         .type("zip").versionAsInProject())
88                         .karafVersion("3.0.1").name("OpenDaylight")
89                         .unpackDirectory(new File("target/pax"))
90                         .useDeployFolder(false),
91                 // It is really nice if the container sticks around after the
92                 // test so you can check the contents
93                 // of the data directory when things go wrong.
94                 keepRuntimeFolder(),
95                 // Don't bother with local console output as it just ends up
96                 // cluttering the logs
97                 configureConsole().ignoreLocalConsole(),
98                 // Force the log level to INFO so we have more details during
99                 // the test. It defaults to WARN.
100                 logLevel(LogLevel.INFO),
101                 // Provision the feature exercised by this test
102                 features(
103                         "mvn:org.opendaylight.aaa/features-aaa/0.1.0-SNAPSHOT/xml/features",
104                         "odl-aaa-all"),
105                 mavenBundle().groupId("org.apache.oltu.oauth2")
106                         .artifactId("org.apache.oltu.oauth2.client")
107                         .versionAsInProject()
108         // debugConfiguration("5000", true),
109         };
110     }
111 }