2019-01-29 17:44:30 +01:00
|
|
|
package org.gcube.gcat.client;
|
|
|
|
|
|
|
|
import java.io.StringReader;
|
2021-02-04 12:13:19 +01:00
|
|
|
import java.net.MalformedURLException;
|
2019-01-29 17:44:30 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2019-05-20 17:20:12 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2019-01-29 17:44:30 +01:00
|
|
|
|
2019-05-20 17:20:12 +02:00
|
|
|
import javax.ws.rs.NotFoundException;
|
2019-01-29 17:44:30 +01:00
|
|
|
import javax.xml.parsers.DocumentBuilder;
|
|
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
|
|
2020-08-06 15:04:01 +02:00
|
|
|
import org.gcube.com.fasterxml.jackson.databind.JavaType;
|
|
|
|
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
|
2019-01-29 17:44:30 +01:00
|
|
|
import org.junit.Test;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.xml.sax.InputSource;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
|
|
*/
|
|
|
|
public class ProfileTest extends ContextTest {
|
|
|
|
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(ProfileTest.class);
|
|
|
|
|
2021-02-04 12:13:19 +01:00
|
|
|
@Test
|
|
|
|
public void testCount() throws MalformedURLException {
|
|
|
|
Profile profile = new Profile();
|
|
|
|
int count = profile.count();
|
|
|
|
logger.debug("The number of profiles is {}", count);
|
|
|
|
}
|
|
|
|
|
2019-01-29 17:44:30 +01:00
|
|
|
@Test
|
2019-05-20 17:20:12 +02:00
|
|
|
public void safeTest() throws Exception {
|
2019-01-29 17:44:30 +01:00
|
|
|
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
|
JavaType arrayType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, String.class);
|
|
|
|
|
|
|
|
Profile profile = new Profile();
|
|
|
|
String profilesString = profile.list();
|
2019-05-20 17:20:12 +02:00
|
|
|
logger.debug("Got Profiles {}\n", profilesString);
|
2019-01-29 17:44:30 +01:00
|
|
|
|
|
|
|
List<String> profiles = mapper.readValue(profilesString, arrayType);
|
|
|
|
for(String name : profiles) {
|
|
|
|
String xml = profile.read(name);
|
|
|
|
logger.debug("Got XML Profile {}", xml);
|
|
|
|
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
|
|
|
|
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
|
|
|
|
InputSource is = new InputSource(new StringReader(xml));
|
|
|
|
dBuilder.parse(is);
|
|
|
|
|
2019-05-20 17:20:12 +02:00
|
|
|
String retAsXML = profile.read(name, false);
|
2019-01-29 17:44:30 +01:00
|
|
|
logger.debug("Got XML (explicit) Profile {}", retAsXML);
|
2019-05-20 17:20:12 +02:00
|
|
|
InputSource is2 = new InputSource(new StringReader(retAsXML));
|
|
|
|
dBuilder.parse(is2);
|
2019-01-29 17:44:30 +01:00
|
|
|
|
2019-05-20 17:20:12 +02:00
|
|
|
//Assert.assertTrue(xml.compareTo(retAsXML) == 0);
|
2019-01-29 17:44:30 +01:00
|
|
|
|
|
|
|
String json = profile.read(name, true);
|
2019-05-20 17:20:12 +02:00
|
|
|
logger.debug("Got JSON Profile {}\n", json);
|
2019-01-29 17:44:30 +01:00
|
|
|
mapper.readTree(json);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-20 17:20:12 +02:00
|
|
|
public static final String PROFILE_NAME = "TestEmptyProfile";
|
|
|
|
public static final String PROFILE_XML = "<metadataformat type=\"%s\"><metadatafield><fieldName>test</fieldName><mandatory>false</mandatory><dataType>String</dataType><maxOccurs>1</maxOccurs><note>Test Field</note></metadatafield></metadataformat>";
|
|
|
|
|
|
|
|
// @Test
|
|
|
|
public void delete() throws Exception {
|
|
|
|
Profile profile = new Profile();
|
|
|
|
profile.delete(PROFILE_NAME);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void list() throws Exception {
|
|
|
|
Profile profile = new Profile();
|
|
|
|
String list = profile.list();
|
|
|
|
logger.debug("Got Profiles {}\n", list);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void createDeleteTest() throws Exception {
|
|
|
|
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
|
JavaType arrayType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, String.class);
|
|
|
|
|
|
|
|
Profile profile = new Profile();
|
|
|
|
profile.create(PROFILE_NAME, String.format(PROFILE_XML, PROFILE_NAME));
|
|
|
|
|
2021-12-21 14:10:06 +01:00
|
|
|
Thread.sleep(TimeUnit.SECONDS.toMillis(5));
|
2019-05-20 17:20:12 +02:00
|
|
|
String list = profile.list();
|
|
|
|
logger.debug("Got Profiles {}\n", list);
|
|
|
|
|
|
|
|
boolean found = false;
|
|
|
|
int count = 1;
|
|
|
|
|
|
|
|
while(!found) {
|
|
|
|
List<String> profiles = mapper.readValue(list, arrayType);
|
|
|
|
if(profiles.contains(PROFILE_NAME)) {
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
if(count >= 5) {
|
|
|
|
throw new NotFoundException(String.format("%s not found after %d retries", PROFILE_NAME, count));
|
|
|
|
}
|
|
|
|
if(!found) {
|
2021-12-21 13:56:35 +01:00
|
|
|
Thread.sleep(TimeUnit.SECONDS.toMillis(5*count));
|
2019-05-20 17:20:12 +02:00
|
|
|
++count;
|
|
|
|
}
|
2021-12-21 14:10:06 +01:00
|
|
|
|
|
|
|
list = profile.list();
|
|
|
|
|
2019-05-20 17:20:12 +02:00
|
|
|
}
|
|
|
|
|
2019-09-20 15:28:27 +02:00
|
|
|
profile.read(PROFILE_NAME);
|
2019-05-20 17:20:12 +02:00
|
|
|
|
|
|
|
profile.delete(PROFILE_NAME);
|
|
|
|
|
2021-12-21 14:10:06 +01:00
|
|
|
Thread.sleep(TimeUnit.SECONDS.toMillis(5));
|
|
|
|
list = profile.list();
|
|
|
|
|
2019-05-20 17:20:12 +02:00
|
|
|
while(found) {
|
|
|
|
List<String> profiles = mapper.readValue(list, arrayType);
|
|
|
|
if(!profiles.contains(PROFILE_NAME)) {
|
|
|
|
found = false;
|
|
|
|
}
|
|
|
|
if(count >= 5) {
|
|
|
|
throw new Exception(String.format("%s still found after %d retries", PROFILE_NAME, count));
|
|
|
|
}
|
|
|
|
if(found) {
|
2021-12-21 13:56:35 +01:00
|
|
|
Thread.sleep(TimeUnit.SECONDS.toMillis(5*count));
|
2019-05-20 17:20:12 +02:00
|
|
|
++count;
|
|
|
|
}
|
2021-12-21 14:10:06 +01:00
|
|
|
|
|
|
|
list = profile.list();
|
|
|
|
|
2019-05-20 17:20:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-01-29 17:44:30 +01:00
|
|
|
}
|