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

Added test for key

parent 940f4ca4
......@@ -8,7 +8,11 @@
</parent>
<artifactId>drip-tests</artifactId>
<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>
......@@ -38,6 +42,28 @@
<scope>test</scope>
<type>jar</type>
</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>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
......@@ -50,22 +76,47 @@
<groupId>nl.uva.sne.drip</groupId>
<artifactId>drip-api-json-client</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/../docs/apidocs/drip-api-json-client.jar</systemPath>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.6</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<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>
</build>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
\ No newline at end of file
access.key.id=lferpoiewp
secret.key=erfiru
cloud.provider.name=ec2
cloud.private.key.paths=/home/alogo/Virginia.pem,/home/alogo/California.pem
\ No newline at end of file
cloud.private.key.paths=/home/alogo/Downloads/DRIP/Virginia.pem,/home/alogo/Downloads/DRIP/California.pem
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;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
......@@ -30,6 +32,10 @@ import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
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,31 +43,40 @@ import org.junit.BeforeClass;
*/
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 final String ACCESS_KEY_ID_PROPPERTY_NAME = "access.key.id";
static String CLOUD_PROPVIDER_PROPPERTY_NAME = "cloud.provider.name";
static String SECRET_KEY_PROPERTY_NAME = "secret.key";
static String CLOUD_PRIVATE_KEY_PATHS_PROPERTY_NAME = "cloud.private.key.paths";
public DRIPTest() {
PROPERTIES_FILE_PATHS = new ArrayList<>();
}
public static final String CLOUD_PROPVIDER_PROPPERTY_NAME = "cloud.provider.name";
public static final String SECRET_KEY_PROPERTY_NAME = "secret.key";
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 static final String DRIP_TEST_PROPERTY_FILES_PROPERTY_NAME = "test.property.files";
@BeforeClass
public static void setUpClass() {
for (String propFile : PROPERTIES_FILE_PATHS) {
try (Reader inStream = new FileReader(new File(propFile))) {
Properties prop = new Properties();
prop.load(inStream);
propertiesList.add(prop);
} catch (FileNotFoundException ex) {
Logger.getLogger(TestCloudCredentialsController.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(TestCloudCredentialsController.class.getName()).log(Level.SEVERE, null, ex);
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) {
try (InputStream inStream = ClassLoader.getSystemResourceAsStream(propFile)) {
Properties prop = new Properties();
prop.load(inStream);
propertiesList.add(prop);
} catch (FileNotFoundException ex) {
Logger.getLogger(TestCloudCredentialsController.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException 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 @@
package nl.uva.sne.drip.test.manager;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
......@@ -27,15 +24,6 @@ import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
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.Key;
import nl.uva.sne.drip.data.v1.external.KeyPair;
......@@ -43,9 +31,6 @@ import nl.uva.sne.drip.data.v1.external.KeyType;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
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;
*/
public class TestCloudCredentialsController extends DRIPTest {
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Test
public void testPOST_GETCloudCredentials() {
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) {
try {
KeyPair keyPair = new KeyPair();
......@@ -76,6 +53,7 @@ public class TestCloudCredentialsController extends DRIPTest {
map.put(cloudPrivateKeyPath, cloudPrivateKeyPath);
privateKey.setAttributes(map);
keyPair.setPrivateKey(privateKey);
} catch (IOException ex) {
Logger.getLogger(TestCloudCredentialsController.class.getName()).log(Level.SEVERE, null, ex);
}
......@@ -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 @@
*/
package nl.uva.sne.drip.test.manager;
import nl.uva.sne.drip.test.clients.KeyPairClient;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
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.KeyPair;
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.FilenameUtils;
import org.junit.After;
......@@ -35,62 +45,50 @@ import static org.junit.Assert.*;
/**
*
* @author alogo
* @author S. Koulouzis
*/
public class TestKeysController extends DRIPTest {
public TestKeysController() {
}
@Test
public void test_POST_GET_DELETECoudKeys() throws MalformedURLException {
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) {
try {
KeyPair keyPair = new KeyPair();
Key privateKey = createPrivateCloudKey(cloudPrivateKeyPath);
keyPair.setPrivateKey(privateKey);
Response response = client.postKeyPair(keyPair);
assertEquals(200, response.getStatus());
String keyID = client.getPostKeyPairResponse(response);
assertNotNull(keyID);
@BeforeClass
public static void setUpClass() {
}
response = client.get(keyID);
assertEquals(200, response.getStatus());
KeyPair returnedPair = client.getGetKeyPairResponse(response);
assertNotNull(returnedPair);
assertEquals(keyID, returnedPair.getId());
assertNotNull(returnedPair.getPrivateKey());
@AfterClass
public static void tearDownClass() {
}
response = client.delete(keyID);
assertEquals(200, response.getStatus());
@Before
public void setUp() {
}
} catch (IOException ex) {
Logger.getLogger(TestCloudCredentialsController.class.getName()).log(Level.SEVERE, null, ex);
}
@After
public void tearDown() {
}
// TODO add test methods here.
// The methods must be annotated with annotation @Test. For example:
//
@Test
public void test_POST_GETCoudKeys() {
String[] paths = DRIPTest.CLOUD_PRIVATE_KEY_PATHS_PROPERTY_NAME.split(",");
for (String cloudPrivateKeyPath : paths) {
try {
KeyPair keyPair = new KeyPair();
Key privateKey = createPrivateCloudKey(cloudPrivateKeyPath);
keyPair.setPrivateKey(privateKey);
post(keyPair, 200);
} catch (IOException ex) {
Logger.getLogger(TestCloudCredentialsController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
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 {
Key privateKey = new Key();
privateKey.setName(FileUtils.readFileToString(new File(cloudPrivateKeyPath), "UTF-8"));
privateKey.setKey(FilenameUtils.getBaseName(cloudPrivateKeyPath));
privateKey.setName(FilenameUtils.getBaseName(cloudPrivateKeyPath));
privateKey.setKey(FileUtils.readFileToString(new File(cloudPrivateKeyPath), "UTF-8"));
privateKey.setType(KeyType.PRIVATE);
Map<String, String> map = new HashMap<>();
map.put("attribute", "value");
map.put("domain_name", FilenameUtils.getBaseName(cloudPrivateKeyPath));
privateKey.setAttributes(map);
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