gFeed/test-commons/src/main/java/org/gcube/data/publishing/gCatFeeder/tests/BaseCollectorTest.java

87 lines
3.1 KiB
Java

package org.gcube.data.publishing.gCatFeeder.tests;
import java.io.IOException;
import java.util.Properties;
import java.util.ServiceLoader;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.data.publishing.gCatfeeder.collectors.CollectorPlugin;
import org.gcube.data.publishing.gCatfeeder.collectors.model.CustomData;
import org.gcube.data.publishing.gCatfeeder.collectors.model.PluginDescriptor;
import org.gcube.data.publishing.gCatfeeder.collectors.model.faults.CatalogueNotSupportedException;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class BaseCollectorTest {
private static String testContext=null;
private static final Logger log= LoggerFactory.getLogger(BaseCollectorTest.class);
private static ServiceLoader<CollectorPlugin> collectorPluginsLoader = ServiceLoader.load(CollectorPlugin.class);
@BeforeClass
public static void checkPluginRegistration() {
int pluginsCounter=0;
for(CollectorPlugin pl:collectorPluginsLoader) {
pluginsCounter++;
}
Assert.assertFalse("No plugin is actually registered ",pluginsCounter==0);
for(CollectorPlugin<? extends CustomData> plugin:collectorPluginsLoader) {
PluginDescriptor desc=plugin.getDescriptor();
Assert.assertNotNull(plugin.getClass()+" No Descriptor exposed", desc);
try{
plugin.init();
}catch(Throwable t) {
Assert.fail("Unable to init plugin "+desc.getName());
}
Assert.assertTrue(desc.getName()+": No catalogues supported",plugin.getSupportedCatalogueTypes().size()>0);
for(String supportedCatalogue:plugin.getSupportedCatalogueTypes()) {
try{
Assert.assertNotNull(desc.getName()+": Null configuration for exposed "+supportedCatalogue,plugin.getPublisherControllerConfiguration(supportedCatalogue));
Assert.assertNotNull(desc.getName()+": Null retriever for exposed "+supportedCatalogue,plugin.getRetrieverByCatalogueType(supportedCatalogue));
Assert.assertNotNull(desc.getName()+": Null transformer for exposed "+supportedCatalogue,plugin.getTransformerByCatalogueType(supportedCatalogue));
}catch(CatalogueNotSupportedException e) {
Assert.fail("Exposed supported catalogue actually not covered."+e.getMessage());
}
}
Assert.assertNotNull(desc.getName()+": No actual collector ",plugin.getCollector());
}
// Check if instrastructure is enabled
testContext=System.getProperty("testContext");
System.out.println("TEST CONTEXT = "+testContext);
if(isTestInfrastructureEnabled()) {
Properties props=new Properties();
try{
props.load(BaseCollectorTest.class.getResourceAsStream("/tokens.properties"));
}catch(IOException e) {throw new RuntimeException(e);}
if(!props.containsKey(testContext)) throw new RuntimeException("No token found for scope : "+testContext);
SecurityTokenProvider.instance.set(props.getProperty(testContext));
ScopeProvider.instance.set(testContext);
}
}
protected static boolean isTestInfrastructureEnabled() {
return testContext!=null;
}
}