2019-02-27 13:02:20 +01:00
package org.gcube.gcat.profile ;
2023-03-14 11:58:52 +01:00
import java.io.File ;
import java.io.StringReader ;
import java.net.URL ;
import java.nio.file.Files ;
2019-02-27 13:02:20 +01:00
import java.util.Iterator ;
2024-01-17 14:53:10 +01:00
import java.util.regex.Pattern ;
2019-02-27 13:02:20 +01:00
import java.util.stream.Collectors ;
2023-03-14 11:58:52 +01:00
import java.util.stream.Stream ;
2019-02-27 13:02:20 +01:00
2023-03-14 11:58:52 +01:00
import javax.xml.XMLConstants ;
import javax.xml.parsers.DocumentBuilder ;
import javax.xml.parsers.DocumentBuilderFactory ;
import javax.xml.transform.dom.DOMSource ;
import javax.xml.validation.Schema ;
import javax.xml.validation.SchemaFactory ;
import javax.xml.validation.Validator ;
import org.gcube.com.fasterxml.jackson.databind.JsonNode ;
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode ;
2019-02-27 13:02:20 +01:00
import org.gcube.gcat.ContextTest ;
import org.junit.Test ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2023-03-14 11:58:52 +01:00
import org.w3c.dom.Document ;
import org.xml.sax.InputSource ;
2019-02-27 13:02:20 +01:00
public class ProfileTest extends ContextTest {
private static Logger logger = LoggerFactory . getLogger ( ProfileTest . class ) ;
2021-02-03 21:55:32 +01:00
@Test
public void count ( ) throws Exception {
ISProfile profile = new ISProfile ( ) ;
int count = profile . count ( ) ;
logger . debug ( " We have {} types " , count ) ;
}
2019-02-27 13:02:20 +01:00
@Test
public void list ( ) throws Exception {
ISProfile profile = new ISProfile ( ) ;
ArrayNode arrayNode = profile . list ( ) ;
logger . debug ( " {} " , arrayNode ) ;
}
@Test
public void read ( ) throws Exception {
String profileID = " EmptyProfile " ;
ISProfile profile = new ISProfile ( ) ;
boolean xml = true ;
String ret = profile . read ( profileID , xml ) ;
logger . debug ( " XML : \ n{} " , ret ) ;
xml = false ;
ret = profile . read ( profileID , xml ) ;
logger . debug ( " JSON : \ n{} " , ret ) ;
}
@Test
public void listRead ( ) throws Exception {
ISProfile profile = new ISProfile ( ) ;
ArrayNode arrayNode = profile . list ( ) ;
logger . debug ( " Found {} profiles " , arrayNode . size ( ) ) ;
Iterator < JsonNode > iterator = arrayNode . iterator ( ) ;
while ( iterator . hasNext ( ) ) {
String profileID = iterator . next ( ) . asText ( ) ;
boolean xml = true ;
String ret = profile . read ( profileID , xml ) ;
logger . debug ( " XML : \ n{} " , ret ) ;
xml = false ;
ret = profile . read ( profileID , xml ) ;
logger . debug ( " JSON : \ n{} " , ret ) ;
}
}
@Test
public void testCreateOrUpdate ( ) throws Exception {
2019-02-27 17:36:09 +01:00
String xml = " <metadataformat type= \" TestProfile \" ><metadatafield><fieldName>test</fieldName><mandatory>false</mandatory><dataType>String</dataType><maxOccurs>1</maxOccurs><note>Test Field</note></metadatafield><metadatafield><fieldName>Population</fieldName><mandatory>false</mandatory><dataType>String</dataType><maxOccurs>*</maxOccurs><defaultValue/><note>The population of the model</note><tagging create= \" true \" separator= \" _ \" >onValue</tagging><grouping create= \" true \" >onFieldName_onValue</grouping></metadatafield></metadataformat> " ;
2019-02-27 13:02:20 +01:00
ISProfile profile = new ISProfile ( ) ;
profile . createOrUpdate ( " TestProfile " , xml ) ;
}
@Test
public void testDelete ( ) throws Exception {
ISProfile profile = new ISProfile ( ) ;
profile . delete ( " TestProfile " ) ;
}
2023-03-14 11:58:52 +01:00
public File getResourcesDirectory ( ) throws Exception {
URL logbackFileURL = this . getClass ( ) . getClassLoader ( ) . getResource ( " logback-test.xml " ) ;
File logbackFile = new File ( logbackFileURL . toURI ( ) ) ;
File resourcesDirectory = logbackFile . getParentFile ( ) ;
return resourcesDirectory ;
}
public File getProfilesDirectory ( ) throws Exception {
File resourcesDirectory = getResourcesDirectory ( ) ;
return new File ( resourcesDirectory , " profile_examples " ) ;
}
public File getSchemaDirectory ( ) throws Exception {
File resourcesDirectory = getResourcesDirectory ( ) ;
return new File ( resourcesDirectory , " profile_schemas " ) ;
}
2019-02-27 13:02:20 +01:00
public static String PROFILE_NAME_EXAMPLE = " EmptyProfile " ;
2023-03-14 11:58:52 +01:00
public static String PROFILE_EXAMPLE_FILENAME = PROFILE_NAME_EXAMPLE + " .xml " ;
public String fileToString ( File file ) throws Exception {
try ( Stream < String > lines = Files . lines ( file . toPath ( ) ) ) {
String content = lines . collect ( Collectors . joining ( System . lineSeparator ( ) ) ) ;
return content ;
} catch ( Exception e ) {
throw e ;
}
}
2019-02-27 13:02:20 +01:00
@Test
public void testCreateUpdateDeleteGenericResource ( ) throws Exception {
2023-03-14 11:58:52 +01:00
File profilesDirectory = getProfilesDirectory ( ) ;
File profileFile = new File ( profilesDirectory , PROFILE_EXAMPLE_FILENAME ) ;
String xml = fileToString ( profileFile ) ;
2019-02-27 13:02:20 +01:00
ISProfile profile = new ISProfile ( ) ;
profile . createOrUpdate ( PROFILE_NAME_EXAMPLE , xml ) ;
/ *
Thread . sleep ( TimeUnit . SECONDS . toMillis ( 30 ) ) ;
profile . createOrUpdate ( PROFILE_NAME_EXAMPLE , " <metadataformat type= \" " + PROFILE_NAME_EXAMPLE + " \" /> " ) ;
Thread . sleep ( TimeUnit . SECONDS . toMillis ( 30 ) ) ;
profile . delete ( PROFILE_NAME_EXAMPLE ) ;
* /
}
2023-03-14 11:58:52 +01:00
protected File getSchemaFile ( ) throws Exception {
File schemaDirectory = getSchemaDirectory ( ) ;
return new File ( schemaDirectory , " profile5.xsd " ) ;
}
@Test
public void validateAgainstXSD ( ) throws Exception {
File profilesDirectory = getProfilesDirectory ( ) ;
File schemaFile = getSchemaFile ( ) ;
SchemaFactory factory = SchemaFactory . newInstance ( XMLConstants . W3C_XML_SCHEMA_NS_URI ) ;
Schema schema = factory . newSchema ( schemaFile . toURI ( ) . toURL ( ) ) ;
Validator validator = schema . newValidator ( ) ;
for ( File profileFile : profilesDirectory . listFiles ( ) ) {
String xmlString = fileToString ( profileFile ) ;
DocumentBuilder db = DocumentBuilderFactory . newInstance ( ) . newDocumentBuilder ( ) ;
Document doc = db . parse ( new InputSource ( new StringReader ( xmlString ) ) ) ;
DOMSource xml = new DOMSource ( doc ) ;
try {
validator . validate ( xml ) ;
logger . info ( " File {} has been successfuly validated against schema. " , profileFile . getName ( ) ) ;
} catch ( Exception e ) {
logger . error ( " Error while validating file {} " , profileFile . getName ( ) ) ;
throw e ;
}
}
}
2024-01-17 14:53:10 +01:00
@Test
public void testRegex ( ) throws Exception {
File resDirectory = getResourcesDirectory ( ) ;
File regexFile = new File ( resDirectory , " regex.txt " ) ;
String regex = fileToString ( regexFile ) ;
Pattern . compile ( regex ) ;
}
2019-02-27 13:02:20 +01:00
}