From 19813a46bba788de83f8705b42ace44284057f00 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Mon, 22 Nov 2021 17:49:05 +0100 Subject: [PATCH] Adding tests --- .../AccountingPersistenceQuery.java | 3 +- ...AccountingPersistenceBackendQueryTest.java | 52 +++++++++ .../accounting/analytics/ContextTest.java | 106 ++++++++++++++++++ src/test/resources/.gitignore | 4 + src/test/resources/logback-test.xml | 1 + 5 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/gcube/accounting/analytics/AccountingPersistenceBackendQueryTest.java create mode 100644 src/test/java/org/gcube/accounting/analytics/ContextTest.java create mode 100644 src/test/resources/.gitignore diff --git a/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceQuery.java b/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceQuery.java index 2d6336a..c347074 100644 --- a/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceQuery.java +++ b/src/main/java/org/gcube/accounting/analytics/persistence/AccountingPersistenceQuery.java @@ -36,7 +36,6 @@ import org.gcube.documentstore.records.Record; /** * @author Luca Frosini (ISTI - CNR) - * */ public class AccountingPersistenceQuery implements AccountingPersistenceBackendQuery { @@ -130,6 +129,8 @@ public class AccountingPersistenceQuery implements AccountingPersistenceBackendQ String key = keys.next(); objectNode.put(key, 0); } + }else { + } return objectNode; } diff --git a/src/test/java/org/gcube/accounting/analytics/AccountingPersistenceBackendQueryTest.java b/src/test/java/org/gcube/accounting/analytics/AccountingPersistenceBackendQueryTest.java new file mode 100644 index 0000000..32d13b3 --- /dev/null +++ b/src/test/java/org/gcube/accounting/analytics/AccountingPersistenceBackendQueryTest.java @@ -0,0 +1,52 @@ +package org.gcube.accounting.analytics; + +import java.util.Calendar; +import java.util.SortedMap; + +import org.gcube.accounting.analytics.TemporalConstraint.AggregationMode; +import org.gcube.accounting.analytics.exception.DuplicatedKeyFilterException; +import org.gcube.accounting.analytics.exception.KeyException; +import org.gcube.accounting.analytics.exception.ValueException; +import org.gcube.accounting.analytics.persistence.AccountingPersistenceBackendQuery; +import org.gcube.accounting.analytics.persistence.AccountingPersistenceBackendQueryFactory; +import org.gcube.accounting.datamodel.aggregation.AggregatedStorageStatusRecord; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class AccountingPersistenceBackendQueryTest { + + private static Logger logger = LoggerFactory.getLogger(AccountingPersistenceBackendQueryTest.class); + + @Test + public void testStorageTimeSeries() throws DuplicatedKeyFilterException, KeyException, ValueException, Exception { + AccountingPersistenceBackendQuery apbq = AccountingPersistenceBackendQueryFactory.getInstance(); + + Calendar startTimeCalendar = Calendar.getInstance(); + startTimeCalendar.set(Calendar.YEAR, 2021); + startTimeCalendar.set(Calendar.MONTH, Calendar.OCTOBER); + startTimeCalendar.set(Calendar.DAY_OF_MONTH, 1); + startTimeCalendar.set(Calendar.HOUR_OF_DAY, 0); + startTimeCalendar.set(Calendar.MINUTE, 0); + + Calendar entTimeCalendar = Calendar.getInstance(); + entTimeCalendar.set(Calendar.YEAR, 2021); + entTimeCalendar.set(Calendar.MONTH, Calendar.NOVEMBER); + entTimeCalendar.set(Calendar.DAY_OF_MONTH, 22); + entTimeCalendar.set(Calendar.HOUR_OF_DAY, 0); + entTimeCalendar.set(Calendar.MINUTE, 0); + + TemporalConstraint temporalConstraint = new TemporalConstraint(startTimeCalendar.getTimeInMillis(), entTimeCalendar.getTimeInMillis(), AggregationMode.DAILY); + + apbq.setRequestedRecords(AggregatedStorageStatusRecord.class); + apbq.setTemporalConstraint(temporalConstraint); + + SortedMap timeseries = apbq.getTimeSeries(); + for(Calendar c : timeseries.keySet()) { + Info info = timeseries.get(c); + logger.debug("{}", info); + } + + } + +} diff --git a/src/test/java/org/gcube/accounting/analytics/ContextTest.java b/src/test/java/org/gcube/accounting/analytics/ContextTest.java new file mode 100644 index 0000000..1afbfd0 --- /dev/null +++ b/src/test/java/org/gcube/accounting/analytics/ContextTest.java @@ -0,0 +1,106 @@ +/** + * + */ +package org.gcube.accounting.analytics; + +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); + } + + public 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(); + } + +} diff --git a/src/test/resources/.gitignore b/src/test/resources/.gitignore new file mode 100644 index 0000000..22f985a --- /dev/null +++ b/src/test/resources/.gitignore @@ -0,0 +1,4 @@ +/devNext.gcubekey +/devsec.gcubekey +/gcube.gcubekey +/token.properties diff --git a/src/test/resources/logback-test.xml b/src/test/resources/logback-test.xml index b2f0587..a94998e 100644 --- a/src/test/resources/logback-test.xml +++ b/src/test/resources/logback-test.xml @@ -10,6 +10,7 @@ +