Implementing library
This commit is contained in:
parent
bc7dc206b4
commit
7b8734bf47
5
pom.xml
5
pom.xml
|
@ -64,6 +64,11 @@
|
||||||
<artifactId>logback-classic</artifactId>
|
<artifactId>logback-classic</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.gcube.accounting</groupId>
|
||||||
|
<artifactId>accounting-lib</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
|
|
@ -62,7 +62,7 @@ public class PersistencePostgreSQL extends PersistenceBackend {
|
||||||
StringBuffer values = new StringBuffer();
|
StringBuffer values = new StringBuffer();
|
||||||
for(String key : keys) {
|
for(String key : keys) {
|
||||||
if(first) {
|
if(first) {
|
||||||
sql.append(" ");
|
sql.append(" (");
|
||||||
values.append(" (");
|
values.append(" (");
|
||||||
first = false;
|
first = false;
|
||||||
}else {
|
}else {
|
||||||
|
|
|
@ -0,0 +1,106 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.gcube.documentstore.persistence;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import org.gcube.common.authorization.client.Constants;
|
||||||
|
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
|
||||||
|
import org.gcube.common.authorization.library.AuthorizationEntry;
|
||||||
|
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
|
||||||
|
import org.gcube.common.authorization.library.provider.ClientInfo;
|
||||||
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
||||||
|
import org.gcube.common.authorization.library.utils.Caller;
|
||||||
|
import org.gcube.common.scope.api.ScopeProvider;
|
||||||
|
import org.junit.AfterClass;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Luca Frosini (ISTI - CNR)
|
||||||
|
*/
|
||||||
|
public class ContextTest {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(ContextTest.class);
|
||||||
|
|
||||||
|
protected static Properties properties;
|
||||||
|
protected static final String PROPERTIES_FILENAME = "token.properties";
|
||||||
|
|
||||||
|
public static final String PARENT_DEFAULT_TEST_SCOPE;
|
||||||
|
public static final String DEFAULT_TEST_SCOPE;
|
||||||
|
public static final String ALTERNATIVE_TEST_SCOPE;
|
||||||
|
|
||||||
|
public static final String DEFAULT_TEST_SCOPE_ANOTHER_USER;
|
||||||
|
|
||||||
|
static {
|
||||||
|
properties = new Properties();
|
||||||
|
InputStream input = ContextTest.class.getClassLoader().getResourceAsStream(PROPERTIES_FILENAME);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// load the properties file
|
||||||
|
properties.load(input);
|
||||||
|
} catch(IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
// PARENT_DEFAULT_TEST_SCOPE = "/pred4s"
|
||||||
|
// DEFAULT_TEST_SCOPE_NAME = PARENT_DEFAULT_TEST_SCOPE + "/preprod";
|
||||||
|
// ALTERNATIVE_TEST_SCOPE = DEFAULT_TEST_SCOPE_NAME + "/preVRE";
|
||||||
|
|
||||||
|
|
||||||
|
PARENT_DEFAULT_TEST_SCOPE = "/gcube";
|
||||||
|
DEFAULT_TEST_SCOPE = PARENT_DEFAULT_TEST_SCOPE + "/devNext";
|
||||||
|
ALTERNATIVE_TEST_SCOPE = DEFAULT_TEST_SCOPE + "/NextNext";
|
||||||
|
|
||||||
|
DEFAULT_TEST_SCOPE_ANOTHER_USER = "lucio.lelii_" + DEFAULT_TEST_SCOPE;
|
||||||
|
|
||||||
|
try {
|
||||||
|
setContextByName(DEFAULT_TEST_SCOPE);
|
||||||
|
} catch(Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getCurrentContextFullName() throws ObjectNotFound, Exception {
|
||||||
|
return getContextFullNameByToken(SecurityTokenProvider.instance.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getContextFullNameByToken(String token) throws ObjectNotFound, Exception {
|
||||||
|
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
|
||||||
|
String context = authorizationEntry.getContext();
|
||||||
|
logger.info("Context of token {} is {}", token, context);
|
||||||
|
return context;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setContextByName(String fullContextName) throws ObjectNotFound, Exception {
|
||||||
|
String token = ContextTest.properties.getProperty(fullContextName);
|
||||||
|
setContext(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void setContext(String token) throws ObjectNotFound, Exception {
|
||||||
|
SecurityTokenProvider.instance.set(token);
|
||||||
|
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
|
||||||
|
ClientInfo clientInfo = authorizationEntry.getClientInfo();
|
||||||
|
logger.debug("User : {} - Type : {}", clientInfo.getId(), clientInfo.getType().name());
|
||||||
|
String qualifier = authorizationEntry.getQualifier();
|
||||||
|
Caller caller = new Caller(clientInfo, qualifier);
|
||||||
|
AuthorizationProvider.instance.set(caller);
|
||||||
|
ScopeProvider.instance.set(getContextFullNameByToken(token));
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void beforeClass() throws Exception {
|
||||||
|
setContextByName(DEFAULT_TEST_SCOPE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterClass
|
||||||
|
public static void afterClass() throws Exception {
|
||||||
|
SecurityTokenProvider.instance.reset();
|
||||||
|
ScopeProvider.instance.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,78 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.gcube.documentstore.persistence;
|
||||||
|
|
||||||
|
import java.util.Calendar;
|
||||||
|
|
||||||
|
import org.gcube.accounting.datamodel.UsageRecord;
|
||||||
|
import org.gcube.accounting.datamodel.UsageRecord.OperationResult;
|
||||||
|
import org.gcube.accounting.datamodel.aggregation.AggregatedServiceUsageRecord;
|
||||||
|
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Luca Frosini (ISTI - CNR)
|
||||||
|
*/
|
||||||
|
public class PersistencePostgreSQLTest extends ContextTest {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(PersistencePostgreSQLTest.class);
|
||||||
|
|
||||||
|
protected UsageRecord getTestServiceUsageRecord() throws Exception {
|
||||||
|
AggregatedServiceUsageRecord serviceUsageRecord = new AggregatedServiceUsageRecord();
|
||||||
|
serviceUsageRecord.setAggregated(true);
|
||||||
|
|
||||||
|
Calendar creationTime = Calendar.getInstance();
|
||||||
|
serviceUsageRecord.setCreationTime(creationTime);
|
||||||
|
|
||||||
|
Calendar startTime = Calendar.getInstance();
|
||||||
|
serviceUsageRecord.setStartTime(startTime);
|
||||||
|
|
||||||
|
serviceUsageRecord.setCalledMethod("createResource");
|
||||||
|
serviceUsageRecord.setCallerHost("localhost");
|
||||||
|
serviceUsageRecord.setHost("localhost");
|
||||||
|
serviceUsageRecord.setConsumerId("luca.frosini");
|
||||||
|
serviceUsageRecord.setCallerQualifier("TOKEN");
|
||||||
|
serviceUsageRecord.setDuration((long) 100);
|
||||||
|
|
||||||
|
serviceUsageRecord.setMaxInvocationTime((long) 150);
|
||||||
|
serviceUsageRecord.setMinInvocationTime((long) 50);
|
||||||
|
serviceUsageRecord.setOperationCount(9);
|
||||||
|
serviceUsageRecord.setOperationResult(OperationResult.SUCCESS);
|
||||||
|
serviceUsageRecord.setScope(ContextTest.getCurrentContextFullName());
|
||||||
|
|
||||||
|
Calendar endTime = Calendar.getInstance();
|
||||||
|
endTime.add(Calendar.MINUTE, 5);
|
||||||
|
serviceUsageRecord.setEndTime(endTime);
|
||||||
|
|
||||||
|
serviceUsageRecord.setServiceClass("information-system");
|
||||||
|
serviceUsageRecord.setServiceName("resource-registry");
|
||||||
|
|
||||||
|
return serviceUsageRecord;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSQLStatementString() throws Exception {
|
||||||
|
UsageRecord usageRecord = getTestServiceUsageRecord();
|
||||||
|
PersistencePostgreSQL persistencePostgreSQL = new PersistencePostgreSQL();
|
||||||
|
String sql = persistencePostgreSQL.getSQLInsertCommand(usageRecord);
|
||||||
|
logger.debug(sql);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void persistenceIsPostgreSQL() throws ObjectNotFound, Exception {
|
||||||
|
logger.debug("Going to check if the Persistence is CouchBase");
|
||||||
|
PersistenceBackendFactory.setFallbackLocation(null);
|
||||||
|
String context = ContextTest.getCurrentContextFullName();
|
||||||
|
FallbackPersistenceBackend fallbackPersistenceBackend = PersistenceBackendFactory.createFallback(context);
|
||||||
|
|
||||||
|
PersistenceBackend persistenceBackend = PersistenceBackendFactory.rediscoverPersistenceBackend(fallbackPersistenceBackend, context);
|
||||||
|
|
||||||
|
Assert.assertTrue(persistenceBackend instanceof PersistencePostgreSQL);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,2 @@
|
||||||
|
/token.properties
|
||||||
|
/*.gcubekey
|
|
@ -0,0 +1,19 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE xml>
|
||||||
|
<configuration>
|
||||||
|
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{0}: %msg%n</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
|
||||||
|
<logger name="org.gcube" level="INFO" />
|
||||||
|
<logger name="org.gcube.documentstore.persistence" level="TRACE" />
|
||||||
|
|
||||||
|
<root level="WARN">
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
</root>
|
||||||
|
|
||||||
|
</configuration>
|
Loading…
Reference in New Issue