import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
-import static org.ops4j.pax.exam.CoreOptions.*;
-import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.*;
+import static org.ops4j.pax.exam.CoreOptions.maven;
+import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
+import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.configureConsole;
+import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.features;
+import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.karafDistributionConfiguration;
+import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder;
+import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.logLevel;
import java.io.File;
+import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.net.URL;
import javax.inject.Inject;
import org.ops4j.pax.exam.junit.PaxExam;
import org.ops4j.pax.exam.karaf.options.LogLevelOption.LogLevel;
+import com.sun.jersey.core.util.Base64;
+
/**
* Karaf integration tests for AAA.
*
*/
@RunWith(PaxExam.class)
public class AAATest {
+ private static final String BASIC_AUTH = "admin:admin";
private static final String TOKEN_URL = "http://localhost:8181/oauth2/token";
+ private static final String TOASTER_JSON = "{'toaster:toaster':{'toaster:toasterManufacturer':'GeneralElectric','toaster:toasterModelNumber':'123','toaster:toasterStatus':'up'}}";
@Inject
private CredentialAuth<PasswordCredentials> ca;
@Test
- public void testAuthN() throws OAuthSystemException, OAuthProblemException {
- // Test create token
- OAuthClientRequest request = OAuthClientRequest
- .tokenLocation(TOKEN_URL).setGrantType(GrantType.PASSWORD)
- .setClientId("dlux").setClientSecret("secrete")
- .setUsername("admin").setPassword("admin").setScope("sdn")
- .buildQueryMessage();
- OAuthClient client = new OAuthClient(new URLConnectionClient());
- OAuthJSONAccessTokenResponse resp = client.accessToken(request);
- assertNotNull(resp.getAccessToken());
- assertEquals(Long.valueOf(3600), resp.getExpiresIn());
+ public void testAuthN() throws OAuthSystemException, OAuthProblemException,
+ IOException {
+ OAuthClient oauthClient = null;
+ HttpURLConnection httpClient = null;
+ try {
+ // Test create token
+ OAuthClientRequest oauthRequest = OAuthClientRequest
+ .tokenLocation(TOKEN_URL).setGrantType(GrantType.PASSWORD)
+ .setClientId("dlux").setClientSecret("secrete")
+ .setUsername("admin").setPassword("admin").setScope("sdn")
+ .buildQueryMessage();
+ oauthClient = new OAuthClient(new URLConnectionClient());
+ OAuthJSONAccessTokenResponse resp = oauthClient
+ .accessToken(oauthRequest);
+ String token = resp.getAccessToken();
+ assertNotNull(token);
+ assertEquals(Long.valueOf(3600), resp.getExpiresIn());
+
+ // Test credential auth
+ PasswordCredentials creds = new PasswordCredentialBuilder()
+ .setUserName("admin").setPassword("admin").build();
+ Claim claim = ca.authenticate(creds, "sdn");
+ assertEquals("admin", claim.user());
+ assertFalse(claim.roles().isEmpty());
- // Test credential auth
- PasswordCredentials creds = new PasswordCredentialBuilder()
- .setUserName("admin").setPassword("admin").build();
- Claim claim = ca.authenticate(creds, "sdn");
- assertEquals("admin", claim.user());
- assertFalse(claim.roles().isEmpty());
+ // Test basic auth
+ // Create toaster with token
+ httpClient = (HttpURLConnection) new URL(
+ "http://localhost:8181/restconf/config").openConnection();
+ httpClient.setDoOutput(true);
+ httpClient.setRequestProperty("Authorization", "Bearer " + token);
+ httpClient.setRequestProperty("Content-Type", "application/json");
+ httpClient.getOutputStream().write(TOASTER_JSON.getBytes());
+ httpClient.connect(); // do it!
+ assertEquals(204, httpClient.getResponseCode());
+ // Now retrieve toaster with basic auth
+ httpClient.disconnect();
+ httpClient = (HttpURLConnection) new URL(
+ "http://localhost:8181/restconf/config/toaster:toaster")
+ .openConnection();
+ httpClient.setRequestProperty("Authorization", "Basic "
+ + new String(Base64.encode(BASIC_AUTH.getBytes("utf-8"))));
+ httpClient.connect();
+ assertEquals(200, httpClient.getResponseCode());
+ } finally {
+ if (oauthClient != null)
+ oauthClient.shutdown();
+ if (httpClient != null)
+ httpClient.disconnect();
+ }
}
@Configuration
// Don't bother with local console output as it just ends up
// cluttering the logs
configureConsole().ignoreLocalConsole(),
- // Force the log level to INFO so we have more details during
- // the test. It defaults to WARN.
- logLevel(LogLevel.INFO),
+ logLevel(LogLevel.WARN),
features(
- "mvn:org.opendaylight.aaa/features-aaa/0.1.0-SNAPSHOT/xml/features",
+ maven().groupId("org.opendaylight.aaa")
+ .artifactId("features-aaa").type("xml")
+ .classifier("features").versionAsInProject(),
"odl-aaa-all"),
+ features(
+ maven().groupId("org.opendaylight.controller")
+ .artifactId("features-mdsal").type("xml")
+ .classifier("features").versionAsInProject(),
+ "odl-toaster", "odl-restconf"),
mavenBundle().groupId("org.apache.oltu.oauth2")
.artifactId("org.apache.oltu.oauth2.client")
- .versionAsInProject(), };
+ .versionAsInProject() };
}
}