2021-09-20 16:47:35 +02:00
|
|
|
package org.gcube.application.geoportal.clients;
|
|
|
|
|
|
|
|
import static org.gcube.application.geoportal.client.GeoportalAbstractPlugin.mongoConcessioni;
|
|
|
|
import static org.junit.Assert.*;
|
2021-09-27 15:23:03 +02:00
|
|
|
import static org.junit.Assume.assumeTrue;
|
2021-09-20 16:47:35 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2021-12-01 11:13:34 +01:00
|
|
|
import org.bson.Document;
|
2021-09-21 17:45:18 +02:00
|
|
|
import org.gcube.application.cms.tests.model.TestFilters;
|
2021-09-20 18:11:51 +02:00
|
|
|
import org.gcube.application.cms.tests.model.TestModel;
|
2021-09-21 17:45:18 +02:00
|
|
|
import org.gcube.application.cms.tests.model.TestQueries;
|
2021-09-20 16:47:35 +02:00
|
|
|
import org.gcube.application.geoportal.client.utils.Queries;
|
2021-11-12 16:41:25 +01:00
|
|
|
import org.gcube.application.geoportal.client.utils.Serialization;
|
2021-10-12 15:43:35 +02:00
|
|
|
import org.gcube.application.geoportal.common.model.legacy.*;
|
2021-09-20 16:47:35 +02:00
|
|
|
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;
|
2021-12-01 11:13:34 +01:00
|
|
|
import org.gcube.application.geoportal.common.model.rest.QueryRequest;
|
2021-09-20 16:47:35 +02:00
|
|
|
import org.gcube.application.geoportal.common.rest.MongoConcessioni;
|
2021-09-21 17:45:18 +02:00
|
|
|
import org.gcube.application.geoportal.common.utils.FileSets;
|
2021-09-20 16:47:35 +02:00
|
|
|
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 {
|
2021-09-21 17:45:18 +02:00
|
|
|
TestFilters.filters.forEach((k,v)->{
|
|
|
|
try {
|
|
|
|
System.out.println("Count for " + k + "\t" + count(client.search(v)));
|
|
|
|
}catch (Exception e){
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
});
|
2021-12-01 11:13:34 +01:00
|
|
|
|
|
|
|
|
2021-09-20 16:47:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void query() throws Exception {
|
2021-12-01 11:13:34 +01:00
|
|
|
QueryRequest req=new QueryRequest();
|
|
|
|
req.setFilter(Document.parse("{ \"nome\" : { \"$eq\" : \"mock\" } }"));
|
|
|
|
|
|
|
|
System.out.println("FRA "+ count(client.query(req)));
|
|
|
|
|
2021-09-20 16:47:35 +02:00
|
|
|
// No Transformation
|
|
|
|
System.out.print("First Registered \t");
|
|
|
|
Iterator<Concessione> queriedDocuments=client.query(
|
2021-09-21 17:45:18 +02:00
|
|
|
Queries.parse(TestQueries.queries.get("firstRegistered.json")));
|
2021-09-20 16:47:35 +02:00
|
|
|
// Expected one result
|
|
|
|
assertTrue(count(queriedDocuments)==1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
System.out.print("Last Registered \t");
|
|
|
|
// Expected one result
|
|
|
|
queriedDocuments=client.query(
|
2021-09-21 17:45:18 +02:00
|
|
|
Queries.parse(TestQueries.queries.get("lastRegistered.json")));
|
2021-09-20 16:47:35 +02:00
|
|
|
assertTrue(count(queriedDocuments)==1);
|
|
|
|
|
|
|
|
queriedDocuments.forEachRemaining((Concessione c)->{System.out.println(c.getNome());});
|
|
|
|
|
|
|
|
|
|
|
|
// Transformations
|
|
|
|
System.out.println(
|
|
|
|
client.queryForJSON(
|
2021-09-21 17:45:18 +02:00
|
|
|
Queries.parse(TestQueries.queries.get("lastNameRegisteredByFabio.json"))));
|
2021-09-20 16:47:35 +02:00
|
|
|
|
|
|
|
System.out.println(
|
|
|
|
client.queryForJSON(
|
2021-09-21 17:45:18 +02:00
|
|
|
Queries.parse(TestQueries.queries.get("publicationWarningMessages.json"))));
|
2021-09-20 16:47:35 +02:00
|
|
|
|
|
|
|
// String query=Files.readFileAsString(filterFile.getAbsolutePath(), Charset.defaultCharset());
|
|
|
|
// System.out.println("Count for "+filterFile.getName()+"\t"+ count(client.search(query)));
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2021-09-27 15:23:03 +02:00
|
|
|
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 {
|
2021-10-01 17:42:23 +02:00
|
|
|
Concessione testObject= client.createNew(TestModel.prepareConcessione());
|
2021-09-27 15:23:03 +02:00
|
|
|
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);
|
2021-09-20 16:47:35 +02:00
|
|
|
|
2021-09-27 15:23:03 +02:00
|
|
|
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
|
2021-11-12 16:41:25 +01:00
|
|
|
public void testPublish() throws Exception {
|
2021-10-01 17:42:23 +02:00
|
|
|
Concessione c=prepare();
|
2021-09-27 15:23:03 +02:00
|
|
|
|
|
|
|
assertTrue(c.getReport().getStatus().equals(ValidationStatus.PASSED));
|
|
|
|
|
2021-10-01 17:42:23 +02:00
|
|
|
|
2021-09-27 15:23:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testCleanFileSet() throws Exception {
|
2021-09-20 16:47:35 +02:00
|
|
|
|
2021-10-01 17:42:23 +02:00
|
|
|
Concessione testObject=prepare();
|
2021-10-12 15:43:35 +02:00
|
|
|
System.out.println("Object is "+testObject.getPosizionamentoScavo());
|
2021-09-20 16:47:35 +02:00
|
|
|
//Precheck to be sure
|
2021-09-27 15:23:03 +02:00
|
|
|
assertFalse(testObject.getPosizionamentoScavo().getActualContent().isEmpty());
|
|
|
|
assertFalse(testObject.getPianteFineScavo().get(0).getActualContent().isEmpty());
|
2021-09-20 16:47:35 +02:00
|
|
|
|
2021-10-01 17:42:23 +02:00
|
|
|
client.unPublish(testObject.getMongo_id());
|
|
|
|
|
2021-10-12 15:43:35 +02:00
|
|
|
// check unpublish
|
|
|
|
testObject= client.getById(testObject.getMongo_id());
|
|
|
|
for(LayerConcessione l:testObject.getPianteFineScavo())
|
|
|
|
for(PersistedContent pc: l.getActualContent())
|
|
|
|
if(pc instanceof GeoServerContent) throw new Exception ("Concessione not properly unpublished");
|
|
|
|
|
|
|
|
for(PersistedContent pc: testObject.getPosizionamentoScavo().getActualContent())
|
|
|
|
if(pc instanceof GeoServerContent) throw new Exception ("Concessione not properly unpublished");
|
|
|
|
|
|
|
|
|
2021-09-20 16:47:35 +02:00
|
|
|
//Clear pos
|
2021-09-27 15:23:03 +02:00
|
|
|
testObject=client.cleanFileSet(testObject.getMongo_id(),Paths.POSIZIONAMENTO);
|
|
|
|
assertTrue(testObject.getPosizionamentoScavo().getActualContent().isEmpty());
|
2021-09-20 16:47:35 +02:00
|
|
|
|
|
|
|
//Clear pianta [0]
|
2021-09-27 15:23:03 +02:00
|
|
|
testObject =client.cleanFileSet(testObject.getMongo_id(),Paths.piantaByIndex(0));
|
|
|
|
assertTrue(testObject.getPianteFineScavo().get(0).getActualContent().isEmpty());
|
2021-09-20 16:47:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-10-01 17:42:23 +02:00
|
|
|
private Concessione prepare() throws Exception {
|
2021-10-12 15:43:35 +02:00
|
|
|
int numImgs=1;
|
2021-10-01 17:42:23 +02:00
|
|
|
Concessione c= client.createNew(TestModel.prepareConcessione(1,numImgs));
|
|
|
|
StorageUtils storage = new StorageUtils();
|
|
|
|
|
2021-11-12 16:41:25 +01:00
|
|
|
c.getRelazioneScavo().setPolicy(AccessPolicy.EMBARGOED);
|
2021-10-01 17:42:23 +02:00
|
|
|
String mongoId=c.getMongo_id();
|
2021-11-12 16:41:25 +01:00
|
|
|
c=client.update(mongoId, Serialization.write(c));
|
2021-10-01 17:42:23 +02:00
|
|
|
|
2021-11-12 16:41:25 +01:00
|
|
|
assertEquals(AccessPolicy.EMBARGOED,c.getRelazioneScavo().getPolicy());
|
2021-10-01 17:42:23 +02:00
|
|
|
|
2021-11-12 16:41:25 +01:00
|
|
|
c=client.registerFileSet(mongoId,
|
2021-10-01 17:42:23 +02:00
|
|
|
FileSets.prepareRequest(storage,Paths.RELAZIONE,new File(TestModel.getBaseFolder(),"relazione.pdf")));
|
|
|
|
|
2021-11-12 16:41:25 +01:00
|
|
|
assertEquals(AccessPolicy.EMBARGOED,c.getRelazioneScavo().getPolicy());
|
|
|
|
|
|
|
|
c=client.registerFileSet(mongoId,
|
|
|
|
FileSets.prepareRequest(storage,Paths.ABSTRACT_RELAZIONE,new File(TestModel.getBaseFolder(),"relazione.pdf")));
|
2021-10-12 15:43:35 +02:00
|
|
|
|
2021-11-12 16:41:25 +01:00
|
|
|
assertEquals(AccessPolicy.EMBARGOED,c.getRelazioneScavo().getPolicy());
|
2021-10-12 15:43:35 +02:00
|
|
|
|
2021-10-01 17:42:23 +02:00
|
|
|
for(int i=0;i<numImgs;i++)
|
2021-11-12 16:41:25 +01:00
|
|
|
c=client.registerFileSet(mongoId,
|
2021-10-01 17:42:23 +02:00
|
|
|
FileSets.build(Paths.imgByIndex(i)).
|
2021-10-12 15:43:35 +02:00
|
|
|
add(storage.putOntoStorage(new File(TestModel.getBaseFolder(),"immagine"+(i+1)+".png"),
|
2021-10-01 17:42:23 +02:00
|
|
|
i+"San Mauro_drone totale.JPG")).getTheRequest());
|
|
|
|
|
2021-11-12 16:41:25 +01:00
|
|
|
assertEquals(AccessPolicy.EMBARGOED,c.getRelazioneScavo().getPolicy());
|
2021-10-01 17:42:23 +02:00
|
|
|
|
2021-11-12 16:41:25 +01:00
|
|
|
c=client.registerFileSet(mongoId,
|
2021-10-01 17:42:23 +02:00
|
|
|
FileSets.prepareRequest(storage,Paths.POSIZIONAMENTO,new File(TestModel.getBaseFolder(),"pos.shp")));
|
|
|
|
|
2021-11-12 16:41:25 +01:00
|
|
|
assertEquals(AccessPolicy.EMBARGOED,c.getRelazioneScavo().getPolicy());
|
|
|
|
|
|
|
|
c=client.registerFileSet(mongoId,
|
2021-10-01 17:42:23 +02:00
|
|
|
FileSets.prepareRequest(storage,Paths.piantaByIndex(0),new File(TestModel.getBaseFolder(),"pianta.shp")));
|
|
|
|
|
2021-11-12 16:41:25 +01:00
|
|
|
assertEquals(AccessPolicy.EMBARGOED,c.getRelazioneScavo().getPolicy());
|
2021-10-01 17:42:23 +02:00
|
|
|
|
|
|
|
c=client.publish(mongoId);
|
2021-11-12 16:41:25 +01:00
|
|
|
assertEquals(AccessPolicy.EMBARGOED,c.getRelazioneScavo().getPolicy());
|
|
|
|
|
2021-10-01 17:42:23 +02:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2021-09-20 16:47:35 +02:00
|
|
|
// UTILS
|
|
|
|
|
|
|
|
public static long count(Iterator<?> iterator){
|
|
|
|
AtomicLong l=new AtomicLong(0);
|
|
|
|
iterator.forEachRemaining(el->{l.incrementAndGet();});
|
|
|
|
return l.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|