Added integration test for basic auth with toaster example. 96/11196/1
authorLiem Nguyen <liem_m_nguyen@hp.com>
Mon, 15 Sep 2014 17:12:58 +0000 (10:12 -0700)
committerLiem Nguyen <liem_m_nguyen@hp.com>
Mon, 15 Sep 2014 17:12:58 +0000 (10:12 -0700)
Change-Id: I95e9f4eef2ecdcebf13f4933573145a353f75a16
Signed-off-by: Liem Nguyen <liem_m_nguyen@hp.com>
aaa-it/pom.xml
aaa-it/src/test/java/org/opendaylight/aaa/AAATest.java

index 7c83b3f57387220625e8a5919a5d842587871602..08d0a5b2870be2d3ad25398ce625fe64d311ea9b 100644 (file)
             <classifier>features</classifier>
             <type>xml</type>
         </dependency>
+        <!-- MDSAL -->
+        <dependency>
+            <groupId>org.opendaylight.controller</groupId>
+            <artifactId>features-mdsal</artifactId>
+            <version>${mdsal.version}</version>
+            <classifier>features</classifier>
+            <type>xml</type>
+        </dependency>
         <!-- Dependencies for pax exam karaf container -->
         <dependency>
             <groupId>org.ops4j.pax.exam</groupId>
index 216d6cc8dad3b7e12b7eab9511a794f4316a7c7c..f2e693cdeef6d544f680f54c42b0d9a12a89b701 100644 (file)
@@ -11,10 +11,18 @@ package org.opendaylight.aaa;
 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;
 
@@ -35,6 +43,8 @@ import org.ops4j.pax.exam.Option;
 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.
  *
@@ -43,30 +53,64 @@ import org.ops4j.pax.exam.karaf.options.LogLevelOption.LogLevel;
  */
 @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
@@ -89,14 +133,19 @@ public class AAATest {
                 // 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() };
     }
 }