Added a Database Discovery Class
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/private/luca.frosini/infrastructure-tests@142795 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
5fd90193e4
commit
2cec592c63
|
@ -0,0 +1,120 @@
|
|||
package org.gcube.test;
|
||||
|
||||
import java.security.Key;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.gcube.common.encryption.StringEncrypter;
|
||||
import org.gcube.common.resources.gcore.ServiceEndpoint;
|
||||
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
|
||||
import org.gcube.common.resources.gcore.ServiceEndpoint.Property;
|
||||
import org.gcube.common.resources.gcore.utils.Group;
|
||||
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
||||
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
||||
import org.gcube.resources.discovery.icclient.ICFactory;
|
||||
|
||||
public class DatabaseDiscovery {
|
||||
|
||||
protected String serviceEndpointCategory;
|
||||
protected String serviceEndpointName;
|
||||
protected String entryName;
|
||||
|
||||
protected String url;
|
||||
protected String username;
|
||||
protected String password;
|
||||
|
||||
protected Map<String, Property> propertyMap;
|
||||
|
||||
protected void init(){
|
||||
this.propertyMap = new HashMap<String, Property>();
|
||||
}
|
||||
|
||||
public DatabaseDiscovery() throws Exception {
|
||||
this.propertyMap = new HashMap<String, Property>();
|
||||
}
|
||||
|
||||
public DatabaseDiscovery(String serviceEndpointCategory, String serviceEndpointName, String entryName) throws Exception {
|
||||
this.propertyMap = new HashMap<String, Property>();
|
||||
this.serviceEndpointCategory = serviceEndpointCategory;
|
||||
this.serviceEndpointName = serviceEndpointName;
|
||||
this.entryName = entryName;
|
||||
ServiceEndpoint serviceEndpoint = getServiceEndpoint(serviceEndpointCategory, serviceEndpointName, entryName);
|
||||
setCredentialFromServiceEndpoint(serviceEndpoint);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the url
|
||||
*/
|
||||
public String getURL() {
|
||||
return url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param uri the uri to set
|
||||
*/
|
||||
public void setURL(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the username
|
||||
*/
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param username the username to set
|
||||
*/
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the password
|
||||
*/
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param password the password to set
|
||||
*/
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
protected ServiceEndpoint getServiceEndpoint(String serviceEndpointCategory, String serviceEndpointName, String entryName){
|
||||
SimpleQuery query = ICFactory.queryFor(ServiceEndpoint.class);
|
||||
query.addCondition(String.format("$resource/Profile/Category/text() eq '%s'", serviceEndpointCategory));
|
||||
query.addCondition(String.format("$resource/Profile/Name/text() eq '%s'", serviceEndpointName));
|
||||
query.addCondition(String.format("$resource/Profile/AccessPoint/Interface/Endpoint/@EntryName eq '%s'", entryName));
|
||||
query.setResult("$resource");
|
||||
|
||||
DiscoveryClient<ServiceEndpoint> client = ICFactory.clientFor(ServiceEndpoint.class);
|
||||
List<ServiceEndpoint> serviceEndpoints = client.submit(query);
|
||||
return serviceEndpoints.get(0);
|
||||
}
|
||||
|
||||
private static String decrypt(String encrypted, Key... key) throws Exception {
|
||||
return StringEncrypter.getEncrypter().decrypt(encrypted);
|
||||
}
|
||||
|
||||
protected void setCredentialFromServiceEndpoint(ServiceEndpoint serviceEndpoint) throws Exception{
|
||||
Group<AccessPoint> accessPoints = serviceEndpoint.profile().accessPoints();
|
||||
for(AccessPoint accessPoint : accessPoints){
|
||||
if(accessPoint.name().compareTo(entryName)==0){
|
||||
this.url = accessPoint.address();
|
||||
this.username = accessPoint.username();
|
||||
|
||||
String encryptedPassword = accessPoint.password();
|
||||
String password = decrypt(encryptedPassword);
|
||||
|
||||
this.password = password;
|
||||
this.propertyMap = accessPoint.propertyMap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -15,6 +15,7 @@ import org.gcube.common.scope.api.ScopeProvider;
|
|||
import org.gcube.informationsystem.publisher.RegistryPublisherFactory;
|
||||
import org.gcube.informationsystem.publisher.ScopedPublisher;
|
||||
import org.gcube.informationsystem.publisher.exception.RegistryNotFoundException;
|
||||
import org.gcube.testutility.ScopedTest;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
@ -23,7 +24,7 @@ import org.slf4j.LoggerFactory;
|
|||
* @author Luca Frosini (ISTI - CNR)
|
||||
*
|
||||
*/
|
||||
public class TempTest {
|
||||
public class TempTest extends ScopedTest {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(TempTest.class);
|
||||
|
||||
|
@ -78,4 +79,20 @@ public class TempTest {
|
|||
logger.error("Not found", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static final String SERVICE_ENDPOINT_CATEGORY = "Database";
|
||||
public static final String SERVICE_ENDPOINT_NAME = "TwitterMonitorDatabase";
|
||||
public static final String ENTRY_NAME = "postgress";
|
||||
|
||||
@Test
|
||||
public void testTWMonitorDiscovery() throws Exception {
|
||||
ScopedTest.setContext(GCUBE_DEVSEC);
|
||||
|
||||
DatabaseDiscovery databaseDiscovery = new DatabaseDiscovery(SERVICE_ENDPOINT_CATEGORY, SERVICE_ENDPOINT_NAME, ENTRY_NAME);
|
||||
logger.debug("Username : {}", databaseDiscovery.getUsername());
|
||||
logger.debug("Password : {}", databaseDiscovery.getPassword());
|
||||
logger.debug("URL : {}", databaseDiscovery.getURL());
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue