2018-12-04 12:06:22 +01:00
package org.gcube.gcat.rest ;
2019-01-10 12:29:47 +01:00
import java.io.BufferedReader ;
import java.io.InputStream ;
import java.io.InputStreamReader ;
2018-12-04 12:06:22 +01:00
import java.util.Iterator ;
2019-01-10 12:29:47 +01:00
import java.util.stream.Collectors ;
2018-12-04 12:06:22 +01:00
import javax.ws.rs.core.MediaType ;
import org.gcube.gcat.ContextTest ;
import org.junit.Test ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import com.fasterxml.jackson.databind.JsonNode ;
import com.fasterxml.jackson.databind.ObjectMapper ;
import com.fasterxml.jackson.databind.node.ArrayNode ;
public class ProfileTest extends ContextTest {
private static Logger logger = LoggerFactory . getLogger ( ProfileTest . class ) ;
@Test
public void list ( ) throws Exception {
Profile profile = new Profile ( ) ;
String ret = profile . list ( ) ;
logger . debug ( " {} " , ret ) ;
}
@Test
public void read ( ) throws Exception {
2019-02-27 13:02:20 +01:00
String profileID = " EmptyProfile " ;
2018-12-04 12:06:22 +01:00
Profile profile = new Profile ( ) ;
String ret = profile . read ( profileID , MediaType . APPLICATION_XML ) ;
logger . debug ( " XML : \ n{} " , ret ) ;
ret = profile . read ( profileID , MediaType . APPLICATION_JSON ) ;
logger . debug ( " JSON : \ n{} " , ret ) ;
}
@Test
public void listRead ( ) throws Exception {
Profile profile = new Profile ( ) ;
String ret = profile . list ( ) ;
ObjectMapper mapper = new ObjectMapper ( ) ;
ArrayNode arrayNode = ( ArrayNode ) mapper . readTree ( ret ) ;
logger . debug ( " Found {} profiles " , arrayNode . size ( ) ) ;
Iterator < JsonNode > iterator = arrayNode . iterator ( ) ;
while ( iterator . hasNext ( ) ) {
String profileID = iterator . next ( ) . asText ( ) ;
ret = profile . read ( profileID , MediaType . APPLICATION_XML ) ;
logger . debug ( " XML : \ n{} " , ret ) ;
ret = profile . read ( profileID , MediaType . APPLICATION_JSON ) ;
logger . debug ( " JSON : \ n{} " , ret ) ;
}
}
2019-02-27 13:02:20 +01:00
@Test
public void testCreateOrUpdate ( ) throws Exception {
String xml = " <metadataformat type= \" TestProfile \" ><metadatafield><fieldName>test</fieldName><mandatory>false</mandatory><dataType>String</dataType><maxOccurs>1</maxOccurs><note>Test Field</note></metadatafield><metadatafield categoryref= \" FSKX_model_scope \" ><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> " ;
Profile profile = new Profile ( ) ;
profile . create ( " TestProfile " , xml ) ;
}
@Test
public void testDelete ( ) throws Exception {
Profile profile = new Profile ( ) ;
profile . delete ( " TestProfile " ) ;
}
2019-01-10 12:29:47 +01:00
public static String PROFILE_EXAMPLE_FILENAME = " EmptyProfileExample.xml " ;
public static String PROFILE_NAME_EXAMPLE = " EmptyProfile " ;
@Test
public void testCreateUpdateDeleteGenericResource ( ) throws Exception {
InputStream inputStream = this . getClass ( ) . getClassLoader ( ) . getResourceAsStream ( PROFILE_EXAMPLE_FILENAME ) ;
String xml = new BufferedReader ( new InputStreamReader ( inputStream ) ) . lines ( )
. collect ( Collectors . joining ( " \ n " ) ) ;
logger . debug ( " Body \ n{} " , xml ) ;
Profile profile = new Profile ( ) ;
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 ) ;
* /
}
2018-12-04 12:06:22 +01:00
}