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-service/src/test/java/org/gcube/application/geoportal/service/ConcessioniOverMongoTest.java

373 lines
14 KiB
Java

package org.gcube.application.geoportal.service;
import org.gcube.application.cms.tests.TokenSetter;
import org.gcube.application.cms.tests.model.TestFilters;
import org.gcube.application.cms.tests.model.TestQueries;
import org.gcube.application.cms.tests.model.TestModel;
import org.gcube.application.geoportal.common.model.legacy.AccessPolicy;
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.LayerConcessione;
import org.gcube.application.geoportal.common.model.legacy.report.ValidationReport.ValidationStatus;
import org.gcube.application.geoportal.common.rest.InterfaceConstants;
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.gcube.application.geoportal.service.utils.Serialization;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import static org.junit.Assert.*;
public class ConcessioniOverMongoTest extends BasicServiceTestUnit{
private static final String PATH=InterfaceConstants.Methods.MONGO_CONCESSIONI;
private static final String PUBLISH_PATH=InterfaceConstants.Methods.PUBLISH_PATH;
private static final String FILES_PATH=InterfaceConstants.Methods.REGISTER_FILES_PATH;
@Before
public void setContext(){
TokenSetter.set(scope);
}
//Used for local test data
private static Concessione upload(StorageUtils storage, WebTarget target,String id, String path, String ...files) throws Exception {
FileSets.RequestBuilder builder = FileSets.build(path);
for(String file:files)
builder.add(storage.putOntoStorage(new File(TestModel.getBaseFolder(),file),file));
return check(target.path(FILES_PATH).path(id).request(MediaType.APPLICATION_JSON).
post(Entity.entity(Serialization.write(builder.getTheRequest()),
MediaType.APPLICATION_JSON)),Concessione.class);
}
//generic
private static Concessione upload(StorageUtils storage, WebTarget target,String id, String path, File ...files) throws Exception {
FileSets.RequestBuilder builder = FileSets.build(path);
for(File file:files)
builder.add(storage.putOntoStorage(file,file.getName()));
return check(target.path(FILES_PATH).path(id).request(MediaType.APPLICATION_JSON).
post(Entity.entity(Serialization.write(builder.getTheRequest()),
MediaType.APPLICATION_JSON)),Concessione.class);
}
private static Concessione update(Concessione c, WebTarget target) throws Exception {
return check(target.path(c.getMongo_id()).request(MediaType.APPLICATION_JSON).
put(Entity.entity(Serialization.write(c),
MediaType.APPLICATION_JSON)),Concessione.class);
}
private static Concessione publish(WebTarget target, String id) throws Exception {
Response resp=target.path(PUBLISH_PATH).path(id).request(MediaType.APPLICATION_JSON).
put(Entity.entity(Serialization.write(id), MediaType.APPLICATION_JSON));
return check(resp,Concessione.class);
}
private static Concessione unpublish(WebTarget target, String id) throws Exception {
Response resp=target.path(PUBLISH_PATH).path(id).request(MediaType.APPLICATION_JSON).
delete();
return check(resp,Concessione.class);
}
private static Concessione register(WebTarget target, Concessione c) throws Exception {
Response resp=target.request(MediaType.APPLICATION_JSON).post(Entity.entity(Serialization.write(c), MediaType.APPLICATION_JSON));
return check(resp,Concessione.class);
}
private static Concessione get(WebTarget target) throws Exception {
return register(target, TestModel.prepareConcessione());
}
private static Concessione getById(WebTarget target ,String id) throws Exception {
return check(target.path(id).request(MediaType.APPLICATION_JSON).get(),Concessione.class);
}
private static Iterator<Concessione> search(String query, WebTarget target) throws Exception {
String result= check(target.path(InterfaceConstants.Methods.SEARCH_PATH).request(MediaType.APPLICATION_JSON_TYPE).post(
Entity.entity(query,MediaType.APPLICATION_JSON)),String.class);
return Serialization.readCollection(result,Concessione.class);
}
private static <T> Iterator<T> queryFile(String filename, WebTarget target, Class<T> clazz) throws Exception {
String queryString= TestQueries.queries.get(filename);
String result = check(target.path(InterfaceConstants.Methods.QUERY_PATH).request(MediaType.APPLICATION_JSON_TYPE).post(
Entity.entity(queryString,MediaType.APPLICATION_JSON)),String.class);
return Serialization.readCollection(result,clazz);
}
private static Iterator<Concessione> queryFile(String filename, WebTarget target) throws Exception {
return queryFile(filename,target,Concessione.class);
}
// ********** TESTS
@Test
public void list() {
WebTarget target=target(PATH);
System.out.println(target.request(MediaType.APPLICATION_JSON).get(List.class));
}
@Test
public void search() throws Exception {
WebTarget target=target(PATH);
AtomicLong validatedCount= new AtomicLong(0);
Iterator<Concessione> it=search(TestFilters.filters.get("validated.json"),target);
it.forEachRemaining(concessione -> {validatedCount.incrementAndGet();});
System.out.println("Validated : "+ validatedCount.get());
}
@Test
public void query() throws Exception {
WebTarget target=target(PATH);
Iterator<Concessione> it = queryFile("lastRegistered.json", target);
assertEquals(1, count(it));
it=queryFile("firstRegistered.json", target);
assertEquals(1, count(it));
System.out.println("Last Names by Fabio : ");
queryFile("lastNameRegisteredByFabio.json", target, JSONObject.class).forEachRemaining(c -> {System.out.println(c);});
System.out.println("Publication warning messages : ");
queryFile("publicationWarningMessages.json", target, JSONObject.class).forEachRemaining(c -> {System.out.println(c);});
}
@Test
public void getConfiguration() throws Exception {
WebTarget target=target(PATH);
System.out.println(check(target.path(InterfaceConstants.Methods.CONFIGURATION_PATH).request(MediaType.APPLICATION_JSON).get(),String.class));
}
@Test
public void createNew() throws Exception {
WebTarget target=target(PATH);
Concessione c=register(target,TestModel.prepareConcessione());
Assert.assertTrue(c.getMongo_id()!=null&&!c.getMongo_id().isEmpty());
}
@Test
public void delete() throws Exception {
WebTarget target=target(PATH);
Concessione c = get(target);
check(target.path(c.getMongo_id()).request(MediaType.APPLICATION_JSON).delete(),null);
Concessione published=getFullPublished(target);
check(target.path(published.getMongo_id()).request(MediaType.APPLICATION_JSON).delete(),null);
}
@Test
public void republish() throws Exception{
WebTarget target=target(PATH);
Concessione published=getFullPublished(target);
//Concessione published=getById(target,"6155ba6002ad3d2c23b72b5a");
published = unpublish(target,published.getMongo_id());
System.out.println("Republishing..");
published=publish(target,published.getMongo_id());
Assert.assertEquals(published.getReport().getStatus(),ValidationStatus.PASSED);
}
@Test
public void handlePrecise() throws Exception {
//Republishing
WebTarget target=target(PATH);
String id="610415af02ad3d05b5f81ee3";
publish(target,unpublish(target,id).getMongo_id());
target.path(id).queryParam(InterfaceConstants.Parameters.FORCE,true).request(MediaType.APPLICATION_JSON).delete();
}
@Test
public void getById() throws Exception {
WebTarget target=target(PATH);
Concessione c = get(target);
Response resp=target.path(c.getMongo_id()).request(MediaType.APPLICATION_JSON).get();
Concessione loaded=check(resp,Concessione.class);
Assert.assertTrue(loaded.getMongo_id()!=null&&!loaded.getMongo_id().isEmpty());
System.out.println("Got by ID "+loaded);
}
@Test
public void update() throws Exception {
WebTarget target=target(PATH);
Concessione c = get(target);
String newTitle="Questo titolo l'ho modificato mo nel test quello proprio apposta pewr questa cosa'";
c.setNome(newTitle);
Response resp=target.request(MediaType.APPLICATION_JSON).put(Entity.entity(Serialization.write(c), MediaType.APPLICATION_JSON));
Assert.assertTrue(check(resp,Concessione.class).getNome().equals(newTitle));
}
@Test
public void uploadFile() throws Exception {
WebTarget target=target(PATH);
Response resp=target.request(MediaType.APPLICATION_JSON).post(Entity.entity(Serialization.write(TestModel.prepareEmptyConcessione()), MediaType.APPLICATION_JSON));
Concessione c=check(resp,Concessione.class);
Assert.assertTrue(c.getMongo_id()!=null&&!c.getMongo_id().isEmpty());
System.out.println("ID IS "+c.getMongo_id());
// Insert section
c.setRelazioneScavo(TestModel.prepareConcessione().getRelazioneScavo());
// c.getRelazioneScavo().setMongo_id(TestModel.rnd());
resp=target.request(MediaType.APPLICATION_JSON).put(Entity.entity(Serialization.write(c), MediaType.APPLICATION_JSON));
c=upload(new StorageUtils(),target,c.getMongo_id(),Paths.RELAZIONE,"relazione.pdf");
assertNotNull(c.getRelazioneScavo().getActualContent());
assertTrue(c.getRelazioneScavo().getActualContent().size()>0);
System.out.println("File is "+c.getRelazioneScavo().getActualContent().get(0));
}
@Test
public void testClearFileSet() throws Exception {
WebTarget target=target(PATH);
Concessione published=getFullPublished(target);
String path=Paths.POSIZIONAMENTO;
Response resp=
target.path(InterfaceConstants.Methods.DELETE_FILES_PATH).path(published.getMongo_id()).
request(MediaType.APPLICATION_JSON).put(Entity.entity(path, MediaType.APPLICATION_JSON));
//Expecting error for deletion
assertTrue(resp.getStatus()>=300);
System.out.println("Error for deletion is "+resp.readEntity(String.class));
//unpublish
unpublish(target,published.getMongo_id());
//Actually cleaning posizionamento
published=check(
target.path(InterfaceConstants.Methods.DELETE_FILES_PATH).path(published.getMongo_id()).
request(MediaType.APPLICATION_JSON).post(Entity.entity(path, MediaType.APPLICATION_JSON)),Concessione.class);
assertTrue(published.getPosizionamentoScavo().getActualContent().isEmpty());
path=Paths.piantaByIndex(0);
published=check(
target.path(InterfaceConstants.Methods.DELETE_FILES_PATH).path(published.getMongo_id()).
request(MediaType.APPLICATION_JSON).post(Entity.entity(path, MediaType.APPLICATION_JSON)),Concessione.class);
assertTrue(published.getPianteFineScavo().get(0).getActualContent().isEmpty());
}
@Test
public void publish() throws Exception {
WebTarget target=target(PATH);
Concessione published=getFullPublished(target);
System.out.println("Published : "+published);
System.out.println("Report is : "+published.getReport());
assertNotNull(published.getReport());
assertEquals(ValidationStatus.PASSED,published.getReport().getStatus());
assertNotNull(published.getPosizionamentoScavo().getWmsLink());
for(LayerConcessione l : published.getPianteFineScavo()) {
assertNotNull(l.getWmsLink());
}
assertNotNull(published.getCentroidLat());
assertNotNull(published.getCentroidLong());
}
private Concessione getFullPublished(WebTarget target) throws Exception {
File layerFolder=TestModel.getBaseFolder();
Map<String, List<File>> layers = Files.getAllShapeSet(layerFolder,true);
Concessione c=TestModel.prepareConcessione(layers.size(),1);
c.getRelazioneScavo().setPolicy(AccessPolicy.EMBARGOED);
assertEquals(AccessPolicy.EMBARGOED,c.getRelazioneScavo().getPolicy());
c.setNome("Concessione : publish test ");
StorageUtils storage=new StorageUtils();
// Register new
c=register(target,c);
assertEquals(AccessPolicy.EMBARGOED,c.getRelazioneScavo().getPolicy());
//Upload files
c=upload(storage,target,c.getMongo_id(),Paths.RELAZIONE,"relazione.pdf");
assertEquals(AccessPolicy.EMBARGOED,c.getRelazioneScavo().getPolicy());
c=upload(storage,target,c.getMongo_id(),Paths.ABSTRACT_RELAZIONE,"relazione.pdf");
String[] keys=layers.keySet().toArray(new String [0]);
c=upload(storage,target,c.getMongo_id(),Paths.POSIZIONAMENTO,
// TestModel.getBaseFolder().list((file,name)->{return name.startsWith("pianta.shp");}));
layers.get(keys[0]).toArray(new File[0]));
assertEquals(AccessPolicy.EMBARGOED,c.getRelazioneScavo().getPolicy());
// Clash on workspaces
for(int i=0;i<c.getPianteFineScavo().size();i++) {
String key=keys[0];
String path=Paths.piantaByIndex(i);
c.getContentByPath(path).setTitolo("Pianta from "+key.replace(layerFolder.getAbsolutePath(),""));
c=update(c,target);
c=upload(storage, target, c.getMongo_id(), path,
// TestModel.getBaseFolder().list((file,name)->{return name.startsWith("pianta.shp");}));
layers.get(key).toArray(new File[0]));
}
assertEquals(AccessPolicy.EMBARGOED,c.getRelazioneScavo().getPolicy());
// Immagini
for (int i = 0; i <c.getImmaginiRappresentative().size() ; i++) {
c=upload(storage,target,c.getMongo_id(),Paths.imgByIndex(0),"immagine"+(i+1)+".png");
}
assertEquals(AccessPolicy.EMBARGOED,c.getRelazioneScavo().getPolicy());
Concessione toReturn= publish(target, c.getMongo_id());
assertEquals(AccessPolicy.EMBARGOED,toReturn.getRelazioneScavo().getPolicy());
return toReturn;
}
public static long count(Iterator<?> iterator){
AtomicLong l=new AtomicLong(0);
iterator.forEachRemaining(el->{l.incrementAndGet();});
return l.get();
}
}