Commit 0c2670b1 authored by Spiros Koulouzis's avatar Spiros Koulouzis

Added test for key

parent 940f4ca4
...@@ -8,7 +8,11 @@ ...@@ -8,7 +8,11 @@
</parent> </parent>
<artifactId>drip-tests</artifactId> <artifactId>drip-tests</artifactId>
<packaging>jar</packaging> <packaging>jar</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!--<maven.test.skip>true</maven.test.skip>-->
</properties>
<dependencies> <dependencies>
...@@ -38,6 +42,28 @@ ...@@ -38,6 +42,28 @@
<scope>test</scope> <scope>test</scope>
<type>jar</type> <type>jar</type>
</dependency> </dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
<scope>test</scope>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.glassfish.grizzly</groupId>
<artifactId>grizzly-framework</artifactId>
<version>2.3.28</version>
<scope>test</scope>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.25.1</version>
<scope>test</scope>
<type>jar</type>
</dependency>
<dependency> <dependency>
<groupId>javax</groupId> <groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId> <artifactId>javaee-web-api</artifactId>
...@@ -50,22 +76,47 @@ ...@@ -50,22 +76,47 @@
<groupId>nl.uva.sne.drip</groupId> <groupId>nl.uva.sne.drip</groupId>
<artifactId>drip-api-json-client</artifactId> <artifactId>drip-api-json-client</artifactId>
<version>1.0</version> <version>1.0</version>
<scope>system</scope> <scope>test</scope>
<systemPath>${basedir}/../docs/apidocs/drip-api-json-client.jar</systemPath> </dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.6</version>
<scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<executions>
<execution>
<id>install-external</id>
<phase>clean</phase>
<configuration>
<file>${basedir}/../docs/apidocs/drip-api-json-client.jar</file>
<repositoryLayout>default</repositoryLayout>
<groupId>nl.uva.sne.drip</groupId>
<artifactId>drip-api-json-client</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
<goals>
<goal>install-file</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins> </plugins>
</build> </build>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project> </project>
\ No newline at end of file
access.key.id=lferpoiewp access.key.id=lferpoiewp
secret.key=erfiru secret.key=erfiru
cloud.provider.name=ec2 cloud.provider.name=ec2
cloud.private.key.paths=/home/alogo/Virginia.pem,/home/alogo/California.pem cloud.private.key.paths=/home/alogo/Downloads/DRIP/Virginia.pem,/home/alogo/Downloads/DRIP/California.pem
\ No newline at end of file drip.host=http://localhost:8080/drip-api
\ No newline at end of file
test.property.files=ec2_test.properties
\ No newline at end of file
/*
* Copyright 2017 S. Koulouzis, Wang Junchao, Huan Zhou, Yang Hu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.uva.sne.drip.test.clients;
import com.fasterxml.jackson.core.JsonProcessingException;
import java.io.IOException;
import java.net.URL;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;
import nl.uva.sne.drip.data.v1.external.KeyPair;
/**
*
* @author S. Koulouzis
*/
public class KeyPairClient extends RestClient {
private static final String PATH = "/user/v1.0/keys";
public KeyPairClient(URL host) {
super(host);
}
public Response postKeyPair(KeyPair pair) throws JsonProcessingException {
String jsonInString = getMapper().writeValueAsString(pair);
Entity<String> entity = Entity.json(jsonInString);
// Entity<KeyPair> entity = Entity.entity(pair, "application/json");
return getWebTarget().
path(PATH).
request().
header("Authorization", "Basic dXNlcjoxMjM=").post(entity);
}
public String getPostKeyPairResponse(Response response) {
return response.readEntity(String.class);
}
public Response get(String keyID) {
return getWebTarget().
path(PATH).
path(keyID).
request().
header("Authorization", "Basic dXNlcjoxMjM=").get();
}
public KeyPair getGetKeyPairResponse(Response response) throws IOException {
String json = response.readEntity(String.class);
return getMapper().readValue(json, KeyPair.class);
}
public Response delete(String keyID) {
return getWebTarget().
path(PATH).
path(keyID).
request().
header("Authorization", "Basic dXNlcjoxMjM=").delete();
}
}
/*
* Copyright 2017 S. Koulouzis, Wang Junchao, Huan Zhou, Yang Hu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.uva.sne.drip.test.clients;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.net.URL;
import javax.net.ssl.SSLContext;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import org.glassfish.grizzly.ssl.SSLContextConfigurator;
/**
*
* @author S. Koulouzis
*/
public class RestClient {
private final Client client;
private final WebTarget webTarget;
private final ObjectMapper mapper;
public RestClient(URL host) {
this.client = createClient();
this.webTarget = client.target(host.toString());
this.mapper = new ObjectMapper();
}
private Client createClient() {
ClientBuilder builder = ClientBuilder.newBuilder();
SSLContextConfigurator sslContextConfigurator = new SSLContextConfigurator();
SSLContext sc = sslContextConfigurator.createSSLContext(true);
builder = builder.sslContext(sc);
builder.hostnameVerifier(new javax.net.ssl.HostnameVerifier() {
public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) {
return true;
}
});
return builder.build();
}
/**
* @return the webTarget
*/
public WebTarget getWebTarget() {
return webTarget;
}
/**
* @return the mapper
*/
public ObjectMapper getMapper() {
return mapper;
}
}
...@@ -19,8 +19,10 @@ import java.io.File; ...@@ -19,8 +19,10 @@ import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.Reader; import java.io.Reader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.logging.Level; import java.util.logging.Level;
...@@ -30,6 +32,10 @@ import org.junit.After; ...@@ -30,6 +32,10 @@ import org.junit.After;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.apache.http.*;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
/** /**
* *
...@@ -37,22 +43,25 @@ import org.junit.BeforeClass; ...@@ -37,22 +43,25 @@ import org.junit.BeforeClass;
*/ */
public class DRIPTest { public class DRIPTest {
private static List<String> PROPERTIES_FILE_PATHS; private static List<String> PROPERTIES_FILE_PATHS = new ArrayList<>();
public static List<Properties> propertiesList = new ArrayList<>(); public static List<Properties> propertiesList = new ArrayList<>();
public static final String ACCESS_KEY_ID_PROPPERTY_NAME = "access.key.id"; public static final String ACCESS_KEY_ID_PROPPERTY_NAME = "access.key.id";
static String CLOUD_PROPVIDER_PROPPERTY_NAME = "cloud.provider.name"; public static final String CLOUD_PROPVIDER_PROPPERTY_NAME = "cloud.provider.name";
static String SECRET_KEY_PROPERTY_NAME = "secret.key"; public static final String SECRET_KEY_PROPERTY_NAME = "secret.key";
static String CLOUD_PRIVATE_KEY_PATHS_PROPERTY_NAME = "cloud.private.key.paths"; public static final String CLOUD_PRIVATE_KEY_PATHS_PROPERTY_NAME = "cloud.private.key.paths";
public static final String DRIP_HOST_PROPERTY_NAME = "drip.host";
public DRIPTest() { public static final String DRIP_TEST_PROPERTY_FILES_PROPERTY_NAME = "test.property.files";
PROPERTIES_FILE_PATHS = new ArrayList<>();
}
@BeforeClass @BeforeClass
public static void setUpClass() { public static void setUpClass() {
try (InputStream is = ClassLoader.getSystemResourceAsStream("test.properties")) {
Properties mainPropertyFile = new Properties();
mainPropertyFile.load(is);
PROPERTIES_FILE_PATHS = Arrays.asList(mainPropertyFile.getProperty(DRIP_TEST_PROPERTY_FILES_PROPERTY_NAME).split(","));
for (String propFile : PROPERTIES_FILE_PATHS) { for (String propFile : PROPERTIES_FILE_PATHS) {
try (Reader inStream = new FileReader(new File(propFile))) { try (InputStream inStream = ClassLoader.getSystemResourceAsStream(propFile)) {
Properties prop = new Properties(); Properties prop = new Properties();
prop.load(inStream); prop.load(inStream);
propertiesList.add(prop); propertiesList.add(prop);
...@@ -62,6 +71,12 @@ public class DRIPTest { ...@@ -62,6 +71,12 @@ public class DRIPTest {
Logger.getLogger(TestCloudCredentialsController.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(TestCloudCredentialsController.class.getName()).log(Level.SEVERE, null, ex);
} }
} }
} catch (IOException ex) {
Logger.getLogger(DRIPTest.class.getName()).log(Level.SEVERE, null, ex);
}
} }
......
...@@ -16,10 +16,7 @@ ...@@ -16,10 +16,7 @@
package nl.uva.sne.drip.test.manager; package nl.uva.sne.drip.test.manager;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
...@@ -27,15 +24,6 @@ import java.util.Map; ...@@ -27,15 +24,6 @@ import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.junit.After;
import org.junit.AfterClass;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.BeforeClass;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.core.Response;
import nl.uva.sne.drip.data.v1.external.CloudCredentials; import nl.uva.sne.drip.data.v1.external.CloudCredentials;
import nl.uva.sne.drip.data.v1.external.Key; import nl.uva.sne.drip.data.v1.external.Key;
import nl.uva.sne.drip.data.v1.external.KeyPair; import nl.uva.sne.drip.data.v1.external.KeyPair;
...@@ -43,9 +31,6 @@ import nl.uva.sne.drip.data.v1.external.KeyType; ...@@ -43,9 +31,6 @@ import nl.uva.sne.drip.data.v1.external.KeyType;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.FilenameUtils;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertEquals;
/** /**
* *
...@@ -53,18 +38,10 @@ import static org.junit.Assert.assertEquals; ...@@ -53,18 +38,10 @@ import static org.junit.Assert.assertEquals;
*/ */
public class TestCloudCredentialsController extends DRIPTest { public class TestCloudCredentialsController extends DRIPTest {
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Test @Test
public void testPOST_GETCloudCredentials() { public void testPOST_GETCloudCredentials() {
for (Properties p : DRIPTest.propertiesList) { for (Properties p : DRIPTest.propertiesList) {
String[] paths = DRIPTest.CLOUD_PRIVATE_KEY_PATHS_PROPERTY_NAME.split(","); String[] paths = p.getProperty(DRIPTest.CLOUD_PRIVATE_KEY_PATHS_PROPERTY_NAME).split(",");
for (String cloudPrivateKeyPath : paths) { for (String cloudPrivateKeyPath : paths) {
try { try {
KeyPair keyPair = new KeyPair(); KeyPair keyPair = new KeyPair();
...@@ -76,6 +53,7 @@ public class TestCloudCredentialsController extends DRIPTest { ...@@ -76,6 +53,7 @@ public class TestCloudCredentialsController extends DRIPTest {
map.put(cloudPrivateKeyPath, cloudPrivateKeyPath); map.put(cloudPrivateKeyPath, cloudPrivateKeyPath);
privateKey.setAttributes(map); privateKey.setAttributes(map);
keyPair.setPrivateKey(privateKey); keyPair.setPrivateKey(privateKey);
} catch (IOException ex) { } catch (IOException ex) {
Logger.getLogger(TestCloudCredentialsController.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(TestCloudCredentialsController.class.getName()).log(Level.SEVERE, null, ex);
} }
...@@ -91,14 +69,4 @@ public class TestCloudCredentialsController extends DRIPTest { ...@@ -91,14 +69,4 @@ public class TestCloudCredentialsController extends DRIPTest {
} }
} }
private void post(int expected) {
// String payload = "{\"local-storage-id\" : \"" + storageId + "\"}";
// Entity<String> entity = Entity.entity(payload, "application/json");
// Response resp = getWebTarget().path(TRIGGER_PATH).path(lcmId).path("metadata").path(metadataId)
// .request().header(AUTH_USER_HEADER, "admin")
// .header(BasicAuthenticationManager.BASIC_AUTHENTICATION_HEADER, basicAuthTokenAdmin)
// .post(entity);
// assertEquals(expected, resp.getStatus());
}
} }
...@@ -15,15 +15,25 @@ ...@@ -15,15 +15,25 @@
*/ */
package nl.uva.sne.drip.test.manager; package nl.uva.sne.drip.test.manager;
import nl.uva.sne.drip.test.clients.KeyPairClient;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.ws.rs.core.Response;
import nl.uva.sne.drip.data.v1.external.CloudCredentials;
import nl.uva.sne.drip.data.v1.external.Key; import nl.uva.sne.drip.data.v1.external.Key;
import nl.uva.sne.drip.data.v1.external.KeyPair; import nl.uva.sne.drip.data.v1.external.KeyPair;
import nl.uva.sne.drip.data.v1.external.KeyType; import nl.uva.sne.drip.data.v1.external.KeyType;
import static nl.uva.sne.drip.test.manager.DRIPTest.ACCESS_KEY_ID_PROPPERTY_NAME;
import static nl.uva.sne.drip.test.manager.DRIPTest.CLOUD_PROPVIDER_PROPPERTY_NAME;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.FilenameUtils;
import org.junit.After; import org.junit.After;
...@@ -35,43 +45,34 @@ import static org.junit.Assert.*; ...@@ -35,43 +45,34 @@ import static org.junit.Assert.*;
/** /**
* *
* @author alogo * @author S. Koulouzis
*/ */
public class TestKeysController extends DRIPTest { public class TestKeysController extends DRIPTest {
public TestKeysController() {
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
// TODO add test methods here.
// The methods must be annotated with annotation @Test. For example:
//
@Test @Test
public void test_POST_GETCoudKeys() { public void test_POST_GET_DELETECoudKeys() throws MalformedURLException {
String[] paths = DRIPTest.CLOUD_PRIVATE_KEY_PATHS_PROPERTY_NAME.split(","); for (Properties p : DRIPTest.propertiesList) {
String[] paths = p.getProperty(DRIPTest.CLOUD_PRIVATE_KEY_PATHS_PROPERTY_NAME).split(",");
KeyPairClient client = new KeyPairClient(new URL(p.getProperty(DRIPTest.DRIP_HOST_PROPERTY_NAME)));
for (String cloudPrivateKeyPath : paths) { for (String cloudPrivateKeyPath : paths) {
try { try {
KeyPair keyPair = new KeyPair(); KeyPair keyPair = new KeyPair();
Key privateKey = createPrivateCloudKey(cloudPrivateKeyPath); Key privateKey = createPrivateCloudKey(cloudPrivateKeyPath);
keyPair.setPrivateKey(privateKey); keyPair.setPrivateKey(privateKey);
post(keyPair, 200); Response response = client.postKeyPair(keyPair);
assertEquals(200, response.getStatus());
String keyID = client.getPostKeyPairResponse(response);
assertNotNull(keyID);
response = client.get(keyID);
assertEquals(200, response.getStatus());
KeyPair returnedPair = client.getGetKeyPairResponse(response);
assertNotNull(returnedPair);
assertEquals(keyID, returnedPair.getId());
assertNotNull(returnedPair.getPrivateKey());
response = client.delete(keyID);
assertEquals(200, response.getStatus());
} catch (IOException ex) { } catch (IOException ex) {
Logger.getLogger(TestCloudCredentialsController.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(TestCloudCredentialsController.class.getName()).log(Level.SEVERE, null, ex);
...@@ -79,18 +80,15 @@ public class TestKeysController extends DRIPTest { ...@@ -79,18 +80,15 @@ public class TestKeysController extends DRIPTest {
} }
} }
private void post(KeyPair keyPair, int expected) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
} }
private Key createPrivateCloudKey(String cloudPrivateKeyPath) throws IOException { private Key createPrivateCloudKey(String cloudPrivateKeyPath) throws IOException {
Key privateKey = new Key(); Key privateKey = new Key();
privateKey.setName(FileUtils.readFileToString(new File(cloudPrivateKeyPath), "UTF-8")); privateKey.setName(FilenameUtils.getBaseName(cloudPrivateKeyPath));
privateKey.setKey(FilenameUtils.getBaseName(cloudPrivateKeyPath)); privateKey.setKey(FileUtils.readFileToString(new File(cloudPrivateKeyPath), "UTF-8"));
privateKey.setType(KeyType.PRIVATE); privateKey.setType(KeyType.PRIVATE);
Map<String, String> map = new HashMap<>(); Map<String, String> map = new HashMap<>();
map.put("attribute", "value"); map.put("domain_name", FilenameUtils.getBaseName(cloudPrivateKeyPath));
privateKey.setAttributes(map); privateKey.setAttributes(map);
return privateKey; return privateKey;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment