refs #2209: Remove the use of ScopeProvider if any from document-store-lib-couchdb
https://support.d4science.org/issues/2209 git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-publishing/document-store-lib-couchdb@124029 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
1a1e392d3b
commit
7b1f28a52f
8
pom.xml
8
pom.xml
|
@ -8,7 +8,7 @@
|
||||||
</parent>
|
</parent>
|
||||||
<groupId>org.gcube.data.publishing</groupId>
|
<groupId>org.gcube.data.publishing</groupId>
|
||||||
<artifactId>document-store-lib-couchdb</artifactId>
|
<artifactId>document-store-lib-couchdb</artifactId>
|
||||||
<version>1.0.0-SNAPSHOT</version>
|
<version>1.1.0-SNAPSHOT</version>
|
||||||
<name>Document Store CouchDB</name>
|
<name>Document Store CouchDB</name>
|
||||||
<description>Document Store CouchDB Implementation</description>
|
<description>Document Store CouchDB Implementation</description>
|
||||||
|
|
||||||
|
@ -44,19 +44,19 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.gcube.accounting</groupId>
|
<groupId>org.gcube.accounting</groupId>
|
||||||
<artifactId>accounting-lib</artifactId>
|
<artifactId>accounting-lib</artifactId>
|
||||||
<version>[2.0.0-SNAPSHOT, 3.0.0-SNAPSHOT)</version>
|
<version>[2.1.0-SNAPSHOT, 3.0.0-SNAPSHOT)</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.gcube.core</groupId>
|
<groupId>org.gcube.core</groupId>
|
||||||
<artifactId>common-encryption</artifactId>
|
<artifactId>common-encryption</artifactId>
|
||||||
<version>[1.0.2-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
|
<version>[1.0.0-SNAPSHOT, 3.0.0-SNAPSHOT)</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.gcube.resources</groupId>
|
<groupId>org.gcube.resources</groupId>
|
||||||
<artifactId>registry-publisher</artifactId>
|
<artifactId>registry-publisher</artifactId>
|
||||||
<version>[1.0.0-SNAPSHOT,2.0.0-SNAPSHOT)</version>
|
<version>[1.0.0-SNAPSHOT,3.0.0-SNAPSHOT)</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|
|
@ -4,21 +4,21 @@
|
||||||
package org.gcube.accounting.persistence;
|
package org.gcube.accounting.persistence;
|
||||||
|
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
|
import org.gcube.accounting.datamodel.BasicUsageRecord;
|
||||||
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
||||||
import org.gcube.common.resources.gcore.Resource;
|
import org.gcube.common.resources.gcore.Resource;
|
||||||
import org.gcube.common.resources.gcore.Resources;
|
import org.gcube.common.resources.gcore.Resources;
|
||||||
import org.gcube.common.resources.gcore.ServiceEndpoint;
|
import org.gcube.common.resources.gcore.ServiceEndpoint;
|
||||||
import org.gcube.common.scope.api.ScopeProvider;
|
|
||||||
import org.gcube.documentstore.persistence.PersistenceBackend;
|
import org.gcube.documentstore.persistence.PersistenceBackend;
|
||||||
import org.gcube.documentstore.persistence.PersistenceBackendFactory;
|
import org.gcube.documentstore.persistence.PersistenceBackendFactory;
|
||||||
import org.gcube.documentstore.persistence.PersistenceCouchDB;
|
import org.gcube.documentstore.persistence.PersistenceCouchDB;
|
||||||
|
import org.gcube.informationsystem.publisher.RegistryPublisher;
|
||||||
import org.gcube.informationsystem.publisher.RegistryPublisherFactory;
|
import org.gcube.informationsystem.publisher.RegistryPublisherFactory;
|
||||||
import org.gcube.informationsystem.publisher.ScopedPublisher;
|
import org.junit.After;
|
||||||
import org.gcube.informationsystem.publisher.exception.RegistryNotFoundException;
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
@ -31,10 +31,19 @@ public class PersistenceCouchDBTest {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(PersistenceCouchDBTest.class);
|
private static final Logger logger = LoggerFactory.getLogger(PersistenceCouchDBTest.class);
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void before() throws Exception{
|
||||||
|
SecurityTokenProvider.instance.set(TestUtility.TOKEN);
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void after(){
|
||||||
|
SecurityTokenProvider.instance.reset();
|
||||||
|
}
|
||||||
|
|
||||||
public static PersistenceBackend getPersistence(){
|
public static PersistenceBackend getPersistence(){
|
||||||
ScopeProvider.instance.set("/gcube/devNext");
|
|
||||||
PersistenceBackendFactory.setFallbackLocation(null);
|
PersistenceBackendFactory.setFallbackLocation(null);
|
||||||
return PersistenceBackendFactory.getPersistenceBackend(ScopeProvider.instance.get());
|
return PersistenceBackendFactory.getPersistenceBackend(BasicUsageRecord.getScopeFromToken());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -43,33 +52,38 @@ public class PersistenceCouchDBTest {
|
||||||
Assert.assertTrue(persistence instanceof PersistenceCouchDB);
|
Assert.assertTrue(persistence instanceof PersistenceCouchDB);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void publishScopedResource(Resource resource, List<String> scopes) throws Exception {
|
private static void publishResource(Resource resource) throws Exception {
|
||||||
StringWriter stringWriter = new StringWriter();
|
StringWriter stringWriter = new StringWriter();
|
||||||
Resources.marshal(resource, stringWriter);
|
Resources.marshal(resource, stringWriter);
|
||||||
|
|
||||||
ScopedPublisher scopedPublisher = RegistryPublisherFactory.scopedPublisher();
|
RegistryPublisher registryPublisher = RegistryPublisherFactory.create();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
logger.debug("Trying to publish to {}:\n{}", scopes, stringWriter);
|
logger.debug("Trying to publish to {}:\n{}", BasicUsageRecord.getScopeFromToken(), stringWriter);
|
||||||
scopedPublisher.create(resource, scopes);
|
registryPublisher.create(resource);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("The resource was not published", e);
|
logger.error("The resource was not published", e);
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void unPublishScopedResource(Resource resource, List<String> scopes) throws RegistryNotFoundException, Exception {
|
private static void unPublishResource(Resource resource) throws Exception {
|
||||||
ScopedPublisher scopedPublisher = RegistryPublisherFactory.scopedPublisher();
|
//StringWriter stringWriter = new StringWriter();
|
||||||
String id = resource.id();
|
//Resources.marshal(resource, stringWriter);
|
||||||
logger.debug("Trying to remove {} with ID {} from {}", resource.getClass().getSimpleName(), id, scopes);
|
|
||||||
scopedPublisher.remove(resource, scopes);
|
RegistryPublisher registryPublisher = RegistryPublisherFactory.create();
|
||||||
|
|
||||||
|
String id = resource.id();
|
||||||
|
logger.debug("Trying to remove {} with ID {} from {}",
|
||||||
|
resource.getClass().getSimpleName(), id,
|
||||||
|
BasicUsageRecord.getScopeFromToken());
|
||||||
|
|
||||||
|
registryPublisher.remove(resource);
|
||||||
|
|
||||||
logger.debug("{} with ID {} removed successfully", resource.getClass().getSimpleName(), id);
|
logger.debug("{} with ID {} removed successfully", resource.getClass().getSimpleName(), id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testScopeRecheck() throws Exception {
|
public void testScopeRecheck() throws Exception {
|
||||||
final String vreScopeToUse = "/gcube/devsec/LucioVRE";
|
|
||||||
final String parentVREScopeToUse = "/gcube/devsec";
|
|
||||||
|
|
||||||
ScopeProvider.instance.set(vreScopeToUse);
|
|
||||||
|
|
||||||
ServiceEndpoint serviceEndpoint = null;
|
ServiceEndpoint serviceEndpoint = null;
|
||||||
try {
|
try {
|
||||||
|
@ -79,18 +93,16 @@ public class PersistenceCouchDBTest {
|
||||||
AccountingPersistenceConfiguration.SERVICE_ENDPOINT_CATEGORY,
|
AccountingPersistenceConfiguration.SERVICE_ENDPOINT_CATEGORY,
|
||||||
AccountingPersistenceConfiguration.SERVICE_ENDPOINT_NAME,
|
AccountingPersistenceConfiguration.SERVICE_ENDPOINT_NAME,
|
||||||
PersistenceCouchDB.class);
|
PersistenceCouchDB.class);
|
||||||
List<String> scopes = new ArrayList<String>();
|
unPublishResource(serviceEndpoint);
|
||||||
scopes.add(ScopeProvider.instance.get());
|
|
||||||
unPublishScopedResource(serviceEndpoint, scopes);
|
|
||||||
} catch(IndexOutOfBoundsException e){
|
} catch(IndexOutOfBoundsException e){
|
||||||
ScopeProvider.instance.set(parentVREScopeToUse);
|
SecurityTokenProvider.instance.set(TestUtility.PARENT_TOKEN);
|
||||||
AccountingPersistenceConfiguration persitenceConfiguration =
|
AccountingPersistenceConfiguration persitenceConfiguration =
|
||||||
new AccountingPersistenceConfiguration(PersistenceCouchDB.class);
|
new AccountingPersistenceConfiguration(PersistenceCouchDB.class);
|
||||||
serviceEndpoint = persitenceConfiguration.getServiceEndpoint(
|
serviceEndpoint = persitenceConfiguration.getServiceEndpoint(
|
||||||
AccountingPersistenceConfiguration.SERVICE_ENDPOINT_CATEGORY,
|
AccountingPersistenceConfiguration.SERVICE_ENDPOINT_CATEGORY,
|
||||||
AccountingPersistenceConfiguration.SERVICE_ENDPOINT_NAME,
|
AccountingPersistenceConfiguration.SERVICE_ENDPOINT_NAME,
|
||||||
PersistenceCouchDB.class);
|
PersistenceCouchDB.class);
|
||||||
ScopeProvider.instance.set(vreScopeToUse);
|
SecurityTokenProvider.instance.set(TestUtility.TOKEN);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -101,12 +113,10 @@ public class PersistenceCouchDBTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.debug("Going to check First Time");
|
logger.debug("Going to check First Time");
|
||||||
PersistenceBackend first = PersistenceBackendFactory.getPersistenceBackend(ScopeProvider.instance.get());
|
PersistenceBackend first = PersistenceBackendFactory.getPersistenceBackend(BasicUsageRecord.getScopeFromToken());
|
||||||
logger.debug("First {} : {}", PersistenceBackend.class.getSimpleName(), first);
|
logger.debug("First {} : {}", PersistenceBackend.class.getSimpleName(), first);
|
||||||
|
|
||||||
List<String> scopes = new ArrayList<String>();
|
publishResource(serviceEndpoint);
|
||||||
scopes.add(ScopeProvider.instance.get());
|
|
||||||
publishScopedResource(serviceEndpoint, scopes);
|
|
||||||
|
|
||||||
startTime = Calendar.getInstance().getTimeInMillis();
|
startTime = Calendar.getInstance().getTimeInMillis();
|
||||||
endTime = startTime;
|
endTime = startTime;
|
||||||
|
@ -115,7 +125,7 @@ public class PersistenceCouchDBTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.debug("Going to check Second Time");
|
logger.debug("Going to check Second Time");
|
||||||
PersistenceBackend second = PersistenceBackendFactory.getPersistenceBackend(ScopeProvider.instance.get());
|
PersistenceBackend second = PersistenceBackendFactory.getPersistenceBackend(BasicUsageRecord.getScopeFromToken());
|
||||||
logger.debug("Second {} : {}", PersistenceBackend.class.getSimpleName(), second);
|
logger.debug("Second {} : {}", PersistenceBackend.class.getSimpleName(), second);
|
||||||
|
|
||||||
Assert.assertNotEquals(first, second);
|
Assert.assertNotEquals(first, second);
|
||||||
|
|
|
@ -0,0 +1,86 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.gcube.accounting.persistence;
|
||||||
|
|
||||||
|
import java.io.StringWriter;
|
||||||
|
|
||||||
|
import org.gcube.common.authorization.client.Constants;
|
||||||
|
import org.gcube.common.authorization.library.AuthorizationEntry;
|
||||||
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
||||||
|
import org.gcube.common.resources.gcore.Resource;
|
||||||
|
import org.gcube.common.resources.gcore.Resources;
|
||||||
|
import org.gcube.informationsystem.publisher.RegistryPublisher;
|
||||||
|
import org.gcube.informationsystem.publisher.RegistryPublisherFactory;
|
||||||
|
import org.gcube.informationsystem.publisher.exception.RegistryNotFoundException;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class TestUtility {
|
||||||
|
|
||||||
|
public static final String TOKEN = "";
|
||||||
|
public static final String PARENT_TOKEN = "";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Logger
|
||||||
|
*/
|
||||||
|
private static Logger logger = LoggerFactory.getLogger(TestUtility.class);
|
||||||
|
|
||||||
|
public static String getScopeFromToken(){
|
||||||
|
String token = SecurityTokenProvider.instance.get();
|
||||||
|
AuthorizationEntry authorizationEntry;
|
||||||
|
try {
|
||||||
|
authorizationEntry = Constants.authorizationService().get(token);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
String scope = authorizationEntry.getContext();
|
||||||
|
return scope;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Publish the provided resource on current scope
|
||||||
|
* @param resource to be published
|
||||||
|
* @throws RegistryNotFoundException if the Registry is not found so the
|
||||||
|
* resource has not be published
|
||||||
|
*/
|
||||||
|
public static void publishResource(Resource resource) throws Exception {
|
||||||
|
StringWriter stringWriter = new StringWriter();
|
||||||
|
Resources.marshal(resource, stringWriter);
|
||||||
|
|
||||||
|
RegistryPublisher registryPublisher = RegistryPublisherFactory.create();
|
||||||
|
|
||||||
|
try {
|
||||||
|
logger.debug("Trying to publish to {}:\n{}", getScopeFromToken(), stringWriter);
|
||||||
|
registryPublisher.create(resource);
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("The resource was not published", e);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove the resource from IS from curretn scope
|
||||||
|
* @param resource to be unpublished
|
||||||
|
* @throws RegistryNotFoundException if the Registry is not found so the
|
||||||
|
* resource has not be published
|
||||||
|
*/
|
||||||
|
public static void unPublishResource(Resource resource) throws Exception {
|
||||||
|
//StringWriter stringWriter = new StringWriter();
|
||||||
|
//Resources.marshal(resource, stringWriter);
|
||||||
|
|
||||||
|
RegistryPublisher registryPublisher = RegistryPublisherFactory.create();
|
||||||
|
|
||||||
|
String id = resource.id();
|
||||||
|
logger.debug("Trying to remove {} with ID {} from {}", resource.getClass().getSimpleName(), id, getScopeFromToken());
|
||||||
|
|
||||||
|
registryPublisher.remove(resource);
|
||||||
|
|
||||||
|
logger.debug("{} with ID {} removed successfully", resource.getClass().getSimpleName(), id);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -7,9 +7,12 @@ import java.net.URL;
|
||||||
|
|
||||||
import org.codehaus.jackson.JsonNode;
|
import org.codehaus.jackson.JsonNode;
|
||||||
import org.gcube.accounting.datamodel.basetypes.TestUsageRecord;
|
import org.gcube.accounting.datamodel.basetypes.TestUsageRecord;
|
||||||
import org.gcube.common.scope.api.ScopeProvider;
|
import org.gcube.accounting.persistence.TestUtility;
|
||||||
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
||||||
import org.gcube.documentstore.records.Record;
|
import org.gcube.documentstore.records.Record;
|
||||||
|
import org.junit.After;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
@ -22,9 +25,18 @@ public class PersistenceCouchBaseTest {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(PersistenceCouchBaseTest.class);
|
private static final Logger logger = LoggerFactory.getLogger(PersistenceCouchBaseTest.class);
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void before() throws Exception {
|
||||||
|
SecurityTokenProvider.instance.set(TestUtility.TOKEN);
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void after(){
|
||||||
|
SecurityTokenProvider.instance.reset();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testJsonNodeUsageRecordConversions() throws Exception {
|
public void testJsonNodeUsageRecordConversions() throws Exception {
|
||||||
ScopeProvider.instance.set("/gcube/devNext");
|
|
||||||
Record record = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
|
Record record = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
|
||||||
logger.debug("UsageRecord : {}", record.toString());
|
logger.debug("UsageRecord : {}", record.toString());
|
||||||
JsonNode node = PersistenceCouchDB.usageRecordToJsonNode(record);
|
JsonNode node = PersistenceCouchDB.usageRecordToJsonNode(record);
|
||||||
|
@ -36,7 +48,6 @@ public class PersistenceCouchBaseTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testJsonNodeUsageRecordConversionsWithNestedMap() throws Exception {
|
public void testJsonNodeUsageRecordConversionsWithNestedMap() throws Exception {
|
||||||
ScopeProvider.instance.set("/gcube/devNext");
|
|
||||||
Record record = TestUsageRecord.createTestTaskUsageRecordAutomaticScope();
|
Record record = TestUsageRecord.createTestTaskUsageRecordAutomaticScope();
|
||||||
logger.debug("UsageRecord : {}", record.toString());
|
logger.debug("UsageRecord : {}", record.toString());
|
||||||
JsonNode node = PersistenceCouchDB.usageRecordToJsonNode(record);
|
JsonNode node = PersistenceCouchDB.usageRecordToJsonNode(record);
|
||||||
|
@ -46,6 +57,7 @@ public class PersistenceCouchBaseTest {
|
||||||
Assert.assertEquals(0, r.compareTo(record));
|
Assert.assertEquals(0, r.compareTo(record));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//@Test
|
||||||
public void testProxyWithTestConfiguration() throws Exception{
|
public void testProxyWithTestConfiguration() throws Exception{
|
||||||
// Production-Preproduction Nodes
|
// Production-Preproduction Nodes
|
||||||
//URL url = new URL("http://accounting-d4s.d4science.org");
|
//URL url = new URL("http://accounting-d4s.d4science.org");
|
||||||
|
@ -65,7 +77,6 @@ public class PersistenceCouchBaseTest {
|
||||||
PersistenceCouchDB couch = new PersistenceCouchDB();
|
PersistenceCouchDB couch = new PersistenceCouchDB();
|
||||||
couch.prepareConnection(persitenceConfiguration);
|
couch.prepareConnection(persitenceConfiguration);
|
||||||
|
|
||||||
ScopeProvider.instance.set("/gcube/devNext");
|
|
||||||
Record record = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
|
Record record = TestUsageRecord.createTestServiceUsageRecordAutomaticScope();
|
||||||
couch.reallyAccount(record);
|
couch.reallyAccount(record);
|
||||||
|
|
||||||
|
|
Reference in New Issue