318 lines
12 KiB
Java
318 lines
12 KiB
Java
package org.gcube.application.geoportal.service;
|
|
|
|
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.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.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.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);
|
|
}
|
|
|
|
|
|
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);
|
|
}
|
|
|
|
|
|
private static Concessione publish(WebTarget target, Concessione conc) throws Exception {
|
|
Response resp=target.path(PUBLISH_PATH).path(conc.getMongo_id()).request(MediaType.APPLICATION_JSON).
|
|
put(Entity.entity(Serialization.write(conc), 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=null;
|
|
it=queryFile("lastRegistered.json", target);
|
|
assertTrue(count(it)==1);
|
|
|
|
|
|
it=queryFile("firstRegistered.json", target);
|
|
assertTrue(count(it)==1);
|
|
|
|
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);
|
|
published = unpublish(target,published.getMongo_id());
|
|
System.out.println("Republishing..");
|
|
published=publish(target,published);
|
|
Assert.assertEquals(published.getReport().getStatus(),ValidationStatus.PASSED);
|
|
}
|
|
|
|
|
|
// @Test
|
|
// public void handlePrecise() throws Exception {
|
|
// WebTarget target=target(PATH);
|
|
// String id="610415af02ad3d05b5f81ee3";
|
|
// publish(target,unpublish(target,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));
|
|
resp=
|
|
target.path(InterfaceConstants.Methods.PUBLISH_PATH).path(published.getMongo_id()).
|
|
request(MediaType.APPLICATION_JSON).delete();
|
|
check(resp,null);
|
|
|
|
//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());
|
|
|
|
assertEquals(published.getImmaginiRappresentative().size(),2);
|
|
assertEquals(published.getPianteFineScavo().size(),1);
|
|
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 {
|
|
Concessione c=TestModel.prepareConcessione(1,2);
|
|
|
|
c.setNome("Concessione : publish test");
|
|
StorageUtils storage=new StorageUtils();
|
|
|
|
|
|
// Register new
|
|
c=register(target,c);
|
|
|
|
//Upload files
|
|
upload(storage,target,c.getMongo_id(),Paths.RELAZIONE,"relazione.pdf");
|
|
upload(storage,target,c.getMongo_id(),Paths.ABSTRACT_RELAZIONE,"relazione.pdf");
|
|
|
|
upload(storage,target,c.getMongo_id(),Paths.POSIZIONAMENTO,"pos.shp","pos.shx");
|
|
|
|
// Clash on workspaces
|
|
upload(storage,target,c.getMongo_id(),Paths.piantaByIndex(0),"pianta.shp","pianta.shx");
|
|
upload(storage,target,c.getMongo_id(),Paths.imgByIndex(0),"immagine.png");
|
|
upload(storage,target,c.getMongo_id(),Paths.imgByIndex(1),"immagine2.png");
|
|
|
|
|
|
|
|
// Immagini
|
|
Concessione published=publish(target, c);
|
|
return published;
|
|
}
|
|
|
|
public static long count(Iterator<?> iterator){
|
|
AtomicLong l=new AtomicLong(0);
|
|
iterator.forEachRemaining(el->{l.incrementAndGet();});
|
|
return l.get();
|
|
}
|
|
}
|