You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gcube-cms-suite/geoportal-client/src/test/java/org/gcube/application/geoportal/clients/StatelessClientTests.java

202 lines
6.5 KiB
Java

package org.gcube.application.geoportal.clients;
import static org.gcube.application.geoportal.client.GeoportalAbstractPlugin.mongoConcessioni;
import static org.junit.Assert.*;
import static org.junit.Assume.assumeTrue;
import java.io.File;
import java.io.FileInputStream;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.Iterator;
import java.util.concurrent.atomic.AtomicLong;
import org.gcube.application.cms.tests.model.TestFilters;
import org.gcube.application.cms.tests.model.TestModel;
import org.gcube.application.cms.tests.model.TestQueries;
import org.gcube.application.geoportal.client.utils.Queries;
import org.gcube.application.geoportal.common.model.legacy.Concessione;
import org.gcube.application.geoportal.common.model.legacy.Concessione.Paths;
import org.gcube.application.geoportal.common.model.legacy.report.ValidationReport.ValidationStatus;
import org.gcube.application.geoportal.common.model.rest.AddSectionToConcessioneRequest;
import org.gcube.application.geoportal.common.rest.MongoConcessioni;
import org.gcube.application.geoportal.common.utils.FileSets;
import org.gcube.application.geoportal.common.utils.Files;
import org.gcube.application.geoportal.common.utils.StorageUtils;
import org.junit.Test;
public class StatelessClientTests extends BasicVreTests{
private MongoConcessioni client=mongoConcessioni().build();
@Test
public void getConfiguration() throws Exception {
System.out.println(client.getCurrentConfiguration());
}
@Test
public void searches() throws Exception {
TestFilters.filters.forEach((k,v)->{
try {
System.out.println("Count for " + k + "\t" + count(client.search(v)));
}catch (Exception e){
throw new RuntimeException(e);
}
});
}
@Test
public void query() throws Exception {
// No Transformation
System.out.print("First Registered \t");
Iterator<Concessione> queriedDocuments=client.query(
Queries.parse(TestQueries.queries.get("firstRegistered.json")));
// Expected one result
assertTrue(count(queriedDocuments)==1);
System.out.print("Last Registered \t");
// Expected one result
queriedDocuments=client.query(
Queries.parse(TestQueries.queries.get("lastRegistered.json")));
assertTrue(count(queriedDocuments)==1);
queriedDocuments.forEachRemaining((Concessione c)->{System.out.println(c.getNome());});
// Transformations
System.out.println(
client.queryForJSON(
Queries.parse(TestQueries.queries.get("lastNameRegisteredByFabio.json"))));
System.out.println(
client.queryForJSON(
Queries.parse(TestQueries.queries.get("publicationWarningMessages.json"))));
// String query=Files.readFileAsString(filterFile.getAbsolutePath(), Charset.defaultCharset());
// System.out.println("Count for "+filterFile.getName()+"\t"+ count(client.search(query)));
// }
}
@Test
public void testList() throws Exception {
final AtomicLong counter=new AtomicLong();
long before=System.currentTimeMillis();
client.getList().forEachRemaining((Concessione c)-> {counter.addAndGet(1);});
System.out.println("Loaded "+counter+" in "+(System.currentTimeMillis()-before)+" ms");
}
@Test
public void testCreateNew() throws Exception {
Concessione c= client.createNew(TestModel.prepareEmptyConcessione());
assertNotNull(c);
assertNotNull(c.getMongo_id());
}
@Test
public void testReplace() throws Exception {
Concessione testObject= client.createNew(TestModel.prepareEmptyConcessione());
String title="My new shiny Title";
testObject.setNome(title);
Concessione c1=client.replace(testObject);
assertEquals(title, c1.getNome());
}
@Test
public void testUploadFileSet() throws Exception {
Concessione testObject= client.createNew(TestModel.prepareConcessione());
AddSectionToConcessioneRequest request=
// FileSets.prepareRequest(new StorageUtils(),Paths.RELAZIONE,new File(TestModel.getBaseFolder(),"relazione.pdf"));
FileSets.build(Paths.ABSTRACT_RELAZIONE).add(
new StorageUtils().putOntoStorage(new File(TestModel.getBaseFolder(),"relazione.pdf"),"San Mauro_drone totale.JPG"))
.getTheRequest();
testObject= client.registerFileSet(testObject.getMongo_id(), request);
assertNotNull(testObject.getContentByPath(Paths.ABSTRACT_RELAZIONE).getActualContent());
assertNotNull(testObject.getContentByPath(Paths.ABSTRACT_RELAZIONE).getActualContent().get(0));
}
@Test
public void testDeleteById() throws Exception {
Concessione c= client.createNew(TestModel.prepareEmptyConcessione());
client.deleteById(c.getMongo_id());
}
@Test
public void testPublsh() throws Exception {
Concessione c=prepare();
assertTrue(c.getReport().getStatus().equals(ValidationStatus.PASSED));
}
@Test
public void testCleanFileSet() throws Exception {
Concessione testObject=prepare();
//Precheck to be sure
assertFalse(testObject.getPosizionamentoScavo().getActualContent().isEmpty());
assertFalse(testObject.getPianteFineScavo().get(0).getActualContent().isEmpty());
client.unPublish(testObject.getMongo_id());
//Clear pos
testObject=client.cleanFileSet(testObject.getMongo_id(),Paths.POSIZIONAMENTO);
assertTrue(testObject.getPosizionamentoScavo().getActualContent().isEmpty());
//Clear pianta [0]
testObject =client.cleanFileSet(testObject.getMongo_id(),Paths.piantaByIndex(0));
assertTrue(testObject.getPianteFineScavo().get(0).getActualContent().isEmpty());
}
private Concessione prepare() throws Exception {
int numImgs=5;
Concessione c= client.createNew(TestModel.prepareConcessione(1,numImgs));
StorageUtils storage = new StorageUtils();
String mongoId=c.getMongo_id();
client.registerFileSet(mongoId,
FileSets.prepareRequest(storage,Paths.RELAZIONE,new File(TestModel.getBaseFolder(),"relazione.pdf")));
client.registerFileSet(mongoId,
FileSets.prepareRequest(storage,Paths.RELAZIONE,new File(TestModel.getBaseFolder(),"relazione.pdf")));
for(int i=0;i<numImgs;i++)
client.registerFileSet(mongoId,
FileSets.build(Paths.imgByIndex(i)).
add(storage.putOntoStorage(new File(TestModel.getBaseFolder(),"immagine.png"),
i+"San Mauro_drone totale.JPG")).getTheRequest());
client.registerFileSet(mongoId,
FileSets.prepareRequest(storage,Paths.POSIZIONAMENTO,new File(TestModel.getBaseFolder(),"pos.shp")));
client.registerFileSet(mongoId,
FileSets.prepareRequest(storage,Paths.piantaByIndex(0),new File(TestModel.getBaseFolder(),"pianta.shp")));
c=client.publish(mongoId);
return c;
}
// UTILS
public static long count(Iterator<?> iterator){
AtomicLong l=new AtomicLong(0);
iterator.forEachRemaining(el->{l.incrementAndGet();});
return l.get();
}
}